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.h"
29 #define VERBOSE GNUNET_NO
33 struct MeshPeer *prev;
35 struct MeshPeer *next;
37 struct GNUNET_TESTING_Daemon *daemon;
39 struct GNUNET_MESH_Handle *mesh_handle;
45 unsigned long long total_mesh_bytes;
49 // static struct MeshPeer *peer_head;
51 // static struct MeshPeer *peer_tail;
54 * How long until we give up on connecting the peers?
56 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
66 * Total number of peers in the test.
68 static unsigned long long num_peers;
71 * Global configuration file
73 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
76 * Total number of currently running peers.
78 static unsigned long long peers_running;
81 * Current round we are in.
83 static unsigned long long current_round;
86 * Peers desired in the next round.
88 static unsigned long long peers_next_round;
91 * Total number of connections in the whole network.
93 static unsigned int total_connections;
96 * The currently running peer group.
98 static struct GNUNET_TESTING_PeerGroup *pg;
101 * File to report results to.
103 static struct GNUNET_DISK_FileHandle *output_file;
106 * File to log connection info, statistics to.
108 static struct GNUNET_DISK_FileHandle *data_file;
111 * How many data points to capture before triggering next round?
113 static struct GNUNET_TIME_Relative wait_time;
116 * Task called to disconnect peers.
118 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
121 * Task called to shutdown test.
123 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
126 * Task used to churn the network.
128 static GNUNET_SCHEDULER_TaskIdentifier churn_task;
130 static char *topology_file;
132 static char *data_filename;
135 * Check whether peers successfully shut down.
138 shutdown_callback (void *cls, const char *emsg)
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
159 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 // struct NSEPeer *pos;
163 fprintf (stderr, "Ending test.\n");
166 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
168 GNUNET_SCHEDULER_cancel (disconnect_task);
169 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
171 // while (NULL != (pos = peer_head))
173 // if (pos->nse_handle != NULL)
174 // GNUNET_NSE_disconnect(pos->nse_handle);
175 // GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
179 if (data_file != NULL)
180 GNUNET_DISK_file_close (data_file);
181 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
186 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
188 // struct NSEPeer *current_peer;
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to mesh service of peers\n");
194 for (i = 0; i < num_peers; i++)
196 // if ((connection_limit > 0) && (i % (num_peers / connection_limit) != 0))
199 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
200 "test_mesh_small: connecting to mesh service of peer %d\n", i);
202 /* current_peer = GNUNET_malloc(sizeof(struct NSEPeer));
203 current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i);
204 if (GNUNET_YES == GNUNET_TESTING_daemon_running(GNUNET_TESTING_daemon_get(pg, i)))
206 current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg,
209 GNUNET_assert(current_peer->nse_handle != NULL);
211 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);*/
217 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
221 * Continuation called by the "get_all" and "get" functions.
223 * @param cls struct StatsContext
224 * @param success GNUNET_OK if statistics were
225 * successfully obtained, GNUNET_SYSERR if not.
228 stats_finished_callback (void *cls, int success)
230 struct StatsContext *stats_context = cls;
234 if ((GNUNET_OK == success) && (data_file != NULL))
236 /* Stats lookup successful, write out data */
239 GNUNET_asprintf (&buf, "TOTAL_MESH_BYTES: %u\n",
240 stats_context->total_mesh_bytes);
243 GNUNET_DISK_file_write (data_file, buf, buf_len);
245 GNUNET_free_non_null (buf);
248 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
249 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
250 GNUNET_free (stats_context);
255 * Callback function to process statistic values.
257 * @param cls struct StatsContext
258 * @param peer the peer the statistics belong to
259 * @param subsystem name of subsystem that created the statistic
260 * @param name the name of the datum
261 * @param value the current value
262 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
263 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
266 statistics_iterator (void *cls, const struct GNUNET_PeerIdentity *peer,
267 const char *subsystem, const char *name, uint64_t value,
270 struct StatsContext *stats_context = cls;
273 // if ( (0 == strstr(subsystem, "nse")) &&
274 // (0 == strstr(name, "# flood messages received")) )
275 stats_context->total_mesh_bytes += 1; //value;
281 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
283 // struct NSEPeer *pos;
285 struct StatsContext *stats_context;
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnecting mesh service of peers\n");
288 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
290 // while (NULL != (pos = peer_head))
292 // if (pos->nse_handle != NULL)
294 // GNUNET_NSE_disconnect(pos->nse_handle);
295 // pos->nse_handle = NULL;
297 // GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
301 GNUNET_asprintf (&buf, "round%llu", current_round);
303 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
304 buf, &peers_next_round))
307 GNUNET_assert (churn_task == GNUNET_SCHEDULER_NO_TASK);
308 churn_task = GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
310 else /* No more rounds, let's shut it down! */
312 stats_context = GNUNET_malloc (sizeof (struct StatsContext));
313 GNUNET_SCHEDULER_cancel (shutdown_handle);
314 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
315 GNUNET_TESTING_get_statistics (pg, &stats_finished_callback,
316 &statistics_iterator, stats_context);
326 * @param emsg NULL on success
329 topology_output_callback (void *cls, const char *emsg)
332 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
333 GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
341 * @param emsg NULL on success
344 churn_callback (void *cls, const char *emsg)
346 char *temp_output_file;
348 if (emsg == NULL) /* Everything is okay! */
350 peers_running = peers_next_round;
351 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
352 "Round %llu, churn finished successfully.\n", current_round);
353 GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
354 GNUNET_asprintf (&temp_output_file, "%s_%llu.dot", topology_file,
356 GNUNET_TESTING_peergroup_topology_to_file (pg, temp_output_file,
357 &topology_output_callback, NULL);
358 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Writing topology to file %s\n",
360 GNUNET_free (temp_output_file);
364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %llu, churn FAILED!!\n",
366 GNUNET_SCHEDULER_cancel (shutdown_handle);
367 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
373 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
375 /* peers_running = GNUNET_TESTING_daemons_running(pg); */
376 churn_task = GNUNET_SCHEDULER_NO_TASK;
377 if (peers_next_round == peers_running)
379 /* Nothing to do... */
380 GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
381 GNUNET_assert (disconnect_task == GNUNET_SCHEDULER_NO_TASK);
383 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
384 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Round %lu, doing nothing!\n",
389 if (peers_next_round > num_peers)
391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
392 "Asked to turn on more peers than we have!!\n");
393 GNUNET_SCHEDULER_cancel (shutdown_handle);
394 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
396 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
397 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
400 peers_next_round) ? peers_running - peers_next_round : 0,
402 peers_running) ? peers_next_round - peers_running : 0);
403 GNUNET_TESTING_daemons_churn (pg, "nse",
405 peers_next_round) ? peers_running -
406 peers_next_round : 0,
408 peers_running) ? peers_next_round -
409 peers_running : 0, wait_time, &churn_callback,
416 my_cb (void *cls, const char *emsg)
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Peergroup callback called with error, aborting test!\n");
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
427 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer Group started successfully!\n");
433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
435 if (data_file != NULL)
438 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
440 GNUNET_DISK_file_write (data_file, buf, buf_len);
443 peers_running = GNUNET_TESTING_daemons_running (pg);
445 GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
447 GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
453 * Function that will be called whenever two daemons are connected by
454 * the testing library.
457 * @param first peer id for first daemon
458 * @param second peer id for the second daemon
459 * @param distance distance between the connected peers
460 * @param first_cfg config for the first daemon
461 * @param second_cfg config for the second daemon
462 * @param first_daemon handle for the first daemon
463 * @param second_daemon handle for the second daemon
464 * @param emsg error message (NULL on success)
467 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
468 const struct GNUNET_PeerIdentity *second, uint32_t distance,
469 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
470 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
471 struct GNUNET_TESTING_Daemon *first_daemon,
472 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
480 run (void *cls, char *const *args, const char *cfgfile,
481 const struct GNUNET_CONFIGURATION_Handle *cfg)
484 unsigned long long temp_wait;
485 struct GNUNET_TESTING_Host *hosts;
488 testing_cfg = (struct GNUNET_CONFIGURATION_Handle *) cfg; // GNUNET_CONFIGURATION_create();
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
491 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
492 "use_progressbars", "YES");
495 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
496 "num_peers", &num_peers))
498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
499 "Option TESTING:NUM_PEERS is required!\n");
504 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
505 "wait_time", &temp_wait))
507 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
508 "Option nsetest_mesh_small:wait_time is required!\n");
513 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
514 "topology_output_file",
517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
518 "Option test_mesh_small:topology_output_file is required!\n");
523 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
527 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
528 "Option test_mesh_small:data_output_file is required!\n");
533 GNUNET_DISK_file_open (data_filename,
534 GNUNET_DISK_OPEN_READWRITE |
535 GNUNET_DISK_OPEN_CREATE,
536 GNUNET_DISK_PERM_USER_READ |
537 GNUNET_DISK_PERM_USER_WRITE);
538 if (data_file == NULL)
539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
541 GNUNET_free (data_filename);
544 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
547 GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
548 "output_file", &temp_str))
551 GNUNET_DISK_file_open (temp_str,
552 GNUNET_DISK_OPEN_READWRITE |
553 GNUNET_DISK_OPEN_CREATE,
554 GNUNET_DISK_PERM_USER_READ |
555 GNUNET_DISK_PERM_USER_WRITE);
556 if (output_file == NULL)
557 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
560 GNUNET_free_non_null (temp_str);
562 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
564 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
565 &connect_cb, &my_cb, NULL, hosts);
566 GNUNET_assert (pg != NULL);
568 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
569 &shutdown_task, NULL);
575 * test_mesh_small command line options
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
586 main (int argc, char *argv[])
588 GNUNET_log_setup ("test_mesh_small",
595 GNUNET_PROGRAM_run (argc, argv, "test_mesh_small",
596 gettext_noop ("Test mesh in a small network."), options,
599 GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
604 /* end of test_mesh_small.c */