move GNUNET_TRANSPORT_ATS_ to GNUNET_ATS_
[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  * Total number of connections in the whole network.
83  */
84 static unsigned int total_connections;
85
86 /**
87  * The currently running peer group.
88  */
89 static struct GNUNET_TESTING_PeerGroup *pg;
90
91 /**
92  * File to report results to.
93  */
94 static struct GNUNET_DISK_FileHandle *output_file;
95
96 /**
97  * File to log connection info, statistics to.
98  */
99 static struct GNUNET_DISK_FileHandle *data_file;
100
101 /**
102  * Task called to disconnect peers.
103  */
104 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
105
106 /**
107  * Task called to shutdown test.
108  */
109 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
110
111 static char *topology_file;
112
113 static char *data_filename;
114
115 static struct GNUNET_TIME_Relative time_out;
116
117 /**
118  * Check whether peers successfully shut down.
119  */
120 static void
121 shutdown_callback (void *cls, const char *emsg)
122 {
123   if (emsg != NULL)
124   {
125 #if VERBOSE
126     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
127 #endif
128     if (ok == 0)
129       ok = 666;
130   }
131   else
132   {
133 #if VERBOSE
134     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
135 #endif
136     ok = 0;
137   }
138 }
139
140
141 static void
142 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
143 {
144 #if VERBOSE
145   fprintf (stderr, "Ending test.\n");
146 #endif
147
148   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
149   {
150     GNUNET_SCHEDULER_cancel (disconnect_task);
151     disconnect_task = GNUNET_SCHEDULER_NO_TASK;
152   }
153   if (data_file != NULL)
154     GNUNET_DISK_file_close (data_file);
155   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
156   GNUNET_CONFIGURATION_destroy (testing_cfg);
157 }
158
159
160 /**
161  * Handlers, for diverse services
162  */
163 static struct GNUNET_MESH_MessageHandler handlers[] = {
164 //    {&callback, 1, 0},
165   {NULL, 0, 0}
166 };
167
168
169 static void
170 disconnect_mesh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 {
172   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173               "test: disconnecting mesh service of peers\n");
174   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
175   GNUNET_SCHEDULER_cancel (shutdown_handle);
176   shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
177 }
178
179
180 /**
181  * Function called whenever an inbound tunnel is destroyed.  Should clean up
182  * any associated state.
183  *
184  * @param cls closure (set from GNUNET_MESH_connect)
185  * @param tunnel connection to the other end (henceforth invalid)
186  * @param tunnel_ctx place where local state associated
187  *                   with the tunnel is stored
188  */
189 static void
190 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
191                 void *tunnel_ctx)
192 {
193 #if VERBOSE
194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel disconnected\n");
195 #endif
196   return;
197 }
198
199 /**
200  * Method called whenever a tunnel falls apart.
201  *
202  * @param cls closure
203  * @param peer peer identity the tunnel stopped working with
204  */
205 static void
206 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
207 {
208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer disconnected\n");
209   return;
210 }
211
212
213 /**
214  * Method called whenever a tunnel is established.
215  *
216  * @param cls closure
217  * @param peer peer identity the tunnel was created to, NULL on timeout
218  * @param atsi performance data for the connection
219  */
220 static void
221 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
222     const struct GNUNET_ATS_Information *atsi)
223 {
224   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer connected\n");
225   return;
226 }
227
228
229 /**
230  * connect_mesh_service: connect to the mesh service of one of the peers
231  *
232  */
233 static void
234 connect_mesh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
235 {
236   struct GNUNET_TESTING_Daemon *d;
237   struct GNUNET_MESH_Handle *h;
238   struct GNUNET_MESH_Tunnel *t;
239   GNUNET_MESH_ApplicationType app;
240
241   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect_mesh_service\n");
242
243   d = GNUNET_TESTING_daemon_get (pg, 1);
244   app = (GNUNET_MESH_ApplicationType) 0;
245
246 #if VERBOSE
247   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248               "connecting to mesh service of peer %s\n", GNUNET_i2s (&d->id));
249 #endif
250   h = GNUNET_MESH_connect (d->cfg, 10, NULL, NULL, &tunnel_cleaner, handlers,
251                            &app);
252 #if VERBOSE
253   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected to mesh service of peer %s\n",
254               GNUNET_i2s (&d->id));
255 #endif
256   t = GNUNET_MESH_tunnel_create (h, NULL, &ch, &dh, NULL);
257   GNUNET_MESH_tunnel_destroy (t);
258   GNUNET_MESH_disconnect (h);
259 }
260
261
262 /**
263  * peergroup_ready: start test when all peers are connected
264  * @param cls closure
265  * @param emsg error message
266  */
267 static void
268 peergroup_ready (void *cls, const char *emsg)
269 {
270   char *buf;
271   int buf_len;
272
273   if (emsg != NULL)
274   {
275     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276                 "Peergroup callback called with error, aborting test!\n");
277     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
278     ok = 1;
279     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
280     return;
281   }
282 #if VERBOSE
283   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer Group started successfully!\n");
284 #endif
285
286   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
287               total_connections);
288   if (data_file != NULL)
289   {
290     buf = NULL;
291     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
292     if (buf_len > 0)
293       GNUNET_DISK_file_write (data_file, buf, buf_len);
294     GNUNET_free (buf);
295   }
296   peers_running = GNUNET_TESTING_daemons_running (pg);
297
298   GNUNET_SCHEDULER_add_now (&connect_mesh, NULL);
299   disconnect_task =
300       GNUNET_SCHEDULER_add_delayed (time_out, &disconnect_mesh, NULL);
301
302 }
303
304
305 /**
306  * Function that will be called whenever two daemons are connected by
307  * the testing library.
308  *
309  * @param cls closure
310  * @param first peer id for first daemon
311  * @param second peer id for the second daemon
312  * @param distance distance between the connected peers
313  * @param first_cfg config for the first daemon
314  * @param second_cfg config for the second daemon
315  * @param first_daemon handle for the first daemon
316  * @param second_daemon handle for the second daemon
317  * @param emsg error message (NULL on success)
318  */
319 static void
320 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
321             const struct GNUNET_PeerIdentity *second, uint32_t distance,
322             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
323             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
324             struct GNUNET_TESTING_Daemon *first_daemon,
325             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
326 {
327   if (emsg == NULL)
328     total_connections++;
329 }
330
331
332 /**
333  * run: load configuration options and schedule test to run (start peergroup)
334  * @param cls closure
335  * @param args argv
336  * @param cfgfile configuration file name (can be NULL)
337  * @param cfg configuration handle
338  */
339 static void
340 run (void *cls, char *const *args, const char *cfgfile,
341      const struct GNUNET_CONFIGURATION_Handle *cfg)
342 {
343   char *temp_str;
344   struct GNUNET_TESTING_Host *hosts;
345
346   ok = 1;
347   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
348
349   GNUNET_log_setup ("test_mesh_small",
350 #if VERBOSE
351                     "DEBUG",
352 #else
353                     "WARNING",
354 #endif
355                     NULL);
356
357 #if VERBOSE
358   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
359   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
360                                          "use_progressbars", "YES");
361 #endif
362
363   time_out = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
364
365   if (GNUNET_OK !=
366       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
367                                              "num_peers", &num_peers))
368   {
369     GNUNET_assert (GNUNET_OK ==
370                    GNUNET_CONFIGURATION_load (testing_cfg,
371                                               "test_mesh_small.conf"));
372     if (GNUNET_OK !=
373         GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
374                                                "num_peers", &num_peers))
375     {
376       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
377                   "Option TESTING:NUM_PEERS is required!\n");
378       return;
379     }
380   }
381
382   if (GNUNET_OK !=
383       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
384                                              "topology_output_file",
385                                              &topology_file))
386   {
387     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
388                 "Option test_mesh_small:topology_output_file is required!\n");
389     return;
390   }
391
392   if (GNUNET_OK !=
393       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
394                                              "data_output_file",
395                                              &data_filename))
396   {
397     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
398                 "Option test_mesh_small:data_output_file is required!\n");
399     return;
400   }
401
402   data_file =
403       GNUNET_DISK_file_open (data_filename,
404                              GNUNET_DISK_OPEN_READWRITE |
405                              GNUNET_DISK_OPEN_CREATE,
406                              GNUNET_DISK_PERM_USER_READ |
407                              GNUNET_DISK_PERM_USER_WRITE);
408   if (data_file == NULL)
409   {
410     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
411                 data_filename);
412     GNUNET_free (data_filename);
413   }
414
415   if (GNUNET_YES ==
416       GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
417                                              "output_file", &temp_str))
418   {
419     output_file =
420         GNUNET_DISK_file_open (temp_str,
421                                GNUNET_DISK_OPEN_READWRITE |
422                                GNUNET_DISK_OPEN_CREATE,
423                                GNUNET_DISK_PERM_USER_READ |
424                                GNUNET_DISK_PERM_USER_WRITE);
425     if (output_file == NULL)
426       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
427                   temp_str);
428   }
429   GNUNET_free_non_null (temp_str);
430
431   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
432
433   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
434                                        &connect_cb, &peergroup_ready, NULL,
435                                        hosts);
436   GNUNET_assert (pg != NULL);
437   shutdown_handle =
438       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
439                                     &shutdown_task, NULL);
440 }
441
442
443
444 /**
445  * test_mesh_small command line options
446  */
447 static struct GNUNET_GETOPT_CommandLineOption options[] = {
448   {'V', "verbose", NULL,
449    gettext_noop ("be verbose (print progress information)"),
450    0, &GNUNET_GETOPT_set_one, &verbose},
451   GNUNET_GETOPT_OPTION_END
452 };
453
454
455 /**
456  * Main: start test
457  */
458 int
459 main (int argc, char *argv[])
460 {
461   GNUNET_PROGRAM_run (argc, argv, "test_mesh_small",
462                       gettext_noop ("Test mesh in a small network."), options,
463                       &run, NULL);
464 #if REMOVE_DIR
465   GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
466 #endif
467   return ok;
468 }
469
470 /* end of test_mesh_small.c */