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/testbed/test_testbed_api_topology.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
38 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, (5 * NUM_PEERS) + 20)
43 static struct GNUNET_TESTBED_Peer **peers;
48 static struct GNUNET_TESTBED_Operation *op;
53 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
61 * Counter for counting overlay connections
63 static unsigned int overlay_connects;
66 * Information we track for a peer in the testbed.
68 struct ExperimentationPeer
71 * Handle with testbed.
73 struct GNUNET_TESTBED_Peer *daemon;
76 * Testbed operation to connect to statistics service
78 struct GNUNET_TESTBED_Operation *stat_op;
81 * Handle to the statistics service
83 struct GNUNET_STATISTICS_Handle *sh;
85 unsigned int active_nodes;
86 unsigned int requested_nodes;
87 unsigned int inactive_nodes;
91 struct ExperimentationPeer ph[NUM_PEERS];
97 * @param tc the task context
100 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
105 for (peer = 0; peer < NUM_PEERS; peer++)
107 if (NULL != ph[peer].stat_op)
108 GNUNET_TESTBED_operation_done (ph[peer].stat_op);
109 ph[peer].stat_op = NULL;
114 GNUNET_TESTBED_operation_done (op);
117 GNUNET_SCHEDULER_shutdown ();
121 * Controller event callback
124 * @param event the controller event
127 controller_event_cb (void *cls,
128 const struct GNUNET_TESTBED_EventInformation *event)
132 case GNUNET_TESTBED_ET_CONNECT:
134 if ((NUM_PEERS * (NUM_PEERS - 1)) == overlay_connects)
137 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers connected \n", NUM_PEERS);
138 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
140 GNUNET_SCHEDULER_cancel (shutdown_task);
142 shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
145 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
149 result = GNUNET_SYSERR;
150 GNUNET_SCHEDULER_cancel (shutdown_task);
151 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
158 static int last_value = 0;
160 unsigned int total_active = 0;
161 unsigned int total_inactive = 0;
162 unsigned int total_requested = 0;
164 for (peer = 0; peer < NUM_PEERS; peer++)
166 total_active += ph[peer].active_nodes;
167 total_requested += ph[peer].requested_nodes;
168 total_inactive += ph[peer].inactive_nodes;
170 if (last_value < total_active)
171 fprintf (stderr, ".");
173 if ((total_active == (NUM_PEERS * (NUM_PEERS -1))) &&
174 (total_requested == 0) && (total_inactive == 0))
176 fprintf (stderr, "\n");
177 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers active in a clique\n", NUM_PEERS);
178 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
185 * Callback function to process statistic values.
187 * @param cls struct StatsContext
188 * @param subsystem name of subsystem that created the statistic
189 * @param name the name of the datum
190 * @param value the current value
191 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
192 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
195 stat_iterator (void *cls, const char *subsystem, const char *name,
196 uint64_t value, int is_persistent)
198 struct ExperimentationPeer *peer = cls;
200 if (0 == strcmp (name, "# nodes active"))
202 peer->active_nodes = value;
204 if (0 == strcmp (name, "# nodes inactive"))
206 peer->inactive_nodes = value;
208 if (0 == strcmp (name, "# nodes requested"))
210 peer->requested_nodes = value;
218 * Called after successfully opening a connection to a peer's statistics
219 * service; we register statistics monitoring here.
221 * @param cls the callback closure from functions generating an operation
222 * @param op the operation that has been finished
223 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
224 * @param emsg error message in case the operation has failed; will be NULL if
225 * operation has executed successfully.
228 stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
229 void *ca_result, const char *emsg )
231 struct GNUNET_STATISTICS_Handle *sh = ca_result;
232 struct ExperimentationPeer *peer = cls;
240 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
241 (sh, "experimentation", "# nodes active",
242 stat_iterator, peer));
243 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
244 (sh, "experimentation", "# nodes inactive",
245 stat_iterator, peer));
246 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
247 (sh, "experimentation", "# nodes requested",
248 stat_iterator, peer));
252 * Called to open a connection to the peer's statistics
254 * @param cls peer context
255 * @param cfg configuration of the peer to connect to; will be available until
256 * GNUNET_TESTBED_operation_done() is called on the operation returned
257 * from GNUNET_TESTBED_service_connect()
258 * @return service handle to return in 'op_result', NULL on error
261 stat_connect_adapter (void *cls,
262 const struct GNUNET_CONFIGURATION_Handle *cfg)
264 struct ExperimentationPeer *peer = cls;
265 peer->sh = GNUNET_STATISTICS_create ("experimentation", cfg);
266 if (NULL == peer->sh)
267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create statistics \n");
273 * Called to disconnect from peer's statistics service
275 * @param cls peer context
276 * @param op_result service handle returned from the connect adapter
279 stat_disconnect_adapter (void *cls, void *op_result)
281 struct ExperimentationPeer *peer = cls;
283 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
284 (peer->sh, "experimentation", "# nodes active",
285 stat_iterator, peer));
286 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
287 (peer->sh, "experimentation", "# nodes inactive",
288 stat_iterator, peer));
289 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
290 (peer->sh, "experimentation", "# nodes requested",
291 stat_iterator, peer));
292 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
299 * Signature of a main function for a testcase.
302 * @param num_peers number of peers in 'peers'
303 * @param peers_ handle to peers run in the testbed
304 * @param links_succeeded the number of overlay link connection attempts that
306 * @param links_failed the number of overlay link connection attempts that
310 test_master (void *cls, unsigned int num_peers,
311 struct GNUNET_TESTBED_Peer **peers_,
312 unsigned int links_succeeded,
313 unsigned int links_failed)
317 GNUNET_assert (NULL == cls);
318 GNUNET_assert (NUM_PEERS == num_peers);
319 GNUNET_assert (NULL != peers_);
320 for (peer = 0; peer < num_peers; peer++)
322 GNUNET_assert (NULL != peers_[peer]);
323 /* Connect to peer's statistic service */
324 ph[peer].stat_op = GNUNET_TESTBED_service_connect (NULL,
325 peers_[peer], "statistics",
326 &stat_comp_cb, &ph[peer],
327 &stat_connect_adapter,
328 &stat_disconnect_adapter,
333 overlay_connects = 0;
334 op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
337 GNUNET_TESTBED_TOPOLOGY_CLIQUE,
338 /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
340 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
341 GNUNET_assert (NULL != op);
342 shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
350 main (int argc, char **argv)
354 result = GNUNET_SYSERR;
356 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
357 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
358 (void) GNUNET_TESTBED_test_run ("test_experimentation_clique",
359 "test_experimentation_clique.conf", NUM_PEERS,
360 event_mask, &controller_event_cb, NULL,
362 if (GNUNET_OK != result)
367 /* end of test_testbed_api_topology.c */