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