oops, one more unix path removed
[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, &tunnel_cleaner, handlers, &app);
267 #if VERBOSE
268   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected to mesh service of peer %s\n",
269               GNUNET_i2s (&d->id));
270 #endif
271   t = GNUNET_MESH_tunnel_create (h, &ch, &dh, NULL);
272   GNUNET_MESH_tunnel_destroy (t);
273   GNUNET_MESH_disconnect (h);
274 }
275
276
277 static void
278 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
279
280
281 /**
282  * Continuation called by the "get_all" and "get" functions.
283  *
284  * @param cls struct StatsContext
285  * @param success GNUNET_OK if statistics were
286  *        successfully obtained, GNUNET_SYSERR if not.
287  */
288 static void
289 stats_finished_callback (void *cls, int success)
290 {
291   struct StatsContext *stats_context = cls;
292   char *buf;
293   int buf_len;
294
295   if ((GNUNET_OK == success) && (data_file != NULL))
296   {
297     /* Stats lookup successful, write out data */
298     buf = NULL;
299     buf_len =
300         GNUNET_asprintf (&buf, "TOTAL_MESH_BYTES: %u\n",
301                          stats_context->total_mesh_bytes);
302     if (buf_len > 0)
303     {
304       GNUNET_DISK_file_write (data_file, buf, buf_len);
305     }
306     GNUNET_free_non_null (buf);
307   }
308
309   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
310   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
311   GNUNET_free (stats_context);
312 }
313
314
315 /**
316  * Callback function to process statistic values.
317  *
318  * @param cls struct StatsContext
319  * @param peer the peer the statistics belong to
320  * @param subsystem name of subsystem that created the statistic
321  * @param name the name of the datum
322  * @param value the current value
323  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
324  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
325  */
326 static int
327 statistics_iterator (void *cls, const struct GNUNET_PeerIdentity *peer,
328                      const char *subsystem, const char *name, uint64_t value,
329                      int is_persistent)
330 {
331   struct StatsContext *stats_context = cls;
332
333 //
334 //   if ( (0 == strstr(subsystem, "nse")) &&
335 //        (0 == strstr(name, "# flood messages received")) )
336   stats_context->total_mesh_bytes += 1; //value;
337   return GNUNET_OK;
338 }
339
340
341 static void
342 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
343 {
344 //   struct NSEPeer *pos;
345   char *buf;
346   struct StatsContext *stats_context;
347
348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting mesh service of peers\n");
349   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
350 //   pos = peer_head;
351 //   while (NULL != (pos = peer_head))
352 //     {
353 //       if (pos->nse_handle != NULL)
354 //         {
355 //           GNUNET_NSE_disconnect(pos->nse_handle);
356 //           pos->nse_handle = NULL;
357 //         }
358 //       GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
359 //       GNUNET_free(pos);
360 //     }
361
362   GNUNET_asprintf (&buf, "round%llu", current_round);
363   if (GNUNET_OK ==
364       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
365                                              buf, &peers_next_round))
366   {
367     current_round++;
368     GNUNET_assert (churn_task == GNUNET_SCHEDULER_NO_TASK);
369     churn_task = GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
370   }
371   else
372   {                             /* No more rounds, let's shut it down! */
373
374     stats_context = GNUNET_malloc (sizeof (struct StatsContext));
375     GNUNET_SCHEDULER_cancel (shutdown_handle);
376     shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
377     GNUNET_TESTING_get_statistics (pg, &stats_finished_callback,
378                                    &statistics_iterator, stats_context);
379   }
380   GNUNET_free (buf);
381 }
382
383
384 /**
385  * FIXME.
386  *
387  * @param cls unused
388  * @param emsg NULL on success
389  */
390 static void
391 topology_output_callback (void *cls, const char *emsg)
392 {
393   disconnect_task =
394       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
395   GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
396 }
397
398
399 /**
400  * FIXME.
401  *
402  * @param cls closure
403  * @param emsg NULL on success
404  */
405 static void
406 churn_callback (void *cls, const char *emsg)
407 {
408   char *temp_output_file;
409
410   if (emsg == NULL)
411   {                             /* Everything is okay! */
412     peers_running = peers_next_round;
413     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
414                 "Round %llu, churn finished successfully.\n", current_round);
415     GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
416     GNUNET_asprintf (&temp_output_file, "%s_%llu.dot", topology_file,
417                      current_round);
418     GNUNET_TESTING_peergroup_topology_to_file (pg, temp_output_file,
419                                                &topology_output_callback, NULL);
420     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Writing topology to file %s\n",
421                 temp_output_file);
422     GNUNET_free (temp_output_file);
423   }
424   else
425   {
426     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %llu, churn FAILED!!\n",
427                 current_round);
428     GNUNET_SCHEDULER_cancel (shutdown_handle);
429     shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
430   }
431 }
432
433
434 static void
435 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
436 {
437   /* peers_running = GNUNET_TESTING_daemons_running(pg); */
438   churn_task = GNUNET_SCHEDULER_NO_TASK;
439   if (peers_next_round == peers_running)
440   {
441     /* Nothing to do... */
442     GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
443     GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
444     disconnect_task =
445         GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
446     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %lu, doing nothing!\n",
447                 current_round);
448   }
449   else
450   {
451     if (peers_next_round > num_peers)
452     {
453       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
454                   "Asked to turn on more peers than we have!!\n");
455       GNUNET_SCHEDULER_cancel (shutdown_handle);
456       GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
457     }
458     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
459                 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
460                 current_round,
461                 (peers_running >
462                  peers_next_round) ? peers_running - peers_next_round : 0,
463                 (peers_next_round >
464                  peers_running) ? peers_next_round - peers_running : 0);
465     GNUNET_TESTING_daemons_churn (pg, "nse",
466                                   (peers_running >
467                                    peers_next_round) ? peers_running -
468                                   peers_next_round : 0,
469                                   (peers_next_round >
470                                    peers_running) ? peers_next_round -
471                                   peers_running : 0, wait_time, &churn_callback,
472                                   NULL);
473   }
474 }
475
476
477 /**
478  * peergroup_ready: start test when all peers are connected
479  * @param cls closure
480  * @param emsg error message
481  */
482 static void
483 peergroup_ready (void *cls, const char *emsg)
484 {
485   char *buf;
486   int buf_len;
487
488   if (emsg != NULL)
489   {
490     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491                 "Peergroup callback called with error, aborting test!\n");
492     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
493     ok = 1;
494     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
495     return;
496   }
497 #if VERBOSE
498   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer Group started successfully!\n");
499 #endif
500
501   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
502               total_connections);
503   if (data_file != NULL)
504   {
505     buf = NULL;
506     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
507     if (buf_len > 0)
508       GNUNET_DISK_file_write (data_file, buf, buf_len);
509     GNUNET_free (buf);
510   }
511   peers_running = GNUNET_TESTING_daemons_running (pg);
512
513   GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
514   disconnect_task =
515       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
516
517 }
518
519
520 /**
521  * Function that will be called whenever two daemons are connected by
522  * the testing library.
523  *
524  * @param cls closure
525  * @param first peer id for first daemon
526  * @param second peer id for the second daemon
527  * @param distance distance between the connected peers
528  * @param first_cfg config for the first daemon
529  * @param second_cfg config for the second daemon
530  * @param first_daemon handle for the first daemon
531  * @param second_daemon handle for the second daemon
532  * @param emsg error message (NULL on success)
533  */
534 static void
535 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
536             const struct GNUNET_PeerIdentity *second, uint32_t distance,
537             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
538             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
539             struct GNUNET_TESTING_Daemon *first_daemon,
540             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
541 {
542   if (emsg == NULL)
543     total_connections++;
544 }
545
546
547 /**
548  * run: load configuration options and schedule test to run (start peergroup)
549  * @param cls closure
550  * @param args argv
551  * @param cfgfile configuration file name (can be NULL)
552  * @param cfg configuration handle
553  */
554 static void
555 run (void *cls, char *const *args, const char *cfgfile,
556      const struct GNUNET_CONFIGURATION_Handle *cfg)
557 {
558   char *temp_str;
559   unsigned long long temp_wait;
560   struct GNUNET_TESTING_Host *hosts;
561
562   ok = 1;
563   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
564
565   GNUNET_log_setup ("test_mesh_small",
566 #if VERBOSE
567                     "DEBUG",
568 #else
569                     "WARNING",
570 #endif
571                     NULL);
572
573 #if VERBOSE
574   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
575   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
576                                          "use_progressbars", "YES");
577 #endif
578
579   if (GNUNET_OK !=
580       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
581                                              "num_peers", &num_peers))
582   {
583     GNUNET_CONFIGURATION_load (testing_cfg, "test_mesh_small.conf");
584     if (GNUNET_OK !=
585         GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
586                                                "num_peers", &num_peers))
587     {
588       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
589                   "Option TESTING:NUM_PEERS is required!\n");
590       return;
591     }
592   }
593
594   if (GNUNET_OK !=
595       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
596                                              "wait_time", &temp_wait))
597   {
598     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
599                 "Option nsetest_mesh_small:wait_time is required!\n");
600     return;
601   }
602
603   if (GNUNET_OK !=
604       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
605                                              "topology_output_file",
606                                              &topology_file))
607   {
608     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609                 "Option test_mesh_small:topology_output_file is required!\n");
610     return;
611   }
612
613   if (GNUNET_OK !=
614       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
615                                              "data_output_file",
616                                              &data_filename))
617   {
618     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
619                 "Option test_mesh_small:data_output_file is required!\n");
620     return;
621   }
622
623   data_file =
624       GNUNET_DISK_file_open (data_filename,
625                              GNUNET_DISK_OPEN_READWRITE |
626                              GNUNET_DISK_OPEN_CREATE,
627                              GNUNET_DISK_PERM_USER_READ |
628                              GNUNET_DISK_PERM_USER_WRITE);
629   if (data_file == NULL)
630   {
631     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
632                 data_filename);
633     GNUNET_free (data_filename);
634   }
635
636   wait_time =
637       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
638
639   if (GNUNET_YES ==
640       GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
641                                              "output_file", &temp_str))
642   {
643     output_file =
644         GNUNET_DISK_file_open (temp_str,
645                                GNUNET_DISK_OPEN_READWRITE |
646                                GNUNET_DISK_OPEN_CREATE,
647                                GNUNET_DISK_PERM_USER_READ |
648                                GNUNET_DISK_PERM_USER_WRITE);
649     if (output_file == NULL)
650       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
651                   temp_str);
652   }
653   GNUNET_free_non_null (temp_str);
654
655   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
656
657   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
658                                        &connect_cb, &peergroup_ready, NULL,
659                                        hosts);
660   GNUNET_assert (pg != NULL);
661   shutdown_handle =
662       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
663                                     &shutdown_task, NULL);
664 }
665
666
667
668 /**
669  * test_mesh_small command line options
670  */
671 static struct GNUNET_GETOPT_CommandLineOption options[] = {
672   {'V', "verbose", NULL,
673    gettext_noop ("be verbose (print progress information)"),
674    0, &GNUNET_GETOPT_set_one, &verbose},
675   GNUNET_GETOPT_OPTION_END
676 };
677
678
679 /**
680  * Main: start test
681  */
682 int
683 main (int argc, char *argv[])
684 {
685   GNUNET_PROGRAM_run (argc, argv, "test_mesh_small",
686                       gettext_noop ("Test mesh in a small network."), options,
687                       &run, NULL);
688 #if REMOVE_DIR
689   GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
690 #endif
691   return ok;
692 }
693
694 /* end of test_mesh_small.c */