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, 10, NULL, NULL, &tunnel_cleaner, handlers,
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected to mesh service of peer %s\n",
270 GNUNET_i2s (&d->id));
272 t = GNUNET_MESH_tunnel_create (h, NULL, &ch, &dh, NULL);
273 GNUNET_MESH_tunnel_destroy (t);
274 GNUNET_MESH_disconnect (h);
279 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
283 * Continuation called by the "get_all" and "get" functions.
285 * @param cls struct StatsContext
286 * @param success GNUNET_OK if statistics were
287 * successfully obtained, GNUNET_SYSERR if not.
290 stats_finished_callback (void *cls, int success)
292 struct StatsContext *stats_context = cls;
296 if ((GNUNET_OK == success) && (data_file != NULL))
298 /* Stats lookup successful, write out data */
301 GNUNET_asprintf (&buf, "TOTAL_MESH_BYTES: %u\n",
302 stats_context->total_mesh_bytes);
305 GNUNET_DISK_file_write (data_file, buf, buf_len);
307 GNUNET_free_non_null (buf);
310 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
311 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
312 GNUNET_free (stats_context);
317 * Callback function to process statistic values.
319 * @param cls struct StatsContext
320 * @param peer the peer the statistics belong to
321 * @param subsystem name of subsystem that created the statistic
322 * @param name the name of the datum
323 * @param value the current value
324 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
325 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
328 statistics_iterator (void *cls, const struct GNUNET_PeerIdentity *peer,
329 const char *subsystem, const char *name, uint64_t value,
332 struct StatsContext *stats_context = cls;
335 // if ( (0 == strstr(subsystem, "nse")) &&
336 // (0 == strstr(name, "# flood messages received")) )
337 stats_context->total_mesh_bytes += 1; //value;
343 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
345 // struct NSEPeer *pos;
347 struct StatsContext *stats_context;
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting mesh service of peers\n");
350 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
352 // while (NULL != (pos = peer_head))
354 // if (pos->nse_handle != NULL)
356 // GNUNET_NSE_disconnect(pos->nse_handle);
357 // pos->nse_handle = NULL;
359 // GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
363 GNUNET_asprintf (&buf, "round%llu", current_round);
365 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
366 buf, &peers_next_round))
369 GNUNET_assert (churn_task == GNUNET_SCHEDULER_NO_TASK);
370 churn_task = GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
373 { /* No more rounds, let's shut it down! */
375 stats_context = GNUNET_malloc (sizeof (struct StatsContext));
376 GNUNET_SCHEDULER_cancel (shutdown_handle);
377 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
378 GNUNET_TESTING_get_statistics (pg, &stats_finished_callback,
379 &statistics_iterator, stats_context);
389 * @param emsg NULL on success
392 topology_output_callback (void *cls, const char *emsg)
395 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
396 GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
404 * @param emsg NULL on success
407 churn_callback (void *cls, const char *emsg)
409 char *temp_output_file;
412 { /* Everything is okay! */
413 peers_running = peers_next_round;
414 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
415 "Round %llu, churn finished successfully.\n", current_round);
416 GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
417 GNUNET_asprintf (&temp_output_file, "%s_%llu.dot", topology_file,
419 GNUNET_TESTING_peergroup_topology_to_file (pg, temp_output_file,
420 &topology_output_callback, NULL);
421 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Writing topology to file %s\n",
423 GNUNET_free (temp_output_file);
427 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %llu, churn FAILED!!\n",
429 GNUNET_SCHEDULER_cancel (shutdown_handle);
430 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
436 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
438 /* peers_running = GNUNET_TESTING_daemons_running(pg); */
439 churn_task = GNUNET_SCHEDULER_NO_TASK;
440 if (peers_next_round == peers_running)
442 /* Nothing to do... */
443 GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
444 GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
446 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
447 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %lu, doing nothing!\n",
452 if (peers_next_round > num_peers)
454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
455 "Asked to turn on more peers than we have!!\n");
456 GNUNET_SCHEDULER_cancel (shutdown_handle);
457 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
459 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
460 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
463 peers_next_round) ? peers_running - peers_next_round : 0,
465 peers_running) ? peers_next_round - peers_running : 0);
466 GNUNET_TESTING_daemons_churn (pg, "nse",
468 peers_next_round) ? peers_running -
469 peers_next_round : 0,
471 peers_running) ? peers_next_round -
472 peers_running : 0, wait_time, &churn_callback,
479 * peergroup_ready: start test when all peers are connected
481 * @param emsg error message
484 peergroup_ready (void *cls, const char *emsg)
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492 "Peergroup callback called with error, aborting test!\n");
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
495 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer Group started successfully!\n");
502 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
504 if (data_file != NULL)
507 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
509 GNUNET_DISK_file_write (data_file, buf, buf_len);
512 peers_running = GNUNET_TESTING_daemons_running (pg);
514 GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
516 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
522 * Function that will be called whenever two daemons are connected by
523 * the testing library.
526 * @param first peer id for first daemon
527 * @param second peer id for the second daemon
528 * @param distance distance between the connected peers
529 * @param first_cfg config for the first daemon
530 * @param second_cfg config for the second daemon
531 * @param first_daemon handle for the first daemon
532 * @param second_daemon handle for the second daemon
533 * @param emsg error message (NULL on success)
536 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
537 const struct GNUNET_PeerIdentity *second, uint32_t distance,
538 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
539 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
540 struct GNUNET_TESTING_Daemon *first_daemon,
541 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
549 * run: load configuration options and schedule test to run (start peergroup)
552 * @param cfgfile configuration file name (can be NULL)
553 * @param cfg configuration handle
556 run (void *cls, char *const *args, const char *cfgfile,
557 const struct GNUNET_CONFIGURATION_Handle *cfg)
560 unsigned long long temp_wait;
561 struct GNUNET_TESTING_Host *hosts;
564 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
566 GNUNET_log_setup ("test_mesh_small",
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
576 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
577 "use_progressbars", "YES");
581 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
582 "num_peers", &num_peers))
584 GNUNET_CONFIGURATION_load (testing_cfg, "test_mesh_small.conf");
586 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
587 "num_peers", &num_peers))
589 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
590 "Option TESTING:NUM_PEERS is required!\n");
596 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
597 "wait_time", &temp_wait))
599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
600 "Option nsetest_mesh_small:wait_time is required!\n");
605 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
606 "topology_output_file",
609 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
610 "Option test_mesh_small:topology_output_file is required!\n");
615 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
620 "Option test_mesh_small:data_output_file is required!\n");
625 GNUNET_DISK_file_open (data_filename,
626 GNUNET_DISK_OPEN_READWRITE |
627 GNUNET_DISK_OPEN_CREATE,
628 GNUNET_DISK_PERM_USER_READ |
629 GNUNET_DISK_PERM_USER_WRITE);
630 if (data_file == NULL)
632 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
634 GNUNET_free (data_filename);
638 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
641 GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
642 "output_file", &temp_str))
645 GNUNET_DISK_file_open (temp_str,
646 GNUNET_DISK_OPEN_READWRITE |
647 GNUNET_DISK_OPEN_CREATE,
648 GNUNET_DISK_PERM_USER_READ |
649 GNUNET_DISK_PERM_USER_WRITE);
650 if (output_file == NULL)
651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
654 GNUNET_free_non_null (temp_str);
656 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
658 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
659 &connect_cb, &peergroup_ready, NULL,
661 GNUNET_assert (pg != NULL);
663 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
664 &shutdown_task, NULL);
670 * test_mesh_small command line options
672 static struct GNUNET_GETOPT_CommandLineOption options[] = {
673 {'V', "verbose", NULL,
674 gettext_noop ("be verbose (print progress information)"),
675 0, &GNUNET_GETOPT_set_one, &verbose},
676 GNUNET_GETOPT_OPTION_END
684 main (int argc, char *argv[])
686 GNUNET_PROGRAM_run (argc, argv, "test_mesh_small",
687 gettext_noop ("Test mesh in a small network."), options,
690 GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
695 /* end of test_mesh_small.c */