move GNUNET_TRANSPORT_ATS_ to GNUNET_ATS_
[oweals/gnunet.git] / src / mesh / test_mesh_small_unicast_far.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_unicast_far.c
22  *
23  * @brief Test for the mesh service: unicast to opposite point of 2d grid.
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  * How many data points to capture before triggering next round?
103  */
104 static struct GNUNET_TIME_Relative wait_time;
105
106 /**
107  * Task called to disconnect peers.
108  */
109 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
110
111 /**
112  * Task To perform tests
113  */
114 static GNUNET_SCHEDULER_TaskIdentifier test_task;
115
116 /**
117  * Task called to shutdown test.
118  */
119 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
120
121 static char *topology_file;
122
123 static char *data_filename;
124
125 static struct GNUNET_TESTING_Daemon *d1;
126
127 static struct GNUNET_TESTING_Daemon *d2;
128
129 static struct GNUNET_MESH_Handle *h1;
130
131 static struct GNUNET_MESH_Handle *h2;
132
133 static struct GNUNET_MESH_Tunnel *t;
134
135 static uint16_t *mesh_peers;
136
137 /**
138  * Check whether peers successfully shut down.
139  */
140 static void
141 shutdown_callback (void *cls, const char *emsg)
142 {
143   if (emsg != NULL)
144   {
145 #if VERBOSE
146     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Shutdown of peers failed!\n");
147 #endif
148     ok++;
149   }
150   else
151   {
152 #if VERBOSE
153     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: All peers successfully shut down!\n");
154 #endif
155   }
156 }
157
158
159 static void
160 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 {
162 #if VERBOSE
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
164 #endif
165
166   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
167   {
168     GNUNET_SCHEDULER_cancel (disconnect_task);
169     disconnect_task = GNUNET_SCHEDULER_NO_TASK;
170   }
171
172   if (data_file != NULL)
173     GNUNET_DISK_file_close (data_file);
174   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
175   GNUNET_CONFIGURATION_destroy (testing_cfg);
176 }
177
178
179 /**
180  * Handlers, for diverse services
181  */
182 static struct GNUNET_MESH_MessageHandler handlers[] = {
183 //    {&callback, 1, 0},
184   {NULL, 0, 0}
185 };
186
187
188 static void
189 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 {
191   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192               "test: disconnecting mesh service of peers\n");
193   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
194   GNUNET_MESH_disconnect(h1);
195   GNUNET_MESH_disconnect(h2);
196   GNUNET_SCHEDULER_cancel (shutdown_handle);
197   shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
198 }
199
200
201 /**
202  * Method called whenever another peer has added us to a tunnel
203  * the other peer initiated.
204  *
205  * @param cls closure
206  * @param tunnel new handle to the tunnel
207  * @param initiator peer that started the tunnel
208  * @param atsi performance information for the tunnel
209  * @return initial tunnel context for the tunnel
210  *         (can be NULL -- that's not an error)
211  */
212 static void *
213 incoming_tunnel (void *cls,
214                  struct GNUNET_MESH_Tunnel * tunnel,
215                  const struct GNUNET_PeerIdentity * initiator,
216                  const struct GNUNET_ATS_Information * atsi)
217 {
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219               "test: Incoming tunnel from %s\n",
220               GNUNET_i2s(initiator));
221   GNUNET_SCHEDULER_cancel (disconnect_task);
222   disconnect_task = GNUNET_SCHEDULER_add_now(&disconnect_mesh_peers, NULL);
223   ok = 0;
224   return NULL;
225 }
226
227 /**
228  * Function called whenever an inbound tunnel is destroyed.  Should clean up
229  * any associated state.
230  *
231  * @param cls closure (set from GNUNET_MESH_connect)
232  * @param tunnel connection to the other end (henceforth invalid)
233  * @param tunnel_ctx place where local state associated
234  *                   with the tunnel is stored
235  */
236 static void
237 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
238                 void *tunnel_ctx)
239 {
240 #if VERBOSE
241   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: tunnel disconnected\n");
242 #endif
243   
244   return;
245 }
246
247 /**
248  * Method called whenever a tunnel falls apart.
249  *
250  * @param cls closure
251  * @param peer peer identity the tunnel stopped working with
252  */
253 static void
254 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
255 {
256   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257               "test: peer %s disconnected\n",
258               GNUNET_i2s(peer));
259   return;
260 }
261
262
263 /**
264  * Method called whenever a tunnel is established.
265  *
266  * @param cls closure
267  * @param peer peer identity the tunnel was created to, NULL on timeout
268  * @param atsi performance data for the connection
269  */
270 static void
271 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
272     const struct GNUNET_ATS_Information *atsi)
273 {
274   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275               "test: peer %s connected\n",
276               GNUNET_i2s(peer));
277   return;
278 }
279
280
281 static void
282 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
283 {
284   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: test_task\n");
285   GNUNET_MESH_peer_request_connect_add(t, &d2->id);
286   GNUNET_SCHEDULER_cancel (disconnect_task);
287   disconnect_task = GNUNET_SCHEDULER_add_delayed(
288           GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
289                                &disconnect_mesh_peers, NULL);
290 }
291
292
293 /**
294  * connect_mesh_service: connect to the mesh service of one of the peers
295  *
296  */
297 static void
298 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
299 {
300   GNUNET_MESH_ApplicationType app;
301   struct GNUNET_PeerIdentity id;
302
303   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: connect_mesh_service\n");
304
305   GNUNET_PEER_resolve(11, &id);
306   d2 = GNUNET_TESTING_daemon_get_by_id (pg, &id);
307   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308               "test: Peer searched: %s\n",
309               GNUNET_i2s (&d2->id));
310   app = (GNUNET_MESH_ApplicationType) 0;
311
312 #if VERBOSE
313   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314               "test: connecting to mesh service of peer %s\n",
315               GNUNET_i2s (&d1->id));
316   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317               "test: connecting to mesh service of peer %s\n",
318               GNUNET_i2s (&d2->id));
319 #endif
320   h1 = GNUNET_MESH_connect (d1->cfg,
321                             10,
322                             NULL,
323                             NULL,
324                             &tunnel_cleaner,
325                             handlers,
326                             &app);
327   h2 = GNUNET_MESH_connect (d2->cfg,
328                             10,
329                             NULL,
330                             &incoming_tunnel,
331                             &tunnel_cleaner,
332                             handlers,
333                             &app);
334 #if VERBOSE
335   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336               "test: connected to mesh service of peer %s\n",
337               GNUNET_i2s (&d1->id));
338   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339               "test: connected to mesh service of peer %s\n",
340               GNUNET_i2s (&d2->id));
341 #endif
342   t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, NULL);
343   test_task =
344       GNUNET_SCHEDULER_add_delayed(
345           GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 6),
346           &do_test, NULL);
347 }
348
349
350
351 /**
352  * peergroup_ready: start test when all peers are connected
353  * @param cls closure
354  * @param emsg error message
355  */
356 static void
357 peergroup_ready (void *cls, const char *emsg)
358 {
359   char *buf;
360   int buf_len;
361   unsigned int i;
362
363   if (emsg != NULL)
364   {
365     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366                 "test: Peergroup callback called with error, aborting test!\n");
367     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368                 "test: Error from testing: `%s'\n", emsg);
369     ok++;
370     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
371     return;
372   }
373 #if VERBOSE
374   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375               "************************************************************\n");
376   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377               "test: Peer Group started successfully!\n");
378   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379               "test: Have %u connections\n",
380               total_connections);
381 #endif
382
383   if (data_file != NULL)
384   {
385     buf = NULL;
386     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
387     if (buf_len > 0)
388       GNUNET_DISK_file_write (data_file, buf, buf_len);
389     GNUNET_free (buf);
390   }
391   peers_running = GNUNET_TESTING_daemons_running (pg);
392   for (i = 0; i < num_peers; i++)
393   {
394     d1 = GNUNET_TESTING_daemon_get (pg, i);
395     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396                 "test:   %u: %s\n",
397                 GNUNET_PEER_intern(&d1->id),
398                 GNUNET_i2s (&d1->id));
399   }
400   d1 = GNUNET_TESTING_daemon_get (pg, 0);
401   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402               "test: Peer looking: %s\n",
403               GNUNET_i2s (&d1->id));
404
405   GNUNET_SCHEDULER_add_now (&connect_mesh_service,
406                                 NULL);
407   disconnect_task =
408       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
409
410 }
411
412
413 /**
414  * Function that will be called whenever two daemons are connected by
415  * the testing library.
416  *
417  * @param cls closure
418  * @param first peer id for first daemon
419  * @param second peer id for the second daemon
420  * @param distance distance between the connected peers
421  * @param first_cfg config for the first daemon
422  * @param second_cfg config for the second daemon
423  * @param first_daemon handle for the first daemon
424  * @param second_daemon handle for the second daemon
425  * @param emsg error message (NULL on success)
426  */
427 static void
428 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
429             const struct GNUNET_PeerIdentity *second, uint32_t distance,
430             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
431             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
432             struct GNUNET_TESTING_Daemon *first_daemon,
433             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
434 {
435   if (emsg == NULL)
436   {
437     total_connections++;
438   }
439   else
440   {
441     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442                 "test: Problem with new connection (%s)\n",
443                 emsg);
444   }
445
446 }
447
448
449 /**
450  * run: load configuration options and schedule test to run (start peergroup)
451  * @param cls closure
452  * @param args argv
453  * @param cfgfile configuration file name (can be NULL)
454  * @param cfg configuration handle
455  */
456 static void
457 run (void *cls, char *const *args, const char *cfgfile,
458      const struct GNUNET_CONFIGURATION_Handle *cfg)
459 {
460   char *temp_str;
461   unsigned long long temp_wait;
462   struct GNUNET_TESTING_Host *hosts;
463
464   ok = 1;
465   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
466
467   GNUNET_log_setup ("test_mesh_small_unicast",
468 #if VERBOSE
469                     "DEBUG",
470 #else
471                     "WARNING",
472 #endif
473                     NULL);
474
475 #if VERBOSE
476   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
477   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
478                                          "use_progressbars", "YES");
479 #endif
480
481   if (GNUNET_OK !=
482       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
483                                              "num_peers", &num_peers))
484   {
485     GNUNET_assert (GNUNET_OK ==
486                    GNUNET_CONFIGURATION_load (testing_cfg,
487                                               "test_mesh_small.conf"));
488     if (GNUNET_OK !=
489         GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
490                                                "num_peers", &num_peers))
491     {
492       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
493                   "Option TESTING:NUM_PEERS is required!\n");
494       return;
495     }
496   }
497
498   mesh_peers = GNUNET_malloc (sizeof(GNUNET_PEER_Id) * (num_peers + 1));
499
500   if (GNUNET_OK !=
501       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
502                                              "wait_time", &temp_wait))
503   {
504     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
505                 "Option test_mesh_small:wait_time is required!\n");
506     return;
507   }
508
509   if (GNUNET_OK !=
510       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
511                                              "topology_output_file",
512                                              &topology_file))
513   {
514     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
515                 "Option test_mesh_small:topology_output_file is required!\n");
516     return;
517   }
518
519   if (GNUNET_OK !=
520       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
521                                              "data_output_file",
522                                              &data_filename))
523   {
524     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
525                 "Option test_mesh_small:data_output_file is required!\n");
526     return;
527   }
528
529   data_file =
530       GNUNET_DISK_file_open (data_filename,
531                              GNUNET_DISK_OPEN_READWRITE |
532                              GNUNET_DISK_OPEN_CREATE,
533                              GNUNET_DISK_PERM_USER_READ |
534                              GNUNET_DISK_PERM_USER_WRITE);
535   if (data_file == NULL)
536   {
537     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
538                 data_filename);
539     GNUNET_free (data_filename);
540   }
541
542   wait_time =
543       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
544
545   if (GNUNET_YES ==
546       GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
547                                              "output_file", &temp_str))
548   {
549     output_file =
550         GNUNET_DISK_file_open (temp_str,
551                                GNUNET_DISK_OPEN_READWRITE |
552                                GNUNET_DISK_OPEN_CREATE,
553                                GNUNET_DISK_PERM_USER_READ |
554                                GNUNET_DISK_PERM_USER_WRITE);
555     if (output_file == NULL)
556       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
557                   temp_str);
558   }
559   GNUNET_free_non_null (temp_str);
560
561   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
562
563   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
564                                        &connect_cb, &peergroup_ready, NULL,
565                                        hosts);
566   GNUNET_assert (pg != NULL);
567   shutdown_handle =
568       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
569                                     &shutdown_task, NULL);
570 }
571
572
573
574 /**
575  * test_mesh_small command line options
576  */
577 static struct GNUNET_GETOPT_CommandLineOption options[] = {
578   {'V', "verbose", NULL,
579    gettext_noop ("be verbose (print progress information)"),
580    0, &GNUNET_GETOPT_set_one, &verbose},
581   GNUNET_GETOPT_OPTION_END
582 };
583
584
585 /**
586  * Main: start test
587  */
588 int
589 main (int argc, char *argv[])
590 {
591   GNUNET_PROGRAM_run (argc, argv, "test_mesh_small_unicast",
592                       gettext_noop ("Test mesh unicast in a small network."), options,
593                       &run, NULL);
594 #if REMOVE_DIR
595   GNUNET_DISK_directory_remove ("/tmp/test_mesh_small_unicast");
596 #endif
597   if (0 != ok)
598   {
599     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
600   }
601   return ok;
602 }
603
604 /* end of test_mesh_small_unicast.c */