2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file mesh/test_mesh_small.c
23 * @brief Test for the mesh service.
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_mesh_service_new.h"
29 #define VERBOSE GNUNET_YES
30 #define REMOVE_DIR GNUNET_YES
34 struct MeshPeer *prev;
36 struct MeshPeer *next;
38 struct GNUNET_TESTING_Daemon *daemon;
40 struct GNUNET_MESH_Handle *mesh_handle;
46 unsigned long long total_mesh_bytes;
50 // static struct MeshPeer *peer_head;
52 // static struct MeshPeer *peer_tail;
55 * How long until we give up on connecting the peers?
57 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
67 * Total number of peers in the test.
69 static unsigned long long num_peers;
72 * Global configuration file
74 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
77 * Total number of currently running peers.
79 static unsigned long long peers_running;
82 * Current round we are in.
84 static unsigned long long current_round;
87 * Peers desired in the next round.
89 static unsigned long long peers_next_round;
92 * Total number of connections in the whole network.
94 static unsigned int total_connections;
97 * The currently running peer group.
99 static struct GNUNET_TESTING_PeerGroup *pg;
102 * File to report results to.
104 static struct GNUNET_DISK_FileHandle *output_file;
107 * File to log connection info, statistics to.
109 static struct GNUNET_DISK_FileHandle *data_file;
112 * How many data points to capture before triggering next round?
114 static struct GNUNET_TIME_Relative wait_time;
117 * Task called to disconnect peers.
119 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
122 * Task called to shutdown test.
124 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
127 * Task used to churn the network.
129 static GNUNET_SCHEDULER_TaskIdentifier churn_task;
131 static char *topology_file;
133 static char *data_filename;
136 * Check whether peers successfully shut down.
139 shutdown_callback (void *cls, const char *emsg)
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
160 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162 // struct NSEPeer *pos;
164 fprintf (stderr, "Ending test.\n");
167 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
169 GNUNET_SCHEDULER_cancel (disconnect_task);
170 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
172 // while (NULL != (pos = peer_head))
174 // if (pos->nse_handle != NULL)
175 // GNUNET_NSE_disconnect(pos->nse_handle);
176 // GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
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);
188 * Handlers, for diverse services
190 static struct GNUNET_MESH_MessageHandler handlers[] = {
191 // {&callback, 1, 0},
197 * Function called whenever an inbound tunnel is destroyed. Should clean up
198 * any associated state.
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
206 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel disconnected\n");
216 * Method called whenever a tunnel falls apart.
219 * @param peer peer identity the tunnel stopped working with
222 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer disconnected\n");
230 * Method called whenever a tunnel is established.
233 * @param peer peer identity the tunnel was created to, NULL on timeout
234 * @param atsi performance data for the connection
237 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
238 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer connected\n");
246 * connect_mesh_service: connect to the mesh service of one of the peers
250 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
252 struct GNUNET_TESTING_Daemon *d;
253 struct GNUNET_MESH_Handle *h;
254 struct GNUNET_MESH_Tunnel *t;
255 GNUNET_MESH_ApplicationType app;
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect_mesh_service\n");
259 d = GNUNET_TESTING_daemon_get (pg, 1);
260 app = (GNUNET_MESH_ApplicationType) 0;
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "connecting to mesh service of peer %s\n", GNUNET_i2s (&d->id));
266 h = GNUNET_MESH_connect (d->cfg, NULL, &tunnel_cleaner, handlers, &app);
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected to mesh service of peer %s\n",
269 GNUNET_i2s (&d->id));
271 t = GNUNET_MESH_tunnel_create (h, &ch, &dh, NULL);
272 GNUNET_MESH_tunnel_destroy (t);
273 GNUNET_MESH_disconnect (h);
278 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
282 * Continuation called by the "get_all" and "get" functions.
284 * @param cls struct StatsContext
285 * @param success GNUNET_OK if statistics were
286 * successfully obtained, GNUNET_SYSERR if not.
289 stats_finished_callback (void *cls, int success)
291 struct StatsContext *stats_context = cls;
295 if ((GNUNET_OK == success) && (data_file != NULL))
297 /* Stats lookup successful, write out data */
300 GNUNET_asprintf (&buf, "TOTAL_MESH_BYTES: %u\n",
301 stats_context->total_mesh_bytes);
304 GNUNET_DISK_file_write (data_file, buf, buf_len);
306 GNUNET_free_non_null (buf);
309 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
310 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
311 GNUNET_free (stats_context);
316 * Callback function to process statistic values.
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
327 statistics_iterator (void *cls, const struct GNUNET_PeerIdentity *peer,
328 const char *subsystem, const char *name, uint64_t value,
331 struct StatsContext *stats_context = cls;
334 // if ( (0 == strstr(subsystem, "nse")) &&
335 // (0 == strstr(name, "# flood messages received")) )
336 stats_context->total_mesh_bytes += 1; //value;
342 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
344 // struct NSEPeer *pos;
346 struct StatsContext *stats_context;
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting mesh service of peers\n");
349 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
351 // while (NULL != (pos = peer_head))
353 // if (pos->nse_handle != NULL)
355 // GNUNET_NSE_disconnect(pos->nse_handle);
356 // pos->nse_handle = NULL;
358 // GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
362 GNUNET_asprintf (&buf, "round%llu", current_round);
364 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
365 buf, &peers_next_round))
368 GNUNET_assert (churn_task == GNUNET_SCHEDULER_NO_TASK);
369 churn_task = GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
371 else /* No more rounds, let's shut it down! */
373 stats_context = GNUNET_malloc (sizeof (struct StatsContext));
374 GNUNET_SCHEDULER_cancel (shutdown_handle);
375 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
376 GNUNET_TESTING_get_statistics (pg, &stats_finished_callback,
377 &statistics_iterator, stats_context);
387 * @param emsg NULL on success
390 topology_output_callback (void *cls, const char *emsg)
393 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
394 GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
402 * @param emsg NULL on success
405 churn_callback (void *cls, const char *emsg)
407 char *temp_output_file;
409 if (emsg == NULL) /* Everything is okay! */
411 peers_running = peers_next_round;
412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
413 "Round %llu, churn finished successfully.\n", current_round);
414 GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
415 GNUNET_asprintf (&temp_output_file, "%s_%llu.dot", topology_file,
417 GNUNET_TESTING_peergroup_topology_to_file (pg, temp_output_file,
418 &topology_output_callback, NULL);
419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Writing topology to file %s\n",
421 GNUNET_free (temp_output_file);
425 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %llu, churn FAILED!!\n",
427 GNUNET_SCHEDULER_cancel (shutdown_handle);
428 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
434 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
436 /* peers_running = GNUNET_TESTING_daemons_running(pg); */
437 churn_task = GNUNET_SCHEDULER_NO_TASK;
438 if (peers_next_round == peers_running)
440 /* Nothing to do... */
441 GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
442 GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
444 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
445 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %lu, doing nothing!\n",
450 if (peers_next_round > num_peers)
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
453 "Asked to turn on more peers than we have!!\n");
454 GNUNET_SCHEDULER_cancel (shutdown_handle);
455 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
457 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
458 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
461 peers_next_round) ? peers_running - peers_next_round : 0,
463 peers_running) ? peers_next_round - peers_running : 0);
464 GNUNET_TESTING_daemons_churn (pg, "nse",
466 peers_next_round) ? peers_running -
467 peers_next_round : 0,
469 peers_running) ? peers_next_round -
470 peers_running : 0, wait_time, &churn_callback,
477 * peergroup_ready: start test when all peers are connected
479 * @param emsg error message
482 peergroup_ready (void *cls, const char *emsg)
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "Peergroup callback called with error, aborting test!\n");
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
493 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer Group started successfully!\n");
501 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
503 if (data_file != NULL)
506 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
508 GNUNET_DISK_file_write (data_file, buf, buf_len);
511 peers_running = GNUNET_TESTING_daemons_running (pg);
513 GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
515 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
521 * Function that will be called whenever two daemons are connected by
522 * the testing library.
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)
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)
548 * run: load configuration options and schedule test to run (start peergroup)
551 * @param cfgfile configuration file name (can be NULL)
552 * @param cfg configuration handle
555 run (void *cls, char *const *args, const char *cfgfile,
556 const struct GNUNET_CONFIGURATION_Handle *cfg)
559 unsigned long long temp_wait;
560 struct GNUNET_TESTING_Host *hosts;
563 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
565 GNUNET_log_setup ("test_mesh_small",
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
575 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
576 "use_progressbars", "YES");
580 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
581 "num_peers", &num_peers))
583 GNUNET_CONFIGURATION_load (testing_cfg, "test_mesh_small.conf");
585 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
586 "num_peers", &num_peers))
588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
589 "Option TESTING:NUM_PEERS is required!\n");
595 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
596 "wait_time", &temp_wait))
598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
599 "Option nsetest_mesh_small:wait_time is required!\n");
604 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
605 "topology_output_file",
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 "Option test_mesh_small:topology_output_file is required!\n");
614 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
619 "Option test_mesh_small:data_output_file is required!\n");
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)
631 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
633 GNUNET_free (data_filename);
637 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
640 GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
641 "output_file", &temp_str))
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",
653 GNUNET_free_non_null (temp_str);
655 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
657 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
658 &connect_cb, &peergroup_ready, NULL,
660 GNUNET_assert (pg != NULL);
662 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
663 &shutdown_task, NULL);
669 * test_mesh_small command line options
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
683 main (int argc, char *argv[])
685 GNUNET_PROGRAM_run (argc, argv, "test_mesh_small",
686 gettext_noop ("Test mesh in a small network."), options,
689 GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
694 /* end of test_mesh_small.c */