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
41 static struct GNUNET_TESTBED_Peer **peers;
46 static struct GNUNET_TESTBED_Operation *op;
51 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
59 * Counter for counting overlay connections
61 static unsigned int overlay_connects;
64 * Information we track for a peer in the testbed.
66 struct ExperimentationPeer
69 * Handle with testbed.
71 struct GNUNET_TESTBED_Peer *daemon;
74 * Testbed operation to connect to statistics service
76 struct GNUNET_TESTBED_Operation *stat_op;
79 * Handle to the statistics service
81 struct GNUNET_STATISTICS_Handle *sh;
84 struct ExperimentationPeer ph[NUM_PEERS];
90 * @param tc the task context
93 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
98 for (peer = 0; peer < NUM_PEERS; peer++)
100 if (NULL != ph[peer].stat_op)
101 GNUNET_TESTBED_operation_done (ph[peer].stat_op);
102 ph[peer].stat_op = NULL;
107 GNUNET_TESTBED_operation_done (op);
110 GNUNET_SCHEDULER_shutdown ();
114 * Controller event callback
117 * @param event the controller event
120 controller_event_cb (void *cls,
121 const struct GNUNET_TESTBED_EventInformation *event)
125 case GNUNET_TESTBED_ET_CONNECT:
127 if ((NUM_PEERS * (NUM_PEERS - 1)) == overlay_connects)
130 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers connected \n", NUM_PEERS);
132 //GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
135 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
139 result = GNUNET_SYSERR;
140 GNUNET_SCHEDULER_cancel (shutdown_task);
141 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
147 * Callback function to process statistic values.
149 * @param cls struct StatsContext
150 * @param subsystem name of subsystem that created the statistic
151 * @param name the name of the datum
152 * @param value the current value
153 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
154 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
157 stat_iterator (void *cls, const char *subsystem, const char *name,
158 uint64_t value, int is_persistent)
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "STATS `%s' %s %llu\n", subsystem, name, value);
165 * Called after successfully opening a connection to a peer's statistics
166 * service; we register statistics monitoring here.
168 * @param cls the callback closure from functions generating an operation
169 * @param op the operation that has been finished
170 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
171 * @param emsg error message in case the operation has failed; will be NULL if
172 * operation has executed successfully.
175 stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
176 void *ca_result, const char *emsg )
178 struct GNUNET_STATISTICS_Handle *sh = ca_result;
179 struct ExperimentationPeer *peer = cls;
187 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
188 (sh, "experimentation", "# nodes active",
189 stat_iterator, peer));
190 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
191 (sh, "experimentation", "# nodes inactive",
192 stat_iterator, peer));
193 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
194 (sh, "experimentation", "# nodes requested",
195 stat_iterator, peer));
199 * Called to open a connection to the peer's statistics
201 * @param cls peer context
202 * @param cfg configuration of the peer to connect to; will be available until
203 * GNUNET_TESTBED_operation_done() is called on the operation returned
204 * from GNUNET_TESTBED_service_connect()
205 * @return service handle to return in 'op_result', NULL on error
208 stat_connect_adapter (void *cls,
209 const struct GNUNET_CONFIGURATION_Handle *cfg)
211 struct ExperimentationPeer *peer = cls;
212 peer->sh = GNUNET_STATISTICS_create ("experimentation", cfg);
213 if (NULL == peer->sh)
214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create statistics \n");
220 * Called to disconnect from peer's statistics service
222 * @param cls peer context
223 * @param op_result service handle returned from the connect adapter
226 stat_disconnect_adapter (void *cls, void *op_result)
228 struct ExperimentationPeer *peer = cls;
230 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
231 (peer->sh, "experimentation", "# nodes active",
232 stat_iterator, peer));
233 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
234 (peer->sh, "experimentation", "# nodes inactive",
235 stat_iterator, peer));
236 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
237 (peer->sh, "experimentation", "# nodes requested",
238 stat_iterator, peer));
239 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
246 * Signature of a main function for a testcase.
249 * @param num_peers number of peers in 'peers'
250 * @param peers_ handle to peers run in the testbed
251 * @param links_succeeded the number of overlay link connection attempts that
253 * @param links_failed the number of overlay link connection attempts that
257 test_master (void *cls, unsigned int num_peers,
258 struct GNUNET_TESTBED_Peer **peers_,
259 unsigned int links_succeeded,
260 unsigned int links_failed)
264 GNUNET_assert (NULL == cls);
265 GNUNET_assert (NUM_PEERS == num_peers);
266 GNUNET_assert (NULL != peers_);
267 for (peer = 0; peer < num_peers; peer++)
269 GNUNET_assert (NULL != peers_[peer]);
270 /* Connect to peer's statistic service */
271 ph[peer].stat_op = GNUNET_TESTBED_service_connect (NULL,
272 peers_[peer], "statistics",
273 &stat_comp_cb, &ph[peer],
274 &stat_connect_adapter,
275 &stat_disconnect_adapter,
280 overlay_connects = 0;
281 op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
284 GNUNET_TESTBED_TOPOLOGY_CLIQUE,
285 /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
287 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
288 GNUNET_assert (NULL != op);
290 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
291 (GNUNET_TIME_UNIT_SECONDS, 20),
300 main (int argc, char **argv)
304 result = GNUNET_SYSERR;
306 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
307 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
308 (void) GNUNET_TESTBED_test_run ("test_experimentation_clique",
309 "test_experimentation_clique.conf", NUM_PEERS,
310 event_mask, &controller_event_cb, NULL,
312 if (GNUNET_OK != result)
317 /* end of test_testbed_api_topology.c */