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_common.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 ph[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 != ph[peer].stat_op)
114 GNUNET_TESTBED_operation_done (ph[peer].stat_op);
115 ph[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 += ph[peer].active_nodes;
177 total_requested += ph[peer].requested_nodes;
178 total_inactive += ph[peer].inactive_nodes;
179 if (NUM_ISSUER == ph[peer].issuer)
181 if (NUM_EXPERIMENTS == ph[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 num_peers number of peers in 'peers'
349 * @param peers_ handle to peers run in the testbed
350 * @param links_succeeded the number of overlay link connection attempts that
352 * @param links_failed the number of overlay link connection attempts that
356 test_master (void *cls, unsigned int num_peers,
357 struct GNUNET_TESTBED_Peer **peers_,
358 unsigned int links_succeeded,
359 unsigned int links_failed)
363 GNUNET_assert (NULL == cls);
364 GNUNET_assert (NUM_PEERS == num_peers);
365 GNUNET_assert (NULL != peers_);
366 for (peer = 0; peer < num_peers; peer++)
368 GNUNET_assert (NULL != peers_[peer]);
369 /* Connect to peer's statistic service */
370 ph[peer].stat_op = GNUNET_TESTBED_service_connect (NULL,
371 peers_[peer], "statistics",
372 &stat_comp_cb, &ph[peer],
373 &stat_connect_adapter,
374 &stat_disconnect_adapter,
379 overlay_connects = 0;
380 op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
383 GNUNET_TESTBED_TOPOLOGY_CLIQUE,
384 /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
386 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
387 GNUNET_assert (NULL != op);
388 shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
396 main (int argc, char **argv)
400 result = GNUNET_SYSERR;
402 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
403 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
404 (void) GNUNET_TESTBED_test_run ("test_experimentation_clique_connect",
405 "test_experimentation_clique.conf", NUM_PEERS,
406 event_mask, &controller_event_cb, NULL,
408 if (GNUNET_OK != result)
413 /* end of test_experimentation_clique_connect.c */