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