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 * Maximum number of connections to NSE services.
93 static unsigned long long connection_limit;
96 * Total number of connections in the whole network.
98 static unsigned int total_connections;
101 * The currently running peer group.
103 static struct GNUNET_TESTING_PeerGroup *pg;
106 * File to report results to.
108 static struct GNUNET_DISK_FileHandle *output_file;
111 * File to log connection info, statistics to.
113 static struct GNUNET_DISK_FileHandle *data_file;
116 * How many data points to capture before triggering next round?
118 static struct GNUNET_TIME_Relative wait_time;
121 * Task called to disconnect peers.
123 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
126 * Task called to shutdown test.
128 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
131 * Task used to churn the network.
133 static GNUNET_SCHEDULER_TaskIdentifier churn_task;
135 static char *topology_file;
137 static char *data_filename;
140 * Check whether peers successfully shut down.
143 shutdown_callback (void *cls, const char *emsg)
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "All peers successfully shut down!\n");
165 shutdown_task (void *cls,
166 const struct GNUNET_SCHEDULER_TaskContext *tc)
168 // struct NSEPeer *pos;
170 fprintf(stderr, "Ending test.\n");
173 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
175 GNUNET_SCHEDULER_cancel(disconnect_task);
176 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
178 // while (NULL != (pos = peer_head))
180 // if (pos->nse_handle != NULL)
181 // GNUNET_NSE_disconnect(pos->nse_handle);
182 // GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
186 if (data_file != NULL)
187 GNUNET_DISK_file_close(data_file);
188 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
193 connect_mesh_service (void *cls,
194 const struct GNUNET_SCHEDULER_TaskContext *tc)
196 // struct NSEPeer *current_peer;
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201 "Connecting to mesh service of peers\n");
203 for (i = 0; i < num_peers; i++)
205 if ((connection_limit > 0) && (i % (num_peers / connection_limit) != 0))
208 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test_mesh_small: connecting to mesh service of peer %d\n", i);
210 /* current_peer = GNUNET_malloc(sizeof(struct NSEPeer));
211 current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i);
212 if (GNUNET_YES == GNUNET_TESTING_daemon_running(GNUNET_TESTING_daemon_get(pg, i)))
214 current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg,
217 GNUNET_assert(current_peer->nse_handle != NULL);
219 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);*/
225 churn_peers (void *cls,
226 const struct GNUNET_SCHEDULER_TaskContext *tc);
230 * Continuation called by the "get_all" and "get" functions.
232 * @param cls struct StatsContext
233 * @param success GNUNET_OK if statistics were
234 * successfully obtained, GNUNET_SYSERR if not.
237 stats_finished_callback (void *cls, int success)
239 struct StatsContext *stats_context = cls;
243 if ( (GNUNET_OK == success) &&
244 (data_file != NULL) )
246 /* Stats lookup successful, write out data */
248 buf_len = GNUNET_asprintf(&buf,
249 "TOTAL_MESH_BYTES: %u\n",
250 stats_context->total_mesh_bytes);
253 GNUNET_DISK_file_write(data_file, buf, buf_len);
255 GNUNET_free_non_null(buf);
258 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == shutdown_handle);
259 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
260 GNUNET_free(stats_context);
265 * Callback function to process statistic values.
267 * @param cls struct StatsContext
268 * @param peer the peer the statistics belong to
269 * @param subsystem name of subsystem that created the statistic
270 * @param name the name of the datum
271 * @param value the current value
272 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
273 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
276 statistics_iterator (void *cls,
277 const struct GNUNET_PeerIdentity *peer,
278 const char *subsystem,
283 struct StatsContext *stats_context = cls;
285 // if ( (0 == strstr(subsystem, "nse")) &&
286 // (0 == strstr(name, "# flood messages received")) )
287 stats_context->total_mesh_bytes += 1; //value;
293 disconnect_mesh_peers (void *cls,
294 const struct GNUNET_SCHEDULER_TaskContext *tc)
296 // struct NSEPeer *pos;
298 struct StatsContext *stats_context;
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "disconnecting mesh service of peers\n");
302 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
304 // while (NULL != (pos = peer_head))
306 // if (pos->nse_handle != NULL)
308 // GNUNET_NSE_disconnect(pos->nse_handle);
309 // pos->nse_handle = NULL;
311 // GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
315 GNUNET_asprintf(&buf,
318 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (testing_cfg,
324 GNUNET_assert(churn_task == GNUNET_SCHEDULER_NO_TASK);
325 churn_task = GNUNET_SCHEDULER_add_now(&churn_peers, NULL);
327 else /* No more rounds, let's shut it down! */
329 stats_context = GNUNET_malloc(sizeof(struct StatsContext));
330 GNUNET_SCHEDULER_cancel(shutdown_handle);
331 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
332 GNUNET_TESTING_get_statistics(pg,
333 &stats_finished_callback,
334 &statistics_iterator,
345 * @param emsg NULL on success
348 topology_output_callback (void *cls, const char *emsg)
350 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time,
351 &disconnect_mesh_peers, NULL);
352 GNUNET_SCHEDULER_add_now(&connect_mesh_service, NULL);
360 * @param emsg NULL on success
363 churn_callback (void *cls, const char *emsg)
365 char *temp_output_file;
367 if (emsg == NULL) /* Everything is okay! */
369 peers_running = peers_next_round;
370 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
371 "Round %llu, churn finished successfully.\n",
373 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
374 GNUNET_asprintf(&temp_output_file,
378 GNUNET_TESTING_peergroup_topology_to_file(pg,
380 &topology_output_callback,
382 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
383 "Writing topology to file %s\n",
385 GNUNET_free(temp_output_file);
389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
390 "Round %llu, churn FAILED!!\n",
392 GNUNET_SCHEDULER_cancel(shutdown_handle);
393 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
399 churn_peers (void *cls,
400 const struct GNUNET_SCHEDULER_TaskContext *tc)
402 /* peers_running = GNUNET_TESTING_daemons_running(pg); */
403 churn_task = GNUNET_SCHEDULER_NO_TASK;
404 if (peers_next_round == peers_running)
406 /* Nothing to do... */
407 GNUNET_SCHEDULER_add_now(&connect_mesh_service, NULL);
408 GNUNET_assert(disconnect_task == GNUNET_SCHEDULER_NO_TASK);
409 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time,
410 &disconnect_mesh_peers, NULL);
411 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
412 "Round %lu, doing nothing!\n",
417 if (peers_next_round > num_peers)
419 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
420 "Asked to turn on more peers than we have!!\n");
421 GNUNET_SCHEDULER_cancel(shutdown_handle);
422 GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
425 "Round %llu, turning off %llu peers, turning on %llu peers!\n",
427 (peers_running > peers_next_round)
428 ? peers_running - peers_next_round
430 (peers_next_round > peers_running)
431 ? peers_next_round - peers_running
433 GNUNET_TESTING_daemons_churn (pg, "nse",
434 (peers_running > peers_next_round)
435 ? peers_running - peers_next_round
437 (peers_next_round > peers_running)
438 ? peers_next_round - peers_running
441 &churn_callback, NULL);
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Peergroup callback called with error, aborting test!\n");
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
458 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "Peer Group started successfully!\n");
465 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
466 "Have %u connections\n",
468 if (data_file != NULL)
471 buf_len = GNUNET_asprintf(&buf,
472 "CONNECTIONS_0: %u\n",
475 GNUNET_DISK_file_write(data_file, buf, buf_len);
478 peers_running = GNUNET_TESTING_daemons_running(pg);
480 GNUNET_SCHEDULER_add_now(&connect_mesh_service, NULL);
481 disconnect_task = GNUNET_SCHEDULER_add_delayed(wait_time, &disconnect_mesh_peers, NULL);
487 * Function that will be called whenever two daemons are connected by
488 * the testing library.
491 * @param first peer id for first daemon
492 * @param second peer id for the second daemon
493 * @param distance distance between the connected peers
494 * @param first_cfg config for the first daemon
495 * @param second_cfg config for the second daemon
496 * @param first_daemon handle for the first daemon
497 * @param second_daemon handle for the second daemon
498 * @param emsg error message (NULL on success)
501 connect_cb (void *cls,
502 const struct GNUNET_PeerIdentity *first,
503 const struct GNUNET_PeerIdentity *second,
505 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
506 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
507 struct GNUNET_TESTING_Daemon *first_daemon,
508 struct GNUNET_TESTING_Daemon *second_daemon,
519 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
522 unsigned long long temp_wait;
523 struct GNUNET_TESTING_Host *hosts;
526 testing_cfg = (struct GNUNET_CONFIGURATION_Handle *)cfg; // GNUNET_CONFIGURATION_create();
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
529 GNUNET_CONFIGURATION_set_value_string (testing_cfg,
534 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
536 "num_peers", &num_peers))
538 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Option TESTING:NUM_PEERS is required!\n");
542 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
547 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
548 "Option nsetest_mesh_small:wait_time is required!\n");
552 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg,
553 "test_mesh_small", "connection_limit",
556 connection_limit = 0;
559 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg,
560 "testing", "topology_output_file",
563 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
564 "Option test_mesh_small:topology_output_file is required!\n");
568 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg,
569 "test_mesh_small", "data_output_file",
572 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
573 "Option test_mesh_small:data_output_file is required!\n");
577 data_file = GNUNET_DISK_file_open (data_filename,
578 GNUNET_DISK_OPEN_READWRITE
579 | GNUNET_DISK_OPEN_CREATE,
580 GNUNET_DISK_PERM_USER_READ |
581 GNUNET_DISK_PERM_USER_WRITE);
582 if (data_file == NULL)
583 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
584 "Failed to open %s for output!\n",
586 GNUNET_free(data_filename);
588 wait_time = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_wait);
590 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(cfg,
595 output_file = GNUNET_DISK_file_open (temp_str, GNUNET_DISK_OPEN_READWRITE
596 | GNUNET_DISK_OPEN_CREATE,
597 GNUNET_DISK_PERM_USER_READ |
598 GNUNET_DISK_PERM_USER_WRITE);
599 if (output_file == NULL)
600 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
601 "Failed to open %s for output!\n",
604 GNUNET_free_non_null(temp_str);
606 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
608 pg = GNUNET_TESTING_peergroup_start(testing_cfg,
614 GNUNET_assert (pg != NULL);
615 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever(),
623 * test_mesh_small command line options
625 static struct GNUNET_GETOPT_CommandLineOption options[] = {
626 {'V', "verbose", NULL,
627 gettext_noop ("be verbose (print progress information)"),
628 0, &GNUNET_GETOPT_set_one, &verbose},
629 GNUNET_GETOPT_OPTION_END
634 main (int argc, char *argv[])
636 GNUNET_log_setup ("test_mesh_small",
643 GNUNET_PROGRAM_run (argc,
644 argv, "test_mesh_small",
645 gettext_noop ("Test mesh in a small network."),
649 GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
654 /* end of test_mesh_small.c */