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_run.c
23 * @brief test case to run experiments with 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_active;
94 unsigned int experiments_running;
98 struct ExperimentationPeer ph[NUM_PEERS];
104 * @param tc the task context
107 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
112 for (peer = 0; peer < NUM_PEERS; peer++)
114 if (NULL != ph[peer].stat_op)
115 GNUNET_TESTBED_operation_done (ph[peer].stat_op);
116 ph[peer].stat_op = NULL;
121 GNUNET_TESTBED_operation_done (op);
124 GNUNET_SCHEDULER_shutdown ();
128 * Controller event callback
131 * @param event the controller event
134 controller_event_cb (void *cls,
135 const struct GNUNET_TESTBED_EventInformation *event)
139 case GNUNET_TESTBED_ET_CONNECT:
141 if ((NUM_PEERS * (NUM_PEERS - 1)) == overlay_connects)
144 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers connected \n", NUM_PEERS);
145 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
147 GNUNET_SCHEDULER_cancel (shutdown_task);
149 shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
152 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
156 result = GNUNET_SYSERR;
157 GNUNET_SCHEDULER_cancel (shutdown_task);
158 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
166 static int last_experiments_value = 0;
168 unsigned int t_running_experiments = 0;
170 for (peer = 0; peer < NUM_PEERS; peer++)
172 t_running_experiments += ph[peer].experiments_running;
175 if (last_experiments_value < t_running_experiments)
176 fprintf (stderr, ".");
177 last_experiments_value = t_running_experiments;
179 if (t_running_experiments == (NUM_PEERS * NUM_EXPERIMENTS))
181 fprintf (stderr, "\n");
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "All %u peers are running experiments\n", NUM_PEERS);
183 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
190 * Callback function to process statistic values.
192 * @param cls struct StatsContext
193 * @param subsystem name of subsystem that created the statistic
194 * @param name the name of the datum
195 * @param value the current value
196 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
197 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
200 stat_iterator (void *cls, const char *subsystem, const char *name,
201 uint64_t value, int is_persistent)
203 struct ExperimentationPeer *peer = cls;
205 if (0 == strcmp (name, "# experiments active"))
207 peer->experiments_active = value;
210 if (0 == strcmp (name, "# experiments running"))
212 peer->experiments_running = value;
221 * Called after successfully opening a connection to a peer's statistics
222 * service; we register statistics monitoring here.
224 * @param cls the callback closure from functions generating an operation
225 * @param op the operation that has been finished
226 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
227 * @param emsg error message in case the operation has failed; will be NULL if
228 * operation has executed successfully.
231 stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
232 void *ca_result, const char *emsg )
234 //struct GNUNET_STATISTICS_Handle *sh = ca_result;
235 struct ExperimentationPeer *peer = cls;
243 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
244 (peer->sh, "experimentation", "# experiments active",
245 stat_iterator, peer));
246 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
247 (peer->sh, "experimentation", "# experiments running",
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;
282 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
283 (peer->sh, "experimentation", "# experiments active",
284 stat_iterator, peer));
285 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
286 (peer->sh, "experimentation", "# experiments running",
287 stat_iterator, peer));
288 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
295 * Signature of a main function for a testcase.
298 * @param num_peers number of peers in 'peers'
299 * @param peers_ handle to peers run in the testbed
300 * @param links_succeeded the number of overlay link connection attempts that
302 * @param links_failed the number of overlay link connection attempts that
306 test_master (void *cls, unsigned int num_peers,
307 struct GNUNET_TESTBED_Peer **peers_,
308 unsigned int links_succeeded,
309 unsigned int links_failed)
313 GNUNET_assert (NULL == cls);
314 GNUNET_assert (NUM_PEERS == num_peers);
315 GNUNET_assert (NULL != peers_);
316 for (peer = 0; peer < num_peers; peer++)
318 GNUNET_assert (NULL != peers_[peer]);
319 /* Connect to peer's statistic service */
320 ph[peer].stat_op = GNUNET_TESTBED_service_connect (NULL,
321 peers_[peer], "statistics",
322 &stat_comp_cb, &ph[peer],
323 &stat_connect_adapter,
324 &stat_disconnect_adapter,
329 overlay_connects = 0;
330 op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
333 GNUNET_TESTBED_TOPOLOGY_CLIQUE,
334 /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
336 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
337 GNUNET_assert (NULL != op);
338 shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
346 main (int argc, char **argv)
350 result = GNUNET_SYSERR;
352 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
353 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
354 (void) GNUNET_TESTBED_test_run ("test_experimentation_clique_run",
355 "test_experimentation_clique.conf", NUM_PEERS,
356 event_mask, &controller_event_cb, NULL,
358 if (GNUNET_OK != result)
363 /* end of test_experimentation_clique_run.c */