mesh connections in regex profiler
[oweals/gnunet.git] / src / mesh / gnunet-regex-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 mesh/gnunet-regex-profiler.c
23  * @brief Regex profiler for testing distributed regex use.
24  * @author Bart Polot
25  * @author Max Szengel
26  */
27
28 #include <string.h>
29
30 #include "platform.h"
31 #include "gnunet_applications.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_mesh_service.h"
34 #include "gnunet_stream_lib.h"
35 #include "gnunet_testbed_service.h"
36
37 /**
38  * DLL of operations
39  */
40 struct DLLOperation
41 {
42   /**
43    * The testbed operation handle
44    */
45   struct GNUNET_TESTBED_Operation *op;
46
47   /**
48    * Closure
49    */
50   void *cls;
51
52   /**
53    * The next pointer for DLL
54    */
55   struct DLLOperation *next;
56
57   /**
58    * The prev pointer for DLL
59    */
60   struct DLLOperation *prev;
61 };
62
63
64 /**
65  * Available states during profiling
66  */
67 enum State
68 {
69   /**
70    * Initial state
71    */
72   STATE_INIT = 0,
73
74   /**
75    * Starting slaves
76    */
77   STATE_SLAVES_STARTING,
78
79   /**
80    * Creating peers
81    */
82   STATE_PEERS_CREATING,
83
84   /**
85    * Starting peers
86    */
87   STATE_PEERS_STARTING,
88
89   /**
90    * Linking peers
91    */
92   STATE_PEERS_LINKING,
93
94   /**
95    * Destroying peers; we can do this as the controller takes care of stopping a
96    * peer if it is running
97    */
98   STATE_PEERS_DESTROYING
99 };
100
101
102 /**
103  * An array of hosts loaded from the hostkeys file
104  */
105 static struct GNUNET_TESTBED_Host **hosts;
106
107 /**
108  * Peer handles.
109  */
110 struct Peer
111 {
112   /**
113    * The actual testbed peer handle.
114    */
115   struct GNUNET_TESTBED_Peer *peer_handle;
116
117   /**
118    * Peer's mesh handle.
119    */
120   struct GNUNET_MESH_Handle *mesh_handle;
121
122   /**
123    * Host on which the peer is running.
124    */
125   struct GNUNET_TESTBED_Host *host_handle;
126
127   /**
128    * Testbed operation handle.
129    */
130   struct GNUNET_TESTBED_Operation *op_handle;
131 };
132
133 /**
134  * Array of peer handles used to pass to
135  * GNUNET_TESTBED_overlay_configure_topology
136  */
137 struct GNUNET_TESTBED_Peer **peer_handles;
138
139 /**
140  * The array of peers; we fill this as the peers are given to us by the testbed
141  */
142 static struct Peer *peers;
143
144 /**
145  * Host registration handle
146  */
147 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
148
149 /**
150  * Handle to the master controller process
151  */
152 struct GNUNET_TESTBED_ControllerProc *mc_proc;
153
154 /**
155  * Handle to the master controller
156  */
157 struct GNUNET_TESTBED_Controller *mc;
158
159 /**
160  * Handle to global configuration
161  */
162 struct GNUNET_CONFIGURATION_Handle *cfg;
163
164 /**
165  * Head of the operations list
166  */
167 struct DLLOperation *dll_op_head;
168
169 /**
170  * Tail of the operations list
171  */
172 struct DLLOperation *dll_op_tail;
173
174 /**
175  * Peer linking - topology operation
176  */
177 struct GNUNET_TESTBED_Operation *topology_op;
178
179 /**
180  * Abort task identifier
181  */
182 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
183
184 /**
185  * Host registration task identifier
186  */
187 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
188
189 /**
190  * Global event mask for all testbed events
191  */
192 uint64_t event_mask;
193
194 /**
195  * The starting time of a profiling step
196  */
197 struct GNUNET_TIME_Absolute prof_start_time;
198
199 /**
200  * Duration profiling step has taken
201  */
202 struct GNUNET_TIME_Relative prof_time;
203
204 /**
205  * Current peer id
206  */
207 unsigned int peer_id;
208
209 /**
210  * Number of peers to be started by the profiler
211  */
212 static unsigned int num_peers;
213
214 /**
215  * Number of hosts in the hosts array
216  */
217 static unsigned int num_hosts;
218
219 /**
220  * Number of random links to be established between peers
221  */
222 static unsigned int num_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 /**
236  * Shutdown nicely
237  *
238  * @param cls NULL
239  * @param tc the task context
240  */
241 static void
242 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
243 {
244   struct DLLOperation *dll_op;
245   unsigned int nhost;
246
247   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
248     GNUNET_SCHEDULER_cancel (abort_task);
249   if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
250     GNUNET_SCHEDULER_cancel (register_hosts_task);
251   if (NULL != reg_handle)
252     GNUNET_TESTBED_cancel_registration (reg_handle);
253   if (NULL != topology_op)
254     GNUNET_TESTBED_operation_cancel (topology_op);
255   for (nhost = 0; nhost < num_hosts; nhost++)
256     if (NULL != hosts[nhost])
257       GNUNET_TESTBED_host_destroy (hosts[nhost]);
258   GNUNET_free_non_null (hosts);
259   while (NULL != (dll_op = dll_op_head))
260   {
261     GNUNET_TESTBED_operation_cancel (dll_op->op);
262     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
263     GNUNET_free (dll_op);
264   }
265   if (NULL != mc)
266     GNUNET_TESTBED_controller_disconnect (mc);
267   if (NULL != mc_proc)
268     GNUNET_TESTBED_controller_stop (mc_proc);
269   if (NULL != cfg)
270     GNUNET_CONFIGURATION_destroy (cfg);
271
272   //FIXME       GNUNET_MESH_disconnect (peers[i].mesh_handle);
273
274   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
275 }
276
277
278 /**
279  * abort task to run on test timed out
280  *
281  * @param cls NULL
282  * @param tc the task context
283  */
284 static void
285 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
286 {
287   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
288   abort_task = GNUNET_SCHEDULER_NO_TASK;
289   result = GNUNET_SYSERR;
290   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
291 }
292
293
294 /**
295  * Method called whenever another peer has added us to a tunnel
296  * the other peer initiated.
297  * Only called (once) upon reception of data with a message type which was
298  * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
299  * causes te tunnel to be ignored and no further notifications are sent about
300  * the same tunnel.
301  *
302  * @param cls closure
303  * @param tunnel new handle to the tunnel
304  * @param initiator peer that started the tunnel
305  * @param atsi performance information for the tunnel
306  * @return initial tunnel context for the tunnel
307  *         (can be NULL -- that's not an error)
308  */
309 void *
310 mesh_inbound_tunnel_handler (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
311                              const struct GNUNET_PeerIdentity *initiator,
312                              const struct GNUNET_ATS_Information *atsi)
313 {
314   return NULL;
315 }
316
317
318 /**
319  * Function called whenever an inbound tunnel is destroyed.  Should clean up
320  * any associated state.  This function is NOT called if the client has
321  * explicitly asked for the tunnel to be destroyed using
322  * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
323  * the tunnel.
324  *
325  * @param cls closure (set from GNUNET_MESH_connect)
326  * @param tunnel connection to the other end (henceforth invalid)
327  * @param tunnel_ctx place where local state associated
328  *                   with the tunnel is stored
329  */
330 void
331 mesh_tunnel_end_handler (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
332                          void *tunnel_ctx)
333 {
334
335 }
336
337
338 /**
339  * Mesh connect callback.
340  *
341  * @param cls internal peer id.
342  * @param op operation handle.
343  * @param ca_result connect adapter result.
344  * @param emsg error message.
345  */
346 void
347 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
348                  void *ca_result, const char *emsg)
349 {
350   static unsigned int connected_mesh_handles;
351   struct Peer *peer = (struct Peer *) cls;
352   unsigned int peer_cnt;
353
354   if (NULL != emsg)
355   {
356     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Mesh connect failed: %s\n", emsg);
357     GNUNET_assert (0);
358   }
359
360   GNUNET_assert (peer->op_handle == op);
361   GNUNET_assert (peer->mesh_handle == ca_result);
362
363   //  GNUNET_TESTBED_operation_done (op);
364
365   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh connect callback for peer\n");
366
367   if (++connected_mesh_handles == num_peers)
368   {
369     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All mesh handles connected\n");
370
371     // TODO announce regexes...
372
373     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
374     {
375       GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
376     }
377   }
378   
379 }
380
381
382 /**
383  * Mesh connect adapter.
384  *
385  * @param cls not used.
386  * @param cfg configuration handle.
387  *
388  * @return
389  */
390 void *
391 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
392 {
393   struct Peer *peer = (struct Peer *) cls;
394
395   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh connect adapter\n");
396
397   static struct GNUNET_MESH_MessageHandler handlers[] = {
398     {NULL, 0, 0}
399   };
400
401   static GNUNET_MESH_ApplicationType apptypes[] = {
402     GNUNET_APPLICATION_TYPE_END
403   };
404
405   peer->mesh_handle =
406       GNUNET_MESH_connect (cfg, cls, &mesh_inbound_tunnel_handler,
407                            &mesh_tunnel_end_handler, handlers, apptypes);
408
409   return peer->mesh_handle;
410 }
411
412
413 /**
414  * Adapter function called to destroy a connection to
415  * the mesh service
416  *
417  * @param cls closure
418  * @param op_result service handle returned from the connect adapter
419  */
420 void
421 mesh_da (void *cls, void *op_result)
422 {
423   static unsigned int disconnected_mesh_handles;
424   struct Peer *peer = (struct Peer *) cls;
425
426   GNUNET_assert (peer->mesh_handle == op_result);
427
428   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh disconnect adapter\n");
429
430   GNUNET_MESH_disconnect (peer->mesh_handle);
431   peer->mesh_handle = NULL;
432
433   if (++disconnected_mesh_handles == num_peers)
434   {
435     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
436   }
437 }
438
439
440 /**
441  * Functions of this signature are called when a peer has been successfully
442  * started or stopped.
443  *
444  * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
445  * @param emsg NULL on success; otherwise an error description
446  */
447 static void
448 peer_churn_cb (void *cls, const char *emsg)
449 {
450   struct DLLOperation *dll_op = cls;
451   struct GNUNET_TESTBED_Operation *op;
452   static unsigned int started_peers;
453   unsigned int peer_cnt;
454
455   op = dll_op->op;
456   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
457   GNUNET_free (dll_op);
458   if (NULL != emsg)
459   {
460     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
461          _("An operation has failed while starting peers\n"));
462     GNUNET_TESTBED_operation_done (op);
463     GNUNET_SCHEDULER_cancel (abort_task);
464     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
465     return;
466   }
467   GNUNET_TESTBED_operation_done (op);
468   if (++started_peers == num_peers)
469   {
470     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
471     printf ("All peers started successfully in %.2f seconds\n",
472             ((double) prof_time.rel_value) / 1000.00);
473     result = GNUNET_OK;
474     if (0 == num_links)
475     {
476       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
477       return;
478     }
479
480     peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
481     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
482       peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
483
484     state = STATE_PEERS_LINKING;
485     /* Do overlay connect */
486     prof_start_time = GNUNET_TIME_absolute_get ();
487     topology_op =
488         GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
489                                                    GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
490                                                    num_links);
491     if (NULL == topology_op)
492     {
493       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
494                   "Cannot create topology, op handle was NULL\n");
495       GNUNET_assert (0);
496     }
497   }
498 }
499
500
501 /**
502  * Functions of this signature are called when a peer has been successfully
503  * created
504  *
505  * @param cls the closure from GNUNET_TESTBED_peer_create()
506  * @param peer the handle for the created peer; NULL on any error during
507  *          creation
508  * @param emsg NULL if peer is not NULL; else MAY contain the error description
509  */
510 static void
511 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
512 {
513   struct DLLOperation *dll_op = cls;
514   struct Peer *peer_ptr;
515   static unsigned int created_peers;
516   unsigned int peer_cnt;
517
518   if (NULL != emsg)
519   {
520     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
521          _("Creating a peer failed. Error: %s\n"), emsg);
522     GNUNET_TESTBED_operation_done (dll_op->op);
523     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
524     GNUNET_free (dll_op);
525     GNUNET_SCHEDULER_cancel (abort_task);
526     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
527     return;
528   }
529
530   peer_ptr = dll_op->cls;
531   GNUNET_assert (NULL == peer_ptr->peer_handle);
532   peer_ptr->peer_handle = peer;
533   GNUNET_TESTBED_operation_done (dll_op->op);
534   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
535   GNUNET_free (dll_op);
536
537   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
538               created_peers,
539               GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
540
541   if (++created_peers == num_peers)
542   {
543     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
544     printf ("All peers created successfully in %.2f seconds\n",
545             ((double) prof_time.rel_value) / 1000.00);
546     /* Now peers are to be started */
547     state = STATE_PEERS_STARTING;
548     prof_start_time = GNUNET_TIME_absolute_get ();
549     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
550     {
551       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
552       dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt].peer_handle,
553                                               &peer_churn_cb, dll_op);
554       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
555     }
556   }
557 }
558
559
560 /**
561  * Controller event callback
562  *
563  * @param cls NULL
564  * @param event the controller event
565  */
566 static void
567 controller_event_cb (void *cls,
568                      const struct GNUNET_TESTBED_EventInformation *event)
569 {
570   struct DLLOperation *dll_op;
571   struct GNUNET_TESTBED_Operation *op;
572
573   switch (state)
574   {
575   case STATE_SLAVES_STARTING:
576     switch (event->type)
577     {
578     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
579       {
580         static unsigned int slaves_started;
581         unsigned int peer_cnt;
582
583         dll_op = event->details.operation_finished.op_cls;
584         GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
585         GNUNET_free (dll_op);
586         op = event->details.operation_finished.operation;
587         if (NULL != event->details.operation_finished.emsg)
588         {
589           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
590                _("An operation has failed while starting slaves\n"));
591           GNUNET_TESTBED_operation_done (op);
592           GNUNET_SCHEDULER_cancel (abort_task);
593           abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
594           return;
595         }
596         GNUNET_TESTBED_operation_done (op);
597         /* Proceed to start peers */
598         if (++slaves_started == num_hosts - 1)
599         {
600           printf ("All slaves started successfully\n");
601           state = STATE_PEERS_CREATING;
602           prof_start_time = GNUNET_TIME_absolute_get ();
603           peers = GNUNET_malloc (sizeof (struct Peer) * num_peers);
604           for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
605           {
606             GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating peer %i on host %s\n",
607                         peer_cnt,
608                         GNUNET_TESTBED_host_get_hostname (hosts[peer_cnt % num_hosts]));
609
610             peers[peer_cnt].host_handle = hosts[peer_cnt % num_hosts];
611
612             dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
613             dll_op->cls = &peers[peer_cnt];
614             dll_op->op = GNUNET_TESTBED_peer_create (mc,
615                                                      hosts[peer_cnt % num_hosts],
616                                                      cfg,
617                                                      &peer_create_cb,
618                                                      dll_op);
619             GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
620           }
621         }
622       }
623       break;
624     default:
625       GNUNET_assert (0);
626     }
627     break;
628   case STATE_PEERS_STARTING:
629     switch (event->type)
630     {
631     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
632       /* Control reaches here when peer start fails */
633     case GNUNET_TESTBED_ET_PEER_START:
634       /* we handle peer starts in peer_churn_cb */
635       break;
636     default:
637       GNUNET_assert (0);
638     }
639     break;
640   case STATE_PEERS_LINKING:
641    switch (event->type)
642     {
643     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
644       /* Control reaches here when a peer linking operation fails */
645       if (NULL != event->details.operation_finished.emsg)
646       {
647         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
648              _("An operation has failed while linking\n"));
649         GNUNET_SCHEDULER_cancel (abort_task);
650         abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
651       }
652       break;
653     case GNUNET_TESTBED_ET_CONNECT:
654       {
655         static unsigned int established_links;
656         unsigned int peer_cnt;
657
658         if (0 == established_links)
659           printf ("Establishing links\n .");
660         else
661         {
662           printf (".");
663           fflush (stdout);
664         }
665         if (++established_links == num_links)
666         {
667           prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
668           printf ("\n%u links established in %.2f seconds\n",
669                   num_links, ((double) prof_time.rel_value) / 1000.00);
670           result = GNUNET_OK;
671           GNUNET_free (peer_handles);
672           printf ("\nConnecting to mesh service...\n");
673           for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
674           {
675             peers[peer_cnt].op_handle = GNUNET_TESTBED_service_connect (NULL,
676                                                                         peers[peer_cnt].peer_handle,
677                                                                         "mesh",
678                                                                         &mesh_connect_cb,
679                                                                         &peers[peer_cnt],
680                                                                         &mesh_ca,
681                                                                         &mesh_da,
682                                                                         &peers[peer_cnt]);
683           }
684         }
685       }
686       break;
687     default:
688       GNUNET_assert (0);
689     }
690     break;
691   default:
692     GNUNET_assert (0);
693   }
694 }
695
696
697 /**
698  * Task to register all hosts available in the global host list
699  *
700  * @param cls NULL
701  * @param tc the scheduler task context
702  */
703 static void
704 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
705
706
707 /**
708  * Callback which will be called to after a host registration succeeded or failed
709  *
710  * @param cls the closure
711  * @param emsg the error message; NULL if host registration is successful
712  */
713 static void
714 host_registration_completion (void *cls, const char *emsg)
715 {
716   reg_handle = NULL;
717   if (NULL != emsg)
718   {
719     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
720                 _("Host registration failed for a host. Error: %s\n"), emsg);
721     GNUNET_SCHEDULER_cancel (abort_task);
722     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
723     return;
724   }
725   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
726 }
727
728
729 /**
730  * Task to register all hosts available in the global host list
731  *
732  * @param cls NULL
733  * @param tc the scheduler task context
734  */
735 static void
736 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
737 {
738   struct DLLOperation *dll_op;
739   static unsigned int reg_host;
740   unsigned int slave;
741
742   register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
743   if (reg_host == num_hosts - 1)
744   {
745     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746                 "All hosts successfully registered\n");
747     /* Start slaves */
748     state = STATE_SLAVES_STARTING;
749     for (slave = 1; slave < num_hosts; slave++)
750     {
751       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
752       dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
753                                                    mc,
754                                                    hosts[slave],
755                                                    hosts[0],
756                                                    cfg,
757                                                    GNUNET_YES);
758       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
759     }
760     return;
761   }
762   reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
763                                              host_registration_completion,
764                                              NULL);
765 }
766
767
768 /**
769  * Callback to signal successfull startup of the controller process
770  *
771  * @param cls the closure from GNUNET_TESTBED_controller_start()
772  * @param cfg the configuration with which the controller has been started;
773  *          NULL if status is not GNUNET_OK
774  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
775  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
776  */
777 static void
778 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
779 {
780   GNUNET_SCHEDULER_cancel (abort_task);
781   if (GNUNET_OK != status)
782   {
783     mc_proc = NULL;
784     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
785     return;
786   }
787   event_mask = 0;
788   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
789   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
790   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
791   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
792   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
793   mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
794                                           &controller_event_cb, NULL);
795   if (NULL == mc)
796   {
797     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
798                 _("Unable to connect to master controller -- Check config\n"));
799     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
800     return;
801   }
802   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
803   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
804                                              &do_abort, NULL);
805 }
806
807
808 /**
809  * Main function that will be run by the scheduler.
810  *
811  * @param cls closure
812  * @param args remaining command-line arguments
813  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
814  * @param config configuration
815  */
816 static void
817 run (void *cls, char *const *args, const char *cfgfile,
818      const struct GNUNET_CONFIGURATION_Handle *config)
819 {
820   unsigned int nhost;
821
822   if (NULL == args[0])
823   {
824     fprintf (stderr, _("No hosts-file specified on command line\n"));
825     return;
826   }
827   if (0 == num_peers)
828   {
829     result = GNUNET_OK;
830     return;
831   }
832   num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
833   if (0 == num_hosts)
834   {
835     fprintf (stderr, _("No hosts loaded. Need at least one host\n"));
836     return;
837   }
838   for (nhost = 0; nhost < num_hosts; nhost++)
839   {
840     if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
841     {
842       fprintf (stderr, _("Host %s cannot start testbed\n"),
843                          GNUNET_TESTBED_host_get_hostname (hosts[nhost]));
844       break;
845     }
846   }
847   if (num_hosts != nhost)
848   {
849     fprintf (stderr, _("Exiting\n"));
850     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
851     return;
852   }
853   if (NULL == config)
854   {
855     fprintf (stderr, _("No configuration file given. Exiting\n"));
856     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
857     return;
858   }
859
860   cfg = GNUNET_CONFIGURATION_dup (config);
861   mc_proc =
862       GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
863                                        (hosts[0]),
864                                        hosts[0],
865                                        cfg,
866                                        status_cb,
867                                        NULL);
868   abort_task =
869       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
870                                     (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
871                                     NULL);
872 }
873
874
875 /**
876  * Main function.
877  *
878  * @return 0 on success
879  */
880 int
881 main (int argc, char *const *argv)
882 {
883   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
884     { 'p', "num-peers", "COUNT",
885       gettext_noop ("create COUNT number of peers"),
886       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
887     { 'n', "num-links", "COUNT",
888       gettext_noop ("create COUNT number of random links"),
889       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
890     GNUNET_GETOPT_OPTION_END
891   };
892   int ret;
893
894   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
895     return 2;
896
897   result = GNUNET_SYSERR;
898   ret =
899       GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file",
900                           _("Profiler for regex/mesh"),
901                           options, &run, NULL);
902   if (GNUNET_OK != ret)
903     return ret;
904   if (GNUNET_OK != result)
905     return 1;
906   return 0;
907 }