2 This file is part of GNUnet.
3 (C) 2009, 2012 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
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, const struct GNUNET_SCHEDULER_TaskContext *tc)
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),
111 * Callback to be called when NSE service connect operation is completed
113 * @param cls the callback closure from functions generating an operation
114 * @param op the operation that has been finished
115 * @param ca_result the NSE service handle returned from nse_connect_adapter
116 * @param emsg error message in case the operation has failed; will be NULL if
117 * operation has executed successfully.
120 nse_connect_complete_cb (void *cls,
121 struct GNUNET_TESTBED_Operation *op,
125 struct NSEPeer *peer = cls;
126 struct GNUNET_NSE_Handle *nse = ca_result;
128 GNUNET_assert (op == peer->op);
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
132 "Failed to connect to NSE service: %s\n",
135 GNUNET_SCHEDULER_shutdown ();
138 peer->nse_handle = nse;
143 * Adapter function called to establish a connection to
147 * @param cfg configuration of the peer to connect to; will be available until
148 * GNUNET_TESTBED_operation_done() is called on the operation returned
149 * from GNUNET_TESTBED_service_connect()
150 * @return service handle to return in 'op_result', NULL on error
153 nse_connect_adapter (void *cls,
154 const struct GNUNET_CONFIGURATION_Handle *cfg)
156 return GNUNET_NSE_connect (cfg,
163 * Adapter function called to destroy connection to
167 * @param op_result service handle returned from the connect adapter
170 nse_disconnect_adapter (void *cls,
173 GNUNET_NSE_disconnect (op_result);
178 * Actual "main" function for the testcase.
181 * @param num_peers number of peers in 'peers'
182 * @param peers handle to peers run in the testbed
186 unsigned int num_peers,
187 struct GNUNET_TESTBED_Peer **peers)
191 GNUNET_assert (NUM_PEERS == num_peers);
192 for (i=0;i<num_peers;i++)
193 nse_peers[i].op = GNUNET_TESTBED_service_connect (&nse_peers[i],
196 &nse_connect_complete_cb,
198 &nse_connect_adapter,
199 &nse_disconnect_adapter,
201 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
206 * Entry point for the testcase, sets up the testbed.
210 * @return 0 on success
213 main (int argc, char *argv[])
216 GNUNET_TESTBED_test_run ("test-nse-multipeer",
224 /* end of test_nse_multipeer.c */