d06ec7c3019e7f2f823c41b3ec9012d69600acd8
[oweals/gnunet.git] / src / nse / test_nse_multipeer.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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_testing_lib.h"
30 #include "gnunet_nse_service.h"
31
32 #define VERBOSE GNUNET_NO
33
34 #define NUM_PEERS 4
35
36 struct NSEPeer
37 {
38   struct NSEPeer *prev;
39
40   struct NSEPeer *next;
41
42   struct GNUNET_TESTING_Daemon *daemon;
43
44   struct GNUNET_NSE_Handle *nse_handle;
45 };
46
47 struct NSEPeer *peer_head;
48
49 struct NSEPeer *peer_tail;
50
51 /**
52  * How long do we run the test?
53  */
54 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
55
56 static int ok;
57
58 static int peers_left;
59
60 static unsigned int num_peers;
61
62 static unsigned int total_connections;
63
64 static struct GNUNET_TESTING_PeerGroup *pg;
65
66 /**
67  * Check whether peers successfully shut down.
68  */
69 static void
70 shutdown_callback (void *cls, const char *emsg)
71 {
72   if (emsg != NULL)
73     {
74 #if VERBOSE
75       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76                   "Shutdown of peers failed: %s!\n",
77                   emsg);
78 #endif
79       if (ok == 0)
80         ok = 666;
81     }
82   else
83     {
84 #if VERBOSE
85       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86                   "All peers successfully shut down!\n");
87 #endif
88       ok = 0;
89     }
90 }
91
92 static void
93 shutdown_task (void *cls,
94                const struct GNUNET_SCHEDULER_TaskContext *tc)
95 {
96   struct NSEPeer *pos;
97 #if VERBOSE
98   fprintf(stderr, "Ending test.\n");
99 #endif
100
101   while (NULL != (pos = peer_head))
102     {
103       GNUNET_NSE_disconnect(pos->nse_handle);
104       GNUNET_CONTAINER_DLL_remove(peer_head, peer_tail, pos);
105       GNUNET_free(pos);
106     }
107
108   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
109 }
110
111 /**
112  * Callback to call when network size estimate is updated.
113  *
114  * @param cls closure
115  * @param estimate the value of the current network size estimate
116  * @param std_dev standard deviation (rounded down to nearest integer)
117  *                of the size estimation values seen
118  *
119  */
120 static void
121 handle_estimate (void *cls, double estimate, double std_dev)
122 {
123   struct NSEPeer *peer = cls;
124
125   fprintf (stderr,
126            "Received network size estimate from peer %s. logSize: %f std.dev. %f (%f/%u)\n", 
127            GNUNET_i2s(&peer->daemon->id), 
128            estimate, 
129            std_dev,
130            GNUNET_NSE_log_estimate_to_n (estimate),
131            num_peers);
132 }
133
134
135 static void
136 connect_nse_service (void *cls,
137                      const struct GNUNET_SCHEDULER_TaskContext *tc)
138 {
139   struct NSEPeer *current_peer;
140   unsigned int i;
141 #if VERBOSE
142   fprintf(stderr, "TEST_NSE_MULTIPEER: connecting to nse service of peers\n");
143 #endif
144   for (i = 0; i < num_peers; i++)
145     {
146       current_peer = GNUNET_malloc(sizeof(struct NSEPeer));
147       current_peer->daemon = GNUNET_TESTING_daemon_get(pg, i);
148       current_peer->nse_handle = GNUNET_NSE_connect (current_peer->daemon->cfg,
149                                                      &handle_estimate, 
150                                                      current_peer);
151       GNUNET_assert(current_peer->nse_handle != NULL);
152       GNUNET_CONTAINER_DLL_insert (peer_head, peer_tail, current_peer);
153     }
154 }
155
156
157 static void
158 my_cb (void *cls,
159        const char *emsg)
160 {
161   if (emsg != NULL)
162     {
163       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164                   "Peergroup callback called with error, aborting test!\n");
165       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n");
166       ok = 1;
167       GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
168       return;
169     }
170 #if VERBOSE
171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172               "Peer Group started successfully, connecting to NSE service for each peer!\n");
173 #endif
174   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 
175               "Have %u connections\n", total_connections);
176
177   GNUNET_SCHEDULER_add_now(&connect_nse_service, NULL);
178 }
179
180
181 /**
182  * Function that will be called whenever
183  * two daemons are connected by the testing library.
184  *
185  * @param cls closure
186  * @param first peer id for first daemon
187  * @param second peer id for the second daemon
188  * @param distance distance between the connected peers
189  * @param first_cfg config for the first daemon
190  * @param second_cfg config for the second daemon
191  * @param first_daemon handle for the first daemon
192  * @param second_daemon handle for the second daemon
193  * @param emsg error message (NULL on success)
194  */
195 static void 
196 connect_cb (void *cls,
197             const struct GNUNET_PeerIdentity *first,
198             const struct GNUNET_PeerIdentity *second,
199             uint32_t distance,
200             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
201             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
202             struct GNUNET_TESTING_Daemon *first_daemon,
203             struct GNUNET_TESTING_Daemon *second_daemon,
204             const char *emsg)
205 {
206   char *second_id;
207
208   second_id = GNUNET_strdup(GNUNET_i2s(second));
209   if (emsg == NULL)
210     {
211       //fprintf(stderr, "Connected %s -> %s\n", GNUNET_i2s(first), second_id);
212       total_connections++;
213     }
214 }
215
216
217
218 static void
219 run (void *cls,
220      char *const *args,
221      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
222 {
223   struct GNUNET_CONFIGURATION_Handle *testing_cfg;
224   unsigned long long total_peers;
225
226   ok = 1;
227   testing_cfg = GNUNET_CONFIGURATION_create();
228   GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load(testing_cfg, cfgfile));
229
230 #if VERBOSE
231   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
232   GNUNET_CONFIGURATION_set_value_string (testing_cfg,
233                                          "testing",
234                                          "use_progressbars",
235                                          "YES");
236 #endif
237   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (testing_cfg, 
238                                                           "testing",
239                                                           "num_peers",
240                                                           &total_peers))
241     total_peers = NUM_PEERS;
242
243   peers_left = total_peers;
244   num_peers = peers_left;
245   pg = GNUNET_TESTING_peergroup_start(testing_cfg,
246                                       peers_left,
247                                       TIMEOUT,
248                                       &connect_cb,
249                                       &my_cb, NULL,
250                                       NULL);
251   GNUNET_assert (pg != NULL);
252   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &shutdown_task, NULL);
253 }
254
255
256 static int
257 check ()
258 {
259   char *const argv[] = { "test-nse-multipeer",
260     "-c",
261     "test_nse.conf",
262 #if VERBOSE
263     "-L", "DEBUG",
264 #endif
265     NULL
266   };
267   struct GNUNET_GETOPT_CommandLineOption options[] = {
268     GNUNET_GETOPT_OPTION_END
269   };
270   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
271                       argv, "test-nse-multipeer", "nohelp",
272                       options, &run, &ok);
273   return ok;
274 }
275
276 int
277 main (int argc, char *argv[])
278 {
279   int ret;
280
281   GNUNET_log_setup ("test-nse-multipeer",
282 #if VERBOSE
283                     "DEBUG",
284 #else
285                     "WARNING",
286 #endif
287                     NULL);
288   ret = check ();
289   GNUNET_DISK_directory_remove ("/tmp/test-nse-multipeer");
290   return ret;
291 }
292
293 /* end of test_nse_multipeer.c */