8e02a55f809895689a9fb033fd4af8f74bcb00a0
[oweals/gnunet.git] / src / testbed / gnunet-testbed-profiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 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 testbed/gnunet-testbed-profiler.c
23  * @brief Profiling driver for the testbed.
24  * @author Sree Harsha Totakura <sreeharsha@totakura.in> 
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_testbed_service.h"
30 #include "testbed_api_hosts.h"
31
32 /**
33  * Generic loggins shorthand
34  */
35 #define LOG(kind,...)                                           \
36   GNUNET_log (kind, __VA_ARGS__)
37
38
39 /**
40  * DLL of operations
41  */
42 struct DLLOperation
43 {
44   /**
45    * The testbed operation handle
46    */
47   struct GNUNET_TESTBED_Operation *op;
48
49   /**
50    * Closure
51    */
52   void *cls;
53
54   /**
55    * The next pointer for DLL
56    */
57   struct DLLOperation *next;
58
59   /**
60    * The prev pointer for DLL
61    */
62   struct DLLOperation *prev;
63 };
64
65
66 /**
67  * Availanle states during profiling
68  */
69 enum State
70 {
71   /**
72    * Initial state
73    */
74   STATE_INIT = 0,
75
76   /**
77    * Starting slaves
78    */
79   STATE_SLAVES_STARTING,
80
81   /**
82    * Creating peers
83    */
84   STATE_PEERS_CREATING,
85
86   /**
87    * Starting peers
88    */
89   STATE_PEERS_STARTING,
90
91   /**
92    * Linking peers
93    */
94   STATE_PEERS_LINKING,
95
96   /**
97    * Destroying peers; we can do this as the controller takes care of stopping a
98    * peer if it is running
99    */
100   STATE_PEERS_DESTROYING
101 };
102
103
104 /**
105  * An array of hosts loaded from the hostkeys file
106  */
107 static struct GNUNET_TESTBED_Host **hosts;
108
109 /**
110  * The array of peers; we fill this as the peers are given to us by the testbed
111  */
112 static struct GNUNET_TESTBED_Peer **peers;
113
114 /* /\** */
115 /*  * Operation handle */
116 /*  *\/ */
117 /* static struct GNUNET_TESTBED_Operation *op; */
118
119 /**
120  * Host registration handle
121  */
122 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
123
124 /**
125  * Handle to the master controller process
126  */
127 struct GNUNET_TESTBED_ControllerProc *mc_proc;
128
129 /**
130  * Handle to the master controller
131  */
132 struct GNUNET_TESTBED_Controller *mc;
133
134 /**
135  * Handle to global configuration
136  */
137 struct GNUNET_CONFIGURATION_Handle *cfg;
138
139 /**
140  * Head of the operations list
141  */
142 struct DLLOperation *dll_op_head;
143
144 /**
145  * Tail of the operations list
146  */
147 struct DLLOperation *dll_op_tail;
148
149 /**
150  * Peer linking - topology operation
151  */
152 struct GNUNET_TESTBED_Operation *topology_op;
153
154 /**
155  * Abort task identifier
156  */
157 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
158
159 /**
160  * Shutdown task identifier
161  */
162 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
163
164 /**
165  * Host registration task identifier
166  */
167 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
168
169 /**
170  * Global event mask for all testbed events
171  */
172 uint64_t event_mask;
173
174 /**
175  * The starting time of a profiling step
176  */
177 struct GNUNET_TIME_Absolute prof_start_time;
178
179 /**
180  * Duration profiling step has taken
181  */
182 struct GNUNET_TIME_Relative prof_time;
183
184 /**
185  * Current peer id
186  */
187 unsigned int peer_id;
188
189 /**
190  * Number of peers to be started by the profiler
191  */
192 static unsigned int num_peers;
193
194 /**
195  * Number of hosts in the hosts array
196  */
197 static unsigned int num_hosts;
198
199 /**
200  * Number of random links to be established between peers
201  */
202 static unsigned int num_links;
203
204 /**
205  * Number of timeout failures to tolerate
206  */
207 static unsigned int num_cont_fails;
208
209 /**
210  * Continuous failures during overlay connect operations
211  */
212 static unsigned int cont_fails;
213
214 /**
215  * Links which are successfully established
216  */
217 static unsigned int established_links;
218
219 /**
220  * Links which are not successfully established
221  */
222 static unsigned int failed_links;
223
224 /**
225  * Global testing status
226  */
227 static int result;
228
229 /**
230  * current state of profiling
231  */
232 enum State state;
233
234 /**
235  * The topology we want to acheive
236  */
237 enum GNUNET_TESTBED_TopologyOption topology;
238
239
240 /**
241  * Shutdown nicely
242  *
243  * @param cls NULL
244  * @param tc the task context
245  */
246 static void
247 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
248 {
249   struct DLLOperation *dll_op;
250   unsigned int nhost;
251
252   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
253   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
254     GNUNET_SCHEDULER_cancel (abort_task);
255   if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
256     GNUNET_SCHEDULER_cancel (register_hosts_task);
257   if (NULL != reg_handle)
258     GNUNET_TESTBED_cancel_registration (reg_handle);
259   if (NULL != topology_op)
260     GNUNET_TESTBED_operation_cancel (topology_op);
261   for (nhost = 0; nhost < num_hosts; nhost++)
262     if (NULL != hosts[nhost])
263       GNUNET_TESTBED_host_destroy (hosts[nhost]);
264   GNUNET_free_non_null (hosts);
265   while (NULL != (dll_op = dll_op_head))
266   {
267     GNUNET_TESTBED_operation_cancel (dll_op->op);
268     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
269     GNUNET_free (dll_op);
270   }
271   if (NULL != mc)
272     GNUNET_TESTBED_controller_disconnect (mc);
273   if (NULL != mc_proc)
274     GNUNET_TESTBED_controller_stop (mc_proc);
275   if (NULL != cfg)
276     GNUNET_CONFIGURATION_destroy (cfg);
277   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
278 }
279
280
281 /**
282  * abort task to run on test timed out
283  *
284  * @param cls NULL
285  * @param tc the task context
286  */
287 static void
288 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
289 {
290   LOG (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
291   abort_task = GNUNET_SCHEDULER_NO_TASK;
292   result = GNUNET_SYSERR;
293   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
294     GNUNET_SCHEDULER_cancel (shutdown_task);
295   shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
296 }
297
298
299
300
301
302 /**
303  * Functions of this signature are called when a peer has been successfully
304  * started or stopped.
305  *
306  * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
307  * @param emsg NULL on success; otherwise an error description
308  */
309 static void 
310 peer_churn_cb (void *cls, const char *emsg)
311 {
312   struct DLLOperation *dll_op = cls;
313   struct GNUNET_TESTBED_Operation *op;  
314   static unsigned int started_peers;
315
316   op = dll_op->op;
317   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
318   GNUNET_free (dll_op);
319   if (NULL != emsg)
320   {
321     LOG (GNUNET_ERROR_TYPE_WARNING,
322          _("An operation has failed while starting peers\n"));
323     GNUNET_TESTBED_operation_done (op);
324     GNUNET_SCHEDULER_cancel (abort_task);
325     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
326     return;
327   }
328   GNUNET_TESTBED_operation_done (op);
329   if (++started_peers == num_peers)
330   {
331     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
332     printf ("%u peers started successfully in %.2f seconds\n",
333             num_peers, ((double) prof_time.rel_value) / 1000.00);
334     fflush (stdout);
335     result = GNUNET_OK;
336     if (0 == num_links)
337     {      
338       shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
339       return;
340     }
341     state = STATE_PEERS_LINKING;
342     /* Do overlay connect */
343     prof_start_time = GNUNET_TIME_absolute_get ();
344     switch (topology)
345     {
346     case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
347       topology_op =
348           GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peers,
349                                                      topology,
350                                                      num_links,
351                                                      GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
352                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
353       break;
354     case GNUNET_TESTBED_TOPOLOGY_CLIQUE:
355       topology_op =
356           GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peers,
357                                                      topology,
358                                                      GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
359                                                      GNUNET_TESTBED_TOPOLOGY_OPTION_END);
360       break;
361     default:
362       GNUNET_assert (0);
363     }
364   }
365 }
366
367
368 /**
369  * Functions of this signature are called when a peer has been successfully
370  * created
371  *
372  * @param cls the closure from GNUNET_TESTBED_peer_create()
373  * @param peer the handle for the created peer; NULL on any error during
374  *          creation
375  * @param emsg NULL if peer is not NULL; else MAY contain the error description
376  */
377 static void 
378 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
379 {
380   struct DLLOperation *dll_op = cls;
381   struct GNUNET_TESTBED_Peer **peer_ptr;
382   static unsigned int created_peers;
383   unsigned int peer_cnt;
384
385   if (NULL != emsg)
386   {
387     LOG (GNUNET_ERROR_TYPE_WARNING,
388          _("Creating a peer failed. Error: %s\n"), emsg);
389     GNUNET_TESTBED_operation_done (dll_op->op);
390     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
391     GNUNET_free (dll_op);
392     GNUNET_SCHEDULER_cancel (abort_task);
393     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
394     return;
395   }
396   peer_ptr = dll_op->cls;
397   GNUNET_assert (NULL == *peer_ptr);
398   *peer_ptr = peer;
399   GNUNET_TESTBED_operation_done (dll_op->op);
400   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
401   GNUNET_free (dll_op);
402   if (++created_peers == num_peers)
403   {
404     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);    
405     printf ("%u peers created successfully in %.2f seconds\n",
406             num_peers, ((double) prof_time.rel_value) / 1000.00);
407     fflush (stdout);
408     /* Now peers are to be started */
409     state = STATE_PEERS_STARTING;
410     prof_start_time = GNUNET_TIME_absolute_get ();
411     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
412     {
413       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
414       dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt], 
415                                               &peer_churn_cb, dll_op);
416       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
417     }
418   }
419 }
420
421
422 /**
423  * Function to print summary about how many overlay links we have made and how
424  * many failed
425  */
426 static void
427 print_overlay_links_summary ()
428 {
429   prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
430   printf ("\n%u links established in %.2f seconds\n",
431           established_links, ((double) prof_time.rel_value) / 1000.00);
432   printf ("%u links failed due to timeouts\n", failed_links);
433 }
434
435
436 /**
437  * Function to start peers
438  */
439 static void
440 start_peers ()
441 {
442   struct DLLOperation *dll_op;
443   unsigned int peer_cnt;
444   
445   state = STATE_PEERS_CREATING;
446   prof_start_time = GNUNET_TIME_absolute_get ();
447   peers = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *)
448                          * num_peers);
449   for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
450   {
451     dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
452     dll_op->cls = &peers[peer_cnt];
453     dll_op->op = GNUNET_TESTBED_peer_create (mc,
454                                              hosts
455                                              [peer_cnt % num_hosts],
456                                              cfg,
457                                              &peer_create_cb,
458                                              dll_op);
459     GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
460   }
461 }
462
463
464 /**
465  * Controller event callback
466  *
467  * @param cls NULL
468  * @param event the controller event
469  */
470 static void
471 controller_event_cb (void *cls,
472                      const struct GNUNET_TESTBED_EventInformation *event)
473 {
474   struct DLLOperation *dll_op;
475   struct GNUNET_TESTBED_Operation *op;
476
477   switch (state)
478   {
479   case STATE_SLAVES_STARTING:
480     switch (event->type)
481     {
482     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
483       {
484         static unsigned int slaves_started;
485         
486         dll_op = event->details.operation_finished.op_cls;
487         GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
488         GNUNET_free (dll_op);
489         op = event->details.operation_finished.operation;
490         if (NULL != event->details.operation_finished.emsg)
491         {
492           LOG (GNUNET_ERROR_TYPE_WARNING,
493                _("An operation has failed while starting slaves\n"));
494           GNUNET_TESTBED_operation_done (op);
495           GNUNET_SCHEDULER_cancel (abort_task);
496           abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
497           return;
498         }
499         GNUNET_TESTBED_operation_done (op);
500         /* Proceed to start peers */
501         if (++slaves_started == num_hosts - 1)
502         {
503           printf ("%u controllers started successfully\n", num_hosts);
504           fflush (stdout);
505           start_peers ();
506         }
507       }
508       break;
509     default:
510       GNUNET_assert (0);
511     }
512     break;
513   case STATE_PEERS_STARTING:
514     switch (event->type)
515     {
516     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
517       /* Control reaches here when peer start fails */
518     case GNUNET_TESTBED_ET_PEER_START:
519       /* we handle peer starts in peer_churn_cb */
520       break;
521     default:
522       GNUNET_assert (0);
523     }
524     break;
525   case STATE_PEERS_LINKING:
526    switch (event->type)
527     {
528     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
529       /* Control reaches here when a peer linking operation fails */
530       if (NULL != event->details.operation_finished.emsg)
531       {
532         printf ("F");
533         fflush (stdout);
534         failed_links++;
535         if (++cont_fails > num_cont_fails)
536         {
537           printf ("\nAborting due to very high failure rate");
538           print_overlay_links_summary ();         
539           GNUNET_SCHEDULER_cancel (abort_task);
540           abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
541         }
542       }
543       break;
544     case GNUNET_TESTBED_ET_CONNECT:
545       {
546         if (0 != cont_fails)
547           cont_fails--;
548         if (0 == established_links)
549           printf ("Establishing links. Please wait\n");
550         printf (".");
551         fflush (stdout);
552         established_links++;
553         if ((established_links + failed_links) == 
554             (GNUNET_TESTBED_TOPOLOGY_CLIQUE == topology ? 
555              num_peers * (num_peers -1) : num_links))
556         {
557           print_overlay_links_summary ();
558           result = GNUNET_OK;
559           shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
560         }
561       }
562       break;
563     default:
564       GNUNET_assert (0);
565     }
566     break;
567   default:
568     GNUNET_assert (0);
569   }
570 }
571
572
573 /**
574  * Task to register all hosts available in the global host list
575  *
576  * @param cls NULL
577  * @param tc the scheduler task context
578  */
579 static void
580 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
581
582
583 /**
584  * Callback which will be called to after a host registration succeeded or failed
585  *
586  * @param cls the closure
587  * @param emsg the error message; NULL if host registration is successful
588  */
589 static void
590 host_registration_completion (void *cls, const char *emsg)
591 {
592   reg_handle = NULL;
593   if (NULL != emsg)
594   {
595     LOG (GNUNET_ERROR_TYPE_WARNING,
596          _("Host registration failed for a host. Error: %s\n"), emsg);
597     GNUNET_SCHEDULER_cancel (abort_task);
598     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
599     return;
600   }
601   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
602 }
603
604
605 /**
606  * Task to register all hosts available in the global host list
607  *
608  * @param cls NULL
609  * @param tc the scheduler task context
610  */
611 static void
612 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
613 {
614   struct DLLOperation *dll_op;
615   static unsigned int reg_host;
616   unsigned int slave;
617
618   register_hosts_task = GNUNET_SCHEDULER_NO_TASK;  
619   if (reg_host == num_hosts - 1)
620   {
621     LOG (GNUNET_ERROR_TYPE_DEBUG,
622          "All hosts successfully registered\n");
623     /* Start slaves */
624     state = STATE_SLAVES_STARTING;
625     for (slave = 1; slave < num_hosts; slave++)
626     {
627       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
628       dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
629                                                    mc,
630                                                    hosts[slave],
631                                                    hosts[0],
632                                                    cfg,
633                                                    GNUNET_YES);
634       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
635     }
636     return;
637   }
638   reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
639                                              host_registration_completion,
640                                              NULL);
641 }
642
643
644 /**
645  * Callback to signal successfull startup of the controller process
646  *
647  * @param cls the closure from GNUNET_TESTBED_controller_start()
648  * @param config the configuration with which the controller has been started;
649  *          NULL if status is not GNUNET_OK
650  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
651  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
652  */
653 static void
654 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
655 {
656   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
657     GNUNET_SCHEDULER_cancel (abort_task);
658   if (GNUNET_OK != status)
659   {
660     mc_proc = NULL;
661     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
662     return;
663   }
664   event_mask = 0;
665   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
666   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
667   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
668   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
669   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
670   mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
671                                           &controller_event_cb, NULL);
672   if (NULL == mc)
673   {
674     LOG (GNUNET_ERROR_TYPE_WARNING,
675          _("Unable to connect to master controller -- Check config\n"));
676     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
677     return;
678   }
679   if (num_hosts > 1)
680     register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
681   else
682     start_peers ();
683   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
684                                              &do_abort, NULL);
685 }
686
687
688 /**
689  * Main function that will be run by the scheduler.
690  *
691  * @param cls closure
692  * @param args remaining command-line arguments
693  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
694  * @param config configuration
695  */
696 static void
697 run (void *cls, char *const *args, const char *cfgfile,
698      const struct GNUNET_CONFIGURATION_Handle *config)
699 {
700   unsigned int nhost;
701
702   if (NULL == args[0])
703   {
704     fprintf (stderr, _("No hosts-file specified on command line\n"));
705     return;
706   }
707   if (0 == num_peers)
708   {
709     result = GNUNET_OK;
710     return;
711   }
712   num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
713   if (0 == num_hosts)
714   {
715     fprintf (stderr, _("No hosts loaded. Need atleast one host\n"));
716     return;
717   }
718   for (nhost = 0; nhost < num_hosts; nhost++)
719   {
720     if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
721     {
722       fprintf (stderr, _("Host %s cannot start testbed\n"),
723                GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
724       break;
725     }
726   }
727   if (num_hosts != nhost)
728   {
729     fprintf (stderr, _("Exiting\n"));
730     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
731     return;
732   }
733   cfg = GNUNET_CONFIGURATION_dup (config);
734   mc_proc = 
735       GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname_ 
736                                        (hosts[0]),
737                                        hosts[0],
738                                        cfg,
739                                        status_cb,
740                                        NULL);
741   abort_task =
742       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
743                                     (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
744                                     NULL);
745 }
746
747
748 /**
749  * Set an option of type 'char *' from the command line.
750  * A pointer to this function should be passed as part of the
751  * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
752  * of this type.  It should be followed by a pointer to a value of
753  * type 'char *'.
754  *
755  * @param ctx command line processing context
756  * @param scls additional closure (will point to the 'char *',
757  *             which will be allocated)
758  * @param option name of the option
759  * @param value actual value of the option (a string)
760  * @return GNUNET_OK to continue procesing; GNUNET_SYSERR to signal error
761  */
762 int
763 set_topology (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
764               void *scls, const char *option, const char *value)
765 {
766   enum GNUNET_TESTBED_TopologyOption *val = scls;
767
768   if (0 == strncasecmp ("CLIQUE", value, strlen ("CLIQUE")))
769   {  
770     *val = GNUNET_TESTBED_TOPOLOGY_CLIQUE;
771     return GNUNET_OK;
772   }
773   if (0 == strncasecmp ("RANDOM", value, strlen ("RANDOM")))
774   {  
775     *val = GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI;
776     return GNUNET_OK;
777   }
778   FPRINTF (stderr, _("Only `CLIQUE' and `RANDOM' are permitted"));
779   return GNUNET_SYSERR;
780 }
781
782
783 /**
784  * Main function.
785  *
786  * @return 0 on success
787  */
788 int
789 main (int argc, char *const *argv)
790 {
791   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
792     { 'p', "num-peers", "COUNT",
793       gettext_noop ("create COUNT number of peers"),
794       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
795     { 'n', "num-links", "COUNT",
796       gettext_noop ("create COUNT number of random links"),
797       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
798     { 'e', "num-errors", "COUNT",
799       gettext_noop ("tolerate COUNT number of continious timeout failures"),
800       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails },
801     { 't', "topology", "TOPOLOGY",
802       gettext_noop ("Try to acheive TOPOLOGY. This options takes either CLIQUE "
803                     "or RANDOM. For CLIQUE the parameter -n is ignored. The "
804                     "default is to acheive a random graph topology."),
805       GNUNET_YES, &GNUNET_GETOPT_set_string, &topology },
806     GNUNET_GETOPT_OPTION_END
807   };
808   int ret;
809
810   topology = GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI;
811   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
812     return 2;
813   
814   result = GNUNET_SYSERR;
815   ret =
816       GNUNET_PROGRAM_run (argc, argv, "gnunet-testbed-profiler [OPTIONS] hosts-file",
817                           _("Profiler for testbed"),
818                           options, &run, NULL);
819   if (GNUNET_OK != ret)
820     return ret;
821   if (GNUNET_OK != result)
822     return 1;
823   return 0;
824 }