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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file nse/test_nse_multipeer.c
22 * @brief Testcase for the network size estimation service. Starts
23 * a peergroup with a given number of peers, then waits to
24 * receive size estimates from each peer. Expects to wait
25 * for one message from each peer.
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_nse_service.h"
33 * How many peers do we start?
38 * How long do we run the test?
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
44 * Information we track for each peer.
49 * Handle for NSE connect operation.
51 struct GNUNET_TESTBED_Operation *op;
54 * Handle to NSE service.
56 struct GNUNET_NSE_Handle *nse_handle;
61 * Information for all the peers.
63 static struct NSEPeer nse_peers[NUM_PEERS];
66 * Return value from 'main'.
72 * Task run on timeout to shut everything down.
75 shutdown_task (void *cls)
79 for (i=0;i<NUM_PEERS;i++)
80 GNUNET_TESTBED_operation_done (nse_peers[i].op);
81 GNUNET_SCHEDULER_shutdown ();
86 * Callback to call when network size estimate is updated.
89 * @param timestamp server timestamp
90 * @param estimate the value of the current network size estimate
91 * @param std_dev standard deviation (rounded down to nearest integer)
92 * of the size estimation values seen
96 handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
97 double estimate, double std_dev)
99 struct NSEPeer *peer = cls;
102 "Received network size estimate from peer %u. logSize: %f std.dev. %f (%f/%u)\n",
103 (unsigned int) (peer - nse_peers),
105 GNUNET_NSE_log_estimate_to_n (estimate),
112 * Callback to be called when NSE service connect operation is completed
114 * @param cls the callback closure from functions generating an operation
115 * @param op the operation that has been finished
116 * @param ca_result the NSE service handle returned from nse_connect_adapter
117 * @param emsg error message in case the operation has failed; will be NULL if
118 * operation has executed successfully.
121 nse_connect_complete_cb (void *cls,
122 struct GNUNET_TESTBED_Operation *op,
126 struct NSEPeer *peer = cls;
127 struct GNUNET_NSE_Handle *nse = ca_result;
129 GNUNET_assert (op == peer->op);
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133 "Failed to connect to NSE service: %s\n",
136 GNUNET_SCHEDULER_shutdown ();
139 peer->nse_handle = nse;
144 * Adapter function called to establish a connection to
148 * @param cfg configuration of the peer to connect to; will be available until
149 * GNUNET_TESTBED_operation_done() is called on the operation returned
150 * from GNUNET_TESTBED_service_connect()
151 * @return service handle to return in 'op_result', NULL on error
154 nse_connect_adapter (void *cls,
155 const struct GNUNET_CONFIGURATION_Handle *cfg)
157 return GNUNET_NSE_connect (cfg,
164 * Adapter function called to destroy connection to
168 * @param op_result service handle returned from the connect adapter
171 nse_disconnect_adapter (void *cls,
174 GNUNET_NSE_disconnect (op_result);
179 * Actual "main" function for the testcase.
182 * @param h the run handle
183 * @param num_peers number of peers in 'peers'
184 * @param peers handle to peers run in the testbed
185 * @param links_succeeded the number of overlay link connection attempts that
187 * @param links_failed the number of overlay link connection attempts that
192 struct GNUNET_TESTBED_RunHandle *h,
193 unsigned int num_peers,
194 struct GNUNET_TESTBED_Peer **peers,
195 unsigned int links_succeeded,
196 unsigned int links_failed)
200 GNUNET_assert (NUM_PEERS == num_peers);
201 for (i=0;i<num_peers;i++)
202 nse_peers[i].op = GNUNET_TESTBED_service_connect (&nse_peers[i],
205 &nse_connect_complete_cb,
207 &nse_connect_adapter,
208 &nse_disconnect_adapter,
210 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
211 &shutdown_task, NULL);
216 * Entry point for the testcase, sets up the testbed.
220 * @return 0 on success
223 main (int argc, char *argv[])
226 (void) GNUNET_TESTBED_test_run ("test-nse-multipeer",
234 /* end of test_nse_multipeer.c */