-towards fixing FTBFS in experimentation
[oweals/gnunet.git] / src / experimentation / test_experimentation_clique_connect.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_connect.c
23  * @brief test case to connect 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;
94 };
95
96
97 struct ExperimentationPeer bp_slaves[NUM_PEERS];
98
99 /**
100  * Shutdown nicely
101  *
102  * @param cls NULL
103  * @param tc the task context
104  */
105 static void
106 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
107 {
108   unsigned int peer;
109         shutdown_task = GNUNET_SCHEDULER_NO_TASK;
110
111   for (peer = 0; peer < NUM_PEERS; peer++)
112   {
113         if (NULL != bp_slaves[peer].stat_op)
114                 GNUNET_TESTBED_operation_done (bp_slaves[peer].stat_op);
115         bp_slaves[peer].stat_op = NULL;
116   }
117
118   if (NULL != op)
119   {
120     GNUNET_TESTBED_operation_done (op);
121     op = NULL;
122   }
123   GNUNET_SCHEDULER_shutdown ();
124 }
125
126 /**
127  * Controller event callback
128  *
129  * @param cls NULL
130  * @param event the controller event
131  */
132 static void
133 controller_event_cb (void *cls,
134                      const struct GNUNET_TESTBED_EventInformation *event)
135 {
136   switch (event->type)
137   {
138   case GNUNET_TESTBED_ET_CONNECT:
139     overlay_connects++;
140     if ((NUM_PEERS * (NUM_PEERS - 1)) == overlay_connects)
141     {
142       result = GNUNET_OK;
143       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers connected \n", NUM_PEERS);
144       if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
145       {
146                 GNUNET_SCHEDULER_cancel (shutdown_task);
147       }
148       shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
149     }
150     break;
151   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
152     break;
153   default:
154     GNUNET_break (0);
155     result = GNUNET_SYSERR;
156     GNUNET_SCHEDULER_cancel (shutdown_task);
157     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
158   }
159 }
160
161 static void
162 check_end ()
163 {
164         static int last_active_value = 0;
165         static int last_issuer_value = 0;
166         static int last_experiments_value = 0;
167   unsigned int peer;
168   unsigned int total_active = 0;
169   unsigned int total_inactive = 0;
170   unsigned int total_requested = 0;
171   unsigned int issuer = 0;
172   unsigned int experiments = 0;
173
174         for (peer = 0; peer < NUM_PEERS; peer++)
175         {
176                         total_active += bp_slaves[peer].active_nodes;
177                         total_requested += bp_slaves[peer].requested_nodes;
178                         total_inactive += bp_slaves[peer].inactive_nodes;
179                         if (NUM_ISSUER == bp_slaves[peer].issuer)
180                                 issuer ++;
181                         if (NUM_EXPERIMENTS == bp_slaves[peer].experiments)
182                                 experiments ++;
183         }
184         if ((last_issuer_value < issuer) && (issuer == NUM_PEERS))
185                 fprintf (stderr, "I");
186         last_issuer_value = issuer;
187
188         if ((last_experiments_value < experiments) && (experiments == NUM_PEERS))
189                 fprintf (stderr, "E");
190         last_experiments_value = experiments;
191
192         if (last_active_value < total_active)
193                 fprintf (stderr, ".");
194         last_active_value = total_active;
195
196
197         if ((total_active == (NUM_PEERS * (NUM_PEERS -1))) &&
198                  (total_requested == 0) && (total_inactive == 0) &&
199                  (issuer == NUM_PEERS) && (experiments == NUM_PEERS))
200         {
201                         fprintf (stderr, "\n");
202                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers active in a clique\n", NUM_PEERS);
203                         GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
204         }
205 }
206
207
208
209 /**
210  * Callback function to process statistic values.
211  *
212  * @param cls struct StatsContext
213  * @param subsystem name of subsystem that created the statistic
214  * @param name the name of the datum
215  * @param value the current value
216  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
217  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
218  */
219 static int
220 stat_iterator (void *cls, const char *subsystem, const char *name,
221                      uint64_t value, int is_persistent)
222 {
223   struct ExperimentationPeer *peer = cls;
224
225         if (0 == strcmp (name, "# nodes active"))
226         {
227                         peer->active_nodes = value;
228         }
229         if (0 == strcmp (name, "# nodes inactive"))
230         {
231                         peer->inactive_nodes = value;
232         }
233         if (0 == strcmp (name, "# nodes requested"))
234         {
235                         peer->requested_nodes = value;
236         }
237         if (0 == strcmp (name, "# issuer"))
238         {
239                         peer->issuer = value;
240         }
241         if (0 == strcmp (name, "# experiments"))
242         {
243                         peer->experiments = value;
244         }
245
246         check_end ();
247
248         return GNUNET_OK;
249 }
250
251 /**
252  * Called after successfully opening a connection to a peer's statistics
253  * service; we register statistics monitoring here.
254  *
255  * @param cls the callback closure from functions generating an operation
256  * @param op the operation that has been finished
257  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
258  * @param emsg error message in case the operation has failed; will be NULL if
259  *          operation has executed successfully.
260  */
261 static void
262 stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
263               void *ca_result, const char *emsg )
264 {
265   struct GNUNET_STATISTICS_Handle *sh = ca_result;
266   struct ExperimentationPeer *peer = cls;
267
268   if (NULL != emsg)
269   {
270     GNUNET_break (0);
271     return;
272   }
273
274   GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
275                 (sh, "experimentation", "# nodes active",
276                  stat_iterator, peer));
277   GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
278                 (sh, "experimentation", "# nodes inactive",
279                  stat_iterator, peer));
280   GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
281                 (sh, "experimentation", "# nodes requested",
282                  stat_iterator, peer));
283   GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
284                 (sh, "experimentation", "# issuer",
285                  stat_iterator, peer));
286   GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch
287                 (sh, "experimentation", "# experiments",
288                  stat_iterator, peer));
289 }
290
291 /**
292  * Called to open a connection to the peer's statistics
293  *
294  * @param cls peer context
295  * @param cfg configuration of the peer to connect to; will be available until
296  *          GNUNET_TESTBED_operation_done() is called on the operation returned
297  *          from GNUNET_TESTBED_service_connect()
298  * @return service handle to return in 'op_result', NULL on error
299  */
300 static void *
301 stat_connect_adapter (void *cls,
302                       const struct GNUNET_CONFIGURATION_Handle *cfg)
303 {
304   struct ExperimentationPeer *peer = cls;
305   peer->sh = GNUNET_STATISTICS_create ("experimentation", cfg);
306   if (NULL == peer->sh)
307     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create statistics \n");
308   return peer->sh;
309 }
310
311
312 /**
313  * Called to disconnect from peer's statistics service
314  *
315  * @param cls peer context
316  * @param op_result service handle returned from the connect adapter
317  */
318 static void
319 stat_disconnect_adapter (void *cls, void *op_result)
320 {
321   struct ExperimentationPeer *peer = cls;
322
323   GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
324                 (peer->sh, "experimentation", "# nodes active",
325                  stat_iterator, peer));
326   GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
327                 (peer->sh, "experimentation", "# nodes inactive",
328                  stat_iterator, peer));
329   GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
330                 (peer->sh, "experimentation", "# nodes requested",
331                  stat_iterator, peer));
332   GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
333                 (peer->sh, "experimentation", "# issuer",
334                  stat_iterator, peer));
335   GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel
336                 (peer->sh, "experimentation", "# experiments",
337                  stat_iterator, peer));
338   GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
339   peer->sh = NULL;
340 }
341
342
343
344 /**
345  * Signature of a main function for a testcase.
346  *
347  * @param cls closure
348  * @param h the run handle
349  * @param num_peers number of peers in 'peers'
350  * @param peers_ handle to peers run in the testbed
351  * @param links_succeeded the number of overlay link connection attempts that
352  *          succeeded
353  * @param links_failed the number of overlay link connection attempts that
354  *          failed
355  */
356 static void
357 test_master (void *cls, 
358              struct GNUNET_TESTBED_RunHandle *h,
359              unsigned int num_peers,
360              struct GNUNET_TESTBED_Peer **peers_,
361              unsigned int links_succeeded,
362              unsigned int links_failed)
363 {
364   unsigned int peer;
365
366   GNUNET_assert (NULL == cls);
367   GNUNET_assert (NUM_PEERS == num_peers);
368   GNUNET_assert (NULL != peers_);
369   for (peer = 0; peer < num_peers; peer++)
370   {
371     GNUNET_assert (NULL != peers_[peer]);
372     /* Connect to peer's statistic service */
373     bp_slaves[peer].stat_op = GNUNET_TESTBED_service_connect (NULL,
374                                                                                                                                 peers_[peer], "statistics",
375                                                                                                                                 &stat_comp_cb, &bp_slaves[peer],
376                                     &stat_connect_adapter,
377                                     &stat_disconnect_adapter,
378                                     &bp_slaves[peer]);
379
380   }
381   peers = peers_;
382   overlay_connects = 0;
383   op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
384                                                   NULL,
385                                                   NULL,
386                                                   GNUNET_TESTBED_TOPOLOGY_CLIQUE,
387                                                   /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
388                                                   /* NUM_PEERS, */
389                                                   GNUNET_TESTBED_TOPOLOGY_OPTION_END);
390   GNUNET_assert (NULL != op);
391   shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
392 }
393
394
395 /**
396  * Main function
397  */
398 int
399 main (int argc, char **argv)
400 {
401   uint64_t event_mask;
402
403   result = GNUNET_SYSERR;
404   event_mask = 0;
405   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
406   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
407   (void) GNUNET_TESTBED_test_run ("test_experimentation_clique_connect",
408                                   "test_experimentation_clique.conf", NUM_PEERS,
409                                   event_mask, &controller_event_cb, NULL,
410                                   &test_master, NULL);
411   if (GNUNET_OK != result)
412     return 1;
413   return 0;
414 }
415
416 /* end of test_experimentation_clique_connect.c */