6a60461736e4e9549ea52bf52fdf59d5b665ad25
[oweals/gnunet.git] / src / nse / test_nse_multipeer.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2012 GNUnet e.V.
4
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.
9
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.
14     
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 /**
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.
26  */
27 #include "platform.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_nse_service.h"
30
31
32 /**
33  * How many peers do we start?
34  */
35 #define NUM_PEERS 4
36
37 /**
38  * How long do we run the test?
39  */
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
41
42
43 /**
44  * Information we track for each peer.
45  */
46 struct NSEPeer
47 {
48   /**
49    * Handle for NSE connect operation.
50    */
51   struct GNUNET_TESTBED_Operation *op;
52
53   /**
54    * Handle to NSE service.
55    */
56   struct GNUNET_NSE_Handle *nse_handle;
57 };
58
59
60 /**
61  * Information for all the peers.
62  */
63 static struct NSEPeer nse_peers[NUM_PEERS];
64
65 /**
66  * Return value from 'main'.
67  */
68 static int ok;
69
70
71 /**
72  * Task run on timeout to shut everything down.
73  */
74 static void
75 shutdown_task (void *cls)
76 {
77   unsigned int i;
78
79   for (i=0;i<NUM_PEERS;i++)
80     GNUNET_TESTBED_operation_done (nse_peers[i].op);
81   GNUNET_SCHEDULER_shutdown ();
82 }
83
84
85 /**
86  * Callback to call when network size estimate is updated.
87  *
88  * @param cls closure
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
93  *
94  */
95 static void
96 handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
97                  double estimate, double std_dev)
98 {
99   struct NSEPeer *peer = cls;
100
101   FPRINTF (stderr,
102            "Received network size estimate from peer %u. logSize: %f std.dev. %f (%f/%u)\n",
103            (unsigned int) (peer - nse_peers),
104            estimate, std_dev,
105            GNUNET_NSE_log_estimate_to_n (estimate),
106            NUM_PEERS);
107   ok = 0;
108 }
109
110
111 /**
112  * Callback to be called when NSE service connect operation is completed
113  *
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.
119  */
120 static void
121 nse_connect_complete_cb (void *cls,
122                          struct GNUNET_TESTBED_Operation *op,
123                          void *ca_result,
124                          const char *emsg)
125 {
126   struct NSEPeer *peer = cls;
127   struct GNUNET_NSE_Handle *nse = ca_result;
128
129   GNUNET_assert (op == peer->op);
130   if (NULL != emsg)
131     {
132       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133                   "Failed to connect to NSE service: %s\n",
134                   emsg);
135       ok = 1;
136       GNUNET_SCHEDULER_shutdown ();
137       return;
138     }
139   peer->nse_handle = nse;
140 }
141
142
143 /**
144  * Adapter function called to establish a connection to
145  * the NSE service.
146  *
147  * @param cls closure
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
152  */
153 static void *
154 nse_connect_adapter (void *cls,
155                      const struct GNUNET_CONFIGURATION_Handle *cfg)
156 {
157   return GNUNET_NSE_connect (cfg,
158                              &handle_estimate,
159                              cls);
160 }
161
162
163 /**
164  * Adapter function called to destroy connection to
165  * NSE service.
166  *
167  * @param cls closure
168  * @param op_result service handle returned from the connect adapter
169  */
170 static void
171 nse_disconnect_adapter (void *cls,
172                         void *op_result)
173 {
174   GNUNET_NSE_disconnect (op_result);
175 }
176
177
178 /**
179  * Actual "main" function for the testcase.
180  *
181  * @param cls closure
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
186  *          succeeded
187  * @param links_failed the number of overlay link connection attempts that
188  *          failed
189  */
190 static void
191 run (void *cls,
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)
197 {
198   unsigned int i;
199
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],
203                                                       peers[i],
204                                                       "nse",
205                                                       &nse_connect_complete_cb,
206                                                       &nse_peers[i],
207                                                       &nse_connect_adapter,
208                                                       &nse_disconnect_adapter,
209                                                       &nse_peers[i]);
210   GNUNET_SCHEDULER_add_delayed (TIMEOUT,
211                                 &shutdown_task, NULL);
212 }
213
214
215 /**
216  * Entry point for the testcase, sets up the testbed.
217  *
218  * @param argc unused
219  * @param argv unused
220  * @return 0 on success
221  */
222 int
223 main (int argc, char *argv[])
224 {
225   ok = 1;
226   (void) GNUNET_TESTBED_test_run ("test-nse-multipeer",
227                                   "test_nse.conf",
228                                   NUM_PEERS,
229                                   0, NULL, NULL,
230                                   &run, NULL);
231   return ok;
232 }
233
234 /* end of test_nse_multipeer.c */