ec83e3e8aa736544ff249afb72115de09e981cac
[oweals/gnunet.git] / src / nse / test_nse_multipeer.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2012 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20 /**
21  * @file nse/test_nse_multipeer.c
22  *
23  * @brief Testcase for the network size estimation service.  Starts
24  *        a peergroup with a given number of peers, then waits to
25  *        receive size estimates from each peer.  Expects to wait
26  *        for one message from each peer.
27  */
28 #include "platform.h"
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_nse_service.h"
31
32
33 /**
34  * How many peers do we start?
35  */
36 #define NUM_PEERS 4
37
38 /**
39  * How long do we run the test?
40  */
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
42
43
44 /**
45  * Information we track for each peer.
46  */
47 struct NSEPeer
48 {
49   /**
50    * Handle for NSE connect operation.
51    */
52   struct GNUNET_TESTBED_Operation *op;
53
54   /**
55    * Handle to NSE service.
56    */ 
57   struct GNUNET_NSE_Handle *nse_handle;
58 };
59
60
61 /**
62  * Information for all the peers.
63  */
64 static struct NSEPeer nse_peers[NUM_PEERS];
65
66 /**
67  * Return value from 'main'.
68  */
69 static int ok;
70
71
72 /**
73  * Task run on timeout to shut everything down.
74  */
75 static void
76 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
77 {
78   unsigned int i;
79
80   for (i=0;i<NUM_PEERS;i++)
81     GNUNET_TESTBED_operation_done (nse_peers[i].op);
82   GNUNET_SCHEDULER_shutdown ();
83 }
84
85
86 /**
87  * Callback to call when network size estimate is updated.
88  *
89  * @param cls closure
90  * @param timestamp server timestamp
91  * @param estimate the value of the current network size estimate
92  * @param std_dev standard deviation (rounded down to nearest integer)
93  *                of the size estimation values seen
94  *
95  */
96 static void
97 handle_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp,
98                  double estimate, double std_dev)
99 {
100   struct NSEPeer *peer = cls;
101
102   FPRINTF (stderr,
103            "Received network size estimate from peer %u. logSize: %f std.dev. %f (%f/%u)\n",
104            (unsigned int) (peer - nse_peers), 
105            estimate, std_dev,
106            GNUNET_NSE_log_estimate_to_n (estimate), 
107            NUM_PEERS);
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 num_peers number of peers in 'peers'
183  * @param peers handle to peers run in the testbed
184  */
185 static void
186 run (void *cls,
187      unsigned int num_peers,
188      struct GNUNET_TESTBED_Peer **peers)
189 {
190   unsigned int i;
191
192   GNUNET_assert (NUM_PEERS == num_peers);
193   for (i=0;i<num_peers;i++)
194     nse_peers[i].op = GNUNET_TESTBED_service_connect (&nse_peers[i],
195                                                       peers[i],
196                                                       "nse",
197                                                       &nse_connect_complete_cb,
198                                                       &nse_peers[i],
199                                                       &nse_connect_adapter,
200                                                       &nse_disconnect_adapter,
201                                                       &nse_peers[i]);
202   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
203 }
204
205
206 /**
207  * Entry point for the testcase, sets up the testbed.
208  *
209  * @param argc unused
210  * @param argv unused
211  * @return 0 on success
212  */
213 int
214 main (int argc, char *argv[])
215 {
216   ok = 1;
217   GNUNET_TESTBED_test_run ("test-nse-multipeer",
218                            "test_nse.conf",
219                            NUM_PEERS,
220                            0, NULL, NULL,
221                            &run, NULL);
222   return ok;
223 }
224
225 /* end of test_nse_multipeer.c */