-verbose is independent of logging
[oweals/gnunet.git] / src / dht / test_dht_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 dht/test_dht_multipeer.c
22  * @brief testcase for testing DHT service with
23  *        multiple peers.
24  */
25 #include "platform.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_dht_service.h"
29
30 /* DEFINES */
31 #define VERBOSE GNUNET_NO
32
33 /* Timeout for entire testcase */
34 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30)
35
36 /* Timeout for waiting for replies to get requests */
37 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 300)
38
39 /* */
40 #define START_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
41
42 /* Timeout for waiting for gets to complete */
43 #define GET_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 50)
44
45 /* Timeout for waiting for puts to complete */
46 #define PUT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 50)
47
48 /* If number of peers not in config file, use this number */
49 #define DEFAULT_NUM_PEERS 10
50
51 #define TEST_DATA_SIZE 8
52
53 #define MAX_OUTSTANDING_PUTS 100
54
55 #define MAX_OUTSTANDING_GETS 100
56
57 #define PATH_TRACKING GNUNET_NO
58
59
60
61 struct TestPutContext
62 {
63   /**
64    * This is a linked list
65    */
66   struct TestPutContext *next;
67
68   /**
69    * This is a linked list
70    */
71   struct TestPutContext *prev;
72
73   /**
74    * Handle to the first peers DHT service (via the API)
75    */
76   struct GNUNET_DHT_Handle *dht_handle;
77
78   /**
79    *  Handle to the PUT peer daemon
80    */
81   struct GNUNET_TESTING_Daemon *daemon;
82
83   /**
84    *  Identifier for this PUT
85    */
86   uint32_t uid;
87
88   /**
89    * Task handle for processing of the put.
90    */
91   GNUNET_SCHEDULER_TaskIdentifier task;
92 };
93
94
95 struct TestGetContext
96 {
97   /**
98    * This is a linked list
99    */
100   struct TestGetContext *next;
101
102   /**
103    * This is a linked list
104    */
105   struct TestGetContext *prev;
106
107   /**
108    * Handle to the first peers DHT service (via the API)
109    */
110   struct GNUNET_DHT_Handle *dht_handle;
111
112   /**
113    * Handle for the DHT get request
114    */
115   struct GNUNET_DHT_GetHandle *get_handle;
116
117   /**
118    *  Handle to the GET peer daemon
119    */
120   struct GNUNET_TESTING_Daemon *daemon;
121
122   /**
123    *  Identifier for this GET
124    */
125   uint32_t uid;
126
127   /**
128    * Task for disconnecting DHT handles (and stopping GET)
129    */
130   GNUNET_SCHEDULER_TaskIdentifier task;
131
132   /**
133    * Whether or not this request has been fulfilled already.
134    */
135   int succeeded;
136 };
137
138
139 /**
140  * List of GETS to perform
141  */
142 static struct TestGetContext *all_gets_head;
143
144 /**
145  * List of GETS to perform
146  */
147 static struct TestGetContext *all_gets_tail;
148
149 /**
150  * List of PUTS to perform
151  */
152 static struct TestPutContext *all_puts_head;
153
154 /**
155  * List of PUTS to perform
156  */
157 static struct TestPutContext *all_puts_tail;
158
159 /**
160  * Handle to the set of all peers run for this test.
161  */
162 static struct GNUNET_TESTING_PeerGroup *pg;
163
164 /**
165  * Total number of peers to run, set based on config file.
166  */
167 static unsigned long long num_peers;
168
169 /**
170  * How many puts do we currently have in flight?
171  */
172 static unsigned long long outstanding_puts;
173
174 /**
175  * How many puts are done?
176  */
177 static unsigned long long puts_completed;
178
179 /**
180  * How many puts do we currently have in flight?
181  */
182 static unsigned long long outstanding_gets;
183
184 /**
185  * How many gets are done?
186  */
187 static unsigned long long gets_completed;
188
189 /**
190  * How many gets failed?
191  */
192 static unsigned long long gets_failed;
193
194 /**
195  * Directory to remove on shutdown.
196  */
197 static char *test_directory;
198
199 /**
200  * Option to use when routing.
201  */
202 static enum GNUNET_DHT_RouteOption route_option;
203
204 /**
205  * Task handle to use to schedule test failure / success.
206  */
207 static GNUNET_SCHEDULER_TaskIdentifier die_task;
208
209 /**
210  * Task handle to use to schedule test shutdown
211  */
212 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
213
214 /**
215  * Global return value (0 for success, anything else for failure)
216  */
217 static int ok;
218
219
220 /**
221  * Check whether peers successfully shut down.
222  */
223 static void
224 shutdown_callback (void *cls, const char *emsg)
225 {
226   if (emsg != NULL)
227   {
228     FPRINTF (stderr, "Failed to shutdown testing topology: %s\n", emsg);
229     if (ok == 0)
230       ok = 2;
231   }
232   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown callback completed.\n");
233 }
234
235 static void
236 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
237 {
238   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) == 0)
239   {
240     if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
241     {
242       GNUNET_SCHEDULER_cancel(shutdown_task);
243       shutdown_task = GNUNET_SCHEDULER_NO_TASK;
244     }
245   }
246   else
247   {
248     shutdown_task = GNUNET_SCHEDULER_NO_TASK ;
249   }
250   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown requested.\n");
251   if (NULL != pg)
252     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
253   pg = NULL;
254 }
255
256
257 /**
258  * Master context for 'stat_run'.
259  */
260 struct StatMaster
261 {
262   struct GNUNET_STATISTICS_Handle *stat;
263   unsigned int daemon;
264   unsigned int value;
265 };
266
267 struct StatValues
268 {
269   const char *subsystem;
270   const char *name;
271   unsigned long long total;
272 };
273
274 /**
275  * Statistics we print out.
276  */
277 static struct StatValues stats[] = {
278   {"core", "# bytes decrypted", 0},
279   {"core", "# bytes encrypted", 0},
280   {"core", "# type maps received", 0},
281   {"core", "# session keys confirmed via PONG", 0},
282   {"core", "# entries in session map", 0},
283   {"core", "# key exchanges initiated", 0},
284   {"core", "# send requests dropped (disconnected)", 0},
285   {"core", "# transmissions delayed due to corking", 0},
286   {"core", "# messages discarded (expired prior to transmission)", 0},
287   {"core", "# messages discarded (disconnected)", 0},
288   {"core", "# discarded CORE_SEND requests", 0},
289   {"core", "# discarded lower priority CORE_SEND requests", 0},
290   {"transport", "# bytes received via TCP", 0},
291   {"transport", "# bytes transmitted via TCP", 0},
292   {"dht", "# PUT messages queued for transmission", 0},
293   {"dht", "# P2P PUT requests received", 0},
294   {"dht", "# GET messages queued for transmission", 0},
295   {"dht", "# P2P GET requests received", 0},
296   {"dht", "# RESULT messages queued for transmission", 0},
297   {"dht", "# P2P RESULTS received", 0},
298   {"dht", "# Queued messages discarded (peer disconnected)", 0},
299   {"dht", "# Peers excluded from routing due to Bloomfilter", 0},
300   {"dht", "# Peer selection failed", 0},
301   {"dht", "# FIND PEER requests ignored due to Bloomfilter", 0},
302   {"dht", "# FIND PEER requests ignored due to lack of HELLO", 0},
303   {"dht", "# P2P FIND PEER requests processed", 0},
304   {"dht", "# P2P GET requests ONLY routed", 0},
305   {"dht", "# Preference updates given to core", 0},
306   {"dht", "# REPLIES ignored for CLIENTS (no match)", 0},
307   {"dht", "# GET requests from clients injected", 0},
308   {"dht", "# GET requests received from clients", 0},
309   {"dht", "# GET STOP requests received from clients", 0},
310   {"dht", "# ITEMS stored in datacache", 0},
311   {"dht", "# Good RESULTS found in datacache", 0},
312   {"dht", "# GET requests given to datacache", 0},
313   {NULL, NULL, 0}
314 };
315
316
317 /**
318  * Callback function to process statistic values.
319  *
320  * @param cls closure
321  * @param subsystem name of subsystem that created the statistic
322  * @param name the name of the datum
323  * @param value the current value
324  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
325  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
326  */
327 static int
328 print_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
329             int is_persistent)
330 {
331   struct StatMaster *sm = cls;
332
333   stats[sm->value].total += value;
334   FPRINTF (stderr, "Peer %2u: %12s/%50s = %12llu\n", sm->daemon, subsystem,
335            name, (unsigned long long) value);
336   return GNUNET_OK;
337 }
338
339
340 /**
341  * Function that gathers stats from all daemons.
342  */
343 static void
344 stat_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
345
346
347 /**
348  * Function called when GET operation on stats is done.
349  */
350 static void
351 get_done (void *cls, int success)
352 {
353   struct StatMaster *sm = cls;
354
355   GNUNET_break (GNUNET_OK == success);
356   sm->value++;
357   GNUNET_SCHEDULER_add_now (&stat_run, sm);
358 }
359
360
361 /**
362  * Function that gathers stats from all daemons.
363  */
364 static void
365 stat_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
366 {
367   struct StatMaster *sm = cls;
368   unsigned int i;
369
370   die_task = GNUNET_SCHEDULER_NO_TASK;
371   if (stats[sm->value].name != NULL)
372   {
373     GNUNET_STATISTICS_get (sm->stat,
374 #if 0
375                            NULL, NULL,
376 #else
377                            stats[sm->value].subsystem, stats[sm->value].name,
378 #endif
379                            GNUNET_TIME_UNIT_FOREVER_REL, &get_done, &print_stat,
380                            sm);
381     return;
382   }
383   GNUNET_STATISTICS_destroy (sm->stat, GNUNET_NO);
384   sm->value = 0;
385   sm->daemon++;
386   if (sm->daemon == num_peers)
387   {
388     GNUNET_free (sm);
389     i = 0;
390     while (stats[i].name != NULL)
391     {
392       FPRINTF (stderr, "Total  : %12s/%50s = %12llu\n", stats[i].subsystem,
393                stats[i].name, (unsigned long long) stats[i].total);
394       i++;
395     }
396     die_task = GNUNET_SCHEDULER_add_now (&do_stop, NULL);
397     return;
398   }
399   sm->stat =
400       GNUNET_STATISTICS_create ("<driver>",
401                                 GNUNET_TESTING_daemon_get (pg,
402                                                            sm->daemon)->cfg);
403   die_task = GNUNET_SCHEDULER_add_now (&stat_run, sm);
404 }
405
406
407 /**
408  * Function scheduled to be run on the successful completion of this
409  * testcase.
410  */
411 static void
412 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
413 {
414   struct TestPutContext *test_put;
415   struct TestGetContext *test_get;
416   struct StatMaster *sm;
417
418   die_task = GNUNET_SCHEDULER_NO_TASK;
419   while (NULL != (test_put = all_puts_head))
420   {
421     if (test_put->task != GNUNET_SCHEDULER_NO_TASK)
422       GNUNET_SCHEDULER_cancel (test_put->task);
423     if (test_put->dht_handle != NULL)
424       GNUNET_DHT_disconnect (test_put->dht_handle);
425     GNUNET_CONTAINER_DLL_remove (all_puts_head, all_puts_tail, test_put);
426     GNUNET_free (test_put);
427   }
428
429   while (NULL != (test_get = all_gets_head))
430   {
431     if (test_get->task != GNUNET_SCHEDULER_NO_TASK)
432       GNUNET_SCHEDULER_cancel (test_get->task);
433     if (test_get->get_handle != NULL)
434       GNUNET_DHT_get_stop (test_get->get_handle);
435     if (test_get->dht_handle != NULL)
436       GNUNET_DHT_disconnect (test_get->dht_handle);
437     GNUNET_CONTAINER_DLL_remove (all_gets_head, all_gets_tail, test_get);
438     GNUNET_free (test_get);
439   }
440   sm = GNUNET_malloc (sizeof (struct StatMaster));
441   sm->stat =
442       GNUNET_STATISTICS_create ("<driver>",
443                                 GNUNET_TESTING_daemon_get (pg,
444                                                            sm->daemon)->cfg);
445   die_task = GNUNET_SCHEDULER_add_now (&stat_run, sm);
446 }
447
448
449 /**
450  * Check if the get_handle is being used, if so stop the request.  Either
451  * way, schedule the end_badly_cont function which actually shuts down the
452  * test.
453  */
454 static void
455 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
456 {
457   const char *emsg = cls;
458   struct TestPutContext *test_put;
459   struct TestGetContext *test_get;
460
461   die_task = GNUNET_SCHEDULER_NO_TASK;
462   FPRINTF (stderr, "Failing test with error: `%s'!\n", emsg);
463   while (NULL != (test_put = all_puts_head))
464   {
465     if (test_put->task != GNUNET_SCHEDULER_NO_TASK)
466       GNUNET_SCHEDULER_cancel (test_put->task);
467     if (test_put->dht_handle != NULL)
468       GNUNET_DHT_disconnect (test_put->dht_handle);
469     GNUNET_CONTAINER_DLL_remove (all_puts_head, all_puts_tail, test_put);
470     GNUNET_free (test_put);
471   }
472
473   while (NULL != (test_get = all_gets_head))
474   {
475     if (test_get->task != GNUNET_SCHEDULER_NO_TASK)
476       GNUNET_SCHEDULER_cancel (test_get->task);
477     if (test_get->get_handle != NULL)
478       GNUNET_DHT_get_stop (test_get->get_handle);
479     if (test_get->dht_handle != NULL)
480       GNUNET_DHT_disconnect (test_get->dht_handle);
481     GNUNET_CONTAINER_DLL_remove (all_gets_head, all_gets_tail, test_get);
482     GNUNET_free (test_get);
483   }
484   ok = 1;
485   /* testing_peergroup will do that in its own end_badly() handler */
486   /*GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL); */
487   pg = NULL;
488 }
489
490
491 /**
492  * Task to release get handle.
493  */
494 static void
495 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
496 {
497   struct TestGetContext *test_get = cls;
498   GNUNET_HashCode search_key;   /* Key stored under */
499   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
500
501   test_get->task = GNUNET_SCHEDULER_NO_TASK;
502   memset (original_data, test_get->uid, sizeof (original_data));
503   GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
504   if (test_get->succeeded != GNUNET_YES)
505   {
506     gets_failed++;
507     FPRINTF (stderr, "Get from peer %s for key %s failed!\n",
508              GNUNET_i2s (&test_get->daemon->id), GNUNET_h2s (&search_key));
509   }
510   GNUNET_assert (test_get->get_handle != NULL);
511   GNUNET_DHT_get_stop (test_get->get_handle);
512   test_get->get_handle = NULL;
513
514   outstanding_gets--;           /* GET is really finished */
515   GNUNET_DHT_disconnect (test_get->dht_handle);
516   test_get->dht_handle = NULL;
517
518   GNUNET_CONTAINER_DLL_remove (all_gets_head, all_gets_tail, test_get);
519   GNUNET_free (test_get);
520   if ((gets_failed > 10) && (outstanding_gets == 0))
521   {
522     /* Had more than 10% failures */
523     FPRINTF (stderr, "%llu gets succeeded, %llu gets failed!\n", gets_completed,
524              gets_failed);
525     GNUNET_SCHEDULER_cancel (die_task);
526     ok = 1;
527     die_task =
528         GNUNET_SCHEDULER_add_now (&finish_testing, "not all gets succeeded");
529     return;
530   }
531   if ((gets_completed + gets_failed == num_peers * num_peers) && (outstanding_gets == 0))       /* All gets successful */
532   {
533     FPRINTF (stderr, "%llu gets succeeded, %llu gets failed!\n", gets_completed,
534              gets_failed);
535     GNUNET_SCHEDULER_cancel (die_task);
536     ok = 0;
537     die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
538   }
539 }
540
541
542 /**
543  * Iterator called if the GET request initiated returns a response.
544  *
545  * @param cls closure
546  * @param exp when will this value expire
547  * @param key key of the result
548  * @param type type of the result
549  * @param size number of bytes in data
550  * @param data pointer to the result data
551  */
552 static void
553 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
554                      const GNUNET_HashCode * key,
555                      const struct GNUNET_PeerIdentity *get_path,
556                      unsigned int get_path_length,
557                      const struct GNUNET_PeerIdentity *put_path,
558                      unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
559                      size_t size, const void *data)
560 {
561   struct TestGetContext *test_get = cls;
562   GNUNET_HashCode search_key;   /* Key stored under */
563   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
564
565   memset (original_data, test_get->uid, sizeof (original_data));
566   GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
567   if (test_get->succeeded == GNUNET_YES)
568     return;                     /* Get has already been successful, probably ending now */
569
570 #if PATH_TRACKING
571   if (put_path != NULL)
572   {
573     unsigned int i;
574
575     FPRINTF (stderr, "PUT (%u) Path: ", test_get->uid);
576     for (i = 0; i < put_path_length; i++)
577       FPRINTF (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&put_path[i]));
578     FPRINTF (stderr, "%s",  "\n");
579   }
580   if (get_path != NULL)
581   {
582     unsigned int i;
583
584     FPRINTF (stderr, "GET (%u) Path: ", test_get->uid);
585     for (i = 0; i < get_path_length; i++)
586       FPRINTF (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&get_path[i]));
587     FPRINTF (stderr, "%s%s\n", get_path_length > 0 ? "->" : "",
588              GNUNET_i2s (&test_get->daemon->id));
589   }
590 #endif
591
592   if ((0 != memcmp (&search_key, key, sizeof (GNUNET_HashCode))) ||
593       (0 != memcmp (original_data, data, sizeof (original_data))))
594   {
595     FPRINTF (stderr, "%s",  "Key or data is not the same as was inserted!\n");
596     return;
597   }
598   gets_completed++;
599   test_get->succeeded = GNUNET_YES;
600   GNUNET_SCHEDULER_cancel (test_get->task);
601   test_get->task = GNUNET_SCHEDULER_add_now (&get_stop_task, test_get);
602 }
603
604
605 /**
606  * Set up some data, and call API PUT function
607  */
608 static void
609 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
610 {
611   struct TestGetContext *test_get = cls;
612   GNUNET_HashCode key;          /* Made up key to store data under */
613   char data[TEST_DATA_SIZE];    /* Made up data to store */
614
615   if (outstanding_gets > MAX_OUTSTANDING_GETS)
616   {
617     test_get->task =
618         GNUNET_SCHEDULER_add_delayed (GET_DELAY, &do_get, test_get);
619     return;
620   }
621   memset (data, test_get->uid, sizeof (data));
622   GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
623   test_get->dht_handle = GNUNET_DHT_connect (test_get->daemon->cfg, 10);
624   GNUNET_assert (test_get->dht_handle != NULL);
625   outstanding_gets++;
626   test_get->get_handle =
627       GNUNET_DHT_get_start (test_get->dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
628                             GNUNET_BLOCK_TYPE_TEST, &key, 1, route_option, NULL,
629                             0, &get_result_iterator, test_get);
630   test_get->task =
631       GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &get_stop_task, test_get);
632 }
633
634
635 /**
636  * Task to release DHT handles for PUT
637  */
638 static void
639 put_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
640 {
641   struct TestPutContext *test_put = cls;
642
643   test_put->task = GNUNET_SCHEDULER_NO_TASK;
644   GNUNET_DHT_disconnect (test_put->dht_handle);
645   test_put->dht_handle = NULL;
646   GNUNET_CONTAINER_DLL_remove (all_puts_head, all_puts_tail, test_put);
647   GNUNET_free (test_put);
648 }
649
650
651 /**
652  * Schedule the GET requests
653  */
654 static void
655 start_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
656 {
657   unsigned long long i;
658   unsigned long long j;
659   struct TestGetContext *test_get;
660
661 #if VERBOSE
662   FPRINTF (stderr, "Issuing %llu GETs\n",
663            (unsigned long long) (num_peers * num_peers));
664 #endif
665   for (i = 0; i < num_peers; i++)
666     for (j = 0; j < num_peers; j++)
667     {
668       test_get = GNUNET_malloc (sizeof (struct TestGetContext));
669       test_get->uid = i + j * num_peers;
670       test_get->daemon = GNUNET_TESTING_daemon_get (pg, j);
671       GNUNET_CONTAINER_DLL_insert (all_gets_head, all_gets_tail, test_get);
672       test_get->task = GNUNET_SCHEDULER_add_now (&do_get, test_get);
673     }
674 }
675
676
677 /**
678  * Called when the PUT request has been transmitted to the DHT service.
679  */
680 static void
681 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
682 {
683   struct TestPutContext *test_put = cls;
684
685   outstanding_puts--;
686   puts_completed++;
687   if (GNUNET_SCHEDULER_NO_TASK != test_put->task)
688   {
689     GNUNET_SCHEDULER_cancel (test_put->task);
690   }
691   test_put->task = GNUNET_SCHEDULER_add_now (&put_disconnect_task, test_put);
692   if (puts_completed != num_peers * num_peers)
693     return;
694
695   GNUNET_assert (outstanding_puts == 0);
696   GNUNET_SCHEDULER_add_delayed (START_DELAY, &start_gets, NULL);
697 }
698
699
700 /**
701  * Set up some data, and call API PUT function
702  */
703 static void
704 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
705 {
706   struct TestPutContext *test_put = cls;
707   GNUNET_HashCode key;          /* Made up key to store data under */
708   char data[TEST_DATA_SIZE];    /* Made up data to store */
709
710   test_put->task = GNUNET_SCHEDULER_NO_TASK;
711   if (outstanding_puts > MAX_OUTSTANDING_PUTS)
712   {
713     test_put->task =
714         GNUNET_SCHEDULER_add_delayed (PUT_DELAY, &do_put, test_put);
715     return;
716   }
717   memset (data, test_put->uid, sizeof (data));
718   GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
719   test_put->dht_handle = GNUNET_DHT_connect (test_put->daemon->cfg, 10);
720   GNUNET_assert (test_put->dht_handle != NULL);
721   outstanding_puts++;
722 #if VERBOSE > 2
723   FPRINTF (stderr, "PUT %u at `%s'\n", test_put->uid,
724            GNUNET_i2s (&test_put->daemon->id));
725 #endif
726   GNUNET_DHT_put (test_put->dht_handle, &key, 1, route_option,
727                   GNUNET_BLOCK_TYPE_TEST, sizeof (data), data,
728                   GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
729                   &put_finished, test_put);
730   test_put->task =
731       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
732                                     &put_disconnect_task, test_put);
733 }
734
735
736 static void
737 run_dht_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
738 {
739   unsigned long long i;
740   struct TestPutContext *test_put;
741
742   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
743   {
744     ok = 1;
745     return;
746   }
747 #if PATH_TRACKING
748   route_option =
749       GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
750 #else
751   route_option = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
752 #endif
753   die_task =
754       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
755                                     "from setup puts/gets");
756   FPRINTF (stderr, "Issuing %llu PUTs (one per peer)\n",
757            (unsigned long long) (num_peers * num_peers));
758   for (i = 0; i < num_peers * num_peers; i++)
759   {
760     test_put = GNUNET_malloc (sizeof (struct TestPutContext));
761     test_put->uid = i;
762     test_put->daemon = GNUNET_TESTING_daemon_get (pg, i % num_peers);
763     test_put->task = GNUNET_SCHEDULER_add_now (&do_put, test_put);
764     GNUNET_CONTAINER_DLL_insert (all_puts_head, all_puts_tail, test_put);
765   }
766 }
767
768
769 /**
770  * This function is called once testing has finished setting up the topology.
771  *
772  * @param cls unused
773  * @param emsg variable is NULL on success (peers connected), and non-NULL on
774  * failure (peers failed to connect).
775  */
776 static void
777 startup_done (void *cls, const char *emsg)
778 {
779   if (emsg != NULL)
780   {
781     FPRINTF (stderr, "Failed to setup topology: %s\n", emsg);
782     die_task = GNUNET_SCHEDULER_add_now (&end_badly, "topology setup failed");
783     return;
784   }
785   die_task =
786       GNUNET_SCHEDULER_add_delayed (START_DELAY, &run_dht_test,
787                                     "from setup puts/gets");
788 }
789
790
791 static void
792 run (void *cls, char *const *args, const char *cfgfile,
793      const struct GNUNET_CONFIGURATION_Handle *cfg)
794 {
795   /* Get path from configuration file */
796   if (GNUNET_YES !=
797       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
798                                              &test_directory))
799   {
800     GNUNET_break (0);
801     ok = 404;
802     return;
803   }
804   if (GNUNET_SYSERR ==
805       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
806                                              &num_peers))
807     num_peers = DEFAULT_NUM_PEERS;
808   pg = GNUNET_TESTING_peergroup_start (cfg, num_peers, TIMEOUT, NULL,
809                                        &startup_done, NULL, NULL);
810   GNUNET_assert (NULL != pg);
811   shutdown_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL,
812                                                &do_stop, NULL);
813 }
814
815
816 static int
817 check ()
818 {
819   int ret;
820
821   /* Arguments for GNUNET_PROGRAM_run */
822   char *const argv[] = { "test-dht-multipeer",  /* Name to give running binary */
823     "-c",
824     "test_dht_multipeer_data.conf",     /* Config file to use */
825 #if VERBOSE
826     "-L", "DEBUG",
827 #endif
828     NULL
829   };
830   struct GNUNET_GETOPT_CommandLineOption options[] = {
831     GNUNET_GETOPT_OPTION_END
832   };
833   /* Run the run function as a new program */
834   ret =
835       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
836                           "test-dht-multipeer", "nohelp", options, &run, &ok);
837   if (ret != GNUNET_OK)
838   {
839     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
840                 "`test-dht-multipeer': Failed with error code %d\n", ret);
841   }
842   return ok;
843 }
844
845
846 int
847 main (int argc, char *argv[])
848 {
849   int ret;
850
851
852   GNUNET_log_setup ("test-dht-multipeer",
853 #if VERBOSE
854                     "DEBUG",
855 #else
856                     "WARNING",
857 #endif
858                     NULL);
859   ret = check ();
860   /**
861    * Need to remove base directory, subdirectories taken care
862    * of by the testing framework.
863    */
864   if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
865   {
866     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
867                 "Failed to remove testing directory %s\n", test_directory);
868   }
869   return ret;
870 }
871
872 /* end of test_dht_multipeer.c */