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.
16 * @file nse/test_nse_multipeer.c
17 * @brief Testcase for the network size estimation service. Starts
18 * a peergroup with a given number of peers, then waits to
19 * receive size estimates from each peer. Expects to wait
20 * for one message from each peer.
23 #include "gnunet_testbed_service.h"
24 #include "gnunet_nse_service.h"
28 * How many peers do we start?
33 * How long do we run the test?
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
39 * Information we track for each peer.
44 * Handle for NSE connect operation.
46 struct GNUNET_TESTBED_Operation *op;
49 * Handle to NSE service.
51 struct GNUNET_NSE_Handle *nse_handle;
56 * Information for all the peers.
58 static struct NSEPeer nse_peers[NUM_PEERS];
61 * Return value from 'main'.
67 * Task run on timeout to shut everything down.
70 shutdown_task (void *cls)
74 for (i=0;i<NUM_PEERS;i++)
75 GNUNET_TESTBED_operation_done (nse_peers[i].op);
76 GNUNET_SCHEDULER_shutdown ();
81 * Callback to call when network size estimate is updated.
84 * @param timestamp server timestamp
85 * @param estimate the value of the current network size estimate
86 * @param std_dev standard deviation (rounded down to nearest integer)
87 * of the size estimation values seen
91 handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
92 double estimate, double std_dev)
94 struct NSEPeer *peer = cls;
97 "Received network size estimate from peer %u. logSize: %f std.dev. %f (%f/%u)\n",
98 (unsigned int) (peer - nse_peers),
100 GNUNET_NSE_log_estimate_to_n (estimate),
107 * Callback to be called when NSE service connect operation is completed
109 * @param cls the callback closure from functions generating an operation
110 * @param op the operation that has been finished
111 * @param ca_result the NSE service handle returned from nse_connect_adapter
112 * @param emsg error message in case the operation has failed; will be NULL if
113 * operation has executed successfully.
116 nse_connect_complete_cb (void *cls,
117 struct GNUNET_TESTBED_Operation *op,
121 struct NSEPeer *peer = cls;
122 struct GNUNET_NSE_Handle *nse = ca_result;
124 GNUNET_assert (op == peer->op);
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
128 "Failed to connect to NSE service: %s\n",
131 GNUNET_SCHEDULER_shutdown ();
134 peer->nse_handle = nse;
139 * Adapter function called to establish a connection to
143 * @param cfg configuration of the peer to connect to; will be available until
144 * GNUNET_TESTBED_operation_done() is called on the operation returned
145 * from GNUNET_TESTBED_service_connect()
146 * @return service handle to return in 'op_result', NULL on error
149 nse_connect_adapter (void *cls,
150 const struct GNUNET_CONFIGURATION_Handle *cfg)
152 return GNUNET_NSE_connect (cfg,
159 * Adapter function called to destroy connection to
163 * @param op_result service handle returned from the connect adapter
166 nse_disconnect_adapter (void *cls,
169 GNUNET_NSE_disconnect (op_result);
174 * Actual "main" function for the testcase.
177 * @param h the run handle
178 * @param num_peers number of peers in 'peers'
179 * @param peers handle to peers run in the testbed
180 * @param links_succeeded the number of overlay link connection attempts that
182 * @param links_failed the number of overlay link connection attempts that
187 struct GNUNET_TESTBED_RunHandle *h,
188 unsigned int num_peers,
189 struct GNUNET_TESTBED_Peer **peers,
190 unsigned int links_succeeded,
191 unsigned int links_failed)
195 GNUNET_assert (NUM_PEERS == num_peers);
196 for (i=0;i<num_peers;i++)
197 nse_peers[i].op = GNUNET_TESTBED_service_connect (&nse_peers[i],
200 &nse_connect_complete_cb,
202 &nse_connect_adapter,
203 &nse_disconnect_adapter,
205 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
206 &shutdown_task, NULL);
211 * Entry point for the testcase, sets up the testbed.
215 * @return 0 on success
218 main (int argc, char *argv[])
221 (void) GNUNET_TESTBED_test_run ("test-nse-multipeer",
229 /* end of test_nse_multipeer.c */