renaming ats files
[oweals/gnunet.git] / src / transport / test_transport_ats_multiple_peers.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 testing/test_transport_ats_multiple_peers.c
22  * @brief testcase for ats functionality by starting multiple peers
23  */
24
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27
28 #if HAVE_LIBGLPK
29
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_transport_service.h"
32 #include "gauger.h"
33 #include "gnunet-service-transport_ats.h"
34
35 #define VERBOSE GNUNET_NO
36
37 #define NUM_PEERS 11
38 #define MEASUREMENTS 5
39
40 #define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
42 #define SEND_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
43
44 static int ok;
45
46 static int peers_left;
47
48 static int failed_peers;
49
50 static int measurement_started = GNUNET_NO;
51 static char * config_file;
52
53 static struct GNUNET_TESTING_PeerGroup *pg;
54
55 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
56 static GNUNET_SCHEDULER_TaskIdentifier stats_task;
57 static GNUNET_SCHEDULER_TaskIdentifier send_task;
58 struct GNUNET_TESTING_Daemon * master_deamon;
59 struct GNUNET_TESTING_Daemon * ping_deamon;
60
61 struct GNUNET_STATISTICS_Handle * stats;
62
63 struct TEST_result
64 {
65   uint64_t timestamp;
66   uint64_t duration;
67   uint64_t mechs;
68   uint64_t peers;
69   uint64_t solution;
70   uint64_t state;
71 };
72
73 struct TestMessage
74 {
75   struct GNUNET_MessageHeader header;
76   uint32_t num;
77 };
78
79
80 static int count;
81 static int c_new;
82 static int c_unmodified;
83 static int c_modified;
84 static int connected;
85 static int peers;
86
87 static int force_q_updates;
88 static int force_rebuild;
89 static int send_msg;
90 static int machine_parsable;
91
92 static struct TEST_result results_new       [MEASUREMENTS+1];
93 static struct TEST_result results_modified  [MEASUREMENTS+1];
94 static struct TEST_result results_unmodified[MEASUREMENTS+1];
95 static struct TEST_result current;
96
97 static struct GNUNET_STATISTICS_GetHandle * s_solution;
98 static struct GNUNET_STATISTICS_GetHandle * s_time;
99 static struct GNUNET_STATISTICS_GetHandle * s_peers;
100 static struct GNUNET_STATISTICS_GetHandle * s_mechs;
101 static struct GNUNET_STATISTICS_GetHandle * s_duration;
102 static struct GNUNET_STATISTICS_GetHandle * s_invalid;
103 static struct GNUNET_STATISTICS_GetHandle * s_state;
104
105 struct GNUNET_TRANSPORT_TransmitHandle * t;
106 struct GNUNET_TRANSPORT_Handle * th;
107
108 /**
109  * Check whether peers successfully shut down.
110  */
111 static void
112 shutdown_callback (void *cls, const char *emsg)
113 {
114   if (emsg != NULL)
115     {
116 #if VERBOSE
117       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
118                   "Shutdown of peers failed!\n");
119 #endif
120       if (ok == 0)
121         ok = 666;
122     }
123   else
124     {
125 #if VERBOSE
126       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127                   "All peers successfully shut down!\n");
128         if (stats != NULL)
129                 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
130         stats = NULL;
131 #endif
132     }
133 }
134
135 static void
136 shutdown_peers()
137 {
138   if (shutdown_task != GNUNET_SCHEDULER_NO_TASK)
139     {
140       GNUNET_SCHEDULER_cancel(shutdown_task);
141       shutdown_task = GNUNET_SCHEDULER_NO_TASK;
142     }
143   if (stats_task != GNUNET_SCHEDULER_NO_TASK)
144     {
145       GNUNET_SCHEDULER_cancel(stats_task);
146       stats_task = GNUNET_SCHEDULER_NO_TASK;
147     }
148   if (send_task != GNUNET_SCHEDULER_NO_TASK)
149     {
150       GNUNET_SCHEDULER_cancel(send_task);
151       send_task = GNUNET_SCHEDULER_NO_TASK;
152     }
153   
154   if (t != NULL)
155     {
156       GNUNET_TRANSPORT_notify_transmit_ready_cancel(t);
157       t = NULL;
158     }
159   GNUNET_TRANSPORT_disconnect(th);  
160   if (s_time != NULL)
161     {
162       GNUNET_STATISTICS_get_cancel(s_time);
163       s_time = NULL;
164     }
165   if (s_peers != NULL)
166     {
167       GNUNET_STATISTICS_get_cancel(s_peers);
168       s_peers = NULL;
169     }
170   if (s_mechs != NULL)
171     {
172       GNUNET_STATISTICS_get_cancel(s_mechs);
173       s_mechs = NULL;
174     }
175   if (s_solution != NULL)
176     {
177       GNUNET_STATISTICS_get_cancel(s_solution);
178       s_solution = NULL;
179     }
180   if (s_duration != NULL)
181     {
182       GNUNET_STATISTICS_get_cancel(s_duration);
183       s_duration = NULL;
184     }
185   if (s_invalid != NULL)
186     {
187       GNUNET_STATISTICS_get_cancel(s_invalid);
188       s_invalid = NULL;
189     }
190   if (s_state != NULL)
191     {
192       GNUNET_STATISTICS_get_cancel(s_state);
193       s_state = NULL;
194     }
195   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
196 }
197
198 static void 
199 evaluate_measurements()
200 {
201   int c;
202   //int mechs = 0;
203   double average[3];
204   double stddev[3];
205   //char * output;
206   c = 1;
207   
208   //GNUNET_asprintf(&output, "p,%i,m,%i,",peers, MEASUREMENTS, results_modified[0].mechs,
209   
210   average[0] = 0.0;
211   for (c=0; c<c_new;c++)
212     {
213       average[0] += (double) results_new[c].duration;
214     }
215   average[0] /= c_new;
216   
217   stddev[0] = 0.0;
218   for (c=0; c<c_new;c++)
219     {
220       stddev[0] += (results_new[c].duration - average[0]) *
221           (results_new[c].duration - average[0]);
222     }
223   stddev[0] /= c_new;
224   stddev[0] = sqrt (stddev[0]);
225   if (!machine_parsable)
226     fprintf (stderr,
227              "new, %i measurements, average: %f stddev: %f\n",
228              c_new, average[0], stddev[0]);
229   
230   average[1] = 0.0;
231   for (c=0; c<c_modified;c++)
232     {
233       average[1] += (double) results_modified[c].duration;
234     }
235   average[1] /= c_modified;
236   
237   stddev[1] = 0.0;
238   for (c=0; c<c_modified;c++)
239     {
240       stddev[1] += (results_modified[c].duration - average[1]) *
241           (results_modified[c].duration - average[1]);
242     }
243   stddev[1] /= c_modified;
244   stddev[1] = sqrt (stddev[1]);
245   if (!machine_parsable) 
246     fprintf (stderr,
247              "modified, %i measurements, average: %f stddev: %f\n",
248              c_modified, average[1], stddev[1]);
249   
250   average[2] = 0.0;
251   for (c=0; c<c_unmodified;c++)
252     {
253       average[2] += (double) results_unmodified[c].duration;
254     }
255   average[2] /= c_unmodified;
256   stddev[2] = 0.0;
257   for (c=0; c<c_unmodified;c++)
258     {
259       stddev[2] += (results_unmodified[c].duration - average[2]) *
260           (results_unmodified[c].duration - average[2]);
261     }
262   stddev[2] /= c_unmodified;
263   stddev[2] = sqrt (stddev[2]);
264   
265   if (!machine_parsable) 
266     fprintf (stderr,
267              "unmodified, %i measurements, average: %f stddev: %f\n",
268              c_unmodified, average[2], stddev[2]);
269   
270   if (machine_parsable)
271     fprintf (stderr,
272              "peers,%i,mechs,%llu,"
273              "new,%i,%f,%f,"
274              "mod,%i,%f,%f,"
275              "unmod,%i,%f,%f\n",
276              peers-1, (unsigned long long) results_unmodified[0].mechs,
277              c_new, average[0], stddev[0],
278              c_modified, average[1], stddev[1],
279              c_unmodified, average[2], stddev[2]);
280   shutdown_peers();
281 }
282
283
284 static int 
285 stats_cb (void *cls,
286           const char *subsystem,
287           const char *name,
288           uint64_t value,
289           int is_persistent)
290 {
291   static int printed = GNUNET_NO;
292 #if VERBOSE_ATS
293   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s = %llu\n", name ,value);
294 #endif
295   if (0 == strcmp (name,"ATS invalid solutions"))
296     {
297       if (stats_task != GNUNET_SCHEDULER_NO_TASK)
298         {
299           GNUNET_SCHEDULER_cancel(stats_task);
300           stats_task = GNUNET_SCHEDULER_NO_TASK;
301         }
302       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"MLP produced invalid %llu result(s)!\n",
303           value);
304       shutdown_peers();
305       return GNUNET_SYSERR;
306     }
307   
308   if (0 == strcmp (name,"ATS solution"))
309     {
310       s_solution = NULL;
311     }
312   
313   if (0 == strcmp (name,"ATS peers"))
314     {
315       s_peers = NULL;
316     }
317   
318   if (0 == strcmp (name,"ATS mechanisms"))
319     {
320       s_mechs = NULL;
321     }
322   
323   if (0 == strcmp (name,"ATS duration"))
324     {
325       s_duration = NULL;
326     }
327   if (0 == strcmp (name,"ATS timestamp"))
328     {
329       s_time = NULL;
330     }
331   if (0 == strcmp (name,"ATS state"))
332     {
333       s_state = NULL;
334     }
335   
336   if ( (measurement_started == GNUNET_NO) && 
337        (0 == strcmp (name, "ATS peers")) && 
338        (value == peers-1) )
339     {
340       measurement_started = GNUNET_YES;
341       count = 1;
342       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
343                   "All %llu peers connected\n", 
344                   value);
345 #if !VERBOSE
346       if (! machine_parsable)   
347         fprintf(stderr, "%i", count);
348 #endif
349     }
350   
351   if (measurement_started == GNUNET_YES)
352     {
353       // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s == %llu\n", name ,value);
354       if (0 == strcmp (name,"ATS timestamp"))
355         {
356           if (current.timestamp == 0)
357             {
358               printed = GNUNET_NO;
359               current.timestamp = value;
360             }
361           if (current.timestamp == value)
362             {
363               printed = GNUNET_YES;
364             }
365           if (current.timestamp != value)
366             {
367               if (current.state == ATS_NEW)
368                 {
369                   if (c_new < MEASUREMENTS)
370                     {
371                       results_new[c_new] = current;
372                       c_new++;
373                     }
374                   else
375                     {
376                       force_rebuild = GNUNET_NO;
377                       force_q_updates = GNUNET_NO;
378                       send_msg = GNUNET_NO;
379                     }
380                 }
381               if (current.state == ATS_UNMODIFIED)
382                 {
383                   if (c_unmodified < MEASUREMENTS)
384                     {
385                       results_unmodified[c_unmodified] = current;
386                       c_unmodified++;
387                     }
388                   
389                 }
390               if (current.state == ATS_QUALITY_UPDATED)
391                 {
392                   if (c_modified < MEASUREMENTS)
393                     {
394                       results_modified[c_modified] = current;
395                       c_modified++;
396                     }
397                   else
398                     {
399                       force_q_updates = GNUNET_NO;
400                       force_rebuild = GNUNET_YES;
401                     }
402                 }
403               count ++;
404 #if VERBOSE
405               fprintf (stderr,
406                        "(new: %i / modified: %i / unmodified: %i) of %i \n", 
407                        c_new, c_modified, c_unmodified , MEASUREMENTS);
408 #endif
409               if ((c_modified >= MEASUREMENTS) &&
410                   (c_new >= MEASUREMENTS) &&
411                   (c_unmodified >= MEASUREMENTS))
412                 {
413 #if !VERBOSE
414                   if (!machine_parsable)
415                     fprintf(stdout, "\n");
416 #endif
417                   if (stats_task != GNUNET_SCHEDULER_NO_TASK)
418                     {
419                       GNUNET_SCHEDULER_cancel(stats_task);
420                       stats_task = GNUNET_SCHEDULER_NO_TASK;
421                     }
422                   evaluate_measurements();
423                   return GNUNET_SYSERR;
424                 }
425               
426               printed = GNUNET_NO;
427               current.timestamp = value;
428 #if !VERBOSE
429               if (! machine_parsable)
430                 fprintf(stderr, "..%i", count);
431 #endif
432               return GNUNET_OK;
433             }
434         }
435       
436       if (0 == strcmp (name,"ATS solution"))
437         {
438           current.solution = value;
439           if (printed == GNUNET_NO)
440             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
441                 count, name, value);
442         }
443       
444       if (0 == strcmp (name,"ATS peers"))
445         {
446           current.peers = value;
447           if (printed == GNUNET_NO)
448             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
449                 count, name, value);
450         }
451       
452       if (0 == strcmp (name,"ATS mechanisms"))
453         {
454           current.mechs = value;
455           if (printed == GNUNET_NO) 
456             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
457                 count, name, value);
458         }
459       
460       if (0 == strcmp (name,"ATS duration"))
461         {
462           current.duration = value;
463           if (printed == GNUNET_NO) 
464             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "[%i] %s: %llu \n",
465                 count, name, value);
466         }
467       if (0 == strcmp (name,"ATS state"))
468         {
469           current.state = value;
470           const char * cont;
471           switch (value)
472             {
473             case ATS_NEW:
474               cont = "NEW";
475               break;
476             case ATS_COST_UPDATED:
477               cont = "C_UPDATED";
478               break;
479             case ATS_QUALITY_UPDATED:
480               cont = "Q_UPDATED";
481               break;
482             case ATS_QUALITY_COST_UPDATED:
483               cont = "QC_UPDATED";
484               break;
485             case ATS_UNMODIFIED:
486               cont = "UNMODIFIED";
487               break;
488             default:
489               GNUNET_break (0);
490               cont = "<undefined>";
491               break;
492             }
493           if (printed == GNUNET_NO) 
494             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
495                         "[%i] ATS state: %s\n", 
496                         count, 
497                         cont);
498         }
499     }
500   return GNUNET_OK;
501 }
502
503
504 static void
505 stats_get_task (void *cls,
506                 const struct GNUNET_SCHEDULER_TaskContext *tc)
507 {
508   stats_task = GNUNET_SCHEDULER_NO_TASK;
509   if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
510     return;
511   
512   s_time = GNUNET_STATISTICS_get (stats, "transport", "ATS timestamp",
513       TIMEOUT, NULL, &stats_cb, NULL);
514   s_solution = GNUNET_STATISTICS_get (stats, "transport", "ATS solution",
515       TIMEOUT, NULL, &stats_cb, NULL);
516   s_duration = GNUNET_STATISTICS_get (stats, "transport","ATS duration",
517       TIMEOUT, NULL, &stats_cb, NULL);
518   s_peers = GNUNET_STATISTICS_get (stats, "transport", "ATS peers",
519       TIMEOUT, NULL, &stats_cb, NULL);
520   s_mechs = GNUNET_STATISTICS_get (stats, "transport", "ATS mechanisms",
521       TIMEOUT, NULL, &stats_cb, NULL);
522   s_invalid = GNUNET_STATISTICS_get (stats, "transport", "ATS invalid solutions",
523       TIMEOUT, NULL, &stats_cb, NULL);
524   s_state = GNUNET_STATISTICS_get (stats, "transport", "ATS state",
525       TIMEOUT, NULL, &stats_cb, NULL);
526   
527   stats_task = GNUNET_SCHEDULER_add_delayed(
528       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100),
529       &stats_get_task,
530       NULL);
531 }
532
533
534 static void
535 delay (void *cls,
536        const struct GNUNET_SCHEDULER_TaskContext *tc)
537 {
538   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
539   if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
540     return;
541 #if VERBOSE
542   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
543               "Delay over\n");
544 #endif
545   shutdown_peers ();
546 }
547
548 static void
549 connect_peers()
550 {
551   shutdown_task = GNUNET_SCHEDULER_add_delayed(DELAY, &delay, NULL);
552 }
553
554
555 /* To make compiler happy */
556 void dummy(void)
557 {
558   struct ATS_quality_metric * q = qm;
559   q = NULL;
560   struct ATS_ressource * r = ressources;
561   r = NULL;
562 }
563
564 static size_t 
565 send_dummy_data_task (void *cls, size_t size, void *buf)
566 {
567   int s = sizeof (struct TestMessage);
568   struct TestMessage hdr;
569   
570   hdr.header.size = htons (s);
571   hdr.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ATS);
572   if (force_rebuild)
573     hdr.num = htonl (1);
574   else if (force_q_updates)
575     hdr.num = htonl (2);
576   else
577     hdr.num = htonl (0); 
578   memcpy (buf, &hdr, s);
579   // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sent bytes: %i of %i\n", s, s);
580   t = NULL;
581   return s;
582 }
583
584
585 static void 
586 send_task_f (void *cls,
587              const struct GNUNET_SCHEDULER_TaskContext *tc)
588 {
589   send_task = GNUNET_SCHEDULER_NO_TASK;
590   if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
591     return;
592   
593   if (t!=NULL)
594     {
595       GNUNET_TRANSPORT_notify_transmit_ready_cancel(t);
596       t = NULL;
597     }
598
599   if (send_msg == GNUNET_YES)
600     t = GNUNET_TRANSPORT_notify_transmit_ready(th, 
601                                                &master_deamon->id, 
602                                                sizeof (struct TestMessage), 0, 
603                                                SEND_TIMEOUT, 
604                                                &send_dummy_data_task, NULL);
605   send_task = GNUNET_SCHEDULER_add_delayed(
606       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,1000),
607       &send_task_f,
608       NULL);
609
610 }
611
612 static void
613 notify_connect (void *cls,
614                 const struct GNUNET_PeerIdentity *peer,
615                 const struct GNUNET_TRANSPORT_ATS_Information *ats,
616                 uint32_t ats_count)
617 {
618   send_task = GNUNET_SCHEDULER_add_now(&send_task_f, NULL);
619 }
620
621 static void
622 notify_disconnect (void *cls,
623                    const struct GNUNET_PeerIdentity *peer)
624 {
625   if (GNUNET_SCHEDULER_NO_TASK != send_task)
626     {
627       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628                   "Disconnect event before transmission request could be scheduled!\n");
629       GNUNET_SCHEDULER_cancel (send_task);
630       send_task = GNUNET_SCHEDULER_NO_TASK;
631     }
632   if (NULL != t)
633     {
634       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635                   "Disconnect event before transmission request could be completed!\n");
636       GNUNET_TRANSPORT_notify_transmit_ready_cancel (t);
637       t = NULL;
638     }
639 }
640
641 static void 
642 daemon_connect_cb(void *cls,
643                   const struct GNUNET_PeerIdentity *first,
644                   const struct GNUNET_PeerIdentity *second,
645                   uint32_t distance,
646                   const struct GNUNET_CONFIGURATION_Handle *first_cfg,
647                   const struct GNUNET_CONFIGURATION_Handle *second_cfg,
648                   struct GNUNET_TESTING_Daemon *first_daemon,
649                   struct GNUNET_TESTING_Daemon *second_daemon,
650                   const char *emsg)
651 {
652   char * firstc =  strdup(GNUNET_i2s(first));
653   char * secondc =  strdup(GNUNET_i2s(second));
654   connected++;
655   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656               "Connected peers `%s'<->`%s' (%i/%i)\n", 
657               firstc, secondc, connected, peers-1);
658   GNUNET_free(firstc);
659   GNUNET_free(secondc);
660   
661   if ( ( (first_daemon == ping_deamon) || 
662          (second_daemon == ping_deamon) ) && 
663        (master_deamon != NULL) && 
664        (ping_deamon != NULL) )
665     {
666       th = GNUNET_TRANSPORT_connect (ping_deamon->cfg,
667                                      &ping_deamon->id, 
668                                      NULL, NULL,
669                                      &notify_connect, 
670                                      &notify_disconnect);
671       force_q_updates = GNUNET_YES;
672       send_msg = GNUNET_YES;
673     }
674 }
675
676
677
678 static void
679 daemon_start_cb (void *cls,
680                  const struct GNUNET_PeerIdentity *id,
681                  const struct GNUNET_CONFIGURATION_Handle *cfg,
682                  struct GNUNET_TESTING_Daemon *d, const char *emsg)
683 {
684   if (id == NULL)
685     {
686       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687                   "Start callback called with error (too long starting peers), aborting test!\n");
688       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689                   "Error from testing: `%s'\n");
690       failed_peers++;
691       if (failed_peers == peers_left)
692         {
693           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694                       "Too many peers failed, ending test!\n");
695           ok = 1;
696         shutdown_peers ();
697         }
698       return;
699     }
700   peers_left--;
701
702   if (master_deamon == NULL)
703     {
704       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
705                   "Master peer `%s' '%s'\n",
706                   GNUNET_i2s(id), d->cfgfile);      
707       master_deamon = d;
708       stats = GNUNET_STATISTICS_create("transport", master_deamon->cfg);
709       GNUNET_assert (stats != NULL);
710       stats_task = GNUNET_SCHEDULER_add_now(&stats_get_task, NULL);
711     }
712   else
713     {
714       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
715                   "Connecting peer `%s'\n", 
716                   GNUNET_i2s(id), GNUNET_i2s(&master_deamon->id));
717       GNUNET_TESTING_daemons_connect(d,
718           master_deamon,
719           TIMEOUT,
720           0,
721           GNUNET_YES,
722           &daemon_connect_cb,
723           NULL);
724     }
725   
726   if (peers_left == 0)
727     {
728       if (ping_deamon == NULL)
729         {
730           ping_deamon = d;
731           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
732                       "Ping peer `%s' '%s'\n", GNUNET_i2s(id), d->cfgfile);
733         }
734       
735       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736                   "All peers started successfully!\n");
737       connect_peers();
738       ok = 0;
739     }
740   else if (failed_peers == peers_left)
741     {
742       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743                   "Too many peers failed, ending test!\n");
744       shutdown_peers();
745       ok = 1;
746     }
747 }
748
749
750 static void
751 run (void *cls,
752      char *const *args,
753      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
754 {
755   ok = 1;
756   measurement_started = GNUNET_NO;
757 #if VERBOSE
758   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %i peers.\n", peers);
759 #endif
760   peers_left = peers;
761   pg = GNUNET_TESTING_daemons_start (cfg,
762                                      peers_left, /* Total number of peers */
763                                      peers_left, /* Number of outstanding connections */
764                                      peers_left, /* Number of parallel ssh connections, or peers being started at once */
765                                      TIMEOUT,
766                                      NULL, NULL,
767                                      &daemon_start_cb, NULL, NULL, NULL, NULL);
768   GNUNET_assert (pg != NULL);
769 }
770
771
772 static int
773 check ()
774 {
775   char *const argv[] = { "test-testing",
776     "-c",
777     config_file,
778 #if VERBOSE
779     "-L", "DEBUG",
780 #endif
781     NULL
782   };
783   struct GNUNET_GETOPT_CommandLineOption options[] = {
784     GNUNET_GETOPT_OPTION_END
785   };
786   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
787                       argv, "test_transport_ats_multiple_peers", "nohelp",
788                       options, &run, &ok);
789   return ok;
790 }
791
792 #endif
793
794 int
795 main (int argc, char *argv[])
796 {
797   int ret = 0;
798
799   GNUNET_log_setup ("test-transport-ats-multiple-peers",
800 #if VERBOSE
801                     "DEBUG",
802 #else
803                     "INFO",
804 #endif
805                     NULL);
806
807 #if !HAVE_LIBGLPK
808   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
809               "GLPK not installed, exiting testcase\n");
810   return ret;
811 #else
812   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
813   machine_parsable = GNUNET_NO;
814   peers = NUM_PEERS;
815   config_file = "test_transport_ats_4addr.conf";
816
817   int c = 0;
818   if (argc >= 2)
819     {
820       for (c=0; c<argc; c++)
821         {
822           /* set peers */
823           if ((strcmp(argv[c], "-p") == 0) && c < (argc-1))
824             {
825               peers = atoi(argv[c+1]);
826               peers++;
827             }
828           /* set machine parsable */
829           if (strcmp(argv[c], "-m") == 0)
830             {
831               machine_parsable = GNUNET_YES;
832             }
833           /* set config file */
834           if ((strcmp(argv[c], "-c") == 0) && c < (argc-1))
835             {
836               config_file = argv[c+1];
837             }
838          }
839     }
840
841   ret = check ();
842   /**
843    * Still need to remove the base testing directory here,
844    * because group starts will create subdirectories under this
845    * main dir.  However, we no longer need to sleep, as the
846    * shutdown sequence won't return until everything is cleaned
847    * up.
848    */
849   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
850   return ret;
851 #endif
852 }
853
854 /* end of test_transport_ats_multiple_peers.c*/