even nicer indentation, thanks to LRN's indent patch
[oweals/gnunet.git] / src / mesh / test_mesh_small.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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  * @file mesh/test_mesh_small.c
22  *
23  * @brief Test for the mesh service.
24  */
25 #include "platform.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_mesh_service.h"
28
29 #define VERBOSE GNUNET_NO
30
31 struct MeshPeer
32 {
33   struct MeshPeer *prev;
34
35   struct MeshPeer *next;
36
37   struct GNUNET_TESTING_Daemon *daemon;
38
39   struct GNUNET_MESH_Handle *mesh_handle;
40 };
41
42
43 struct StatsContext
44 {
45   unsigned long long total_mesh_bytes;
46 };
47
48
49 // static struct MeshPeer *peer_head;
50 // 
51 // static struct MeshPeer *peer_tail;
52
53 /**
54  * How long until we give up on connecting the peers?
55  */
56 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
57
58 static int ok;
59
60 /**
61  * Be verbose
62  */
63 static int verbose;
64
65 /**
66  * Total number of peers in the test.
67  */
68 static unsigned long long num_peers;
69
70 /**
71  * Global configuration file
72  */
73 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
74
75 /**
76  * Total number of currently running peers.
77  */
78 static unsigned long long peers_running;
79
80 /**
81  * Current round we are in.
82  */
83 static unsigned long long current_round;
84
85 /**
86  * Peers desired in the next round.
87  */
88 static unsigned long long peers_next_round;
89
90 /**
91  * Total number of connections in the whole network.
92  */
93 static unsigned int total_connections;
94
95 /**
96  * The currently running peer group.
97  */
98 static struct GNUNET_TESTING_PeerGroup *pg;
99
100 /**
101  * File to report results to.
102  */
103 static struct GNUNET_DISK_FileHandle *output_file;
104
105 /**
106  * File to log connection info, statistics to.
107  */
108 static struct GNUNET_DISK_FileHandle *data_file;
109
110 /**
111  * How many data points to capture before triggering next round?
112  */
113 static struct GNUNET_TIME_Relative wait_time;
114
115 /**
116  * Task called to disconnect peers.
117  */
118 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
119
120 /**
121  * Task called to shutdown test.
122  */
123 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
124
125 /**
126  * Task used to churn the network.
127  */
128 static GNUNET_SCHEDULER_TaskIdentifier churn_task;
129
130 static char *topology_file;
131
132 static char *data_filename;
133
134 /**
135  * Check whether peers successfully shut down.
136  */
137 static void
138 shutdown_callback (void *cls, const char *emsg)
139 {
140   if (emsg != NULL)
141   {
142 #if VERBOSE
143     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
144 #endif
145     if (ok == 0)
146       ok = 666;
147   }
148   else
149   {
150 #if VERBOSE
151     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
152 #endif
153     ok = 0;
154   }
155 }
156
157
158 static void
159 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 {
161 //   struct NSEPeer *pos;
162 #if VERBOSE
163   fprintf (stderr, "Ending test.\n");
164 #endif
165
166   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
167   {
168     GNUNET_SCHEDULER_cancel (disconnect_task);
169     disconnect_task = GNUNET_SCHEDULER_NO_TASK;
170   }
171 //   while (NULL != (pos = peer_head))
172 //     {
173 //       if (pos->nse_handle != NULL)
174 //         GNUNET_NSE_disconnect(pos->nse_handle);
175 //       GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
176 //       GNUNET_free(pos);
177 //     }
178
179   if (data_file != NULL)
180     GNUNET_DISK_file_close (data_file);
181   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
182 }
183
184
185 static void
186 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
187 {
188 //   struct NSEPeer *current_peer;
189   unsigned int i;
190
191 #if VERBOSE
192   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to mesh service of peers\n");
193 #endif
194   for (i = 0; i < num_peers; i++)
195   {
196 //       if ((connection_limit > 0) && (i % (num_peers / connection_limit) != 0))
197     continue;
198 #if VERBOSE
199     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
200                 "test_mesh_small: connecting to mesh service of peer %d\n", i);
201 #endif
202 /*      current_peer = GNUNET_malloc(sizeof(struct NSEPeer));
203       current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i);
204       if (GNUNET_YES == GNUNET_TESTING_daemon_running(GNUNET_TESTING_daemon_get(pg, i)))
205         {
206           current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg,
207                                                          &handle_estimate, 
208                                                          current_peer);
209           GNUNET_assert(current_peer->nse_handle != NULL);
210         }
211       GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);*/
212   }
213 }
214
215
216 static void
217 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
218
219
220 /**
221  * Continuation called by the "get_all" and "get" functions.
222  *
223  * @param cls struct StatsContext
224  * @param success GNUNET_OK if statistics were
225  *        successfully obtained, GNUNET_SYSERR if not.
226  */
227 static void
228 stats_finished_callback (void *cls, int success)
229 {
230   struct StatsContext *stats_context = cls;
231   char *buf;
232   int buf_len;
233
234   if ((GNUNET_OK == success) && (data_file != NULL))
235   {
236     /* Stats lookup successful, write out data */
237     buf = NULL;
238     buf_len =
239         GNUNET_asprintf (&buf, "TOTAL_MESH_BYTES: %u\n",
240                          stats_context->total_mesh_bytes);
241     if (buf_len > 0)
242     {
243       GNUNET_DISK_file_write (data_file, buf, buf_len);
244     }
245     GNUNET_free_non_null (buf);
246   }
247
248   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
249   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
250   GNUNET_free (stats_context);
251 }
252
253
254 /**
255  * Callback function to process statistic values.
256  *
257  * @param cls struct StatsContext
258  * @param peer the peer the statistics belong to
259  * @param subsystem name of subsystem that created the statistic
260  * @param name the name of the datum
261  * @param value the current value
262  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
263  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
264  */
265 static int
266 statistics_iterator (void *cls, const struct GNUNET_PeerIdentity *peer,
267                      const char *subsystem, const char *name, uint64_t value,
268                      int is_persistent)
269 {
270   struct StatsContext *stats_context = cls;
271
272 // 
273 //   if ( (0 == strstr(subsystem, "nse")) && 
274 //        (0 == strstr(name, "# flood messages received")) )
275   stats_context->total_mesh_bytes += 1; //value;
276   return GNUNET_OK;
277 }
278
279
280 static void
281 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
282 {
283 //   struct NSEPeer *pos;
284   char *buf;
285   struct StatsContext *stats_context;
286
287   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting mesh service of peers\n");
288   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
289 //   pos = peer_head;
290 //   while (NULL != (pos = peer_head))
291 //     {
292 //       if (pos->nse_handle != NULL)
293 //         {
294 //           GNUNET_NSE_disconnect(pos->nse_handle);
295 //           pos->nse_handle = NULL;
296 //         }
297 //       GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
298 //       GNUNET_free(pos);
299 //     }
300
301   GNUNET_asprintf (&buf, "round%llu", current_round);
302   if (GNUNET_OK ==
303       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
304                                              buf, &peers_next_round))
305   {
306     current_round++;
307     GNUNET_assert (churn_task == GNUNET_SCHEDULER_NO_TASK);
308     churn_task = GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
309   }
310   else                          /* No more rounds, let's shut it down! */
311   {
312     stats_context = GNUNET_malloc (sizeof (struct StatsContext));
313     GNUNET_SCHEDULER_cancel (shutdown_handle);
314     shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
315     GNUNET_TESTING_get_statistics (pg, &stats_finished_callback,
316                                    &statistics_iterator, stats_context);
317   }
318   GNUNET_free (buf);
319 }
320
321
322 /**
323  * FIXME.
324  *
325  * @param cls unused
326  * @param emsg NULL on success
327  */
328 static void
329 topology_output_callback (void *cls, const char *emsg)
330 {
331   disconnect_task =
332       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
333   GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
334 }
335
336
337 /**
338  * FIXME.
339  *
340  * @param cls closure
341  * @param emsg NULL on success
342  */
343 static void
344 churn_callback (void *cls, const char *emsg)
345 {
346   char *temp_output_file;
347
348   if (emsg == NULL)             /* Everything is okay! */
349   {
350     peers_running = peers_next_round;
351     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
352                 "Round %llu, churn finished successfully.\n", current_round);
353     GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
354     GNUNET_asprintf (&temp_output_file, "%s_%llu.dot", topology_file,
355                      current_round);
356     GNUNET_TESTING_peergroup_topology_to_file (pg, temp_output_file,
357                                                &topology_output_callback, NULL);
358     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Writing topology to file %s\n",
359                 temp_output_file);
360     GNUNET_free (temp_output_file);
361   }
362   else
363   {
364     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %llu, churn FAILED!!\n",
365                 current_round);
366     GNUNET_SCHEDULER_cancel (shutdown_handle);
367     shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
368   }
369 }
370
371
372 static void
373 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
374 {
375   /* peers_running = GNUNET_TESTING_daemons_running(pg); */
376   churn_task = GNUNET_SCHEDULER_NO_TASK;
377   if (peers_next_round == peers_running)
378   {
379     /* Nothing to do... */
380     GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
381     GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
382     disconnect_task =
383         GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
384     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %lu, doing nothing!\n",
385                 current_round);
386   }
387   else
388   {
389     if (peers_next_round > num_peers)
390     {
391       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
392                   "Asked to turn on more peers than we have!!\n");
393       GNUNET_SCHEDULER_cancel (shutdown_handle);
394       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
395     }
396     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
397                 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
398                 current_round,
399                 (peers_running >
400                  peers_next_round) ? peers_running - peers_next_round : 0,
401                 (peers_next_round >
402                  peers_running) ? peers_next_round - peers_running : 0);
403     GNUNET_TESTING_daemons_churn (pg, "nse",
404                                   (peers_running >
405                                    peers_next_round) ? peers_running -
406                                   peers_next_round : 0,
407                                   (peers_next_round >
408                                    peers_running) ? peers_next_round -
409                                   peers_running : 0, wait_time, &churn_callback,
410                                   NULL);
411   }
412 }
413
414
415 static void
416 my_cb (void *cls, const char *emsg)
417 {
418   char *buf;
419   int buf_len;
420
421   if (emsg != NULL)
422   {
423     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424                 "Peergroup callback called with error, aborting test!\n");
425     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
426     ok = 1;
427     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
428     return;
429   }
430 #if VERBOSE
431   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer Group started successfully!\n");
432 #endif
433   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
434               total_connections);
435   if (data_file != NULL)
436   {
437     buf = NULL;
438     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
439     if (buf_len > 0)
440       GNUNET_DISK_file_write (data_file, buf, buf_len);
441     GNUNET_free (buf);
442   }
443   peers_running = GNUNET_TESTING_daemons_running (pg);
444
445   GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
446   disconnect_task =
447       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
448
449 }
450
451
452 /**
453  * Function that will be called whenever two daemons are connected by
454  * the testing library.
455  *
456  * @param cls closure
457  * @param first peer id for first daemon
458  * @param second peer id for the second daemon
459  * @param distance distance between the connected peers
460  * @param first_cfg config for the first daemon
461  * @param second_cfg config for the second daemon
462  * @param first_daemon handle for the first daemon
463  * @param second_daemon handle for the second daemon
464  * @param emsg error message (NULL on success)
465  */
466 static void
467 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
468             const struct GNUNET_PeerIdentity *second, uint32_t distance,
469             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
470             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
471             struct GNUNET_TESTING_Daemon *first_daemon,
472             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
473 {
474   if (emsg == NULL)
475     total_connections++;
476 }
477
478
479 static void
480 run (void *cls, char *const *args, const char *cfgfile,
481      const struct GNUNET_CONFIGURATION_Handle *cfg)
482 {
483   char *temp_str;
484   unsigned long long temp_wait;
485   struct GNUNET_TESTING_Host *hosts;
486
487   ok = 1;
488   testing_cfg = (struct GNUNET_CONFIGURATION_Handle *) cfg;     // GNUNET_CONFIGURATION_create();
489 #if VERBOSE
490   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
491   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
492                                          "use_progressbars", "YES");
493 #endif
494   if (GNUNET_OK !=
495       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
496                                              "num_peers", &num_peers))
497   {
498     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
499                 "Option TESTING:NUM_PEERS is required!\n");
500     return;
501   }
502
503   if (GNUNET_OK !=
504       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
505                                              "wait_time", &temp_wait))
506   {
507     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
508                 "Option nsetest_mesh_small:wait_time is required!\n");
509     return;
510   }
511
512   if (GNUNET_OK !=
513       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
514                                              "topology_output_file",
515                                              &topology_file))
516   {
517     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
518                 "Option test_mesh_small:topology_output_file is required!\n");
519     return;
520   }
521
522   if (GNUNET_OK !=
523       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
524                                              "data_output_file",
525                                              &data_filename))
526   {
527     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
528                 "Option test_mesh_small:data_output_file is required!\n");
529     return;
530   }
531
532   data_file =
533       GNUNET_DISK_file_open (data_filename,
534                              GNUNET_DISK_OPEN_READWRITE |
535                              GNUNET_DISK_OPEN_CREATE,
536                              GNUNET_DISK_PERM_USER_READ |
537                              GNUNET_DISK_PERM_USER_WRITE);
538   if (data_file == NULL)
539     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
540                 data_filename);
541   GNUNET_free (data_filename);
542
543   wait_time =
544       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
545
546   if (GNUNET_YES ==
547       GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
548                                              "output_file", &temp_str))
549   {
550     output_file =
551         GNUNET_DISK_file_open (temp_str,
552                                GNUNET_DISK_OPEN_READWRITE |
553                                GNUNET_DISK_OPEN_CREATE,
554                                GNUNET_DISK_PERM_USER_READ |
555                                GNUNET_DISK_PERM_USER_WRITE);
556     if (output_file == NULL)
557       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
558                   temp_str);
559   }
560   GNUNET_free_non_null (temp_str);
561
562   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
563
564   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
565                                        &connect_cb, &my_cb, NULL, hosts);
566   GNUNET_assert (pg != NULL);
567   shutdown_handle =
568       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
569                                     &shutdown_task, NULL);
570 }
571
572
573
574 /**
575  * test_mesh_small command line options
576  */
577 static struct GNUNET_GETOPT_CommandLineOption options[] = {
578   {'V', "verbose", NULL,
579    gettext_noop ("be verbose (print progress information)"),
580    0, &GNUNET_GETOPT_set_one, &verbose},
581   GNUNET_GETOPT_OPTION_END
582 };
583
584
585 int
586 main (int argc, char *argv[])
587 {
588   GNUNET_log_setup ("test_mesh_small",
589 #if VERBOSE
590                     "DEBUG",
591 #else
592                     "WARNING",
593 #endif
594                     NULL);
595   GNUNET_PROGRAM_run (argc, argv, "test_mesh_small",
596                       gettext_noop ("Test mesh in a small network."), options,
597                       &run, NULL);
598 #if REMOVE_DIR
599   GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
600 #endif
601   return ok;
602 }
603
604 /* end of test_mesh_small.c */