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