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