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