-fixes:
[oweals/gnunet.git] / src / testing / testing_peergroup.c
1 /*
2  This file is part of GNUnet
3  (C) 2008-2011 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 /**
22  * @file testing/testing_peergroup.c
23  * @brief API implementation for easy peer group creation
24  * @author Nathan Evans
25  * @author Christian Grothoff
26  *
27  */
28 #include "platform.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_testing_lib.h"
32 #include "gnunet_core_service.h"
33 #include "gnunet_disk_lib.h"
34
35 /** Globals **/
36 #define DEFAULT_CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37
38 #define DEFAULT_CONNECT_ATTEMPTS 2
39
40 /** Struct definitions **/
41
42 struct PeerGroupStartupContext
43 {
44   struct GNUNET_TESTING_PeerGroup *pg;
45   const struct GNUNET_CONFIGURATION_Handle *cfg;
46   unsigned int total;
47   unsigned int peers_left;
48   unsigned long long max_concurrent_connections;
49
50   /**
51    * Maximum attemps to connect two daemons.
52    */
53   unsigned long long connect_attempts;
54
55   /**
56    * How long to spend trying to establish all the connections?
57    */
58   struct GNUNET_TIME_Relative connect_timeout;
59
60   unsigned long long max_concurrent_ssh;
61   struct GNUNET_TIME_Absolute timeout;
62   GNUNET_TESTING_NotifyConnection connect_cb;
63   GNUNET_TESTING_NotifyCompletion peergroup_cb;
64
65   /**
66    * Closure for all peergroup callbacks.
67    */
68   void *cls;
69
70   const struct GNUNET_TESTING_Host *hostnames;
71   
72   /**
73    * FIXME document
74    */
75   enum GNUNET_TESTING_Topology topology;
76
77   float topology_percentage;
78
79   float topology_probability;
80
81   /**
82    * FIXME document
83    */
84   enum GNUNET_TESTING_Topology restrict_topology;
85   
86   /**
87    * FIXME document
88    */
89   char *restrict_transports;
90   
91   /**
92    * Initial connections
93    */
94   enum GNUNET_TESTING_Topology connect_topology;
95   enum GNUNET_TESTING_TopologyOption connect_topology_option;
96   double connect_topology_option_modifier;
97   int verbose;
98
99   struct ProgressMeter *hostkey_meter;
100   struct ProgressMeter *peer_start_meter;
101   struct ProgressMeter *connect_meter;
102
103   /**
104    * Task used to kill the peergroup.
105    */
106   GNUNET_SCHEDULER_TaskIdentifier die_task;
107
108   char *fail_reason;
109
110   /**
111    * Variable used to store the number of connections we should wait for.
112    */
113   unsigned int expected_connections;
114
115   /**
116    * Time when the connecting peers was started.
117    */
118   struct GNUNET_TIME_Absolute connect_start_time;
119
120   /**
121    * The total number of connections that have been created so far.
122    */
123   unsigned int total_connections;
124
125   /**
126    * The total number of connections that have failed so far.
127    */
128   unsigned int failed_connections;
129
130   /**
131    * File handle to write out topology in dot format.
132    */
133   struct GNUNET_DISK_FileHandle *topology_output_file;
134 };
135
136 struct TopologyOutputContext
137 {
138   struct GNUNET_DISK_FileHandle *file;
139   GNUNET_TESTING_NotifyCompletion notify_cb;
140   void *notify_cb_cls;
141 };
142
143 /**
144  * Simple struct to keep track of progress, and print a
145  * percentage meter for long running tasks.
146  */
147 struct ProgressMeter
148 {
149   /**
150    * Total number of tasks to complete.
151    */
152   unsigned int total;
153
154   /**
155    * Print percentage done after modnum tasks.
156    */
157   unsigned int modnum;
158
159   /**
160    * Print a . each dotnum tasks.
161    */
162   unsigned int dotnum;
163
164   /**
165    * Total number completed thus far.
166    */
167   unsigned int completed;
168
169   /**
170    * Whether or not to print.
171    */
172   int print;
173
174   /**
175    * Startup string for progress meter.
176    */
177   char *startup_string;
178 };
179
180
181 /** Utility functions **/
182
183 /**
184  * Create a meter to keep track of the progress of some task.
185  *
186  * @param total the total number of items to complete
187  * @param start_string a string to prefix the meter with (if printing)
188  * @param print GNUNET_YES to print the meter, GNUNET_NO to count
189  *              internally only
190  *
191  * @return the progress meter
192  */
193 static struct ProgressMeter *
194 create_meter (unsigned int total, char *start_string, int print)
195 {
196   struct ProgressMeter *ret;
197
198   ret = GNUNET_malloc (sizeof (struct ProgressMeter));
199   ret->print = print;
200   ret->total = total;
201   ret->modnum = (total / 4 == 0) ? 1 : (total / 4);
202   ret->dotnum = (total / 50) + 1;
203   if (start_string != NULL)
204     ret->startup_string = GNUNET_strdup (start_string);
205   else
206     ret->startup_string = GNUNET_strdup ("");
207
208   return ret;
209 }
210
211 /**
212  * Update progress meter (increment by one).
213  *
214  * @param meter the meter to update and print info for
215  *
216  * @return GNUNET_YES if called the total requested,
217  *         GNUNET_NO if more items expected
218  */
219 static int
220 update_meter (struct ProgressMeter *meter)
221 {
222   if (meter->print == GNUNET_YES)
223   {
224     if (meter->completed % meter->modnum == 0)
225     {
226       if (meter->completed == 0)
227       {
228         FPRINTF (stdout, "%sProgress: [0%%", meter->startup_string);
229       }
230       else
231         FPRINTF (stdout, "%d%%",
232                  (int) (((float) meter->completed / meter->total) * 100));
233     }
234     else if (meter->completed % meter->dotnum == 0)
235       FPRINTF (stdout, "%s",  ".");
236
237     if (meter->completed + 1 == meter->total)
238       FPRINTF (stdout, "%d%%]\n", 100);
239     fflush (stdout);
240   }
241   meter->completed++;
242
243   if (meter->completed == meter->total)
244     return GNUNET_YES;
245   return GNUNET_NO;
246 }
247
248 /**
249  * Reset progress meter.
250  *
251  * @param meter the meter to reset
252  *
253  * @return GNUNET_YES if meter reset,
254  *         GNUNET_SYSERR on error
255  */
256 static int
257 reset_meter (struct ProgressMeter *meter)
258 {
259   if (meter == NULL)
260     return GNUNET_SYSERR;
261
262   meter->completed = 0;
263   return GNUNET_YES;
264 }
265
266 /**
267  * Release resources for meter
268  *
269  * @param meter the meter to free
270  */
271 static void
272 free_meter (struct ProgressMeter *meter)
273 {
274   GNUNET_free_non_null (meter->startup_string);
275   GNUNET_free (meter);
276 }
277
278
279 /** Functions for creating, starting and connecting the peergroup **/
280
281 /**
282  * Check whether peers successfully shut down.
283  */
284 static void
285 internal_shutdown_callback (void *cls, const char *emsg)
286 {
287   struct PeerGroupStartupContext *pg_start_ctx = cls;
288
289   if (emsg != NULL)
290     pg_start_ctx->peergroup_cb (pg_start_ctx->cls, emsg);
291   else
292     pg_start_ctx->peergroup_cb (pg_start_ctx->cls, pg_start_ctx->fail_reason);
293 }
294
295 /**
296  * Check if the get_handle is being used, if so stop the request.  Either
297  * way, schedule the end_badly_cont function which actually shuts down the
298  * test.
299  */
300 static void
301 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
302 {
303   struct PeerGroupStartupContext *pg_start_ctx = cls;
304
305   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
306               "Failing peer group startup with error: `%s'!\n",
307               pg_start_ctx->fail_reason);
308
309   GNUNET_TESTING_daemons_stop (pg_start_ctx->pg,
310                                GNUNET_TIME_absolute_get_remaining
311                                (pg_start_ctx->timeout),
312                                &internal_shutdown_callback, pg_start_ctx);
313
314   if (pg_start_ctx->hostkey_meter != NULL)
315   {
316     free_meter (pg_start_ctx->hostkey_meter);
317     pg_start_ctx->hostkey_meter = NULL;
318   }
319   if (pg_start_ctx->peer_start_meter != NULL)
320   {
321     free_meter (pg_start_ctx->peer_start_meter);
322     pg_start_ctx->peer_start_meter = NULL;
323   }
324   if (pg_start_ctx->connect_meter != NULL)
325   {
326     free_meter (pg_start_ctx->connect_meter);
327     pg_start_ctx->connect_meter = NULL;
328   }
329 }
330
331 /**
332  * This function is called whenever a connection attempt is finished between two of
333  * the started peers (started with GNUNET_TESTING_daemons_start).  The total
334  * number of times this function is called should equal the number returned
335  * from the GNUNET_TESTING_connect_topology call.
336  *
337  * The emsg variable is NULL on success (peers connected), and non-NULL on
338  * failure (peers failed to connect).
339  */
340 static void
341 internal_topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
342                             const struct GNUNET_PeerIdentity *second,
343                             uint32_t distance,
344                             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
345                             const struct GNUNET_CONFIGURATION_Handle
346                             *second_cfg,
347                             struct GNUNET_TESTING_Daemon *first_daemon,
348                             struct GNUNET_TESTING_Daemon *second_daemon,
349                             const char *emsg)
350 {
351   struct PeerGroupStartupContext *pg_start_ctx = cls;
352   char *temp_str;
353   char *second_str;
354   int temp;
355
356 #if TIMING
357   unsigned long long duration;
358   unsigned long long total_duration;
359   unsigned int new_connections;
360   unsigned int new_failed_connections;
361   double conns_per_sec_recent;
362   double conns_per_sec_total;
363   double failed_conns_per_sec_recent;
364   double failed_conns_per_sec_total;
365 #endif
366
367 #if TIMING
368   if (GNUNET_TIME_absolute_get_difference
369       (connect_last_time,
370        GNUNET_TIME_absolute_get ()).rel_value >
371       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
372                                      CONN_UPDATE_DURATION).rel_value)
373   {
374     /* Get number of new connections */
375     new_connections = total_connections - previous_connections;
376
377     /* Get number of new FAILED connections */
378     new_failed_connections = failed_connections - previous_failed_connections;
379
380     /* Get duration in seconds */
381     duration =
382         GNUNET_TIME_absolute_get_difference (connect_last_time,
383                                              GNUNET_TIME_absolute_get
384                                              ()).rel_value / 1000;
385     total_duration =
386         GNUNET_TIME_absolute_get_difference (connect_start_time,
387                                              GNUNET_TIME_absolute_get
388                                              ()).rel_value / 1000;
389
390     failed_conns_per_sec_recent = (double) new_failed_connections / duration;
391     failed_conns_per_sec_total = (double) failed_connections / total_duration;
392     conns_per_sec_recent = (double) new_connections / duration;
393     conns_per_sec_total = (double) total_connections / total_duration;
394     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
395                 "Recent: %.2f/s, Total: %.2f/s, Recent failed: %.2f/s, total failed %.2f/s\n",
396                 conns_per_sec_recent, CONN_UPDATE_DURATION, conns_per_sec_total,
397                 failed_conns_per_sec_recent, failed_conns_per_sec_total);
398     connect_last_time = GNUNET_TIME_absolute_get ();
399     previous_connections = total_connections;
400     previous_failed_connections = failed_connections;
401     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
402                 "have %u total_connections, %u failed\n", total_connections,
403                 failed_connections);
404   }
405 #endif
406
407
408   if (emsg == NULL)
409   {
410     pg_start_ctx->total_connections++;
411 #if VERBOSE > 1
412     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413                 "connected peer %s to peer %s, distance %u\n",
414                 first_daemon->shortname, second_daemon->shortname, distance);
415 #endif
416     if (pg_start_ctx->topology_output_file != NULL)
417     {
418       second_str = GNUNET_strdup (GNUNET_i2s (second));
419       temp =
420           GNUNET_asprintf (&temp_str, "\t\"%s\" -- \"%s\"\n",
421                            GNUNET_i2s (first), second_str);
422       GNUNET_free (second_str);
423       if (temp > 0)
424         GNUNET_DISK_file_write (pg_start_ctx->topology_output_file, temp_str,
425                                 temp);
426       GNUNET_free (temp_str);
427     }
428   }
429   else
430   {
431     pg_start_ctx->failed_connections++;
432 #if VERBOSE
433     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
434                 "Failed to connect peer %s to peer %s with error :\n%s\n",
435                 first_daemon->shortname, second_daemon->shortname, emsg);
436
437     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438                 "Failed to connect peer %s to peer %s with error :\n%s\n",
439                 first_daemon->shortname, second_daemon->shortname, emsg);
440 #endif
441   }
442
443   GNUNET_assert (pg_start_ctx->connect_meter != NULL);
444   if (pg_start_ctx->connect_cb != NULL)
445     pg_start_ctx->connect_cb (pg_start_ctx->cls, first, second, distance,
446                               first_cfg, second_cfg, first_daemon,
447                               second_daemon, emsg);
448   if (GNUNET_YES == update_meter (pg_start_ctx->connect_meter))
449   {
450 #if VERBOSE
451     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452                 "Created %d total connections, which is our target number!  Starting next phase of testing.\n",
453                 pg_start_ctx->total_connections);
454 #endif
455
456 #if TIMING
457     total_duration =
458         GNUNET_TIME_absolute_get_difference (connect_start_time,
459                                              GNUNET_TIME_absolute_get
460                                              ()).rel_value / 1000;
461     failed_conns_per_sec_total = (double) failed_connections / total_duration;
462     conns_per_sec_total = (double) total_connections / total_duration;
463     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
464                 "Overall connection info --- Total: %u, Total Failed %u/s\n",
465                 total_connections, failed_connections);
466     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
467                 "Overall connection info --- Total: %.2f/s, Total Failed %.2f/s\n",
468                 conns_per_sec_total, failed_conns_per_sec_total);
469 #endif
470
471     GNUNET_assert (pg_start_ctx->die_task != GNUNET_SCHEDULER_NO_TASK);
472     GNUNET_SCHEDULER_cancel (pg_start_ctx->die_task);
473
474     /* Call final callback, signifying that the peer group has been started and connected */
475     if (pg_start_ctx->peergroup_cb != NULL)
476       pg_start_ctx->peergroup_cb (pg_start_ctx->cls, NULL);
477
478     if (pg_start_ctx->topology_output_file != NULL)
479     {
480       temp = GNUNET_asprintf (&temp_str, "}\n");
481       if (temp > 0)
482         GNUNET_DISK_file_write (pg_start_ctx->topology_output_file, temp_str,
483                                 temp);
484       GNUNET_free (temp_str);
485       GNUNET_DISK_file_close (pg_start_ctx->topology_output_file);
486     }
487   }
488 }
489
490
491 /**
492  * Callback called for each started daemon.
493  *
494  * @param cls Clause (PG Context).
495  * @param id PeerIdentidy of started daemon.
496  * @param cfg Configuration used by the daemon.
497  * @param d Handle for the daemon.
498  * @param emsg Error message, NULL on success.
499  */
500 static void
501 internal_peers_started_callback (void *cls,
502                                  const struct GNUNET_PeerIdentity *id,
503                                  const struct GNUNET_CONFIGURATION_Handle *cfg,
504                                  struct GNUNET_TESTING_Daemon *d,
505                                  const char *emsg)
506 {
507   struct PeerGroupStartupContext *pg_start_ctx = cls;
508
509   if (emsg != NULL)
510   {
511     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
512                 "Failed to start daemon with error: `%s'\n", emsg);
513     return;
514   }
515   GNUNET_assert (id != NULL);
516
517 #if VERBOSE > 1
518   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
519               (pg_start_ctx->total - pg_start_ctx->peers_left) + 1,
520               pg_start_ctx->total);
521 #endif
522
523   pg_start_ctx->peers_left--;
524
525   if (NULL == pg_start_ctx->peer_start_meter)
526   {
527     /* Cancelled Ctrl-C or error */
528     return;
529   }
530   if (GNUNET_YES == update_meter (pg_start_ctx->peer_start_meter))
531   {
532 #if VERBOSE
533     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
534                 "All %d daemons started, now connecting peers!\n",
535                 pg_start_ctx->total);
536 #endif
537     GNUNET_assert (pg_start_ctx->die_task != GNUNET_SCHEDULER_NO_TASK);
538     GNUNET_SCHEDULER_cancel (pg_start_ctx->die_task);
539
540     pg_start_ctx->expected_connections = UINT_MAX;
541     // FIXME: why whould peers_left be != 0?? Or pg NULL?
542     if ((pg_start_ctx->pg != NULL) && (pg_start_ctx->peers_left == 0))
543     {
544       pg_start_ctx->connect_start_time = GNUNET_TIME_absolute_get ();
545       pg_start_ctx->expected_connections =
546           GNUNET_TESTING_connect_topology (pg_start_ctx->pg,
547                                            pg_start_ctx->connect_topology,
548                                            pg_start_ctx->connect_topology_option,
549                                            pg_start_ctx->connect_topology_option_modifier,
550                                            pg_start_ctx->connect_timeout,
551                                            pg_start_ctx->connect_attempts, NULL,
552                                            NULL);
553
554       pg_start_ctx->connect_meter =
555           create_meter (pg_start_ctx->expected_connections, "Peer connection ",
556                         pg_start_ctx->verbose);
557       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have %d expected connections\n",
558                   pg_start_ctx->expected_connections);
559     }
560
561     if (pg_start_ctx->expected_connections == 0)
562     {
563       GNUNET_free_non_null (pg_start_ctx->fail_reason);
564       pg_start_ctx->fail_reason =
565           GNUNET_strdup ("from connect topology (bad return)");
566       pg_start_ctx->die_task =
567           GNUNET_SCHEDULER_add_now (&end_badly, pg_start_ctx);
568       return;
569     }
570
571     GNUNET_free_non_null (pg_start_ctx->fail_reason);
572     pg_start_ctx->fail_reason =
573         GNUNET_strdup ("from connect topology (timeout)");
574     pg_start_ctx->die_task =
575         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
576                                       (pg_start_ctx->timeout), &end_badly,
577                                       pg_start_ctx);
578   }
579 }
580
581 /**
582  * Callback indicating that the hostkey was created for a peer.
583  *
584  * @param cls NULL
585  * @param id the peer identity
586  * @param d the daemon handle (pretty useless at this point, remove?)
587  * @param emsg non-null on failure
588  */
589 static void
590 internal_hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
591                            struct GNUNET_TESTING_Daemon *d, const char *emsg)
592 {
593   struct PeerGroupStartupContext *pg_start_ctx = cls;
594   unsigned int create_expected_connections;
595
596   if (emsg != NULL)
597   {
598     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
599                 "Hostkey callback received error: %s\n", emsg);
600   }
601
602 #if VERBOSE > 1
603   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604               "Hostkey (%d/%d) created for peer `%s'\n",
605               pg_start_ctx->total - pg_start_ctx->peers_left + 1,
606               pg_start_ctx->total, GNUNET_i2s (id));
607 #endif
608
609   pg_start_ctx->peers_left--;
610   if (GNUNET_YES == update_meter (pg_start_ctx->hostkey_meter))
611   {
612     GNUNET_SCHEDULER_cancel (pg_start_ctx->die_task);
613     GNUNET_free_non_null (pg_start_ctx->fail_reason);
614     /* Set up task in case topology creation doesn't finish
615      * within a reasonable amount of time */
616     pg_start_ctx->fail_reason = GNUNET_strdup ("from create_topology");
617     pg_start_ctx->die_task =
618         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
619                                       (pg_start_ctx->timeout), &end_badly,
620                                       pg_start_ctx);
621     pg_start_ctx->peers_left = pg_start_ctx->total;     /* Reset counter */
622     create_expected_connections =
623         GNUNET_TESTING_create_topology (pg_start_ctx->pg,
624                                         pg_start_ctx->topology,
625                                         pg_start_ctx->restrict_topology,
626                                         pg_start_ctx->restrict_transports);
627     if (create_expected_connections > 0)
628     {
629       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
630                   "Topology set up, have %u expected connections, now starting peers!\n",
631                   create_expected_connections);
632       GNUNET_TESTING_daemons_continue_startup (pg_start_ctx->pg);
633     }
634     else
635     {
636       GNUNET_SCHEDULER_cancel (pg_start_ctx->die_task);
637       GNUNET_free_non_null (pg_start_ctx->fail_reason);
638       pg_start_ctx->fail_reason =
639           GNUNET_strdup ("from create topology (bad return)");
640       pg_start_ctx->die_task =
641           GNUNET_SCHEDULER_add_now (&end_badly, pg_start_ctx);
642       return;
643     }
644
645     GNUNET_SCHEDULER_cancel (pg_start_ctx->die_task);
646     GNUNET_free_non_null (pg_start_ctx->fail_reason);
647     pg_start_ctx->fail_reason =
648         GNUNET_strdup ("from continue startup (timeout)");
649     pg_start_ctx->die_task =
650         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
651                                       (pg_start_ctx->timeout), &end_badly,
652                                       pg_start_ctx);
653   }
654 }
655
656
657 /**
658  * Prototype of a callback function indicating that two peers
659  * are currently connected.
660  *
661  * @param cls closure
662  * @param first peer id for first daemon
663  * @param second peer id for the second daemon
664  * @param emsg error message (NULL on success)
665  */
666 void
667 write_topology_cb (void *cls, const struct GNUNET_PeerIdentity *first,
668                    const struct GNUNET_PeerIdentity *second, const char *emsg)
669 {
670   struct TopologyOutputContext *topo_ctx;
671   int temp;
672   char *temp_str;
673   char *temp_pid2;
674
675   topo_ctx = (struct TopologyOutputContext *) cls;
676   GNUNET_assert (topo_ctx->file != NULL);
677   if ((emsg == NULL) && (first != NULL) && (second != NULL))
678   {
679     GNUNET_assert (first != NULL);
680     GNUNET_assert (second != NULL);
681     temp_pid2 = GNUNET_strdup (GNUNET_i2s (second));
682     temp =
683         GNUNET_asprintf (&temp_str, "\t\"%s\" -- \"%s\"\n", GNUNET_i2s (first),
684                          temp_pid2);
685     GNUNET_free (temp_pid2);
686     GNUNET_DISK_file_write (topo_ctx->file, temp_str, temp);
687   }
688   else if ((emsg == NULL) && (first == NULL) && (second == NULL))
689   {
690     temp = GNUNET_asprintf (&temp_str, "}\n");
691     GNUNET_DISK_file_write (topo_ctx->file, temp_str, temp);
692     GNUNET_DISK_file_close (topo_ctx->file);
693     topo_ctx->notify_cb (topo_ctx->notify_cb_cls, NULL);
694     GNUNET_free (topo_ctx);
695   }
696   else
697   {
698     temp = GNUNET_asprintf (&temp_str, "}\n");
699     GNUNET_DISK_file_write (topo_ctx->file, temp_str, temp);
700     GNUNET_DISK_file_close (topo_ctx->file);
701     topo_ctx->notify_cb (topo_ctx->notify_cb_cls, emsg);
702     GNUNET_free (topo_ctx);
703   }
704 }
705
706 /**
707  * Print current topology to a graphviz readable file.
708  *
709  * @param pg a currently running peergroup to print to file
710  * @param output_filename the file to write the topology to
711  * @param notify_cb callback to call upon completion or failure
712  * @param notify_cb_cls closure for notify_cb
713  *
714  */
715 void
716 GNUNET_TESTING_peergroup_topology_to_file (struct GNUNET_TESTING_PeerGroup *pg,
717                                            const char *output_filename,
718                                            GNUNET_TESTING_NotifyCompletion
719                                            notify_cb, void *notify_cb_cls)
720 {
721   struct TopologyOutputContext *topo_ctx;
722   int temp;
723   char *temp_str;
724
725   topo_ctx = GNUNET_malloc (sizeof (struct TopologyOutputContext));
726
727   topo_ctx->notify_cb = notify_cb;
728   topo_ctx->notify_cb_cls = notify_cb_cls;
729   topo_ctx->file =
730       GNUNET_DISK_file_open (output_filename,
731                              GNUNET_DISK_OPEN_READWRITE |
732                              GNUNET_DISK_OPEN_CREATE,
733                              GNUNET_DISK_PERM_USER_READ |
734                              GNUNET_DISK_PERM_USER_WRITE);
735   if (topo_ctx->file == NULL)
736   {
737     notify_cb (notify_cb_cls, "Failed to open output file!");
738     GNUNET_free (topo_ctx);
739     return;
740   }
741
742   temp = GNUNET_asprintf (&temp_str, "strict graph G {\n");
743   if (temp > 0)
744     GNUNET_DISK_file_write (topo_ctx->file, temp_str, temp);
745   GNUNET_free_non_null (temp_str);
746   GNUNET_TESTING_get_topology (pg, &write_topology_cb, topo_ctx);
747 }
748
749 /**
750  * Start a peer group with a given number of peers.  Notify
751  * on completion of peer startup and connection based on given
752  * topological constraints.  Optionally notify on each
753  * established connection.
754  *
755  * @param cfg configuration template to use
756  * @param total number of daemons to start
757  * @param timeout total time allowed for peers to start
758  * @param connect_cb function to call each time two daemons are connected
759  * @param peergroup_cb function to call once all peers are up and connected
760  * @param peergroup_cls closure for peergroup callbacks
761  * @param hostnames linked list of host structs to use to start peers on
762  *                  (NULL to run on localhost only)
763  *
764  * @return NULL on error, otherwise handle to control peer group
765  */
766 struct GNUNET_TESTING_PeerGroup *
767 GNUNET_TESTING_peergroup_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
768                                 unsigned int total,
769                                 struct GNUNET_TIME_Relative timeout,
770                                 GNUNET_TESTING_NotifyConnection connect_cb,
771                                 GNUNET_TESTING_NotifyCompletion peergroup_cb,
772                                 void *peergroup_cls,
773                                 const struct GNUNET_TESTING_Host *hostnames)
774 {
775   struct PeerGroupStartupContext *pg_start_ctx;
776   char *temp_str;
777   int temp;
778   struct GNUNET_TIME_Relative rtimeout;
779
780   GNUNET_assert (total > 0);
781   GNUNET_assert (cfg != NULL);
782
783   pg_start_ctx = GNUNET_malloc (sizeof (struct PeerGroupStartupContext));
784
785   if (GNUNET_OK !=
786       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_attempts",
787                                              &pg_start_ctx->connect_attempts))
788   {
789     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
790                 "testing", "connect_attempts");
791     GNUNET_free (pg_start_ctx);
792     return NULL;
793   }
794
795   if (GNUNET_OK !=
796       GNUNET_CONFIGURATION_get_value_time (cfg, "testing", "CONNECT_TIMEOUT",
797                                            &pg_start_ctx->connect_timeout))
798   {
799     pg_start_ctx->connect_timeout = DEFAULT_CONNECT_TIMEOUT;
800   }
801
802   if (GNUNET_OK !=
803       GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
804                                              "max_outstanding_connections",
805                                              &pg_start_ctx->max_concurrent_connections))
806   {
807     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
808                 "testing", "max_outstanding_connections");
809     GNUNET_free (pg_start_ctx);
810     return NULL;
811   }
812
813   if (GNUNET_OK !=
814       GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
815                                              "max_concurrent_ssh",
816                                              &pg_start_ctx->max_concurrent_ssh))
817   {
818     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
819                 "testing", "max_concurrent_ssh");
820     GNUNET_free (pg_start_ctx);
821     return NULL;
822   }
823
824   if (GNUNET_SYSERR ==
825       (pg_start_ctx->verbose =
826        GNUNET_CONFIGURATION_get_value_yesno (cfg, "testing",
827                                              "use_progressbars")))
828   {
829     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
830                 "testing", "use_progressbars");
831     GNUNET_free (pg_start_ctx);
832     return NULL;
833   }
834
835   if (GNUNET_OK !=
836       GNUNET_CONFIGURATION_get_value_time (cfg, "testing", "PEERGROUP_TIMEOUT",
837                                            &rtimeout))
838   {
839     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
840                 "testing", "PEERGROUP_TIMEOUT");
841     GNUNET_free (pg_start_ctx);
842     return NULL;
843   }
844   pg_start_ctx->timeout = GNUNET_TIME_relative_to_absolute (rtimeout);
845
846
847   /* Read topology related options from the configuration file */
848   temp_str = NULL;
849   if ((GNUNET_YES ==
850        GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "topology",
851                                               &temp_str)) &&
852       (GNUNET_NO ==
853        GNUNET_TESTING_topology_get (&pg_start_ctx->topology, temp_str)))
854   {
855     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
856                 "Invalid topology `%s' given for section %s option %s\n",
857                 temp_str, "TESTING", "TOPOLOGY");
858     pg_start_ctx->topology = GNUNET_TESTING_TOPOLOGY_CLIQUE;    /* Defaults to NONE, so set better default here */
859   }
860   GNUNET_free_non_null (temp_str);
861
862   if (GNUNET_YES ==
863       GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
864                                              "topology_output_file", &temp_str))
865   {
866     pg_start_ctx->topology_output_file =
867         GNUNET_DISK_file_open (temp_str,
868                                GNUNET_DISK_OPEN_READWRITE |
869                                GNUNET_DISK_OPEN_CREATE,
870                                GNUNET_DISK_PERM_USER_READ |
871                                GNUNET_DISK_PERM_USER_WRITE);
872     if (pg_start_ctx->topology_output_file != NULL)
873     {
874       GNUNET_free (temp_str);
875       temp = GNUNET_asprintf (&temp_str, "strict graph G {\n");
876       if (temp > 0)
877         GNUNET_DISK_file_write (pg_start_ctx->topology_output_file, temp_str,
878                                 temp);
879     }
880     GNUNET_free (temp_str);
881   }
882
883   if (GNUNET_OK !=
884       GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "percentage",
885                                              &temp_str))
886     pg_start_ctx->topology_percentage = 0.5;
887   else
888   {
889     pg_start_ctx->topology_percentage = atof (temp_str);
890     GNUNET_free (temp_str);
891   }
892
893   if (GNUNET_OK !=
894       GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "probability",
895                                              &temp_str))
896     pg_start_ctx->topology_probability = 0.5;
897   else
898   {
899     pg_start_ctx->topology_probability = atof (temp_str);
900     GNUNET_free (temp_str);
901   }
902
903   if ((GNUNET_YES ==
904        GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
905                                               "connect_topology", &temp_str)) &&
906       (GNUNET_NO ==
907        GNUNET_TESTING_topology_get (&pg_start_ctx->connect_topology, temp_str)))
908   {
909     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
910                 "Invalid connect topology `%s' given for section %s option %s\n",
911                 temp_str, "TESTING", "CONNECT_TOPOLOGY");
912   }
913   GNUNET_free_non_null (temp_str);
914
915   if ((GNUNET_YES ==
916        GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
917                                               "connect_topology_option",
918                                               &temp_str)) &&
919       (GNUNET_NO ==
920        GNUNET_TESTING_topology_option_get
921        (&pg_start_ctx->connect_topology_option, temp_str)))
922   {
923     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
924                 "Invalid connect topology option `%s' given for section %s option %s\n",
925                 temp_str, "TESTING", "CONNECT_TOPOLOGY_OPTION");
926     pg_start_ctx->connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
927   }
928   GNUNET_free_non_null (temp_str);
929
930   if (GNUNET_YES ==
931       GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
932                                              "connect_topology_option_modifier",
933                                              &temp_str))
934   {
935     if (SSCANF
936         (temp_str, "%lf", &pg_start_ctx->connect_topology_option_modifier) != 1)
937     {
938       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
939                   _
940                   ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
941                   temp_str, "connect_topology_option_modifier", "TESTING");
942       GNUNET_free (temp_str);
943       GNUNET_free (pg_start_ctx);
944       return NULL;
945     }
946     GNUNET_free (temp_str);
947   }
948
949   if (GNUNET_YES !=
950       GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
951                                              "blacklist_transports",
952                                              &pg_start_ctx->restrict_transports))
953     pg_start_ctx->restrict_transports = NULL;
954
955   pg_start_ctx->restrict_topology = GNUNET_TESTING_TOPOLOGY_NONE;
956   if ((GNUNET_YES ==
957        GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
958                                               "blacklist_topology", &temp_str))
959       && (GNUNET_NO ==
960           GNUNET_TESTING_topology_get (&pg_start_ctx->restrict_topology,
961                                        temp_str)))
962   {
963     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
964                 "Invalid topology `%s' given for section %s option %s\n",
965                 temp_str, "TESTING", "BLACKLIST_TOPOLOGY");
966   }
967
968   GNUNET_free_non_null (temp_str);
969
970   pg_start_ctx->cfg = cfg;
971   pg_start_ctx->total = total;
972   pg_start_ctx->peers_left = total;
973   pg_start_ctx->connect_cb = connect_cb;
974   pg_start_ctx->peergroup_cb = peergroup_cb;
975   pg_start_ctx->cls = peergroup_cls;
976   pg_start_ctx->hostnames = hostnames;
977   pg_start_ctx->hostkey_meter =
978       create_meter (pg_start_ctx->peers_left, "Hostkeys created ",
979                     pg_start_ctx->verbose);
980   pg_start_ctx->peer_start_meter =
981       create_meter (pg_start_ctx->peers_left, "Peers started ",
982                     pg_start_ctx->verbose);
983   /* Make compilers happy */
984   reset_meter (pg_start_ctx->peer_start_meter);
985   pg_start_ctx->fail_reason =
986       GNUNET_strdup
987       ("didn't generate all hostkeys within allowed startup time!");
988   pg_start_ctx->die_task =
989       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
990                                     (pg_start_ctx->timeout), &end_badly,
991                                     pg_start_ctx);
992
993   pg_start_ctx->pg =
994       GNUNET_TESTING_daemons_start (pg_start_ctx->cfg, pg_start_ctx->peers_left,
995                                     pg_start_ctx->max_concurrent_connections,
996                                     pg_start_ctx->max_concurrent_ssh,
997                                     GNUNET_TIME_absolute_get_remaining
998                                     (pg_start_ctx->timeout),
999                                     &internal_hostkey_callback, pg_start_ctx,
1000                                     &internal_peers_started_callback,
1001                                     pg_start_ctx, &internal_topology_callback,
1002                                     pg_start_ctx, pg_start_ctx->hostnames);
1003
1004   return pg_start_ctx->pg;
1005 }
1006
1007 /* end of testing_peergroup.c */