migrating towards XDG configuration specification (#3000)
[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_outbound_running;
95   unsigned int experiments_inbound_running;
96 };
97
98
99 struct ExperimentationPeer bp_slaves[NUM_PEERS];
100
101 /**
102  * Shutdown nicely
103  *
104  * @param cls NULL
105  * @param tc the task context
106  */
107 static void
108 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   unsigned int peer;
111         shutdown_task = GNUNET_SCHEDULER_NO_TASK;
112
113   for (peer = 0; peer < NUM_PEERS; peer++)
114   {
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;
118   }
119
120   if (NULL != op)
121   {
122     GNUNET_TESTBED_operation_done (op);
123     op = NULL;
124   }
125   GNUNET_SCHEDULER_shutdown ();
126 }
127
128 /**
129  * Controller event callback
130  *
131  * @param cls NULL
132  * @param event the controller event
133  */
134 static void
135 controller_event_cb (void *cls,
136                      const struct GNUNET_TESTBED_EventInformation *event)
137 {
138   switch (event->type)
139   {
140   case GNUNET_TESTBED_ET_CONNECT:
141     overlay_connects++;
142     if ((NUM_PEERS * (NUM_PEERS - 1)) == overlay_connects)
143     {
144       result = GNUNET_OK;
145       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All %u peers connected \n", NUM_PEERS);
146       if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
147       {
148                 GNUNET_SCHEDULER_cancel (shutdown_task);
149       }
150       shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
151     }
152     break;
153   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
154     break;
155   default:
156     GNUNET_break (0);
157     result = GNUNET_SYSERR;
158     GNUNET_SCHEDULER_cancel (shutdown_task);
159     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
160   }
161 }
162
163 static void
164 check_end ()
165 {
166         static int last_in_experiments_value = 0;
167         static int last_out_experiments_value = 0;
168   unsigned int peer;
169   unsigned int t_running_outbound_experiments = 0;
170   unsigned int t_running_inbound_experiments = 0;
171
172         for (peer = 0; peer < NUM_PEERS; peer++)
173         {
174                 t_running_outbound_experiments += bp_slaves[peer].experiments_outbound_running;
175                 t_running_inbound_experiments += bp_slaves[peer].experiments_inbound_running;
176
177         }
178
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;
186
187
188
189         if ((t_running_inbound_experiments == (NUM_PEERS * NUM_EXPERIMENTS)) &&
190                         (t_running_outbound_experiments == (NUM_PEERS * NUM_EXPERIMENTS)))
191         {
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);
195         }
196 }
197
198
199
200 /**
201  * Callback function to process statistic values.
202  *
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
209  */
210 static int
211 stat_iterator (void *cls, const char *subsystem, const char *name,
212                      uint64_t value, int is_persistent)
213 {
214   struct ExperimentationPeer *peer = cls;
215
216         if (0 == strcmp (name, "# experiments active"))
217         {
218                         peer->experiments_active = value;
219         }
220
221         if (0 == strcmp (name, "# experiments outbound running"))
222         {
223                         peer->experiments_outbound_running = value;
224         }
225
226         if (0 == strcmp (name, "# experiments inbound running"))
227         {
228                         peer->experiments_inbound_running = value;
229         }
230
231
232         check_end ();
233
234         return GNUNET_OK;
235 }
236
237 /**
238  * Called after successfully opening a connection to a peer's statistics
239  * service; we register statistics monitoring here.
240  *
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.
246  */
247 static void
248 stat_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
249               void *ca_result, const char *emsg )
250 {
251   //struct GNUNET_STATISTICS_Handle *sh = ca_result;
252   struct ExperimentationPeer *peer = cls;
253
254   if (NULL != emsg)
255   {
256     GNUNET_break (0);
257     return;
258   }
259
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));
269 }
270
271 /**
272  * Called to open a connection to the peer's statistics
273  *
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
279  */
280 static void *
281 stat_connect_adapter (void *cls,
282                       const struct GNUNET_CONFIGURATION_Handle *cfg)
283 {
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");
288   return peer->sh;
289 }
290
291
292 /**
293  * Called to disconnect from peer's statistics service
294  *
295  * @param cls peer context
296  * @param op_result service handle returned from the connect adapter
297  */
298 static void
299 stat_disconnect_adapter (void *cls, void *op_result)
300 {
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);
312   peer->sh = NULL;
313 }
314
315
316
317 /**
318  * Signature of a main function for a testcase.
319  *
320  * @param cls closure
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
325  *          succeeded
326  * @param links_failed the number of overlay link connection attempts that
327  *          failed
328  */
329 static void
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)
336 {
337   unsigned int peer;
338
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++)
343   {
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,
351                                     &bp_slaves[peer]);
352
353   }
354   peers = peers_;
355   overlay_connects = 0;
356   op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
357                                                   NULL,
358                                                   NULL,
359                                                   GNUNET_TESTBED_TOPOLOGY_CLIQUE,
360                                                   /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
361                                                   /* NUM_PEERS, */
362                                                   GNUNET_TESTBED_TOPOLOGY_OPTION_END);
363   GNUNET_assert (NULL != op);
364   shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, do_shutdown, NULL);
365 }
366
367
368 /**
369  * Main function
370  */
371 int
372 main (int argc, char **argv)
373 {
374   uint64_t event_mask;
375
376   result = GNUNET_SYSERR;
377   event_mask = 0;
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,
383                                   &test_master, NULL);
384   if (GNUNET_OK != result)
385     return 1;
386   return 0;
387 }
388
389 /* end of test_experimentation_clique_run.c */