2 This file is part of GNUnet.
3 Copyright (C) 2009, 2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file nse/test_nse_multipeer.c
20 * @brief Testcase for the network size estimation service. Starts
21 * a peergroup with a given number of peers, then waits to
22 * receive size estimates from each peer. Expects to wait
23 * for one message from each peer.
26 #include "gnunet_testbed_service.h"
27 #include "gnunet_nse_service.h"
31 * How many peers do we start?
36 * How long do we run the test?
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
42 * Information we track for each peer.
47 * Handle for NSE connect operation.
49 struct GNUNET_TESTBED_Operation *op;
52 * Handle to NSE service.
54 struct GNUNET_NSE_Handle *nse_handle;
59 * Information for all the peers.
61 static struct NSEPeer nse_peers[NUM_PEERS];
64 * Return value from 'main'.
70 * Task run on timeout to shut everything down.
73 shutdown_task (void *cls)
77 for (i=0;i<NUM_PEERS;i++)
78 GNUNET_TESTBED_operation_done (nse_peers[i].op);
79 GNUNET_SCHEDULER_shutdown ();
84 * Callback to call when network size estimate is updated.
87 * @param timestamp server timestamp
88 * @param estimate the value of the current network size estimate
89 * @param std_dev standard deviation (rounded down to nearest integer)
90 * of the size estimation values seen
94 handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
95 double estimate, double std_dev)
97 struct NSEPeer *peer = cls;
100 "Received network size estimate from peer %u. logSize: %f std.dev. %f (%f/%u)\n",
101 (unsigned int) (peer - nse_peers),
103 GNUNET_NSE_log_estimate_to_n (estimate),
110 * Callback to be called when NSE service connect operation is completed
112 * @param cls the callback closure from functions generating an operation
113 * @param op the operation that has been finished
114 * @param ca_result the NSE service handle returned from nse_connect_adapter
115 * @param emsg error message in case the operation has failed; will be NULL if
116 * operation has executed successfully.
119 nse_connect_complete_cb (void *cls,
120 struct GNUNET_TESTBED_Operation *op,
124 struct NSEPeer *peer = cls;
125 struct GNUNET_NSE_Handle *nse = ca_result;
127 GNUNET_assert (op == peer->op);
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
131 "Failed to connect to NSE service: %s\n",
134 GNUNET_SCHEDULER_shutdown ();
137 peer->nse_handle = nse;
142 * Adapter function called to establish a connection to
146 * @param cfg configuration of the peer to connect to; will be available until
147 * GNUNET_TESTBED_operation_done() is called on the operation returned
148 * from GNUNET_TESTBED_service_connect()
149 * @return service handle to return in 'op_result', NULL on error
152 nse_connect_adapter (void *cls,
153 const struct GNUNET_CONFIGURATION_Handle *cfg)
155 return GNUNET_NSE_connect (cfg,
162 * Adapter function called to destroy connection to
166 * @param op_result service handle returned from the connect adapter
169 nse_disconnect_adapter (void *cls,
172 GNUNET_NSE_disconnect (op_result);
177 * Actual "main" function for the testcase.
180 * @param h the run handle
181 * @param num_peers number of peers in 'peers'
182 * @param peers handle to peers run in the testbed
183 * @param links_succeeded the number of overlay link connection attempts that
185 * @param links_failed the number of overlay link connection attempts that
190 struct GNUNET_TESTBED_RunHandle *h,
191 unsigned int num_peers,
192 struct GNUNET_TESTBED_Peer **peers,
193 unsigned int links_succeeded,
194 unsigned int links_failed)
198 GNUNET_assert (NUM_PEERS == num_peers);
199 for (i=0;i<num_peers;i++)
200 nse_peers[i].op = GNUNET_TESTBED_service_connect (&nse_peers[i],
203 &nse_connect_complete_cb,
205 &nse_connect_adapter,
206 &nse_disconnect_adapter,
208 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
209 &shutdown_task, NULL);
214 * Entry point for the testcase, sets up the testbed.
218 * @return 0 on success
221 main (int argc, char *argv[])
224 (void) GNUNET_TESTBED_test_run ("test-nse-multipeer",
232 /* end of test_nse_multipeer.c */