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_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_active;
94 unsigned int experiments_outbound_running;
95 unsigned int experiments_inbound_running;
99 struct ExperimentationPeer bp_slaves[NUM_PEERS];
105 * @param tc the task context
108 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
113 for (peer = 0; peer < NUM_PEERS; peer++)
115 if (NULL != bp_slaves[peer].stat_op)
116 GNUNET_TESTBED_operation_done (bp_slaves[peer].stat_op);
117 bp_slaves[peer].stat_op = NULL;
122 GNUNET_TESTBED_operation_done (op);
125 GNUNET_SCHEDULER_shutdown ();
129 * Controller event callback
132 * @param event the controller event
135 controller_event_cb (void *cls,
136 const struct GNUNET_TESTBED_EventInformation *event)
140 case GNUNET_TESTBED_ET_CONNECT:
142 if ((NUM_PEERS * (NUM_PEERS - 1)) == overlay_connects)
145 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers connected \n", NUM_PEERS);
146 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
148 GNUNET_SCHEDULER_cancel (shutdown_task);
150 shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
153 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
157 result = GNUNET_SYSERR;
158 GNUNET_SCHEDULER_cancel (shutdown_task);
159 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
166 static int last_in_experiments_value = 0;
167 static int last_out_experiments_value = 0;
169 unsigned int t_running_outbound_experiments = 0;
170 unsigned int t_running_inbound_experiments = 0;
172 for (peer = 0; peer < NUM_PEERS; peer++)
174 t_running_outbound_experiments += bp_slaves[peer].experiments_outbound_running;
175 t_running_inbound_experiments += bp_slaves[peer].experiments_inbound_running;
179 //fprintf (stderr, "%u %u \n", t_running_outbound_experiments, t_running_inbound_experiments);
180 if (last_in_experiments_value < t_running_inbound_experiments)
181 fprintf (stderr, ".");
182 last_in_experiments_value = t_running_inbound_experiments;
183 if (last_out_experiments_value < t_running_outbound_experiments)
184 fprintf (stderr, ".");
185 last_out_experiments_value = t_running_outbound_experiments;
189 if ((t_running_inbound_experiments == (NUM_PEERS * NUM_EXPERIMENTS)) &&
190 (t_running_outbound_experiments == (NUM_PEERS * NUM_EXPERIMENTS)))
192 fprintf (stderr, "\n");
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "All %u peers are running experiments\n", NUM_PEERS);
194 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
201 * Callback function to process statistic values.
203 * @param cls struct StatsContext
204 * @param subsystem name of subsystem that created the statistic
205 * @param name the name of the datum
206 * @param value the current value
207 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
208 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
211 stat_iterator (void *cls, const char *subsystem, const char *name,
212 uint64_t value, int is_persistent)
214 struct ExperimentationPeer *peer = cls;
216 if (0 == strcmp (name, "# experiments active"))
218 peer->experiments_active = value;
221 if (0 == strcmp (name, "# experiments outbound running"))
223 peer->experiments_outbound_running = value;
226 if (0 == strcmp (name, "# experiments inbound running"))
228 peer->experiments_inbound_running = value;
238 * Called after successfully opening a connection to a peer's statistics
239 * service; we register statistics monitoring here.
241 * @param cls the callback closure from functions generating an operation
242 * @param op the operation that has been finished
243 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
244 * @param emsg error message in case the operation has failed; will be NULL if
245 * operation has executed successfully.
248 stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
249 void *ca_result, const char *emsg )
251 //struct GNUNET_STATISTICS_Handle *sh = ca_result;
252 struct ExperimentationPeer *peer = cls;
260 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
261 (peer->sh, "experimentation", "# experiments active",
262 stat_iterator, peer));
263 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
264 (peer->sh, "experimentation", "# experiments outbound running",
265 stat_iterator, peer));
266 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
267 (peer->sh, "experimentation", "# experiments inbound running",
268 stat_iterator, peer));
272 * Called to open a connection to the peer's statistics
274 * @param cls peer context
275 * @param cfg configuration of the peer to connect to; will be available until
276 * GNUNET_TESTBED_operation_done() is called on the operation returned
277 * from GNUNET_TESTBED_service_connect()
278 * @return service handle to return in 'op_result', NULL on error
281 stat_connect_adapter (void *cls,
282 const struct GNUNET_CONFIGURATION_Handle *cfg)
284 struct ExperimentationPeer *peer = cls;
285 peer->sh = GNUNET_STATISTICS_create ("experimentation", cfg);
286 if (NULL == peer->sh)
287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create statistics \n");
293 * Called to disconnect from peer's statistics service
295 * @param cls peer context
296 * @param op_result service handle returned from the connect adapter
299 stat_disconnect_adapter (void *cls, void *op_result)
301 struct ExperimentationPeer *peer = cls;
302 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
303 (peer->sh, "experimentation", "# experiments active",
304 stat_iterator, peer));
305 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
306 (peer->sh, "experimentation", "# experiments outbound running",
307 stat_iterator, peer));
308 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
309 (peer->sh, "experimentation", "# experiments inbound running",
310 stat_iterator, peer));
311 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
318 * Signature of a main function for a testcase.
321 * @param h the run handle
322 * @param num_peers number of peers in 'peers'
323 * @param peers_ handle to peers run in the testbed
324 * @param links_succeeded the number of overlay link connection attempts that
326 * @param links_failed the number of overlay link connection attempts that
330 test_master (void *cls,
331 struct GNUNET_TESTBED_RunHandle *h,
332 unsigned int num_peers,
333 struct GNUNET_TESTBED_Peer **peers_,
334 unsigned int links_succeeded,
335 unsigned int links_failed)
339 GNUNET_assert (NULL == cls);
340 GNUNET_assert (NUM_PEERS == num_peers);
341 GNUNET_assert (NULL != peers_);
342 for (peer = 0; peer < num_peers; peer++)
344 GNUNET_assert (NULL != peers_[peer]);
345 /* Connect to peer's statistic service */
346 bp_slaves[peer].stat_op = GNUNET_TESTBED_service_connect (NULL,
347 peers_[peer], "statistics",
348 &stat_comp_cb, &bp_slaves[peer],
349 &stat_connect_adapter,
350 &stat_disconnect_adapter,
355 overlay_connects = 0;
356 op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
359 GNUNET_TESTBED_TOPOLOGY_CLIQUE,
360 /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
362 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
363 GNUNET_assert (NULL != op);
364 shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
372 main (int argc, char **argv)
376 result = GNUNET_SYSERR;
378 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
379 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
380 (void) GNUNET_TESTBED_test_run ("test_experimentation_clique_run",
381 "test_experimentation_clique.conf", NUM_PEERS,
382 event_mask, &controller_event_cb, NULL,
384 if (GNUNET_OK != result)
389 /* end of test_experimentation_clique_run.c */