2 This file is part of GNUnet.
3 (C) 2009 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 nse/test_nse_multipeer.c
23 * @brief Testcase for the network size estimation service. Starts
24 * a peergroup with a given number of peers, then waits to
25 * receive size estimates from each peer. Expects to wait
26 * for one message from each peer.
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_nse_service.h"
32 #define VERBOSE GNUNET_EXTRA_LOGGING
42 struct GNUNET_TESTING_Daemon *daemon;
44 struct GNUNET_NSE_Handle *nse_handle;
47 struct NSEPeer *peer_head;
49 struct NSEPeer *peer_tail;
52 * How long do we run the test?
54 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
58 static int peers_left;
60 static unsigned int num_peers;
62 static unsigned int total_connections;
64 static struct GNUNET_TESTING_PeerGroup *pg;
67 * Check whether peers successfully shut down.
70 shutdown_callback (void *cls, const char *emsg)
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed: %s!\n",
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
91 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 fprintf (stderr, "Ending test.\n");
99 while (NULL != (pos = peer_head))
101 GNUNET_NSE_disconnect (pos->nse_handle);
102 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, pos);
106 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
110 * Callback to call when network size estimate is updated.
113 * @param timestamp server timestamp
114 * @param estimate the value of the current network size estimate
115 * @param std_dev standard deviation (rounded down to nearest integer)
116 * of the size estimation values seen
120 handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
121 double estimate, double std_dev)
123 struct NSEPeer *peer = cls;
126 "Received network size estimate from peer %s. logSize: %f std.dev. %f (%f/%u)\n",
127 GNUNET_i2s (&peer->daemon->id), estimate, std_dev,
128 GNUNET_NSE_log_estimate_to_n (estimate), num_peers);
133 connect_nse_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 struct NSEPeer *current_peer;
139 fprintf (stderr, "TEST_NSE_MULTIPEER: connecting to nse service of peers\n");
141 for (i = 0; i < num_peers; i++)
143 current_peer = GNUNET_malloc (sizeof (struct NSEPeer));
144 current_peer->daemon = GNUNET_TESTING_daemon_get (pg, i);
145 current_peer->nse_handle =
146 GNUNET_NSE_connect (current_peer->daemon->cfg, &handle_estimate,
148 GNUNET_assert (current_peer->nse_handle != NULL);
149 GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
155 my_cb (void *cls, const char *emsg)
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "Peergroup callback called with error, aborting test!\n");
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
163 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Peer Group started successfully, connecting to NSE service for each peer!\n");
170 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Have %u connections\n",
173 GNUNET_SCHEDULER_add_now (&connect_nse_service, NULL);
178 * Function that will be called whenever
179 * two daemons are connected by the testing library.
182 * @param first peer id for first daemon
183 * @param second peer id for the second daemon
184 * @param distance distance between the connected peers
185 * @param first_cfg config for the first daemon
186 * @param second_cfg config for the second daemon
187 * @param first_daemon handle for the first daemon
188 * @param second_daemon handle for the second daemon
189 * @param emsg error message (NULL on success)
192 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
193 const struct GNUNET_PeerIdentity *second, uint32_t distance,
194 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
195 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
196 struct GNUNET_TESTING_Daemon *first_daemon,
197 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
201 //fprintf(stderr, "Connected %s -> %s\n", GNUNET_i2s(first), second_id);
209 run (void *cls, char *const *args, const char *cfgfile,
210 const struct GNUNET_CONFIGURATION_Handle *cfg)
212 struct GNUNET_CONFIGURATION_Handle *testing_cfg;
213 unsigned long long total_peers;
216 testing_cfg = GNUNET_CONFIGURATION_create ();
217 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (testing_cfg, cfgfile));
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
221 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
222 "use_progressbars", "YES");
225 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
226 "num_peers", &total_peers))
227 total_peers = NUM_PEERS;
229 peers_left = total_peers;
230 num_peers = peers_left;
231 pg = GNUNET_TESTING_peergroup_start (testing_cfg, peers_left, TIMEOUT,
232 &connect_cb, &my_cb, NULL, NULL);
233 GNUNET_assert (pg != NULL);
234 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
241 char *const argv[] = { "test-nse-multipeer",
249 struct GNUNET_GETOPT_CommandLineOption options[] = {
250 GNUNET_GETOPT_OPTION_END
252 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
253 "test-nse-multipeer", "nohelp", options, &run, &ok);
259 main (int argc, char *argv[])
263 GNUNET_log_setup ("test-nse-multipeer",
271 // GNUNET_DISK_directory_remove ("/tmp/test-nse-multipeer");
275 /* end of test_nse_multipeer.c */