c60e2c57ea1d6d9d62049875197ca3418efeebac
[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,
152                   "All peers successfully shut down!\n");
153 #endif
154       ok = 0;
155     }
156 }
157
158
159 static void
160 shutdown_task (void *cls,
161                const struct GNUNET_SCHEDULER_TaskContext *tc)
162 {
163 //   struct NSEPeer *pos;
164 #if VERBOSE
165   fprintf(stderr, "Ending test.\n");
166 #endif
167
168   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
169     {
170       GNUNET_SCHEDULER_cancel(disconnect_task);
171       disconnect_task = GNUNET_SCHEDULER_NO_TASK;
172     }
173 //   while (NULL != (pos = peer_head))
174 //     {
175 //       if (pos->nse_handle != NULL)
176 //         GNUNET_NSE_disconnect(pos->nse_handle);
177 //       GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
178 //       GNUNET_free(pos);
179 //     }
180
181   if (data_file != NULL)
182     GNUNET_DISK_file_close(data_file);
183   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
184 }
185
186
187 static void
188 connect_mesh_service (void *cls,
189                      const struct GNUNET_SCHEDULER_TaskContext *tc)
190 {
191 //   struct NSEPeer *current_peer;
192   unsigned int i;
193
194 #if VERBOSE
195   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196               "Connecting to mesh service of peers\n");
197 #endif
198   for (i = 0; i < num_peers; i++)
199     {
200 //       if ((connection_limit > 0) && (i % (num_peers / connection_limit) != 0))
201         continue;
202 #if VERBOSE
203       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test_mesh_small: connecting to mesh service of peer %d\n", i);
204 #endif
205 /*      current_peer = GNUNET_malloc(sizeof(struct NSEPeer));
206       current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i);
207       if (GNUNET_YES == GNUNET_TESTING_daemon_running(GNUNET_TESTING_daemon_get(pg, i)))
208         {
209           current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg,
210                                                          &handle_estimate, 
211                                                          current_peer);
212           GNUNET_assert(current_peer->nse_handle != NULL);
213         }
214       GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);*/
215     }
216 }
217
218
219 static void
220 churn_peers (void *cls,
221              const struct GNUNET_SCHEDULER_TaskContext *tc);
222
223
224 /**
225  * Continuation called by the "get_all" and "get" functions.
226  *
227  * @param cls struct StatsContext
228  * @param success GNUNET_OK if statistics were
229  *        successfully obtained, GNUNET_SYSERR if not.
230  */
231 static void 
232 stats_finished_callback (void *cls, int success)
233 {
234   struct StatsContext *stats_context = cls;
235   char *buf;
236   int buf_len;
237
238   if ( (GNUNET_OK == success) && 
239        (data_file != NULL) )
240     {
241       /* Stats lookup successful, write out data */
242       buf = NULL;
243       buf_len = GNUNET_asprintf(&buf,
244                                 "TOTAL_MESH_BYTES: %u\n", 
245                                 stats_context->total_mesh_bytes);
246       if (buf_len > 0)
247         {
248           GNUNET_DISK_file_write(data_file, buf, buf_len);
249         }
250       GNUNET_free_non_null(buf);
251     }
252
253   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
254   shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
255   GNUNET_free(stats_context);
256 }
257
258
259 /**
260  * Callback function to process statistic values.
261  *
262  * @param cls struct StatsContext
263  * @param peer the peer the statistics belong to
264  * @param subsystem name of subsystem that created the statistic
265  * @param name the name of the datum
266  * @param value the current value
267  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
268  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
269  */
270 static int 
271 statistics_iterator (void *cls,
272                      const struct GNUNET_PeerIdentity *peer,
273                      const char *subsystem,
274                      const char *name,
275                      uint64_t value,
276                      int is_persistent)
277 {
278   struct StatsContext *stats_context = cls;
279 // 
280 //   if ( (0 == strstr(subsystem, "nse")) && 
281 //        (0 == strstr(name, "# flood messages received")) )
282     stats_context->total_mesh_bytes += 1; //value;
283   return GNUNET_OK;
284 }
285
286
287 static void
288 disconnect_mesh_peers (void *cls,
289                       const struct GNUNET_SCHEDULER_TaskContext *tc)
290 {
291 //   struct NSEPeer *pos;
292   char *buf;
293   struct StatsContext *stats_context;
294
295   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296               "disconnecting mesh service of peers\n");
297   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
298 //   pos = peer_head;
299 //   while (NULL != (pos = peer_head))
300 //     {
301 //       if (pos->nse_handle != NULL)
302 //         {
303 //           GNUNET_NSE_disconnect(pos->nse_handle);
304 //           pos->nse_handle = NULL;
305 //         }
306 //       GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
307 //       GNUNET_free(pos);
308 //     }
309
310   GNUNET_asprintf(&buf, 
311                   "round%llu", 
312                   current_round);
313   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (testing_cfg, 
314                                                           "test_mesh_small",
315                                                           buf, 
316                                                           &peers_next_round))
317     {
318       current_round++;
319       GNUNET_assert(churn_task == GNUNET_SCHEDULER_NO_TASK);
320       churn_task = GNUNET_SCHEDULER_add_now(&churn_peers, NULL);
321     }
322   else /* No more rounds, let's shut it down! */
323     {
324       stats_context = GNUNET_malloc(sizeof(struct StatsContext));
325       GNUNET_SCHEDULER_cancel(shutdown_handle);
326       shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
327       GNUNET_TESTING_get_statistics(pg, 
328                                     &stats_finished_callback, 
329                                     &statistics_iterator, 
330                                     stats_context);
331     }
332   GNUNET_free(buf);
333 }
334
335
336 /**
337  * FIXME.
338  *
339  * @param cls unused
340  * @param emsg NULL on success
341  */
342 static void 
343 topology_output_callback (void *cls, const char *emsg)
344 {
345   disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, 
346                                                  &disconnect_mesh_peers, NULL);
347   GNUNET_SCHEDULER_add_now(&connect_mesh_service, NULL);
348 }
349
350
351 /**
352  * FIXME.
353  *
354  * @param cls closure
355  * @param emsg NULL on success
356  */
357 static void
358 churn_callback (void *cls, const char *emsg)
359 {
360   char *temp_output_file;
361
362   if (emsg == NULL) /* Everything is okay! */
363     {
364       peers_running = peers_next_round;
365       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
366                   "Round %llu, churn finished successfully.\n",
367                   current_round);
368       GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
369       GNUNET_asprintf(&temp_output_file, 
370                       "%s_%llu.dot",
371                       topology_file, 
372                       current_round);
373       GNUNET_TESTING_peergroup_topology_to_file(pg,
374                                                 temp_output_file,
375                                                 &topology_output_callback,
376                                                 NULL);
377       GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
378                  "Writing topology to file %s\n",
379                  temp_output_file);
380       GNUNET_free(temp_output_file);
381     }
382   else
383     {
384       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
385                   "Round %llu, churn FAILED!!\n",
386                   current_round);
387       GNUNET_SCHEDULER_cancel(shutdown_handle);
388       shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
389     }
390 }
391
392
393 static void
394 churn_peers (void *cls,
395              const struct GNUNET_SCHEDULER_TaskContext *tc)
396 {
397   /* peers_running = GNUNET_TESTING_daemons_running(pg); */
398   churn_task = GNUNET_SCHEDULER_NO_TASK;
399   if (peers_next_round == peers_running)
400     {
401       /* Nothing to do... */
402       GNUNET_SCHEDULER_add_now(&connect_mesh_service, NULL);
403       GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
404       disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, 
405                                                      &disconnect_mesh_peers, NULL);
406       GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
407                  "Round %lu, doing nothing!\n", 
408                  current_round);
409     }
410   else
411     {
412       if (peers_next_round > num_peers)
413         {
414           GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 
415                      "Asked to turn on more peers than we have!!\n");
416           GNUNET_SCHEDULER_cancel(shutdown_handle);
417           GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
418         }
419       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
420                   "Round %llu, turning off %llu peers, turning on %llu peers!\n",
421                   current_round,
422                   (peers_running > peers_next_round) 
423                   ? peers_running - peers_next_round 
424                   : 0,
425                   (peers_next_round > peers_running) 
426                   ? peers_next_round - peers_running 
427                   : 0);
428       GNUNET_TESTING_daemons_churn (pg, "nse",
429                                     (peers_running > peers_next_round) 
430                                     ? peers_running - peers_next_round
431                                     : 0,
432                                     (peers_next_round > peers_running) 
433                                     ? peers_next_round - peers_running
434                                     : 0, 
435                                     wait_time,
436                                     &churn_callback, NULL);
437     }
438 }
439
440
441 static void
442 my_cb (void *cls,
443        const char *emsg)
444 {
445   char *buf;
446   int buf_len;
447   if (emsg != NULL)
448     {
449       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450                   "Peergroup callback called with error, aborting test!\n");
451       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
452       ok = 1;
453       GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
454       return;
455     }
456 #if VERBOSE
457   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458               "Peer Group started successfully!\n");
459 #endif
460   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
461               "Have %u connections\n",
462               total_connections);
463   if (data_file != NULL)
464     {
465       buf = NULL;
466       buf_len = GNUNET_asprintf(&buf, 
467                                 "CONNECTIONS_0: %u\n",
468                                 total_connections);
469       if (buf_len > 0)
470         GNUNET_DISK_file_write(data_file, buf, buf_len);
471       GNUNET_free (buf);
472     }
473   peers_running = GNUNET_TESTING_daemons_running(pg);
474
475   GNUNET_SCHEDULER_add_now(&connect_mesh_service, NULL);
476   disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, &disconnect_mesh_peers, NULL);
477
478 }
479
480
481 /**
482  * Function that will be called whenever two daemons are connected by
483  * the testing library.
484  *
485  * @param cls closure
486  * @param first peer id for first daemon
487  * @param second peer id for the second daemon
488  * @param distance distance between the connected peers
489  * @param first_cfg config for the first daemon
490  * @param second_cfg config for the second daemon
491  * @param first_daemon handle for the first daemon
492  * @param second_daemon handle for the second daemon
493  * @param emsg error message (NULL on success)
494  */
495 static void 
496 connect_cb (void *cls,
497             const struct GNUNET_PeerIdentity *first,
498             const struct GNUNET_PeerIdentity *second,
499             uint32_t distance,
500             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
501             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
502             struct GNUNET_TESTING_Daemon *first_daemon,
503             struct GNUNET_TESTING_Daemon *second_daemon,
504             const char *emsg)
505 {
506   if (emsg == NULL)
507     total_connections++;
508 }
509
510
511 static void
512 run (void *cls,
513      char *const *args,
514      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
515 {
516   char                          *temp_str;
517   unsigned long long            temp_wait;
518   struct GNUNET_TESTING_Host    *hosts;
519
520   ok = 1;
521   testing_cfg = (struct GNUNET_CONFIGURATION_Handle *)cfg; // GNUNET_CONFIGURATION_create();
522 #if VERBOSE
523   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
524   GNUNET_CONFIGURATION_set_value_string (testing_cfg,
525                                          "testing",
526                                          "use_progressbars",
527                                          "YES");
528 #endif
529   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg, 
530                                                           "testing",
531                                                           "num_peers", &num_peers))
532     {
533       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Option TESTING:NUM_PEERS is required!\n");
534       return;
535     }
536
537   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg, 
538                                                           "test_mesh_small",
539                                                           "wait_time",
540                                                           &temp_wait))
541     {
542       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 
543                  "Option nsetest_mesh_small:wait_time is required!\n");
544       return;
545     }
546
547   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg, 
548                                                           "testing", "topology_output_file", 
549                                                           &topology_file))
550     {
551       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 
552                  "Option test_mesh_small:topology_output_file is required!\n");
553       return;
554     }
555
556   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg, 
557                                                           "test_mesh_small", "data_output_file",
558                                                           &data_filename))
559     {
560       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 
561                  "Option test_mesh_small:data_output_file is required!\n");
562       return;
563     }
564
565   data_file = GNUNET_DISK_file_open (data_filename,
566                                      GNUNET_DISK_OPEN_READWRITE
567                                      | GNUNET_DISK_OPEN_CREATE,
568                                      GNUNET_DISK_PERM_USER_READ |
569                                      GNUNET_DISK_PERM_USER_WRITE);
570   if (data_file == NULL)
571     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 
572                "Failed to open %s for output!\n",
573                data_filename);
574   GNUNET_free(data_filename);
575
576   wait_time = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_wait);
577
578   if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(cfg,
579                                                           "test_mesh_small", 
580                                                           "output_file",
581                                                           &temp_str))
582     {
583       output_file = GNUNET_DISK_file_open (temp_str, GNUNET_DISK_OPEN_READWRITE
584                                                       | GNUNET_DISK_OPEN_CREATE,
585                                                       GNUNET_DISK_PERM_USER_READ |
586                                                       GNUNET_DISK_PERM_USER_WRITE);
587       if (output_file == NULL)
588         GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 
589                    "Failed to open %s for output!\n",
590                    temp_str);
591     }
592   GNUNET_free_non_null(temp_str);
593
594   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
595
596   pg = GNUNET_TESTING_peergroup_start(testing_cfg,
597                                       num_peers,
598                                       TIMEOUT,
599                                       &connect_cb,
600                                       &my_cb, NULL,
601                                       hosts);
602   GNUNET_assert (pg != NULL);
603   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever(),
604                                                   &shutdown_task,
605                                                   NULL);
606 }
607
608
609
610 /**
611  * test_mesh_small command line options
612  */
613 static struct GNUNET_GETOPT_CommandLineOption options[] = {
614   {'V', "verbose", NULL,
615    gettext_noop ("be verbose (print progress information)"),
616    0, &GNUNET_GETOPT_set_one, &verbose},
617   GNUNET_GETOPT_OPTION_END
618 };
619
620
621 int
622 main (int argc, char *argv[])
623 {
624   GNUNET_log_setup ("test_mesh_small",
625 #if VERBOSE
626                     "DEBUG",
627 #else
628                     "WARNING",
629 #endif
630                     NULL);
631   GNUNET_PROGRAM_run (argc,
632                       argv, "test_mesh_small", 
633                       gettext_noop ("Test mesh in a small network."),
634                       options, 
635                       &run, NULL);
636 #if REMOVE_DIR
637   GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
638 #endif
639   return ok;
640 }
641
642 /* end of test_mesh_small.c */