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,
152 "All peers successfully shut down!\n");
160 shutdown_task (void *cls,
161 const struct GNUNET_SCHEDULER_TaskContext *tc)
163 // struct NSEPeer *pos;
165 fprintf(stderr, "Ending test.\n");
168 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
170 GNUNET_SCHEDULER_cancel(disconnect_task);
171 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
173 // while (NULL != (pos = peer_head))
175 // if (pos->nse_handle != NULL)
176 // GNUNET_NSE_disconnect(pos->nse_handle);
177 // GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
181 if (data_file != NULL)
182 GNUNET_DISK_file_close(data_file);
183 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
188 connect_mesh_service (void *cls,
189 const struct GNUNET_SCHEDULER_TaskContext *tc)
191 // struct NSEPeer *current_peer;
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "Connecting to mesh service of peers\n");
198 for (i = 0; i < num_peers; i++)
200 // if ((connection_limit > 0) && (i % (num_peers / connection_limit) != 0))
203 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test_mesh_small: connecting to mesh service of peer %d\n", i);
205 /* current_peer = GNUNET_malloc(sizeof(struct NSEPeer));
206 current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i);
207 if (GNUNET_YES == GNUNET_TESTING_daemon_running(GNUNET_TESTING_daemon_get(pg, i)))
209 current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg,
212 GNUNET_assert(current_peer->nse_handle != NULL);
214 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);*/
220 churn_peers (void *cls,
221 const struct GNUNET_SCHEDULER_TaskContext *tc);
225 * Continuation called by the "get_all" and "get" functions.
227 * @param cls struct StatsContext
228 * @param success GNUNET_OK if statistics were
229 * successfully obtained, GNUNET_SYSERR if not.
232 stats_finished_callback (void *cls, int success)
234 struct StatsContext *stats_context = cls;
238 if ( (GNUNET_OK == success) &&
239 (data_file != NULL) )
241 /* Stats lookup successful, write out data */
243 buf_len = GNUNET_asprintf(&buf,
244 "TOTAL_MESH_BYTES: %u\n",
245 stats_context->total_mesh_bytes);
248 GNUNET_DISK_file_write(data_file, buf, buf_len);
250 GNUNET_free_non_null(buf);
253 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
254 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
255 GNUNET_free(stats_context);
260 * Callback function to process statistic values.
262 * @param cls struct StatsContext
263 * @param peer the peer the statistics belong to
264 * @param subsystem name of subsystem that created the statistic
265 * @param name the name of the datum
266 * @param value the current value
267 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
268 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
271 statistics_iterator (void *cls,
272 const struct GNUNET_PeerIdentity *peer,
273 const char *subsystem,
278 struct StatsContext *stats_context = cls;
280 // if ( (0 == strstr(subsystem, "nse")) &&
281 // (0 == strstr(name, "# flood messages received")) )
282 stats_context->total_mesh_bytes += 1; //value;
288 disconnect_mesh_peers (void *cls,
289 const struct GNUNET_SCHEDULER_TaskContext *tc)
291 // struct NSEPeer *pos;
293 struct StatsContext *stats_context;
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "disconnecting mesh service of peers\n");
297 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
299 // while (NULL != (pos = peer_head))
301 // if (pos->nse_handle != NULL)
303 // GNUNET_NSE_disconnect(pos->nse_handle);
304 // pos->nse_handle = NULL;
306 // GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
310 GNUNET_asprintf(&buf,
313 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (testing_cfg,
319 GNUNET_assert(churn_task == GNUNET_SCHEDULER_NO_TASK);
320 churn_task = GNUNET_SCHEDULER_add_now(&churn_peers, NULL);
322 else /* No more rounds, let's shut it down! */
324 stats_context = GNUNET_malloc(sizeof(struct StatsContext));
325 GNUNET_SCHEDULER_cancel(shutdown_handle);
326 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
327 GNUNET_TESTING_get_statistics(pg,
328 &stats_finished_callback,
329 &statistics_iterator,
340 * @param emsg NULL on success
343 topology_output_callback (void *cls, const char *emsg)
345 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time,
346 &disconnect_mesh_peers, NULL);
347 GNUNET_SCHEDULER_add_now(&connect_mesh_service, NULL);
355 * @param emsg NULL on success
358 churn_callback (void *cls, const char *emsg)
360 char *temp_output_file;
362 if (emsg == NULL) /* Everything is okay! */
364 peers_running = peers_next_round;
365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
366 "Round %llu, churn finished successfully.\n",
368 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
369 GNUNET_asprintf(&temp_output_file,
373 GNUNET_TESTING_peergroup_topology_to_file(pg,
375 &topology_output_callback,
377 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
378 "Writing topology to file %s\n",
380 GNUNET_free(temp_output_file);
384 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
385 "Round %llu, churn FAILED!!\n",
387 GNUNET_SCHEDULER_cancel(shutdown_handle);
388 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
394 churn_peers (void *cls,
395 const struct GNUNET_SCHEDULER_TaskContext *tc)
397 /* peers_running = GNUNET_TESTING_daemons_running(pg); */
398 churn_task = GNUNET_SCHEDULER_NO_TASK;
399 if (peers_next_round == peers_running)
401 /* Nothing to do... */
402 GNUNET_SCHEDULER_add_now(&connect_mesh_service, NULL);
403 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
404 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time,
405 &disconnect_mesh_peers, NULL);
406 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
407 "Round %lu, doing nothing!\n",
412 if (peers_next_round > num_peers)
414 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
415 "Asked to turn on more peers than we have!!\n");
416 GNUNET_SCHEDULER_cancel(shutdown_handle);
417 GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
420 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
422 (peers_running > peers_next_round)
423 ? peers_running - peers_next_round
425 (peers_next_round > peers_running)
426 ? peers_next_round - peers_running
428 GNUNET_TESTING_daemons_churn (pg, "nse",
429 (peers_running > peers_next_round)
430 ? peers_running - peers_next_round
432 (peers_next_round > peers_running)
433 ? peers_next_round - peers_running
436 &churn_callback, NULL);
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450 "Peergroup callback called with error, aborting test!\n");
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
453 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Peer Group started successfully!\n");
460 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
461 "Have %u connections\n",
463 if (data_file != NULL)
466 buf_len = GNUNET_asprintf(&buf,
467 "CONNECTIONS_0: %u\n",
470 GNUNET_DISK_file_write(data_file, buf, buf_len);
473 peers_running = GNUNET_TESTING_daemons_running(pg);
475 GNUNET_SCHEDULER_add_now(&connect_mesh_service, NULL);
476 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, &disconnect_mesh_peers, NULL);
482 * Function that will be called whenever two daemons are connected by
483 * the testing library.
486 * @param first peer id for first daemon
487 * @param second peer id for the second daemon
488 * @param distance distance between the connected peers
489 * @param first_cfg config for the first daemon
490 * @param second_cfg config for the second daemon
491 * @param first_daemon handle for the first daemon
492 * @param second_daemon handle for the second daemon
493 * @param emsg error message (NULL on success)
496 connect_cb (void *cls,
497 const struct GNUNET_PeerIdentity *first,
498 const struct GNUNET_PeerIdentity *second,
500 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
501 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
502 struct GNUNET_TESTING_Daemon *first_daemon,
503 struct GNUNET_TESTING_Daemon *second_daemon,
514 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
517 unsigned long long temp_wait;
518 struct GNUNET_TESTING_Host *hosts;
521 testing_cfg = (struct GNUNET_CONFIGURATION_Handle *)cfg; // GNUNET_CONFIGURATION_create();
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
524 GNUNET_CONFIGURATION_set_value_string (testing_cfg,
529 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
531 "num_peers", &num_peers))
533 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Option TESTING:NUM_PEERS is required!\n");
537 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
542 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
543 "Option nsetest_mesh_small:wait_time is required!\n");
547 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg,
548 "testing", "topology_output_file",
551 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
552 "Option test_mesh_small:topology_output_file is required!\n");
556 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg,
557 "test_mesh_small", "data_output_file",
560 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
561 "Option test_mesh_small:data_output_file is required!\n");
565 data_file = GNUNET_DISK_file_open (data_filename,
566 GNUNET_DISK_OPEN_READWRITE
567 | GNUNET_DISK_OPEN_CREATE,
568 GNUNET_DISK_PERM_USER_READ |
569 GNUNET_DISK_PERM_USER_WRITE);
570 if (data_file == NULL)
571 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
572 "Failed to open %s for output!\n",
574 GNUNET_free(data_filename);
576 wait_time = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_wait);
578 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(cfg,
583 output_file = GNUNET_DISK_file_open (temp_str, GNUNET_DISK_OPEN_READWRITE
584 | GNUNET_DISK_OPEN_CREATE,
585 GNUNET_DISK_PERM_USER_READ |
586 GNUNET_DISK_PERM_USER_WRITE);
587 if (output_file == NULL)
588 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
589 "Failed to open %s for output!\n",
592 GNUNET_free_non_null(temp_str);
594 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
596 pg = GNUNET_TESTING_peergroup_start(testing_cfg,
602 GNUNET_assert (pg != NULL);
603 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever(),
611 * test_mesh_small command line options
613 static struct GNUNET_GETOPT_CommandLineOption options[] = {
614 {'V', "verbose", NULL,
615 gettext_noop ("be verbose (print progress information)"),
616 0, &GNUNET_GETOPT_set_one, &verbose},
617 GNUNET_GETOPT_OPTION_END
622 main (int argc, char *argv[])
624 GNUNET_log_setup ("test_mesh_small",
631 GNUNET_PROGRAM_run (argc,
632 argv, "test_mesh_small",
633 gettext_noop ("Test mesh in a small network."),
637 GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
642 /* end of test_mesh_small.c */