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