changes for experimentation
[oweals/gnunet.git] / src / experimentation / test_experimentation_clique_run.c
1 /*
2   This file is part of GNUnet
3   (C) 2008--2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
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
26  */
27
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_testbed_service.h"
31
32
33 /**
34  * Number of peers we want to start
35  */
36 #define NUM_PEERS 2
37
38 #define NUM_ISSUER 1
39
40 #define NUM_EXPERIMENTS 2
41
42 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, (5 * NUM_PEERS) + 20)
43
44 /**
45  * Array of peers
46  */
47 static struct GNUNET_TESTBED_Peer **peers;
48
49 /**
50  * Operation handle
51  */
52 static struct GNUNET_TESTBED_Operation *op;
53
54 /**
55  * Shutdown task
56  */
57 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
58
59 /**
60  * Testing result
61  */
62 static int result;
63
64 /**
65  * Counter for counting overlay connections
66  */
67 static unsigned int overlay_connects;
68
69 /**
70  * Information we track for a peer in the testbed.
71  */
72 struct ExperimentationPeer
73 {
74   /**
75    * Handle with testbed.
76    */
77   struct GNUNET_TESTBED_Peer *daemon;
78
79   /**
80    * Testbed operation to connect to statistics service
81    */
82   struct GNUNET_TESTBED_Operation *stat_op;
83
84   /**
85    * Handle to the statistics service
86    */
87   struct GNUNET_STATISTICS_Handle *sh;
88
89   unsigned int active_nodes;
90   unsigned int requested_nodes;
91   unsigned int inactive_nodes;
92   unsigned int issuer;
93   unsigned int experiments_active;
94   unsigned int experiments_running;
95 };
96
97
98 struct ExperimentationPeer ph[NUM_PEERS];
99
100 /**
101  * Shutdown nicely
102  *
103  * @param cls NULL
104  * @param tc the task context
105  */
106 static void
107 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
108 {
109   unsigned int peer;
110         shutdown_task = GNUNET_SCHEDULER_NO_TASK;
111
112   for (peer = 0; peer < NUM_PEERS; peer++)
113   {
114         if (NULL != ph[peer].stat_op)
115                 GNUNET_TESTBED_operation_done (ph[peer].stat_op);
116         ph[peer].stat_op = NULL;
117   }
118
119   if (NULL != op)
120   {
121     GNUNET_TESTBED_operation_done (op);
122     op = NULL;
123   }
124   GNUNET_SCHEDULER_shutdown ();
125 }
126
127 /**
128  * Controller event callback
129  *
130  * @param cls NULL
131  * @param event the controller event
132  */
133 static void
134 controller_event_cb (void *cls,
135                      const struct GNUNET_TESTBED_EventInformation *event)
136 {
137   switch (event->type)
138   {
139   case GNUNET_TESTBED_ET_CONNECT:
140     overlay_connects++;
141     if ((NUM_PEERS * (NUM_PEERS - 1)) == overlay_connects)
142     {
143       result = GNUNET_OK;
144       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers connected \n", NUM_PEERS);
145       if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
146       {
147                 GNUNET_SCHEDULER_cancel (shutdown_task);
148       }
149       shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
150     }
151     break;
152   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
153     break;
154   default:
155     GNUNET_break (0);
156     result = GNUNET_SYSERR;
157     GNUNET_SCHEDULER_cancel (shutdown_task);
158     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
159   }
160 }
161
162 static void
163 check_end ()
164 {
165
166         static int last_experiments_value = 0;
167   unsigned int peer;
168   unsigned int t_running_experiments = 0;
169
170         for (peer = 0; peer < NUM_PEERS; peer++)
171         {
172                 t_running_experiments += ph[peer].experiments_running;
173         }
174
175         if (last_experiments_value < t_running_experiments)
176                 fprintf (stderr, ".");
177         last_experiments_value = t_running_experiments;
178
179         if (t_running_experiments == (NUM_PEERS * NUM_EXPERIMENTS))
180         {
181                         fprintf (stderr, "\n");
182                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers are running experiments\n", NUM_PEERS);
183                         GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
184         }
185 }
186
187
188
189 /**
190  * Callback function to process statistic values.
191  *
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
198  */
199 static int
200 stat_iterator (void *cls, const char *subsystem, const char *name,
201                      uint64_t value, int is_persistent)
202 {
203   struct ExperimentationPeer *peer = cls;
204
205         if (0 == strcmp (name, "# experiments active"))
206         {
207                         peer->experiments_active = value;
208         }
209
210         if (0 == strcmp (name, "# experiments running"))
211         {
212                         peer->experiments_running = value;
213         }
214
215         check_end ();
216
217         return GNUNET_OK;
218 }
219
220 /**
221  * Called after successfully opening a connection to a peer's statistics
222  * service; we register statistics monitoring here.
223  *
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.
229  */
230 static void
231 stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
232               void *ca_result, const char *emsg )
233 {
234   //struct GNUNET_STATISTICS_Handle *sh = ca_result;
235   struct ExperimentationPeer *peer = cls;
236
237   if (NULL != emsg)
238   {
239     GNUNET_break (0);
240     return;
241   }
242
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));
249 }
250
251 /**
252  * Called to open a connection to the peer's statistics
253  *
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
259  */
260 static void *
261 stat_connect_adapter (void *cls,
262                       const struct GNUNET_CONFIGURATION_Handle *cfg)
263 {
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");
268   return peer->sh;
269 }
270
271
272 /**
273  * Called to disconnect from peer's statistics service
274  *
275  * @param cls peer context
276  * @param op_result service handle returned from the connect adapter
277  */
278 static void
279 stat_disconnect_adapter (void *cls, void *op_result)
280 {
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);
289   peer->sh = NULL;
290 }
291
292
293
294 /**
295  * Signature of a main function for a testcase.
296  *
297  * @param cls closure
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
301  *          succeeded
302  * @param links_failed the number of overlay link connection attempts that
303  *          failed
304  */
305 static void
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)
310 {
311   unsigned int peer;
312
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++)
317   {
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,
325                                     &ph[peer]);
326
327   }
328   peers = peers_;
329   overlay_connects = 0;
330   op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
331                                                   NULL,
332                                                   NULL,
333                                                   GNUNET_TESTBED_TOPOLOGY_CLIQUE,
334                                                   /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
335                                                   /* NUM_PEERS, */
336                                                   GNUNET_TESTBED_TOPOLOGY_OPTION_END);
337   GNUNET_assert (NULL != op);
338   shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
339 }
340
341
342 /**
343  * Main function
344  */
345 int
346 main (int argc, char **argv)
347 {
348   uint64_t event_mask;
349
350   result = GNUNET_SYSERR;
351   event_mask = 0;
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,
357                                   &test_master, NULL);
358   if (GNUNET_OK != result)
359     return 1;
360   return 0;
361 }
362
363 /* end of test_experimentation_clique_run.c */