Adding a function pick_random_friend ()
[oweals/gnunet.git] / src / dht / test_dht_monitor.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011, 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 dht/test_dht_monitor.c
22  * @brief Test for the dht monitoring API; checks that we receive "some" monitor events
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_testbed_service.h"
27 #include "gnunet_dht_service.h"
28 #include "dht_test_lib.h"
29
30
31 /**
32  * How long do we run the test at most?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
35
36 /**
37  * How often do we run the PUTs?
38  */
39 #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
40
41
42 /**
43  * Information we keep for each GET operation.
44  */
45 struct GetOperation
46 {
47   /**
48    * DLL.
49    */
50   struct GetOperation *next;
51
52   /**
53    * DLL.
54    */
55   struct GetOperation *prev;
56
57   /**
58    * Handle for the operation.
59    */
60   struct GNUNET_DHT_GetHandle *get;
61
62 };
63
64
65 /**
66  * Return value from 'main'.
67  */
68 static int ok;
69
70 /**
71  * Head of list of active GET operations.
72  */
73 static struct GetOperation *get_head;
74
75 /**
76  * Tail of list of active GET operations.
77  */
78 static struct GetOperation *get_tail;
79
80 /**
81  * Array of the testbed's peers.
82  */
83 static struct GNUNET_TESTBED_Peer **my_peers;
84
85 /**
86  * Number of peers to run.
87  */
88 static unsigned int NUM_PEERS = 3;
89
90 /**
91  * Task called to disconnect peers.
92  */
93 static struct GNUNET_SCHEDULER_Task * timeout_task;
94
95 /**
96  * Task to do DHT_puts
97  */
98 static struct GNUNET_SCHEDULER_Task * put_task;
99
100 static struct GNUNET_DHT_MonitorHandle **monitors;
101
102 static unsigned int monitor_counter;
103
104
105 /**
106  * Task run on success or timeout to clean up.
107  * Terminates active get operations and shuts down
108  * the testbed.
109  *
110  * @param cls the 'struct GNUNET_DHT_TestContext'
111  * @param tc scheduler context
112  */
113 static void
114 shutdown_task (void *cls,
115                const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   struct GNUNET_DHT_TEST_Context *ctx = cls;
118   unsigned int i;
119   struct GetOperation *get_op;
120
121   ok = (monitor_counter > NUM_PEERS) ? 0 : 2;
122   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
123               "Received %u monitor events\n",
124               monitor_counter);
125   while (NULL != (get_op = get_tail))
126   {
127     GNUNET_DHT_get_stop (get_op->get);
128     GNUNET_CONTAINER_DLL_remove (get_head,
129                                  get_tail,
130                                  get_op);
131     GNUNET_free (get_op);
132   }
133   for (i=0;i<NUM_PEERS;i++)
134     GNUNET_DHT_monitor_stop (monitors[i]);
135   GNUNET_free (monitors);
136   GNUNET_SCHEDULER_cancel (put_task);
137   GNUNET_DHT_TEST_cleanup (ctx);
138 }
139
140
141 /**
142  * Iterator called on each result obtained for a DHT
143  * operation that expects a reply
144  *
145  * @param cls closure with our 'struct GetOperation'
146  * @param exp when will this value expire
147  * @param key key of the result
148  * @param get_path peers on reply path (or NULL if not recorded)
149  * @param get_path_length number of entries in get_path
150  * @param put_path peers on the PUT path (or NULL if not recorded)
151  * @param put_path_length number of entries in get_path
152  * @param type type of the result
153  * @param size number of bytes in data
154  * @param data pointer to the result data
155  */
156 static void
157 dht_get_handler (void *cls, struct GNUNET_TIME_Absolute exp,
158                  const struct GNUNET_HashCode * key,
159                  const struct GNUNET_PeerIdentity *get_path,
160                  unsigned int get_path_length,
161                  const struct GNUNET_PeerIdentity *put_path,
162                  unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
163                  size_t size, const void *data)
164 {
165   struct GetOperation *get_op = cls;
166   struct GNUNET_HashCode want;
167   struct GNUNET_DHT_TestContext *ctx;
168
169   if (sizeof (struct GNUNET_HashCode) != size)
170   {
171     GNUNET_break (0);
172     return;
173   }
174   GNUNET_CRYPTO_hash (key, sizeof (*key), &want);
175   if (0 != memcmp (&want, data, sizeof (want)))
176   {
177     GNUNET_break (0);
178     return;
179   }
180   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181               "Get successful\n");
182   GNUNET_DHT_get_stop (get_op->get);
183   GNUNET_CONTAINER_DLL_remove (get_head,
184                                get_tail,
185                                get_op);
186   GNUNET_free (get_op);
187   if (NULL != get_head)
188     return;
189   /* all DHT GET operations successful; terminate! */
190   ok = 0;
191   ctx = GNUNET_SCHEDULER_cancel (timeout_task);
192   timeout_task = GNUNET_SCHEDULER_add_now (&shutdown_task, ctx);
193 }
194
195
196 /**
197  * Task to put the id of each peer into the DHT.
198  *
199  * @param cls array with NUM_PEERS DHT handles
200  * @param tc Task context
201  */
202 static void
203 do_puts (void *cls,
204          const struct GNUNET_SCHEDULER_TaskContext *tc)
205 {
206   struct GNUNET_DHT_Handle **hs = cls;
207   struct GNUNET_HashCode key;
208   struct GNUNET_HashCode value;
209   unsigned int i;
210
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212               "Putting values into DHT\n");
213   for (i = 0; i < NUM_PEERS; i++)
214   {
215     GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
216     GNUNET_CRYPTO_hash (&key, sizeof (key), &value);
217     GNUNET_DHT_put (hs[i], &key, 10U,
218                     GNUNET_DHT_RO_RECORD_ROUTE |
219                     GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
220                     GNUNET_BLOCK_TYPE_TEST,
221                     sizeof (value), &value,
222                     GNUNET_TIME_UNIT_FOREVER_ABS,
223                     GNUNET_TIME_UNIT_FOREVER_REL,
224                     NULL, NULL);
225   }
226   put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
227                                            &do_puts, hs);
228 }
229
230
231 /**
232  * Callback called on each GET request going through the DHT.
233  * Prints the info about the intercepted packet and increments a counter.
234  *
235  * @param cls Closure.
236  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
237  * @param type The type of data in the request.
238  * @param hop_count Hop count so far.
239  * @param path_length number of entries in path (or 0 if not recorded).
240  * @param path peers on the GET path (or NULL if not recorded).
241  * @param desired_replication_level Desired replication level.
242  * @param key Key of the requested data.
243  */
244 static void
245 monitor_get_cb (void *cls,
246                 enum GNUNET_DHT_RouteOption options,
247                 enum GNUNET_BLOCK_Type type,
248                 uint32_t hop_count,
249                 uint32_t desired_replication_level,
250                 unsigned int path_length,
251                 const struct GNUNET_PeerIdentity *path,
252                 const struct GNUNET_HashCode * key)
253 {
254   unsigned int i;
255
256   i = (unsigned int) (long) cls;
257   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
258               "%u got a GET message for key %s\n",
259               i,
260               GNUNET_h2s (key));
261   monitor_counter++;
262 }
263
264
265 /**
266  * Callback called on each PUT request going through the DHT.
267  * Prints the info about the intercepted packet and increments a counter.
268  *
269  * @param cls Closure.
270  * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
271  * @param type The type of data in the request.
272  * @param hop_count Hop count so far.
273  * @param path_length number of entries in path (or 0 if not recorded).
274  * @param path peers on the PUT path (or NULL if not recorded).
275  * @param desired_replication_level Desired replication level.
276  * @param exp Expiration time of the data.
277  * @param key Key under which data is to be stored.
278  * @param data Pointer to the data carried.
279  * @param size Number of bytes in data.
280  */
281 static void
282 monitor_put_cb (void *cls,
283                 enum GNUNET_DHT_RouteOption options,
284                 enum GNUNET_BLOCK_Type type,
285                 uint32_t hop_count,
286                 uint32_t desired_replication_level,
287                 unsigned int path_length,
288                 const struct GNUNET_PeerIdentity *path,
289                 struct GNUNET_TIME_Absolute exp,
290                 const struct GNUNET_HashCode * key,
291                 const void *data,
292                 size_t size)
293 {
294   unsigned int i;
295
296   i = (unsigned int) (long) cls;
297   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
298               "%u got a PUT message for key %s with %u bytes\n",
299               i,
300               GNUNET_h2s (key), size);
301   monitor_counter++;
302 }
303
304
305 /**
306  * Callback called on each GET reply going through the DHT.
307  * Prints the info about the intercepted packet and increments a counter.
308  *
309  * @param cls Closure.
310  * @param type The type of data in the result.
311  * @param get_path Peers on GET path (or NULL if not recorded).
312  * @param get_path_length number of entries in get_path.
313  * @param put_path peers on the PUT path (or NULL if not recorded).
314  * @param put_path_length number of entries in get_path.
315  * @param exp Expiration time of the data.
316  * @param key Key of the data.
317  * @param data Pointer to the result data.
318  * @param size Number of bytes in data.
319  */
320 static void
321 monitor_res_cb (void *cls,
322                 enum GNUNET_BLOCK_Type type,
323                 const struct GNUNET_PeerIdentity *get_path,
324                 unsigned int get_path_length,
325                 const struct GNUNET_PeerIdentity *put_path,
326                 unsigned int put_path_length,
327                 struct GNUNET_TIME_Absolute exp,
328                 const struct GNUNET_HashCode * key,
329                 const void *data,
330                 size_t size)
331 {
332   unsigned int i;
333
334   i = (unsigned int) (long) cls;
335   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
336               "%u got a REPLY message for key %s with %u bytes\n",
337               i,
338               GNUNET_h2s (key), size);
339   monitor_counter++;
340 }
341
342
343 /**
344  * Main function of the test.
345  *
346  * @param cls closure (NULL)
347  * @param ctx argument to give to GNUNET_DHT_TEST_cleanup on test end
348  * @param num_peers number of peers that are running
349  * @param peers array of peers
350  * @param dhts handle to each of the DHTs of the peers
351  */
352 static void
353 run (void *cls,
354      struct GNUNET_DHT_TEST_Context *ctx,
355      unsigned int num_peers,
356      struct GNUNET_TESTBED_Peer **peers,
357      struct GNUNET_DHT_Handle **dhts)
358 {
359   unsigned int i;
360   unsigned int j;
361   struct GNUNET_HashCode key;
362   struct GetOperation *get_op;
363
364   GNUNET_assert (NUM_PEERS == num_peers);
365   my_peers = peers;
366   monitors = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_MonitorHandle *));
367   for (i = 0; i < num_peers; i++)
368     monitors[i] = GNUNET_DHT_monitor_start (dhts[i],
369                                             GNUNET_BLOCK_TYPE_ANY,
370                                             NULL,
371                                             &monitor_get_cb,
372                                             &monitor_res_cb,
373                                             &monitor_put_cb,
374                                             (void *)(long)i);
375   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376               "Peers setup, starting test\n");
377   put_task = GNUNET_SCHEDULER_add_now (&do_puts, dhts);
378   for (i=0;i<num_peers;i++)
379   {
380     GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
381     for (j=0;j<num_peers;j++)
382     {
383       get_op = GNUNET_new (struct GetOperation);
384       GNUNET_CONTAINER_DLL_insert (get_head,
385                                    get_tail,
386                                    get_op);
387       get_op->get = GNUNET_DHT_get_start (dhts[j],
388                                           GNUNET_BLOCK_TYPE_TEST, /* type */
389                                           &key,      /*key to search */
390                                           4U,     /* replication level */
391                                           GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
392                                           NULL,        /* xquery */
393                                           0,      /* xquery bits */
394                                           &dht_get_handler, get_op);
395     }
396   }
397   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
398                                                &shutdown_task, ctx);
399 }
400
401
402 /**
403  * Main: start test
404  */
405 int
406 main (int xargc, char *xargv[])
407 {
408   GNUNET_DHT_TEST_run ("test-dht-monitor",
409                        "test_dht_monitor.conf",
410                        NUM_PEERS,
411                        &run, NULL);
412   return ok;
413 }
414
415
416 /* end of test_dht_monitor.c */