LRN: Do not stop daemons twice / #1919
[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_EXTRA_LOGGING
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  * Global return value (0 for success, anything else for failure)
211  */
212 static int ok;
213
214
215 /**
216  * Check whether peers successfully shut down.
217  */
218 static void
219 shutdown_callback (void *cls, const char *emsg)
220 {
221   if (emsg != NULL)
222   {
223     fprintf (stderr, "Failed to shutdown testing topology: %s\n", emsg);
224     if (ok == 0)
225       ok = 2;
226   }
227 }
228
229 static void
230 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
231 {
232   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
233   pg = NULL;
234 }
235
236
237 /**
238  * Master context for 'stat_run'.
239  */
240 struct StatMaster
241 {
242   struct GNUNET_STATISTICS_Handle *stat;
243   unsigned int daemon;
244   unsigned int value;
245 };
246
247 struct StatValues
248 {
249   const char *subsystem;
250   const char *name;
251   unsigned long long total;
252 };
253
254 /**
255  * Statistics we print out.
256  */
257 static struct StatValues stats[] = {
258   {"core", "# bytes decrypted", 0},
259   {"core", "# bytes encrypted", 0},
260   {"core", "# type maps received", 0},
261   {"core", "# session keys confirmed via PONG", 0},
262   {"core", "# entries in session map", 0},
263   {"core", "# key exchanges initiated", 0},
264   {"core", "# send requests dropped (disconnected)", 0},
265   {"core", "# transmissions delayed due to corking", 0},
266   {"core", "# messages discarded (expired prior to transmission)", 0},
267   {"core", "# messages discarded (disconnected)", 0},
268   {"core", "# discarded CORE_SEND requests", 0},
269   {"core", "# discarded lower priority CORE_SEND requests", 0},
270   {"transport", "# bytes received via TCP", 0},
271   {"transport", "# bytes transmitted via TCP", 0},
272   {"dht", "# PUT messages queued for transmission", 0},
273   {"dht", "# P2P PUT requests received", 0},
274   {"dht", "# GET messages queued for transmission", 0},
275   {"dht", "# P2P GET requests received", 0},
276   {"dht", "# RESULT messages queued for transmission", 0},
277   {"dht", "# P2P RESULTS received", 0},
278   {"dht", "# Queued messages discarded (peer disconnected)", 0},
279   {"dht", "# Peers excluded from routing due to Bloomfilter", 0},
280   {"dht", "# Peer selection failed", 0},
281   {"dht", "# FIND PEER requests ignored due to Bloomfilter", 0},
282   {"dht", "# FIND PEER requests ignored due to lack of HELLO", 0},
283   {"dht", "# P2P FIND PEER requests processed", 0},
284   {"dht", "# P2P GET requests ONLY routed", 0},
285   {"dht", "# Preference updates given to core", 0},
286   {"dht", "# REPLIES ignored for CLIENTS (no match)", 0},
287   {"dht", "# GET requests from clients injected", 0},
288   {"dht", "# GET requests received from clients", 0},
289   {"dht", "# GET STOP requests received from clients", 0},
290   {"dht", "# ITEMS stored in datacache", 0},
291   {"dht", "# Good RESULTS found in datacache", 0},
292   {"dht", "# GET requests given to datacache", 0},
293   {NULL, NULL, 0}
294 };
295
296
297 /**
298  * Callback function to process statistic values.
299  *
300  * @param cls closure
301  * @param subsystem name of subsystem that created the statistic
302  * @param name the name of the datum
303  * @param value the current value
304  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
305  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
306  */
307 static int
308 print_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
309             int is_persistent)
310 {
311   struct StatMaster *sm = cls;
312
313   stats[sm->value].total += value;
314   fprintf (stderr, "Peer %2u: %12s/%50s = %12llu\n", sm->daemon, subsystem,
315            name, (unsigned long long) value);
316   return GNUNET_OK;
317 }
318
319
320 /**
321  * Function that gathers stats from all daemons.
322  */
323 static void
324 stat_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
325
326
327 /**
328  * Function called when GET operation on stats is done.
329  */
330 static void
331 get_done (void *cls, int success)
332 {
333   struct StatMaster *sm = cls;
334
335   GNUNET_break (GNUNET_OK == success);
336   sm->value++;
337   GNUNET_SCHEDULER_add_now (&stat_run, sm);
338 }
339
340
341 /**
342  * Function that gathers stats from all daemons.
343  */
344 static void
345 stat_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
346 {
347   struct StatMaster *sm = cls;
348   unsigned int i;
349
350   die_task = GNUNET_SCHEDULER_NO_TASK;
351   if (stats[sm->value].name != NULL)
352   {
353     GNUNET_STATISTICS_get (sm->stat,
354 #if 0
355                            NULL, NULL,
356 #else
357                            stats[sm->value].subsystem, stats[sm->value].name,
358 #endif
359                            GNUNET_TIME_UNIT_FOREVER_REL, &get_done, &print_stat,
360                            sm);
361     return;
362   }
363   GNUNET_STATISTICS_destroy (sm->stat, GNUNET_NO);
364   sm->value = 0;
365   sm->daemon++;
366   if (sm->daemon == num_peers)
367   {
368     GNUNET_free (sm);
369     i = 0;
370     while (stats[i].name != NULL)
371     {
372       fprintf (stderr, "Total  : %12s/%50s = %12llu\n", stats[i].subsystem,
373                stats[i].name, (unsigned long long) stats[i].total);
374       i++;
375     }
376     die_task = GNUNET_SCHEDULER_add_now (&do_stop, NULL);
377     return;
378   }
379   sm->stat =
380       GNUNET_STATISTICS_create ("<driver>",
381                                 GNUNET_TESTING_daemon_get (pg,
382                                                            sm->daemon)->cfg);
383   die_task = GNUNET_SCHEDULER_add_now (&stat_run, sm);
384 }
385
386
387 /**
388  * Function scheduled to be run on the successful completion of this
389  * testcase.
390  */
391 static void
392 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
393 {
394   struct TestPutContext *test_put;
395   struct TestGetContext *test_get;
396   struct StatMaster *sm;
397
398   die_task = GNUNET_SCHEDULER_NO_TASK;
399   while (NULL != (test_put = all_puts_head))
400   {
401     if (test_put->task != GNUNET_SCHEDULER_NO_TASK)
402       GNUNET_SCHEDULER_cancel (test_put->task);
403     if (test_put->dht_handle != NULL)
404       GNUNET_DHT_disconnect (test_put->dht_handle);
405     GNUNET_CONTAINER_DLL_remove (all_puts_head, all_puts_tail, test_put);
406     GNUNET_free (test_put);
407   }
408
409   while (NULL != (test_get = all_gets_head))
410   {
411     if (test_get->task != GNUNET_SCHEDULER_NO_TASK)
412       GNUNET_SCHEDULER_cancel (test_get->task);
413     if (test_get->get_handle != NULL)
414       GNUNET_DHT_get_stop (test_get->get_handle);
415     if (test_get->dht_handle != NULL)
416       GNUNET_DHT_disconnect (test_get->dht_handle);
417     GNUNET_CONTAINER_DLL_remove (all_gets_head, all_gets_tail, test_get);
418     GNUNET_free (test_get);
419   }
420   sm = GNUNET_malloc (sizeof (struct StatMaster));
421   sm->stat =
422       GNUNET_STATISTICS_create ("<driver>",
423                                 GNUNET_TESTING_daemon_get (pg,
424                                                            sm->daemon)->cfg);
425   die_task = GNUNET_SCHEDULER_add_now (&stat_run, sm);
426 }
427
428
429 /**
430  * Check if the get_handle is being used, if so stop the request.  Either
431  * way, schedule the end_badly_cont function which actually shuts down the
432  * test.
433  */
434 static void
435 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
436 {
437   const char *emsg = cls;
438   struct TestPutContext *test_put;
439   struct TestGetContext *test_get;
440
441   die_task = GNUNET_SCHEDULER_NO_TASK;
442   fprintf (stderr, "Failing test with error: `%s'!\n", emsg);
443   while (NULL != (test_put = all_puts_head))
444   {
445     if (test_put->task != GNUNET_SCHEDULER_NO_TASK)
446       GNUNET_SCHEDULER_cancel (test_put->task);
447     if (test_put->dht_handle != NULL)
448       GNUNET_DHT_disconnect (test_put->dht_handle);
449     GNUNET_CONTAINER_DLL_remove (all_puts_head, all_puts_tail, test_put);
450     GNUNET_free (test_put);
451   }
452
453   while (NULL != (test_get = all_gets_head))
454   {
455     if (test_get->task != GNUNET_SCHEDULER_NO_TASK)
456       GNUNET_SCHEDULER_cancel (test_get->task);
457     if (test_get->get_handle != NULL)
458       GNUNET_DHT_get_stop (test_get->get_handle);
459     if (test_get->dht_handle != NULL)
460       GNUNET_DHT_disconnect (test_get->dht_handle);
461     GNUNET_CONTAINER_DLL_remove (all_gets_head, all_gets_tail, test_get);
462     GNUNET_free (test_get);
463   }
464   ok = 1;
465   /* testing_peergroup will do that in its own end_badly() handler */
466   /*GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);*/
467   pg = NULL;
468 }
469
470
471 /**
472  * Task to release get handle.
473  */
474 static void
475 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
476 {
477   struct TestGetContext *test_get = cls;
478   GNUNET_HashCode search_key;   /* Key stored under */
479   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
480
481   test_get->task = GNUNET_SCHEDULER_NO_TASK;
482   memset (original_data, test_get->uid, sizeof (original_data));
483   GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
484   if (test_get->succeeded != GNUNET_YES)
485   {
486     gets_failed++;
487     fprintf (stderr, "Get from peer %s for key %s failed!\n",
488              GNUNET_i2s (&test_get->daemon->id), GNUNET_h2s (&search_key));
489   }
490   GNUNET_assert (test_get->get_handle != NULL);
491   GNUNET_DHT_get_stop (test_get->get_handle);
492   test_get->get_handle = NULL;
493
494   outstanding_gets--;           /* GET is really finished */
495   GNUNET_DHT_disconnect (test_get->dht_handle);
496   test_get->dht_handle = NULL;
497
498   GNUNET_CONTAINER_DLL_remove (all_gets_head, all_gets_tail, test_get);
499   GNUNET_free (test_get);
500   if ((gets_failed > 10) && (outstanding_gets == 0))
501   {
502     /* Had more than 10% failures */
503     fprintf (stderr, "%llu gets succeeded, %llu gets failed!\n", gets_completed,
504              gets_failed);
505     GNUNET_SCHEDULER_cancel (die_task);
506     ok = 1;
507     die_task =
508         GNUNET_SCHEDULER_add_now (&finish_testing, "not all gets succeeded");
509     return;
510   }
511   if ((gets_completed + gets_failed == num_peers * num_peers) && (outstanding_gets == 0))       /* All gets successful */
512   {
513     fprintf (stderr, "%llu gets succeeded, %llu gets failed!\n", gets_completed,
514              gets_failed);
515     GNUNET_SCHEDULER_cancel (die_task);
516     ok = 0;
517     die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
518   }
519 }
520
521
522 /**
523  * Iterator called if the GET request initiated returns a response.
524  *
525  * @param cls closure
526  * @param exp when will this value expire
527  * @param key key of the result
528  * @param type type of the result
529  * @param size number of bytes in data
530  * @param data pointer to the result data
531  */
532 static void
533 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
534                      const GNUNET_HashCode * key,
535                      const struct GNUNET_PeerIdentity *get_path,
536                      unsigned int get_path_length,
537                      const struct GNUNET_PeerIdentity *put_path,
538                      unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
539                      size_t size, const void *data)
540 {
541   struct TestGetContext *test_get = cls;
542   GNUNET_HashCode search_key;   /* Key stored under */
543   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
544
545   memset (original_data, test_get->uid, sizeof (original_data));
546   GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
547   if (test_get->succeeded == GNUNET_YES)
548     return;                     /* Get has already been successful, probably ending now */
549
550 #if PATH_TRACKING
551   if (put_path != NULL)
552   {
553     unsigned int i;
554
555     fprintf (stderr, "PUT (%u) Path: ", test_get->uid);
556     for (i = 0; i < put_path_length; i++)
557       fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&put_path[i]));
558     fprintf (stderr, "\n");
559   }
560   if (get_path != NULL)
561   {
562     unsigned int i;
563
564     fprintf (stderr, "GET (%u) Path: ", test_get->uid);
565     for (i = 0; i < get_path_length; i++)
566       fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&get_path[i]));
567     fprintf (stderr, "%s%s\n", get_path_length > 0 ? "->" : "",
568              GNUNET_i2s (&test_get->daemon->id));
569   }
570 #endif
571
572   if ((0 != memcmp (&search_key, key, sizeof (GNUNET_HashCode))) ||
573       (0 != memcmp (original_data, data, sizeof (original_data))))
574   {
575     fprintf (stderr, "Key or data is not the same as was inserted!\n");
576     return;
577   }
578   gets_completed++;
579   test_get->succeeded = GNUNET_YES;
580   GNUNET_SCHEDULER_cancel (test_get->task);
581   test_get->task = GNUNET_SCHEDULER_add_now (&get_stop_task, test_get);
582 }
583
584
585 /**
586  * Set up some data, and call API PUT function
587  */
588 static void
589 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
590 {
591   struct TestGetContext *test_get = cls;
592   GNUNET_HashCode key;          /* Made up key to store data under */
593   char data[TEST_DATA_SIZE];    /* Made up data to store */
594
595   if (outstanding_gets > MAX_OUTSTANDING_GETS)
596   {
597     test_get->task =
598         GNUNET_SCHEDULER_add_delayed (GET_DELAY, &do_get, test_get);
599     return;
600   }
601   memset (data, test_get->uid, sizeof (data));
602   GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
603   test_get->dht_handle = GNUNET_DHT_connect (test_get->daemon->cfg, 10);
604   GNUNET_assert (test_get->dht_handle != NULL);
605   outstanding_gets++;
606   test_get->get_handle =
607       GNUNET_DHT_get_start (test_get->dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
608                             GNUNET_BLOCK_TYPE_TEST, &key, 1, route_option, NULL,
609                             0, &get_result_iterator, test_get);
610   test_get->task =
611       GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &get_stop_task, test_get);
612 }
613
614
615 /**
616  * Task to release DHT handles for PUT
617  */
618 static void
619 put_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
620 {
621   struct TestPutContext *test_put = cls;
622
623   test_put->task = GNUNET_SCHEDULER_NO_TASK;
624   GNUNET_DHT_disconnect (test_put->dht_handle);
625   test_put->dht_handle = NULL;
626   GNUNET_CONTAINER_DLL_remove (all_puts_head, all_puts_tail, test_put);
627   GNUNET_free (test_put);
628 }
629
630
631 /**
632  * Schedule the GET requests
633  */
634 static void
635 start_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
636 {
637   unsigned long long i;
638   unsigned long long j;
639   struct TestGetContext *test_get;
640
641 #if VERBOSE
642   fprintf (stderr, "Issuing %llu GETs\n",
643            (unsigned long long) (num_peers * num_peers));
644 #endif
645   for (i = 0; i < num_peers; i++)
646     for (j = 0; j < num_peers; j++)
647     {
648       test_get = GNUNET_malloc (sizeof (struct TestGetContext));
649       test_get->uid = i + j * num_peers;
650       test_get->daemon = GNUNET_TESTING_daemon_get (pg, j);
651       GNUNET_CONTAINER_DLL_insert (all_gets_head, all_gets_tail, test_get);
652       test_get->task = GNUNET_SCHEDULER_add_now (&do_get, test_get);
653     }
654 }
655
656
657 /**
658  * Called when the PUT request has been transmitted to the DHT service.
659  */
660 static void
661 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
662 {
663   struct TestPutContext *test_put = cls;
664
665   outstanding_puts--;
666   puts_completed++;
667   GNUNET_SCHEDULER_cancel (test_put->task);
668   test_put->task = GNUNET_SCHEDULER_add_now (&put_disconnect_task, test_put);
669   if (puts_completed != num_peers * num_peers)
670     return;
671
672   GNUNET_assert (outstanding_puts == 0);
673   GNUNET_SCHEDULER_add_delayed (START_DELAY, &start_gets, NULL);
674 }
675
676
677 /**
678  * Set up some data, and call API PUT function
679  */
680 static void
681 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
682 {
683   struct TestPutContext *test_put = cls;
684   GNUNET_HashCode key;          /* Made up key to store data under */
685   char data[TEST_DATA_SIZE];    /* Made up data to store */
686
687   test_put->task = GNUNET_SCHEDULER_NO_TASK;
688   if (outstanding_puts > MAX_OUTSTANDING_PUTS)
689   {
690     test_put->task =
691         GNUNET_SCHEDULER_add_delayed (PUT_DELAY, &do_put, test_put);
692     return;
693   }
694   memset (data, test_put->uid, sizeof (data));
695   GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
696   test_put->dht_handle = GNUNET_DHT_connect (test_put->daemon->cfg, 10);
697   GNUNET_assert (test_put->dht_handle != NULL);
698   outstanding_puts++;
699 #if VERBOSE > 2
700   fprintf (stderr, "PUT %u at `%s'\n", test_put->uid,
701            GNUNET_i2s (&test_put->daemon->id));
702 #endif
703   GNUNET_DHT_put (test_put->dht_handle, &key, 1, route_option,
704                   GNUNET_BLOCK_TYPE_TEST, sizeof (data), data,
705                   GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
706                   &put_finished, test_put);
707   test_put->task =
708       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
709                                     &put_disconnect_task, test_put);
710 }
711
712
713 static void
714 run_dht_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
715 {
716   unsigned long long i;
717   struct TestPutContext *test_put;
718
719 #if PATH_TRACKING
720   route_option =
721       GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
722 #else
723   route_option = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
724 #endif
725   die_task =
726       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
727                                     "from setup puts/gets");
728   fprintf (stderr, "Issuing %llu PUTs (one per peer)\n",
729            (unsigned long long) (num_peers * num_peers));
730   for (i = 0; i < num_peers * num_peers; i++)
731   {
732     test_put = GNUNET_malloc (sizeof (struct TestPutContext));
733     test_put->uid = i;
734     test_put->daemon = GNUNET_TESTING_daemon_get (pg, i % num_peers);
735     test_put->task = GNUNET_SCHEDULER_add_now (&do_put, test_put);
736     GNUNET_CONTAINER_DLL_insert (all_puts_head, all_puts_tail, test_put);
737   }
738 }
739
740
741 /**
742  * This function is called once testing has finished setting up the topology.
743  *
744  * @param cls unused
745  * @param emsg variable is NULL on success (peers connected), and non-NULL on
746  * failure (peers failed to connect).
747  */
748 static void
749 startup_done (void *cls, const char *emsg)
750 {
751   if (emsg != NULL)
752   {
753     fprintf (stderr, "Failed to setup topology: %s\n", emsg);
754     die_task = GNUNET_SCHEDULER_add_now (&end_badly, "topology setup failed");
755     return;
756   }
757   die_task =
758       GNUNET_SCHEDULER_add_delayed (START_DELAY, &run_dht_test,
759                                     "from setup puts/gets");
760 }
761
762
763 static void
764 run (void *cls, char *const *args, const char *cfgfile,
765      const struct GNUNET_CONFIGURATION_Handle *cfg)
766 {
767   /* Get path from configuration file */
768   if (GNUNET_YES !=
769       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
770                                              &test_directory))
771   {
772     GNUNET_break (0);
773     ok = 404;
774     return;
775   }
776   if (GNUNET_SYSERR ==
777       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
778                                              &num_peers))
779     num_peers = DEFAULT_NUM_PEERS;
780   pg = GNUNET_TESTING_peergroup_start (cfg, num_peers, TIMEOUT, NULL,
781                                        &startup_done, NULL, NULL);
782   GNUNET_assert (NULL != pg);
783 }
784
785
786 static int
787 check ()
788 {
789   int ret;
790
791   /* Arguments for GNUNET_PROGRAM_run */
792   char *const argv[] = { "test-dht-multipeer",  /* Name to give running binary */
793     "-c",
794     "test_dht_multipeer_data.conf",     /* Config file to use */
795 #if VERBOSE
796     "-L", "DEBUG",
797 #endif
798     NULL
799   };
800   struct GNUNET_GETOPT_CommandLineOption options[] = {
801     GNUNET_GETOPT_OPTION_END
802   };
803   /* Run the run function as a new program */
804   ret =
805       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
806                           "test-dht-multipeer", "nohelp", options, &run, &ok);
807   if (ret != GNUNET_OK)
808   {
809     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
810                 "`test-dht-multipeer': Failed with error code %d\n", ret);
811   }
812   return ok;
813 }
814
815
816 int
817 main (int argc, char *argv[])
818 {
819   int ret;
820
821
822   GNUNET_log_setup ("test-dht-multipeer",
823 #if VERBOSE
824                     "DEBUG",
825 #else
826                     "WARNING",
827 #endif
828                     NULL);
829   ret = check ();
830   /**
831    * Need to remove base directory, subdirectories taken care
832    * of by the testing framework.
833    */
834   if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
835   {
836     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
837                 "Failed to remove testing directory %s\n", test_directory);
838   }
839   return ret;
840 }
841
842 /* end of test_dht_multipeer.c */