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