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