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