2 This file is part of GNUnet
3 (C) 2008--2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file src/experimentation/test_experimentation_clique_connect.c
23 * @brief test case to connect experimentation daemons in a clique
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25 * @author Matthias Wachs
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
34 * Number of peers we want to start
40 #define NUM_EXPERIMENTS 2
42 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, (5 * NUM_PEERS) + 20)
47 static struct GNUNET_TESTBED_Peer **peers;
52 static struct GNUNET_TESTBED_Operation *op;
57 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
65 * Counter for counting overlay connections
67 static unsigned int overlay_connects;
70 * Information we track for a peer in the testbed.
72 struct ExperimentationPeer
75 * Handle with testbed.
77 struct GNUNET_TESTBED_Peer *daemon;
80 * Testbed operation to connect to statistics service
82 struct GNUNET_TESTBED_Operation *stat_op;
85 * Handle to the statistics service
87 struct GNUNET_STATISTICS_Handle *sh;
89 unsigned int active_nodes;
90 unsigned int requested_nodes;
91 unsigned int inactive_nodes;
93 unsigned int experiments;
97 struct ExperimentationPeer bp_slaves[NUM_PEERS];
103 * @param tc the task context
106 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
111 for (peer = 0; peer < NUM_PEERS; peer++)
113 if (NULL != bp_slaves[peer].stat_op)
114 GNUNET_TESTBED_operation_done (bp_slaves[peer].stat_op);
115 bp_slaves[peer].stat_op = NULL;
120 GNUNET_TESTBED_operation_done (op);
123 GNUNET_SCHEDULER_shutdown ();
127 * Controller event callback
130 * @param event the controller event
133 controller_event_cb (void *cls,
134 const struct GNUNET_TESTBED_EventInformation *event)
138 case GNUNET_TESTBED_ET_CONNECT:
140 if ((NUM_PEERS * (NUM_PEERS - 1)) == overlay_connects)
143 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers connected \n", NUM_PEERS);
144 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
146 GNUNET_SCHEDULER_cancel (shutdown_task);
148 shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
151 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
155 result = GNUNET_SYSERR;
156 GNUNET_SCHEDULER_cancel (shutdown_task);
157 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
164 static int last_active_value = 0;
165 static int last_issuer_value = 0;
166 static int last_experiments_value = 0;
168 unsigned int total_active = 0;
169 unsigned int total_inactive = 0;
170 unsigned int total_requested = 0;
171 unsigned int issuer = 0;
172 unsigned int experiments = 0;
174 for (peer = 0; peer < NUM_PEERS; peer++)
176 total_active += bp_slaves[peer].active_nodes;
177 total_requested += bp_slaves[peer].requested_nodes;
178 total_inactive += bp_slaves[peer].inactive_nodes;
179 if (NUM_ISSUER == bp_slaves[peer].issuer)
181 if (NUM_EXPERIMENTS == bp_slaves[peer].experiments)
184 if ((last_issuer_value < issuer) && (issuer == NUM_PEERS))
185 fprintf (stderr, "I");
186 last_issuer_value = issuer;
188 if ((last_experiments_value < experiments) && (experiments == NUM_PEERS))
189 fprintf (stderr, "E");
190 last_experiments_value = experiments;
192 if (last_active_value < total_active)
193 fprintf (stderr, ".");
194 last_active_value = total_active;
197 if ((total_active == (NUM_PEERS * (NUM_PEERS -1))) &&
198 (total_requested == 0) && (total_inactive == 0) &&
199 (issuer == NUM_PEERS) && (experiments == NUM_PEERS))
201 fprintf (stderr, "\n");
202 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers active in a clique\n", NUM_PEERS);
203 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
210 * Callback function to process statistic values.
212 * @param cls struct StatsContext
213 * @param subsystem name of subsystem that created the statistic
214 * @param name the name of the datum
215 * @param value the current value
216 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
217 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
220 stat_iterator (void *cls, const char *subsystem, const char *name,
221 uint64_t value, int is_persistent)
223 struct ExperimentationPeer *peer = cls;
225 if (0 == strcmp (name, "# nodes active"))
227 peer->active_nodes = value;
229 if (0 == strcmp (name, "# nodes inactive"))
231 peer->inactive_nodes = value;
233 if (0 == strcmp (name, "# nodes requested"))
235 peer->requested_nodes = value;
237 if (0 == strcmp (name, "# issuer"))
239 peer->issuer = value;
241 if (0 == strcmp (name, "# experiments"))
243 peer->experiments = value;
252 * Called after successfully opening a connection to a peer's statistics
253 * service; we register statistics monitoring here.
255 * @param cls the callback closure from functions generating an operation
256 * @param op the operation that has been finished
257 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
258 * @param emsg error message in case the operation has failed; will be NULL if
259 * operation has executed successfully.
262 stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
263 void *ca_result, const char *emsg )
265 struct GNUNET_STATISTICS_Handle *sh = ca_result;
266 struct ExperimentationPeer *peer = cls;
274 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
275 (sh, "experimentation", "# nodes active",
276 stat_iterator, peer));
277 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
278 (sh, "experimentation", "# nodes inactive",
279 stat_iterator, peer));
280 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
281 (sh, "experimentation", "# nodes requested",
282 stat_iterator, peer));
283 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
284 (sh, "experimentation", "# issuer",
285 stat_iterator, peer));
286 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
287 (sh, "experimentation", "# experiments",
288 stat_iterator, peer));
292 * Called to open a connection to the peer's statistics
294 * @param cls peer context
295 * @param cfg configuration of the peer to connect to; will be available until
296 * GNUNET_TESTBED_operation_done() is called on the operation returned
297 * from GNUNET_TESTBED_service_connect()
298 * @return service handle to return in 'op_result', NULL on error
301 stat_connect_adapter (void *cls,
302 const struct GNUNET_CONFIGURATION_Handle *cfg)
304 struct ExperimentationPeer *peer = cls;
305 peer->sh = GNUNET_STATISTICS_create ("experimentation", cfg);
306 if (NULL == peer->sh)
307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create statistics \n");
313 * Called to disconnect from peer's statistics service
315 * @param cls peer context
316 * @param op_result service handle returned from the connect adapter
319 stat_disconnect_adapter (void *cls, void *op_result)
321 struct ExperimentationPeer *peer = cls;
323 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
324 (peer->sh, "experimentation", "# nodes active",
325 stat_iterator, peer));
326 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
327 (peer->sh, "experimentation", "# nodes inactive",
328 stat_iterator, peer));
329 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
330 (peer->sh, "experimentation", "# nodes requested",
331 stat_iterator, peer));
332 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
333 (peer->sh, "experimentation", "# issuer",
334 stat_iterator, peer));
335 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
336 (peer->sh, "experimentation", "# experiments",
337 stat_iterator, peer));
338 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
345 * Signature of a main function for a testcase.
348 * @param h the run handle
349 * @param num_peers number of peers in 'peers'
350 * @param peers_ handle to peers run in the testbed
351 * @param links_succeeded the number of overlay link connection attempts that
353 * @param links_failed the number of overlay link connection attempts that
357 test_master (void *cls,
358 struct GNUNET_TESTBED_RunHandle *h,
359 unsigned int num_peers,
360 struct GNUNET_TESTBED_Peer **peers_,
361 unsigned int links_succeeded,
362 unsigned int links_failed)
366 GNUNET_assert (NULL == cls);
367 GNUNET_assert (NUM_PEERS == num_peers);
368 GNUNET_assert (NULL != peers_);
369 for (peer = 0; peer < num_peers; peer++)
371 GNUNET_assert (NULL != peers_[peer]);
372 /* Connect to peer's statistic service */
373 bp_slaves[peer].stat_op = GNUNET_TESTBED_service_connect (NULL,
374 peers_[peer], "statistics",
375 &stat_comp_cb, &bp_slaves[peer],
376 &stat_connect_adapter,
377 &stat_disconnect_adapter,
382 overlay_connects = 0;
383 op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
386 GNUNET_TESTBED_TOPOLOGY_CLIQUE,
387 /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
389 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
390 GNUNET_assert (NULL != op);
391 shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
399 main (int argc, char **argv)
403 result = GNUNET_SYSERR;
405 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
406 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
407 (void) GNUNET_TESTBED_test_run ("test_experimentation_clique_connect",
408 "test_experimentation_clique.conf", NUM_PEERS,
409 event_mask, &controller_event_cb, NULL,
411 if (GNUNET_OK != result)
416 /* end of test_experimentation_clique_connect.c */