regex profiler fix
[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    * Peer's mesh tunnel handle.
124    */
125   struct GNUNET_MESH_Tunnel *mesh_tunnel_handle;
126
127   /**
128    * Host on which the peer is running.
129    */
130   struct GNUNET_TESTBED_Host *host_handle;
131
132   /**
133    * Testbed operation handle.
134    */
135   struct GNUNET_TESTBED_Operation *op_handle;
136
137   /**
138    * Filename of the peer's policy file.
139    */
140   char *policy_file;
141 };
142
143 /**
144  * Array of peer handles used to pass to
145  * GNUNET_TESTBED_overlay_configure_topology
146  */
147 struct GNUNET_TESTBED_Peer **peer_handles;
148
149 /**
150  * The array of peers; we fill this as the peers are given to us by the testbed
151  */
152 static struct Peer *peers;
153
154 /**
155  * Host registration handle
156  */
157 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
158
159 /**
160  * Handle to the master controller process
161  */
162 struct GNUNET_TESTBED_ControllerProc *mc_proc;
163
164 /**
165  * Handle to the master controller
166  */
167 struct GNUNET_TESTBED_Controller *mc;
168
169 /**
170  * Handle to global configuration
171  */
172 struct GNUNET_CONFIGURATION_Handle *cfg;
173
174 /**
175  * Head of the operations list
176  */
177 struct DLLOperation *dll_op_head;
178
179 /**
180  * Tail of the operations list
181  */
182 struct DLLOperation *dll_op_tail;
183
184 /**
185  * Peer linking - topology operation
186  */
187 struct GNUNET_TESTBED_Operation *topology_op;
188
189 /**
190  * Abort task identifier
191  */
192 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
193
194 /**
195  * Host registration task identifier
196  */
197 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
198
199 /**
200  * Global event mask for all testbed events
201  */
202 uint64_t event_mask;
203
204 /**
205  * The starting time of a profiling step
206  */
207 struct GNUNET_TIME_Absolute prof_start_time;
208
209 /**
210  * Duration profiling step has taken
211  */
212 struct GNUNET_TIME_Relative prof_time;
213
214 /**
215  * Current peer id
216  */
217 unsigned int peer_id;
218
219 /**
220  * Number of peers to be started by the profiler
221  */
222 static unsigned int num_peers;
223
224 /**
225  * Number of hosts in the hosts array
226  */
227 static unsigned int num_hosts;
228
229 /**
230  * Number of random links to be established between peers
231  */
232 static unsigned int num_links;
233
234 /**
235  * Global testing status
236  */
237 static int result;
238
239 /**
240  * current state of profiling
241  */
242 enum State state;
243
244
245 /**
246  * Folder where policy files are stored.
247  */
248 static char * policy_dir;
249
250 /**
251  * Search string.
252  */
253 static char *search_string = "GNUNETVPN0001000IPEX4000110110111101100111";
254
255 /**
256  * Search task identifier
257  */
258 static GNUNET_SCHEDULER_TaskIdentifier search_task;
259
260
261 /**
262  * Shutdown nicely
263  *
264  * @param cls NULL
265  * @param tc the task context
266  */
267 static void
268 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
269 {
270   struct DLLOperation *dll_op;
271   unsigned int nhost;
272   int peer_cnt;
273
274   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
275     GNUNET_SCHEDULER_cancel (abort_task);
276   if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
277     GNUNET_SCHEDULER_cancel (register_hosts_task);
278   if (NULL != reg_handle)
279     GNUNET_TESTBED_cancel_registration (reg_handle);
280   if (NULL != topology_op)
281     GNUNET_TESTBED_operation_cancel (topology_op);
282   for (nhost = 0; nhost < num_hosts; nhost++)
283     if (NULL != hosts[nhost])
284       GNUNET_TESTBED_host_destroy (hosts[nhost]);
285   GNUNET_free_non_null (hosts);
286   while (NULL != (dll_op = dll_op_head))
287   {
288     GNUNET_TESTBED_operation_cancel (dll_op->op);
289     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
290     GNUNET_free (dll_op);
291   }
292   for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
293   {
294     if (NULL != peers[peer_cnt].op_handle)
295       GNUNET_TESTBED_operation_cancel (peers[peer_cnt].op_handle);
296   }
297   if (NULL != mc)
298     GNUNET_TESTBED_controller_disconnect (mc);
299   if (NULL != mc_proc)
300     GNUNET_TESTBED_controller_stop (mc_proc);
301   if (NULL != cfg)
302     GNUNET_CONFIGURATION_destroy (cfg);
303
304   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
305 }
306
307
308 /**
309  * abort task to run on test timed out
310  *
311  * @param cls NULL
312  * @param tc the task context
313  */
314 static void
315 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
316 {
317   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
318   abort_task = GNUNET_SCHEDULER_NO_TASK;
319   result = GNUNET_SYSERR;
320   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
321 }
322
323
324 /**
325  * Method called whenever another peer has added us to a tunnel
326  * the other peer initiated.
327  * Only called (once) upon reception of data with a message type which was
328  * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
329  * causes te tunnel to be ignored and no further notifications are sent about
330  * the same tunnel.
331  *
332  * @param cls closure
333  * @param tunnel new handle to the tunnel
334  * @param initiator peer that started the tunnel
335  * @param atsi performance information for the tunnel
336  * @return initial tunnel context for the tunnel
337  *         (can be NULL -- that's not an error)
338  */
339 void *
340 mesh_inbound_tunnel_handler (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
341                              const struct GNUNET_PeerIdentity *initiator,
342                              const struct GNUNET_ATS_Information *atsi)
343 {
344   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh inbound tunnel handler.\n");
345
346   return NULL;
347 }
348
349
350 /**
351  * Function called whenever an inbound tunnel is destroyed.  Should clean up
352  * any associated state.  This function is NOT called if the client has
353  * explicitly asked for the tunnel to be destroyed using
354  * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
355  * the tunnel.
356  *
357  * @param cls closure (set from GNUNET_MESH_connect)
358  * @param tunnel connection to the other end (henceforth invalid)
359  * @param tunnel_ctx place where local state associated
360  *                   with the tunnel is stored
361  */
362 void
363 mesh_tunnel_end_handler (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
364                          void *tunnel_ctx)
365 {
366   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh tunnel end handler.\n");
367 }
368
369
370 /**
371  * Method called whenever a peer has disconnected from the tunnel.
372  * Implementations of this callback must NOT call
373  * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
374  * to run in some other task later.  However, calling
375  * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
376  *
377  * @param cls closure
378  * @param peer_id peer identity the tunnel stopped working with
379  */
380 void
381 mesh_peer_disconnect_handler (void *cls,
382                               const struct GNUNET_PeerIdentity * peer_id)
383 {
384   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh peer disconnect handler.\n");
385 }
386
387
388 /**
389  * Method called whenever a peer has connected to the tunnel.
390  *
391  * @param cls closure
392  * @param peer_id peer identity the tunnel was created to, NULL on timeout
393  * @param atsi performance data for the connection
394  *
395  */
396 void
397 mesh_peer_connect_handler (void *cls,
398                            const struct GNUNET_PeerIdentity* peer_id,
399                            const struct GNUNET_ATS_Information * atsi)
400 {
401   //  struct Peer *peer = (struct Peer *)cls;
402   unsigned int peer_cnt;
403
404   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh peer connect handler.\n");
405
406   for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
407   {
408     GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
409   }
410 }
411
412
413 /**
414  * Connect by string task that is run to search for a string in the NFA
415  *
416  * @param cls NULL
417  * @param tc the task context
418  */
419 static void
420 do_connect_by_string (void *cls,
421                       const struct GNUNET_SCHEDULER_TaskContext * tc)
422 {
423   printf ("Searching for string \"%s\"\n", search_string);
424
425   peers[0].mesh_tunnel_handle = GNUNET_MESH_tunnel_create (peers[0].mesh_handle,
426                                                            NULL,
427                                                            &mesh_peer_connect_handler,
428                                                            &mesh_peer_disconnect_handler,
429                                                            &peers[0]);
430
431
432   GNUNET_MESH_peer_request_connect_by_string (peers[0].mesh_tunnel_handle,
433                                               search_string);
434 }
435
436
437 /**
438  * Mesh connect callback.
439  *
440  * @param cls internal peer id.
441  * @param op operation handle.
442  * @param ca_result connect adapter result.
443  * @param emsg error message.
444  */
445 void
446 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
447                  void *ca_result, const char *emsg)
448 {
449   static unsigned int connected_mesh_handles;
450   struct Peer *peer = (struct Peer *) cls;
451   char *regex;
452   char *data;
453   char *buf;
454   uint64_t filesize;
455   unsigned int offset;
456
457   if (NULL != emsg)
458   {
459     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Mesh connect failed: %s\n", emsg);
460     GNUNET_assert (0);
461   }
462
463   GNUNET_assert (peer->op_handle == op);
464   GNUNET_assert (peer->mesh_handle == ca_result);
465   GNUNET_assert (NULL != peer->policy_file);
466
467   printf ("Announcing regexes for peer with file %s\n", peer->policy_file);
468   fflush (stdout);
469
470   if (GNUNET_YES != GNUNET_DISK_file_test (peer->policy_file))
471   {
472     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
473                 "Could not find policy file %s\n", peer->policy_file);
474     GNUNET_TESTBED_operation_done (peer->op_handle);
475     return;
476   }
477   if (GNUNET_OK != GNUNET_DISK_file_size (peer->policy_file, &filesize, GNUNET_YES, GNUNET_YES))
478     filesize = 0;
479   if (0 == filesize)
480   {
481     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n", peer->policy_file);
482     GNUNET_TESTBED_operation_done (peer->op_handle);
483     return;
484   }
485   data = GNUNET_malloc (filesize);
486   if (filesize != GNUNET_DISK_fn_read (peer->policy_file, data, filesize))
487   {
488     GNUNET_free (data);
489     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read policy file %s.\n",
490          peer->policy_file);
491     GNUNET_TESTBED_operation_done (peer->op_handle);
492     return;
493   }
494   buf = data;
495   offset = 0;
496   regex = NULL;
497   while (offset < (filesize - 1))
498   {
499     offset++;
500     if (((data[offset] == '\n')) && (buf != &data[offset]))
501     {
502       data[offset] = '\0';
503       regex = buf;
504       GNUNET_assert (NULL != regex);
505       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
506       GNUNET_MESH_announce_regex (peer->mesh_handle, regex);
507       buf = &data[offset + 1];
508     }
509     else if ((data[offset] == '\n') || (data[offset] == '\0'))
510       buf = &data[offset + 1];
511   }
512   GNUNET_free (data);
513
514   if (++connected_mesh_handles == num_peers)
515   {
516     printf ("\nAll mesh handles connected.\n");
517
518     search_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
519                                                 (GNUNET_TIME_UNIT_SECONDS, 5),
520                                                 &do_connect_by_string, NULL);
521   }
522
523 }
524
525
526 /**
527  * Mesh connect adapter.
528  *
529  * @param cls not used.
530  * @param cfg configuration handle.
531  *
532  * @return
533  */
534 void *
535 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
536 {
537   struct Peer *peer = (struct Peer *) cls;
538
539   static struct GNUNET_MESH_MessageHandler handlers[] = {
540     {NULL, 0, 0}
541   };
542
543   static GNUNET_MESH_ApplicationType apptypes[] = {
544     GNUNET_APPLICATION_TYPE_END
545   };
546
547   peer->mesh_handle =
548       GNUNET_MESH_connect (cfg, cls, &mesh_inbound_tunnel_handler,
549                            &mesh_tunnel_end_handler, handlers, apptypes);
550
551   return peer->mesh_handle;
552 }
553
554
555 /**
556  * Adapter function called to destroy a connection to
557  * the mesh service
558  *
559  * @param cls closure
560  * @param op_result service handle returned from the connect adapter
561  */
562 void
563 mesh_da (void *cls, void *op_result)
564 {
565   struct Peer *peer = (struct Peer *) cls;
566
567   GNUNET_assert (peer->mesh_handle == op_result);
568
569   if (NULL != peer->mesh_tunnel_handle)
570   {
571     GNUNET_MESH_tunnel_destroy (peer->mesh_tunnel_handle);
572     peer->mesh_tunnel_handle = NULL;
573   }
574   if (NULL != peer->mesh_handle)
575   {
576     GNUNET_MESH_disconnect (peer->mesh_handle);
577     peer->mesh_handle = NULL;
578   }
579 }
580
581
582 /**
583  * Functions of this signature are called when a peer has been successfully
584  * started or stopped.
585  *
586  * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
587  * @param emsg NULL on success; otherwise an error description
588  */
589 static void
590 peer_churn_cb (void *cls, const char *emsg)
591 {
592   struct DLLOperation *dll_op = cls;
593   struct GNUNET_TESTBED_Operation *op;
594   static unsigned int started_peers;
595   unsigned int peer_cnt;
596
597   op = dll_op->op;
598   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
599   GNUNET_free (dll_op);
600   if (NULL != emsg)
601   {
602     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
603          _("An operation has failed while starting peers\n"));
604     GNUNET_TESTBED_operation_done (op);
605     GNUNET_SCHEDULER_cancel (abort_task);
606     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
607     return;
608   }
609   GNUNET_TESTBED_operation_done (op);
610   if (++started_peers == num_peers)
611   {
612     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
613     printf ("All peers started successfully in %.2f seconds\n",
614             ((double) prof_time.rel_value) / 1000.00);
615     result = GNUNET_OK;
616     if (0 == num_links)
617     {
618       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
619       return;
620     }
621
622     peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
623     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
624       peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
625
626     state = STATE_PEERS_LINKING;
627     /* Do overlay connect */
628     prof_start_time = GNUNET_TIME_absolute_get ();
629     topology_op =
630         GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
631                                                    GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
632                                                    num_links);
633     if (NULL == topology_op)
634     {
635       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
636                   "Cannot create topology, op handle was NULL\n");
637       GNUNET_assert (0);
638     }
639   }
640 }
641
642
643 /**
644  * Functions of this signature are called when a peer has been successfully
645  * created
646  *
647  * @param cls the closure from GNUNET_TESTBED_peer_create()
648  * @param peer the handle for the created peer; NULL on any error during
649  *          creation
650  * @param emsg NULL if peer is not NULL; else MAY contain the error description
651  */
652 static void
653 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
654 {
655   struct DLLOperation *dll_op = cls;
656   struct Peer *peer_ptr;
657   static unsigned int created_peers;
658   unsigned int peer_cnt;
659
660   if (NULL != emsg)
661   {
662     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
663          _("Creating a peer failed. Error: %s\n"), emsg);
664     GNUNET_TESTBED_operation_done (dll_op->op);
665     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
666     GNUNET_free (dll_op);
667     GNUNET_SCHEDULER_cancel (abort_task);
668     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
669     return;
670   }
671
672   peer_ptr = dll_op->cls;
673   GNUNET_assert (NULL == peer_ptr->peer_handle);
674   peer_ptr->peer_handle = peer;
675   GNUNET_TESTBED_operation_done (dll_op->op);
676   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
677   GNUNET_free (dll_op);
678
679   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
680               created_peers,
681               GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
682
683   if (++created_peers == num_peers)
684   {
685     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
686     printf ("All peers created successfully in %.2f seconds\n",
687             ((double) prof_time.rel_value) / 1000.00);
688     /* Now peers are to be started */
689     state = STATE_PEERS_STARTING;
690     prof_start_time = GNUNET_TIME_absolute_get ();
691     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
692     {
693       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
694       dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt].peer_handle,
695                                               &peer_churn_cb, dll_op);
696       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
697     }
698   }
699 }
700
701 /**
702  * Function called with a filename.
703  *
704  * @param cls closure
705  * @param filename complete filename (absolute path)
706  * @return GNUNET_OK to continue to iterate,
707  *  GNUNET_SYSERR to abort iteration with error!
708  */
709 int
710 policy_filename_cb (void *cls, const char *filename)
711 {
712   static unsigned int peer_cnt;
713   struct DLLOperation *dll_op;
714
715   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating peer %i on host %s for policy file %s\n",
716               peer_cnt,
717               GNUNET_TESTBED_host_get_hostname (hosts[peer_cnt % num_hosts]),
718               filename);
719
720   peers[peer_cnt].policy_file = GNUNET_strdup (filename);
721   peers[peer_cnt].host_handle = hosts[peer_cnt % num_hosts];
722
723   dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
724   dll_op->cls = &peers[peer_cnt];
725   dll_op->op = GNUNET_TESTBED_peer_create (mc,
726                                            hosts[peer_cnt % num_hosts],
727                                            cfg,
728                                            &peer_create_cb,
729                                            dll_op);
730   GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
731   peer_cnt++;
732
733   return GNUNET_OK;
734 }
735
736
737 /**
738  * Controller event callback
739  *
740  * @param cls NULL
741  * @param event the controller event
742  */
743 static void
744 controller_event_cb (void *cls,
745                      const struct GNUNET_TESTBED_EventInformation *event)
746 {
747   struct DLLOperation *dll_op;
748   struct GNUNET_TESTBED_Operation *op;
749
750   switch (state)
751   {
752   case STATE_SLAVES_STARTING:
753     switch (event->type)
754     {
755     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
756       {
757         static unsigned int slaves_started;
758
759         dll_op = event->details.operation_finished.op_cls;
760         GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
761         GNUNET_free (dll_op);
762         op = event->details.operation_finished.operation;
763         if (NULL != event->details.operation_finished.emsg)
764         {
765           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
766                _("An operation has failed while starting slaves\n"));
767           GNUNET_TESTBED_operation_done (op);
768           GNUNET_SCHEDULER_cancel (abort_task);
769           abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
770           return;
771         }
772         GNUNET_TESTBED_operation_done (op);
773         /* Proceed to start peers */
774         if (++slaves_started == num_hosts - 1)
775         {
776           printf ("All slaves started successfully\n");
777
778           state = STATE_PEERS_CREATING;
779           prof_start_time = GNUNET_TIME_absolute_get ();
780
781           num_peers = GNUNET_DISK_directory_scan (policy_dir,
782                                                   NULL,
783                                                   NULL);
784           peers = GNUNET_malloc (sizeof (struct Peer) * num_peers);
785
786           GNUNET_DISK_directory_scan (policy_dir,
787                                       &policy_filename_cb,
788                                       NULL);
789         }
790       }
791       break;
792     default:
793       GNUNET_assert (0);
794     }
795     break;
796   case STATE_PEERS_STARTING:
797     switch (event->type)
798     {
799     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
800       /* Control reaches here when peer start fails */
801     case GNUNET_TESTBED_ET_PEER_START:
802       /* we handle peer starts in peer_churn_cb */
803       break;
804     default:
805       GNUNET_assert (0);
806     }
807     break;
808   case STATE_PEERS_LINKING:
809    switch (event->type)
810     {
811     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
812       /* Control reaches here when a peer linking operation fails */
813       if (NULL != event->details.operation_finished.emsg)
814       {
815         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
816              _("An operation has failed while linking\n"));
817         GNUNET_SCHEDULER_cancel (abort_task);
818         abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
819       }
820       break;
821     case GNUNET_TESTBED_ET_CONNECT:
822       {
823         static unsigned int established_links;
824         unsigned int peer_cnt;
825
826         if (0 == established_links)
827           printf ("Establishing links\n .");
828         else
829         {
830           printf (".");
831           fflush (stdout);
832         }
833         if (++established_links == num_links)
834         {
835           prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
836           printf ("\n%u links established in %.2f seconds\n",
837                   num_links, ((double) prof_time.rel_value) / 1000.00);
838           result = GNUNET_OK;
839           GNUNET_free (peer_handles);
840           printf ("\nConnecting to mesh service...\n");
841           for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
842           {
843             peers[peer_cnt].op_handle = GNUNET_TESTBED_service_connect (NULL,
844                                                                         peers[peer_cnt].peer_handle,
845                                                                         "mesh",
846                                                                         &mesh_connect_cb,
847                                                                         &peers[peer_cnt],
848                                                                         &mesh_ca,
849                                                                         &mesh_da,
850                                                                         &peers[peer_cnt]);
851           }
852         }
853       }
854       break;
855     default:
856       GNUNET_assert (0);
857     }
858     break;
859   default:
860     GNUNET_assert (0);
861   }
862 }
863
864
865 /**
866  * Task to register all hosts available in the global host list
867  *
868  * @param cls NULL
869  * @param tc the scheduler task context
870  */
871 static void
872 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
873
874
875 /**
876  * Callback which will be called to after a host registration succeeded or failed
877  *
878  * @param cls the closure
879  * @param emsg the error message; NULL if host registration is successful
880  */
881 static void
882 host_registration_completion (void *cls, const char *emsg)
883 {
884   reg_handle = NULL;
885   if (NULL != emsg)
886   {
887     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
888                 _("Host registration failed for a host. Error: %s\n"), emsg);
889     GNUNET_SCHEDULER_cancel (abort_task);
890     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
891     return;
892   }
893   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
894 }
895
896
897 /**
898  * Task to register all hosts available in the global host list
899  *
900  * @param cls NULL
901  * @param tc the scheduler task context
902  */
903 static void
904 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
905 {
906   struct DLLOperation *dll_op;
907   static unsigned int reg_host;
908   unsigned int slave;
909
910   register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
911   if (reg_host == num_hosts - 1)
912   {
913     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914                 "All hosts successfully registered\n");
915     /* Start slaves */
916     state = STATE_SLAVES_STARTING;
917     for (slave = 1; slave < num_hosts; slave++)
918     {
919       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
920       dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
921                                                    mc,
922                                                    hosts[slave],
923                                                    hosts[0],
924                                                    cfg,
925                                                    GNUNET_YES);
926       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
927     }
928     return;
929   }
930   reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
931                                              host_registration_completion,
932                                              NULL);
933 }
934
935
936 /**
937  * Callback to signal successfull startup of the controller process
938  *
939  * @param cls the closure from GNUNET_TESTBED_controller_start()
940  * @param config the configuration with which the controller has been started;
941  *          NULL if status is not GNUNET_OK
942  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
943  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
944  */
945 static void
946 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
947 {
948   GNUNET_SCHEDULER_cancel (abort_task);
949   if (GNUNET_OK != status)
950   {
951     mc_proc = NULL;
952     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
953     return;
954   }
955   event_mask = 0;
956   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
957   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
958   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
959   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
960   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
961   mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
962                                           &controller_event_cb, NULL);
963   if (NULL == mc)
964   {
965     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
966                 _("Unable to connect to master controller -- Check config\n"));
967     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
968     return;
969   }
970   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
971   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
972                                              &do_abort, NULL);
973 }
974
975
976 /**
977  * Main function that will be run by the scheduler.
978  *
979  * @param cls closure
980  * @param args remaining command-line arguments
981  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
982  * @param config configuration
983  */
984 static void
985 run (void *cls, char *const *args, const char *cfgfile,
986      const struct GNUNET_CONFIGURATION_Handle *config)
987 {
988   unsigned int nhost;
989
990   if (NULL == args[0])
991   {
992     fprintf (stderr, _("No hosts-file specified on command line. Exiting.\n"));
993     return;
994   }
995   if (NULL == args[1])
996   {
997     fprintf (stderr, _("No policy directory specified on command line. Exiting.\n"));
998     return;
999   }
1000   num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
1001   if (0 == num_hosts)
1002   {
1003     fprintf (stderr, _("No hosts loaded. Need at least one host\n"));
1004     return;
1005   }
1006   for (nhost = 0; nhost < num_hosts; nhost++)
1007   {
1008     if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
1009     {
1010       fprintf (stderr, _("Host %s cannot start testbed\n"),
1011                          GNUNET_TESTBED_host_get_hostname (hosts[nhost]));
1012       break;
1013     }
1014   }
1015   if (num_hosts != nhost)
1016   {
1017     fprintf (stderr, _("Exiting\n"));
1018     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1019     return;
1020   }
1021   if (NULL == config)
1022   {
1023     fprintf (stderr, _("No configuration file given. Exiting\n"));
1024     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1025     return;
1026   }
1027   if (GNUNET_YES != GNUNET_DISK_directory_test (args[1]))
1028   {
1029     fprintf (stderr, _("Specified policies directory does not exist. Exiting.\n"));
1030     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1031     return;
1032   }
1033   policy_dir = args[1];
1034
1035   cfg = GNUNET_CONFIGURATION_dup (config);
1036   mc_proc =
1037       GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
1038                                        (hosts[0]),
1039                                        hosts[0],
1040                                        cfg,
1041                                        status_cb,
1042                                        NULL);
1043   abort_task =
1044       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1045                                     (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
1046                                     NULL);
1047 }
1048
1049
1050 /**
1051  * Main function.
1052  *
1053  * @param argc argument count
1054  * @param argv argument values
1055  * @return 0 on success
1056  */
1057 int
1058 main (int argc, char *const *argv)
1059 {
1060   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1061     { 'n', "num-links", "COUNT",
1062       gettext_noop ("create COUNT number of random links"),
1063       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
1064     GNUNET_GETOPT_OPTION_END
1065   };
1066   int ret;
1067
1068   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1069     return 2;
1070
1071   result = GNUNET_SYSERR;
1072   ret =
1073       GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir",
1074                           _("Profiler for regex/mesh"),
1075                           options, &run, NULL);
1076   if (GNUNET_OK != ret)
1077     return ret;
1078   if (GNUNET_OK != result)
1079     return 1;
1080   return 0;
1081 }