- Added regex specific stats to mesh service
[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
29 #include <string.h>
30
31 #include "platform.h"
32 #include "gnunet_applications.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_mesh_service.h"
35 #include "gnunet_stream_lib.h"
36 #include "gnunet_testbed_service.h"
37
38 /**
39  * DLL of operations
40  */
41 struct DLLOperation
42 {
43   /**
44    * The testbed operation handle
45    */
46   struct GNUNET_TESTBED_Operation *op;
47
48   /**
49    * Closure
50    */
51   void *cls;
52
53   /**
54    * The next pointer for DLL
55    */
56   struct DLLOperation *next;
57
58   /**
59    * The prev pointer for DLL
60    */
61   struct DLLOperation *prev;
62 };
63
64
65 /**
66  * Available states during profiling
67  */
68 enum State
69 {
70   /**
71    * Initial state
72    */
73   STATE_INIT = 0,
74
75   /**
76    * Starting slaves
77    */
78   STATE_SLAVES_STARTING,
79
80   /**
81    * Creating peers
82    */
83   STATE_PEERS_CREATING,
84
85   /**
86    * Starting peers
87    */
88   STATE_PEERS_STARTING,
89
90   /**
91    * Linking peers
92    */
93   STATE_PEERS_LINKING,
94
95   /**
96    * Destroying peers; we can do this as the controller takes care of stopping a
97    * peer if it is running
98    */
99   STATE_PEERS_DESTROYING
100 };
101
102
103 /**
104  * An array of hosts loaded from the hostkeys file
105  */
106 static struct GNUNET_TESTBED_Host **hosts;
107
108 /**
109  * Peer handles.
110  */
111 struct RegexPeer
112 {
113   /**
114    * Peer id.
115    */
116   unsigned int id;
117
118   /**
119    * The actual testbed peer handle.
120    */
121   struct GNUNET_TESTBED_Peer *peer_handle;
122
123   /**
124    * Host on which the peer is running.
125    */
126   struct GNUNET_TESTBED_Host *host_handle;
127
128   /**
129    * Filename of the peer's policy file.
130    */
131   char *policy_file;
132
133   /**
134    * Peers search string.
135    */
136   const char *search_str;
137
138   /**
139    * Peer's mesh handle.
140    */
141   struct GNUNET_MESH_Handle *mesh_handle;
142
143   /**
144    * Peer's mesh tunnel handle.
145    */
146   struct GNUNET_MESH_Tunnel *mesh_tunnel_handle;
147
148   /**
149    * Testbed operation handle for the mesh service.
150    */
151   struct GNUNET_TESTBED_Operation *mesh_op_handle;
152
153   /**
154    * Peers's statistics handle.
155    */
156   struct GNUNET_STATISTICS_Handle *stats_handle;
157
158   /**
159    * Testbed operation handle for the statistics service.
160    */
161   struct GNUNET_TESTBED_Operation *stats_op_handle;
162 };
163
164 /**
165  * Array of peer handles used to pass to
166  * GNUNET_TESTBED_overlay_configure_topology
167  */
168 struct GNUNET_TESTBED_Peer **peer_handles;
169
170 /**
171  * The array of peers; we fill this as the peers are given to us by the testbed
172  */
173 static struct RegexPeer *peers;
174
175 /**
176  * Host registration handle
177  */
178 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
179
180 /**
181  * Handle to the master controller process
182  */
183 struct GNUNET_TESTBED_ControllerProc *mc_proc;
184
185 /**
186  * Handle to the master controller
187  */
188 struct GNUNET_TESTBED_Controller *mc;
189
190 /**
191  * Handle to global configuration
192  */
193 struct GNUNET_CONFIGURATION_Handle *cfg;
194
195 /**
196  * Head of the operations list
197  */
198 struct DLLOperation *dll_op_head;
199
200 /**
201  * Tail of the operations list
202  */
203 struct DLLOperation *dll_op_tail;
204
205 /**
206  * Peer linking - topology operation
207  */
208 struct GNUNET_TESTBED_Operation *topology_op;
209
210 /**
211  * Abort task identifier
212  */
213 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
214
215 /**
216  * Host registration task identifier
217  */
218 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
219
220 /**
221  * Global event mask for all testbed events
222  */
223 uint64_t event_mask;
224
225 /**
226  * The starting time of a profiling step
227  */
228 struct GNUNET_TIME_Absolute prof_start_time;
229
230 /**
231  * Duration profiling step has taken
232  */
233 struct GNUNET_TIME_Relative prof_time;
234
235 /**
236  * Current peer id
237  */
238 unsigned int peer_id;
239
240 /**
241  * Number of peers to be started by the profiler
242  */
243 static unsigned int num_peers;
244
245 /**
246  * Number of hosts in the hosts array
247  */
248 static unsigned int num_hosts;
249
250 /**
251  * Number of random links to be established between peers
252  */
253 static unsigned int num_links;
254
255 /**
256  * Number of timeout failures to tolerate
257  */
258 static unsigned int num_cont_fails;
259
260 /**
261  * Number of times we try overlay connect operations
262  */
263 static unsigned int retry_links;
264
265 /**
266  * Continuous failures during overlay connect operations
267  */
268 static unsigned int cont_fails;
269
270 /**
271  * Global testing status
272  */
273 static int result;
274
275 /**
276  * current state of profiling
277  */
278 enum State state;
279
280 /**
281  * Folder where policy files are stored.
282  */
283 static char * policy_dir;
284
285 /**
286  * Search strings.
287  */
288 static char **search_strings;
289
290 /**
291  * Number of search strings.
292  */
293 static int num_search_strings;
294
295 /**
296  * Number of peers found with search strings.
297  */
298 static unsigned int peers_found;
299
300 /**
301  * Search task identifier
302  */
303 static GNUNET_SCHEDULER_TaskIdentifier search_task;
304
305 /**
306  * Search timeout task identifier.
307  */
308 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
309
310 /**
311  * Search timeout in seconds.
312  */
313 static struct GNUNET_TIME_Relative search_timeout = { 60000 };
314
315 /**
316  * How long do we wait before starting the search?
317  * Default: 1 m.
318  */
319 static struct GNUNET_TIME_Relative search_delay = { 60000 };
320
321 /**
322  * File to log statistics to.
323  */
324 static struct GNUNET_DISK_FileHandle *data_file;
325
326 /**
327  * Filename to log statistics to.
328  */
329 static char *data_filename;
330
331
332 /**
333  * Shutdown nicely
334  *
335  * @param cls NULL
336  * @param tc the task context
337  */
338 static void
339 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
340 {
341   struct DLLOperation *dll_op;
342   unsigned int nhost;
343   unsigned int peer_cnt;
344   unsigned int search_str_cnt;
345
346   for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
347   {
348     if (NULL != peers[peer_cnt].mesh_op_handle)
349       GNUNET_TESTBED_operation_cancel (peers[peer_cnt].mesh_op_handle);
350     if (NULL != peers[peer_cnt].stats_op_handle)
351       GNUNET_TESTBED_operation_cancel (peers[peer_cnt].stats_op_handle);
352   }
353   for (search_str_cnt = 0; search_str_cnt < num_search_strings; search_str_cnt++)
354   {
355     GNUNET_free (search_strings[search_str_cnt]);
356   }
357   GNUNET_free (search_strings);
358   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
359     GNUNET_SCHEDULER_cancel (abort_task);
360   if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
361     GNUNET_SCHEDULER_cancel (register_hosts_task);
362   if (NULL != reg_handle)
363     GNUNET_TESTBED_cancel_registration (reg_handle);
364   if (NULL != topology_op)
365     GNUNET_TESTBED_operation_cancel (topology_op);
366   for (nhost = 0; nhost < num_hosts; nhost++)
367     if (NULL != hosts[nhost])
368       GNUNET_TESTBED_host_destroy (hosts[nhost]);
369   GNUNET_free_non_null (hosts);
370   while (NULL != (dll_op = dll_op_head))
371   {
372     GNUNET_TESTBED_operation_cancel (dll_op->op);
373     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
374     GNUNET_free (dll_op);
375   }
376   if (NULL != mc)
377     GNUNET_TESTBED_controller_disconnect (mc);
378   if (NULL != mc_proc)
379     GNUNET_TESTBED_controller_stop (mc_proc);
380   if (NULL != cfg)
381     GNUNET_CONFIGURATION_destroy (cfg);
382   if (NULL != data_file)
383     GNUNET_DISK_file_close (data_file);
384
385   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
386 }
387
388
389 /**
390  * abort task to run on test timed out
391  *
392  * @param cls NULL
393  * @param tc the task context
394  */
395 static void
396 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
397 {
398   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
399   abort_task = GNUNET_SCHEDULER_NO_TASK;
400   result = GNUNET_SYSERR;
401   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
402 }
403
404
405 /******************************************************************************/
406 /*********************  STATISTICS SERVICE CONNECTIONS  ***********************/
407 /******************************************************************************/
408
409 /**
410  * Adapter function called to establish a connection to
411  * statistics service.
412  *
413  * @param cls closure
414  * @param cfg configuration of the peer to connect to; will be available until
415  *          GNUNET_TESTBED_operation_done() is called on the operation returned
416  *          from GNUNET_TESTBED_service_connect()
417  * @return service handle to return in 'op_result', NULL on error
418  */
419 static void *
420 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
421 {
422   return GNUNET_STATISTICS_create ("<driver>", cfg);
423 }
424
425
426 /**
427  * Adapter function called to destroy a connection to
428  * statistics service.
429  *
430  * @param cls closure
431  * @param op_result service handle returned from the connect adapter
432  */
433 static void
434 stats_da (void *cls, void *op_result)
435 {
436   GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
437 }
438
439
440 /**
441  * Process statistic values.
442  *
443  * @param cls closure
444  * @param subsystem name of subsystem that created the statistic
445  * @param name the name of the datum
446  * @param value the current value
447  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
448  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
449  */
450 static int
451 stats_iterator (void *cls, const char *subsystem, const char *name,
452                 uint64_t value, int is_persistent)
453 {
454   struct RegexPeer *peer = cls;
455   char output_buffer[512];
456   size_t size;
457
458   if (NULL == data_file)
459   {
460     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
461                 "%p -> %s [%s]: %llu\n",
462                 peer, subsystem, name, value);
463     return GNUNET_OK;
464   }
465   size =
466     GNUNET_snprintf (output_buffer,
467                      sizeof (output_buffer),
468                      "%p [%s] %llu %s\n",
469                      peer,
470                      subsystem, value, name);
471   if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
472     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
473
474   return GNUNET_OK;
475 }
476
477
478 /**
479  * Stats callback.
480  *
481  * @param cls closure
482  * @param success GNUNET_OK if statistics were
483  *        successfully obtained, GNUNET_SYSERR if not.
484  */
485 static void
486 stats_cb (void *cls,
487           int success)
488 {
489   static unsigned int peer_cnt;
490   struct RegexPeer *peer = cls;
491
492   if (GNUNET_OK != success)
493   {
494     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
495                 "Getting statistics for peer %u failed!\n",
496                 peer->id);
497     return;
498   }
499
500   if (++peer_cnt == num_search_strings)
501   {
502     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
503   }
504 }
505
506
507 /**
508  * Function called by testbed once we are connected to stats service.
509  *
510  * @param cls the 'struct RegexPeer' for which we connected to stats
511  * @param op connect operation handle
512  * @param ca_result handle to stats service
513  * @param emsg error message on failure
514  */
515 static void
516 stats_connect_cb (void *cls,
517                   struct GNUNET_TESTBED_Operation *op,
518                   void *ca_result,
519                   const char *emsg)
520 {
521   struct RegexPeer *peer = cls;
522
523   if (NULL == ca_result)
524   {
525     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
526                 "Failed to connect to statistics service on peer %u: %s\n",
527                 peer->id, emsg);
528     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
529     return;
530   }
531
532   peer->stats_handle = ca_result;
533 }
534
535
536 /******************************************************************************/
537 /************************  MESH SERVICE CONNECTIONS  **************************/
538 /******************************************************************************/
539
540 /**
541  * Method called whenever another peer has added us to a tunnel
542  * the other peer initiated.
543  * Only called (once) upon reception of data with a message type which was
544  * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
545  * causes te tunnel to be ignored and no further notifications are sent about
546  * the same tunnel.
547  *
548  * @param cls closure
549  * @param tunnel new handle to the tunnel
550  * @param initiator peer that started the tunnel
551  * @param atsi performance information for the tunnel
552  * @return initial tunnel context for the tunnel
553  *         (can be NULL -- that's not an error)
554  */
555 void *
556 mesh_inbound_tunnel_handler (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
557                              const struct GNUNET_PeerIdentity *initiator,
558                              const struct GNUNET_ATS_Information *atsi)
559 {
560   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh inbound tunnel handler.\n");
561
562   return NULL;
563 }
564
565
566 /**
567  * Function called whenever an inbound tunnel is destroyed.  Should clean up
568  * any associated state.  This function is NOT called if the client has
569  * explicitly asked for the tunnel to be destroyed using
570  * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
571  * the tunnel.
572  *
573  * @param cls closure (set from GNUNET_MESH_connect)
574  * @param tunnel connection to the other end (henceforth invalid)
575  * @param tunnel_ctx place where local state associated
576  *                   with the tunnel is stored
577  */
578 void
579 mesh_tunnel_end_handler (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
580                          void *tunnel_ctx)
581 {
582   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh tunnel end handler.\n");
583 }
584
585
586 /**
587  * Method called whenever a peer has disconnected from the tunnel.
588  * Implementations of this callback must NOT call
589  * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
590  * to run in some other task later.  However, calling
591  * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
592  *
593  * @param cls closure
594  * @param peer_id peer identity the tunnel stopped working with
595  */
596 void
597 mesh_peer_disconnect_handler (void *cls,
598                               const struct GNUNET_PeerIdentity * peer_id)
599 {
600   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh peer disconnect handler.\n");
601 }
602
603
604 /**
605  * Method called whenever a peer has connected to the tunnel.
606  *
607  * @param cls closure
608  * @param peer_id peer identity the tunnel was created to, NULL on timeout
609  * @param atsi performance data for the connection
610  *
611  */
612 void
613 mesh_peer_connect_handler (void *cls,
614                            const struct GNUNET_PeerIdentity* peer_id,
615                            const struct GNUNET_ATS_Information * atsi)
616 {
617   struct RegexPeer *peer = cls;
618   char output_buffer[512];
619   size_t size;
620
621   peers_found++;
622
623   if (NULL == peer_id)
624   {
625     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
626                 "String matching timed out for string %s on peer %u (%i/%i)\n",
627                 peer->search_str, peer->id, peers_found, num_search_strings);
628   }
629   else
630   {
631     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
632     GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
633                 "String %s successfully matched on peer %u after %s (%i/%i)\n",
634                 peer->search_str, peer->id, GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO), 
635                 peers_found, num_search_strings);
636
637     if (NULL != data_file)
638     {
639       size =
640         GNUNET_snprintf (output_buffer,
641                          sizeof (output_buffer),
642                          "Peer: %u (%p)\nHost: %s\nPolicy file: %s\nSearch string: %s\nSearch duration: %s\n\n",
643                          peer->id,
644                          peer,
645                          GNUNET_TESTBED_host_get_hostname (peer->host_handle),
646                          peer->policy_file,
647                          peer->search_str,
648                          GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
649
650       if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
651         GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
652     }
653
654     if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, "mesh", NULL,
655                                        GNUNET_TIME_UNIT_FOREVER_REL,
656                                        NULL,
657                                        &stats_iterator, peer))
658     {
659       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
660                   "Could not get mesh statistics of peer %u!\n", peer->id);
661     }
662     if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, "transport", NULL,
663                                        GNUNET_TIME_UNIT_FOREVER_REL,
664                                        NULL,
665                                        &stats_iterator, peer))
666     {
667       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
668                   "Could not get transport statistics of peer %u!\n", peer->id);
669     }
670     if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, "dht", NULL,
671                                        GNUNET_TIME_UNIT_FOREVER_REL,
672                                        &stats_cb,
673                                        &stats_iterator, peer))
674     {
675       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
676                   "Could not get dht statistics of peer %u!\n", peer->id);
677     }
678   }
679
680   if (peers_found == num_search_strings)
681   {
682     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
683     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
684                 "All strings successfully matched in %s\n", 
685                 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
686
687     if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
688       GNUNET_SCHEDULER_cancel (search_timeout_task);
689   }
690 }
691
692
693 /**
694  * Connect by string timeout task
695  *
696  * @param cls NULL
697  * @param tc the task context
698  */
699 static void
700 do_connect_by_string_timeout (void *cls,
701                               const struct GNUNET_SCHEDULER_TaskContext * tc)
702 {
703   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
704               "Finding matches to all strings did not succeed after %s.\n",
705               GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
706   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
707               "Found %i of %i strings\n", peers_found, num_search_strings);
708
709   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
710 }
711
712
713 /**
714  * Connect by string task that is run to search for a string in the NFA
715  *
716  * @param cls NULL
717  * @param tc the task context
718  */
719 static void
720 do_connect_by_string (void *cls,
721                       const struct GNUNET_SCHEDULER_TaskContext * tc)
722 {
723   unsigned int search_cnt;
724   struct RegexPeer *peer;
725
726   for (search_cnt = 0; search_cnt < num_search_strings; search_cnt++)
727   {
728     peer = &peers[search_cnt % num_peers];
729     peer->search_str = search_strings[search_cnt];
730
731     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
732                 "Searching for string \"%s\" on peer %d with file %s\n",
733                 peer->search_str, (search_cnt % num_peers), peer->policy_file);
734
735     peer->mesh_tunnel_handle = GNUNET_MESH_tunnel_create (peer->mesh_handle,
736                                                           NULL,
737                                                           &mesh_peer_connect_handler,
738                                                           &mesh_peer_disconnect_handler,
739                                                           peer);
740
741     GNUNET_MESH_peer_request_connect_by_string (peer->mesh_tunnel_handle,
742                                                 peer->search_str);
743   }
744
745   prof_start_time = GNUNET_TIME_absolute_get ();
746
747   search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout,
748                                                       &do_connect_by_string_timeout, NULL);
749 }
750
751
752 /**
753  * Mesh connect callback.
754  *
755  * @param cls internal peer id.
756  * @param op operation handle.
757  * @param ca_result connect adapter result.
758  * @param emsg error message.
759  */
760 void
761 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
762                  void *ca_result, const char *emsg)
763 {
764   static unsigned int connected_mesh_handles;
765   struct RegexPeer *peer = (struct RegexPeer *) cls;
766   char *regex;
767   char *data;
768   char *buf;
769   uint64_t filesize;
770   unsigned int offset;
771
772   if (NULL != emsg)
773   {
774     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Mesh connect failed: %s\n", emsg);
775     GNUNET_assert (0);
776   }
777
778   GNUNET_assert (peer->mesh_op_handle == op);
779   GNUNET_assert (peer->mesh_handle == ca_result);
780   GNUNET_assert (NULL != peer->policy_file);
781
782   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
783               "Announcing regexes for peer %u with file %s\n", 
784               peer->id, peer->policy_file);
785
786   if (GNUNET_YES != GNUNET_DISK_file_test (peer->policy_file))
787   {
788     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
789                 "Could not find policy file %s\n", peer->policy_file);
790     return;
791   }
792   if (GNUNET_OK != GNUNET_DISK_file_size (peer->policy_file, &filesize, GNUNET_YES, GNUNET_YES))
793     filesize = 0;
794   if (0 == filesize)
795   {
796     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n", peer->policy_file);
797     return;
798   }
799   data = GNUNET_malloc (filesize);
800   if (filesize != GNUNET_DISK_fn_read (peer->policy_file, data, filesize))
801   {
802     GNUNET_free (data);
803     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read policy file %s.\n",
804          peer->policy_file);
805     return;
806   }
807   buf = data;
808   offset = 0;
809   regex = NULL;
810   while (offset < (filesize - 1))
811   {
812     offset++;
813     if (((data[offset] == '\n')) && (buf != &data[offset]))
814     {
815       data[offset] = '\0';
816       regex = buf;
817       GNUNET_assert (NULL != regex);
818       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s on peer %u \n", 
819                   regex, peer->id);
820       GNUNET_MESH_announce_regex (peer->mesh_handle, regex);
821       buf = &data[offset + 1];
822     }
823     else if ((data[offset] == '\n') || (data[offset] == '\0'))
824       buf = &data[offset + 1];
825   }
826   GNUNET_free (data);
827
828   if (++connected_mesh_handles == num_peers)
829   {
830     printf ("Waiting %s before starting to search.\n",
831             GNUNET_STRINGS_relative_time_to_string (search_delay, GNUNET_YES));
832     fflush (stdout);
833
834     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
835                 "All mesh handles connected. Waiting %s before starting to search.\n",
836                 GNUNET_STRINGS_relative_time_to_string (search_delay, GNUNET_YES));
837
838     search_task = GNUNET_SCHEDULER_add_delayed (search_delay,
839                                                 &do_connect_by_string, NULL);
840   }
841 }
842
843
844 /**
845  * Mesh connect adapter.
846  *
847  * @param cls not used.
848  * @param cfg configuration handle.
849  *
850  * @return
851  */
852 void *
853 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
854 {
855   GNUNET_MESH_ApplicationType app;
856   struct RegexPeer *peer = cls;
857
858   static struct GNUNET_MESH_MessageHandler handlers[] = {
859     {NULL, 0, 0}
860   };
861
862   app = (GNUNET_MESH_ApplicationType)0;
863
864   peer->mesh_handle =
865     GNUNET_MESH_connect (cfg, cls, NULL, NULL, handlers, &app);
866
867   return peer->mesh_handle;
868 }
869
870
871 /**
872  * Adapter function called to destroy a connection to
873  * the mesh service
874  *
875  * @param cls closure
876  * @param op_result service handle returned from the connect adapter
877  */
878 void
879 mesh_da (void *cls, void *op_result)
880 {
881   struct RegexPeer *peer = (struct RegexPeer *) cls;
882
883   GNUNET_assert (peer->mesh_handle == op_result);
884
885   if (NULL != peer->mesh_tunnel_handle)
886   {
887     GNUNET_MESH_tunnel_destroy (peer->mesh_tunnel_handle);
888     peer->mesh_tunnel_handle = NULL;
889   }
890
891   if (NULL != peer->mesh_handle)
892   {
893     GNUNET_MESH_disconnect (peer->mesh_handle);
894     peer->mesh_handle = NULL;
895   }
896 }
897
898
899 /******************************************************************************/
900 /***************************  TESTBED PEER SETUP  *****************************/
901 /******************************************************************************/
902
903
904 /**
905  * Functions of this signature are called when a peer has been successfully
906  * started or stopped.
907  *
908  * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
909  * @param emsg NULL on success; otherwise an error description
910  */
911 static void
912 peer_churn_cb (void *cls, const char *emsg)
913 {
914   struct DLLOperation *dll_op = cls;
915   struct GNUNET_TESTBED_Operation *op;
916   static unsigned int started_peers;
917   unsigned int peer_cnt;
918
919   op = dll_op->op;
920   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
921   GNUNET_free (dll_op);
922   if (NULL != emsg)
923   {
924     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
925          _("An operation has failed while starting peers\n"));
926     GNUNET_TESTBED_operation_done (op);
927     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
928       GNUNET_SCHEDULER_cancel (abort_task);
929     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
930     return;
931   }
932   GNUNET_TESTBED_operation_done (op);
933   if (++started_peers == num_peers)
934   {
935     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
936     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
937                 "All peers started successfully in %s\n",
938                 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
939     result = GNUNET_OK;
940
941     if (0 == num_links)
942       num_links = num_peers * 5;
943
944     peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
945     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
946       peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
947
948     state = STATE_PEERS_LINKING;
949     /* Do overlay connect */
950     prof_start_time = GNUNET_TIME_absolute_get ();
951     topology_op =
952         GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
953                                                    GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
954                                                    num_links,
955                                                    GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
956                                                    GNUNET_TESTBED_TOPOLOGY_OPTION_END);
957     if (NULL == topology_op)
958     {
959       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
960                   "Cannot create topology, op handle was NULL\n");
961       GNUNET_assert (0);
962     }
963   }
964 }
965
966
967 /**
968  * Functions of this signature are called when a peer has been successfully
969  * created
970  *
971  * @param cls the closure from GNUNET_TESTBED_peer_create()
972  * @param peer the handle for the created peer; NULL on any error during
973  *          creation
974  * @param emsg NULL if peer is not NULL; else MAY contain the error description
975  */
976 static void
977 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
978 {
979   struct DLLOperation *dll_op = cls;
980   struct RegexPeer *peer_ptr;
981   static unsigned int created_peers;
982   unsigned int peer_cnt;
983
984   if (NULL != emsg)
985   {
986     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
987          _("Creating a peer failed. Error: %s\n"), emsg);
988     GNUNET_TESTBED_operation_done (dll_op->op);
989     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
990     GNUNET_free (dll_op);
991     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
992       GNUNET_SCHEDULER_cancel (abort_task);
993     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
994     return;
995   }
996
997   peer_ptr = dll_op->cls;
998   GNUNET_assert (NULL == peer_ptr->peer_handle);
999   peer_ptr->peer_handle = peer;
1000   GNUNET_TESTBED_operation_done (dll_op->op);
1001   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1002   GNUNET_free (dll_op);
1003
1004   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
1005               peer_ptr->id,
1006               GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
1007
1008   if (++created_peers == num_peers)
1009   {
1010     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1011     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1012                 "All peers created successfully in %s\n",
1013                 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1014     /* Now peers are to be started */
1015     state = STATE_PEERS_STARTING;
1016     prof_start_time = GNUNET_TIME_absolute_get ();
1017     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1018     {
1019       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1020       dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt].peer_handle,
1021                                               &peer_churn_cb, dll_op);
1022       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1023     }
1024   }
1025 }
1026
1027 /**
1028  * Function called with a filename.
1029  *
1030  * @param cls closure
1031  * @param filename complete filename (absolute path)
1032  * @return GNUNET_OK to continue to iterate,
1033  *  GNUNET_SYSERR to abort iteration with error!
1034  */
1035 int
1036 policy_filename_cb (void *cls, const char *filename)
1037 {
1038   static unsigned int peer_cnt;
1039   struct DLLOperation *dll_op;
1040   struct RegexPeer *peer = &peers[peer_cnt];
1041
1042   GNUNET_assert (NULL != peer);
1043
1044   peer->id = peer_cnt;
1045   peer->policy_file = GNUNET_strdup (filename);
1046   /* Do not start peers on hosts[0] (master controller) */
1047   peer->host_handle = hosts[1 + (peer_cnt % (num_hosts -1))];
1048   peer->mesh_handle = NULL;
1049   peer->mesh_tunnel_handle = NULL;
1050   peer->stats_handle = NULL;
1051   peer->stats_op_handle = NULL;
1052
1053   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Creating peer %i on host %s for policy file %s\n",
1054               peer->id,
1055               GNUNET_TESTBED_host_get_hostname (peer->host_handle),
1056               filename);
1057
1058   dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1059   dll_op->cls = &peers[peer_cnt];
1060   dll_op->op = GNUNET_TESTBED_peer_create (mc,
1061                                            peer->host_handle,
1062                                            cfg,
1063                                            &peer_create_cb,
1064                                            dll_op);
1065   GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1066   peer_cnt++;
1067
1068   return GNUNET_OK;
1069 }
1070
1071
1072 /**
1073  * Controller event callback.
1074  *
1075  * @param cls NULL
1076  * @param event the controller event
1077  */
1078 static void
1079 controller_event_cb (void *cls,
1080                      const struct GNUNET_TESTBED_EventInformation *event)
1081 {
1082   struct DLLOperation *dll_op;
1083   struct GNUNET_TESTBED_Operation *op;
1084
1085   switch (state)
1086   {
1087   case STATE_SLAVES_STARTING:
1088     switch (event->type)
1089     {
1090     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1091       {
1092         static unsigned int slaves_started;
1093
1094         dll_op = event->details.operation_finished.op_cls;
1095         GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1096         GNUNET_free (dll_op);
1097         op = event->details.operation_finished.operation;
1098         if (NULL != event->details.operation_finished.emsg)
1099         {
1100           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1101                _("An operation has failed while starting slaves\n"));
1102           GNUNET_TESTBED_operation_done (op);
1103           if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1104             GNUNET_SCHEDULER_cancel (abort_task);
1105           abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1106           return;
1107         }
1108         GNUNET_TESTBED_operation_done (op);
1109         /* Proceed to start peers */
1110         if (++slaves_started == num_hosts - 1)
1111         {
1112           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1113                       "All slaves started successfully\n");
1114
1115           state = STATE_PEERS_CREATING;
1116           prof_start_time = GNUNET_TIME_absolute_get ();
1117
1118           num_peers = GNUNET_DISK_directory_scan (policy_dir,
1119                                                   NULL,
1120                                                   NULL);
1121           peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1122
1123           GNUNET_DISK_directory_scan (policy_dir,
1124                                       &policy_filename_cb,
1125                                       NULL);
1126         }
1127       }
1128       break;
1129     default:
1130       GNUNET_assert (0);
1131     }
1132     break;
1133   case STATE_PEERS_STARTING:
1134     switch (event->type)
1135     {
1136     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1137       /* Control reaches here when peer start fails */
1138     case GNUNET_TESTBED_ET_PEER_START:
1139       /* we handle peer starts in peer_churn_cb */
1140       break;
1141     default:
1142       GNUNET_assert (0);
1143     }
1144     break;
1145   case STATE_PEERS_LINKING:
1146    switch (event->type)
1147     {
1148       static unsigned int established_links;
1149     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1150       /* Control reaches here when a peer linking operation fails */
1151       if (NULL != event->details.operation_finished.emsg)
1152       {
1153         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1154              _("An operation has failed while linking\n"));
1155         printf ("F");
1156         fflush (stdout);
1157         retry_links++;
1158         
1159         if (++cont_fails > num_cont_fails)
1160         {
1161           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1162                       "We have a very high peer linking failure rate: %u (threshold: %u)\n",
1163                       cont_fails,
1164                       num_cont_fails);
1165         }
1166       }
1167       /* We do no retries, consider this link as established */
1168       /* break; */
1169     case GNUNET_TESTBED_ET_CONNECT:
1170       {
1171         unsigned int peer_cnt;
1172         char output_buffer[512];
1173         size_t size;
1174
1175         if (0 == established_links)
1176           printf ("Establishing links .");
1177         else
1178         {
1179           printf (".");
1180           fflush (stdout);
1181         }
1182         if (++established_links == num_links)
1183         {
1184           printf ("\n");
1185           fflush (stdout);
1186           prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1187           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1188                       "%u links established in %s\n",
1189                       num_links, 
1190                       GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1191           result = GNUNET_OK;
1192           GNUNET_free (peer_handles);
1193
1194           if (NULL != data_file)
1195           {
1196             size =
1197               GNUNET_snprintf (output_buffer,
1198                                sizeof (output_buffer),
1199                                "# of peers: %u\n# of links established: %u\nTime to establish links: %s\nLinking failures: %u\n\n",
1200                                num_peers,
1201                                (established_links - cont_fails),
1202                                GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
1203                                cont_fails);
1204             
1205             if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
1206               GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
1207           }
1208           
1209           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1210                       "Connecting to mesh and statistics service...\n");
1211           for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1212           {
1213             peers[peer_cnt].mesh_op_handle =
1214               GNUNET_TESTBED_service_connect (NULL,
1215                                               peers[peer_cnt].peer_handle,
1216                                               "mesh",
1217                                               &mesh_connect_cb,
1218                                               &peers[peer_cnt],
1219                                               &mesh_ca,
1220                                               &mesh_da,
1221                                               &peers[peer_cnt]);
1222
1223             peers[peer_cnt].stats_op_handle =
1224               GNUNET_TESTBED_service_connect (NULL,
1225                                               peers[peer_cnt].peer_handle,
1226                                               "statistics",
1227                                               &stats_connect_cb,
1228                                               &peers[peer_cnt],
1229                                               &stats_ca,
1230                                               &stats_da,
1231                                               &peers[peer_cnt]);
1232           }
1233         }
1234       }
1235       break;
1236     default:
1237       GNUNET_assert (0);
1238     }
1239     break;
1240   default:
1241     GNUNET_assert (0);
1242   }
1243 }
1244
1245
1246 /**
1247  * Task to register all hosts available in the global host list
1248  *
1249  * @param cls NULL
1250  * @param tc the scheduler task context
1251  */
1252 static void
1253 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1254
1255
1256 /**
1257  * Callback which will be called to after a host registration succeeded or failed
1258  *
1259  * @param cls the closure
1260  * @param emsg the error message; NULL if host registration is successful
1261  */
1262 static void
1263 host_registration_completion (void *cls, const char *emsg)
1264 {
1265   reg_handle = NULL;
1266   if (NULL != emsg)
1267   {
1268     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1269                 _("Host registration failed for a host. Error: %s\n"), emsg);
1270     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1271       GNUNET_SCHEDULER_cancel (abort_task);
1272     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1273     return;
1274   }
1275   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
1276 }
1277
1278
1279 /**
1280  * Task to register all hosts available in the global host list
1281  *
1282  * @param cls NULL
1283  * @param tc the scheduler task context
1284  */
1285 static void
1286 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1287 {
1288   struct DLLOperation *dll_op;
1289   static unsigned int reg_host;
1290   unsigned int slave;
1291
1292   register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1293   if (reg_host == num_hosts - 1)
1294   {
1295     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1296                 "All hosts successfully registered\n");
1297     /* Start slaves */
1298     state = STATE_SLAVES_STARTING;
1299     for (slave = 1; slave < num_hosts; slave++)
1300     {
1301       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1302       dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
1303                                                    mc,
1304                                                    hosts[slave],
1305                                                    hosts[0],
1306                                                    cfg,
1307                                                    GNUNET_YES);
1308       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1309     }
1310     return;
1311   }
1312   reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
1313                                              host_registration_completion,
1314                                              NULL);
1315 }
1316
1317
1318 /**
1319  * Callback to signal successfull startup of the controller process
1320  *
1321  * @param cls the closure from GNUNET_TESTBED_controller_start()
1322  * @param config the configuration with which the controller has been started;
1323  *          NULL if status is not GNUNET_OK
1324  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1325  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1326  */
1327 static void
1328 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
1329 {
1330   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1331     GNUNET_SCHEDULER_cancel (abort_task);
1332   if (GNUNET_OK != status)
1333   {
1334     mc_proc = NULL;
1335     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1336     return;
1337   }
1338   event_mask = 0;
1339   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1340   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1341   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1342   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1343   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1344   mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
1345                                           &controller_event_cb, NULL);
1346   if (NULL == mc)
1347   {
1348     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1349                 _("Unable to connect to master controller -- Check config\n"));
1350     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1351     return;
1352   }
1353   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
1354   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1355                                              &do_abort, NULL);
1356 }
1357
1358 /**
1359  * Load search strings from given filename. One search string per line.
1360  *
1361  * @param filename filename of the file containing the search strings.
1362  * @param strings set of strings loaded from file. Caller needs to free this
1363  *                if number returned is greater than zero.
1364  * @return number of strings found in the file. GNUNET_SYSERR on error.
1365  */
1366 static int
1367 load_search_strings (const char *filename, char ***strings)
1368 {
1369   char *data;
1370   char *buf;
1371   uint64_t filesize;
1372   unsigned int offset;
1373   int str_cnt;
1374   unsigned int i;
1375
1376   if (NULL == filename)
1377   {
1378     return GNUNET_SYSERR;
1379   }
1380
1381   if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1382   {
1383     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1384                 "Could not find search strings file %s\n", filename);
1385     return GNUNET_SYSERR;
1386   }
1387   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1388     filesize = 0;
1389   if (0 == filesize)
1390   {
1391     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1392     return GNUNET_SYSERR;
1393   }
1394   data = GNUNET_malloc (filesize);
1395   if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1396   {
1397     GNUNET_free (data);
1398     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1399          filename);
1400     return GNUNET_SYSERR;
1401   }
1402   buf = data;
1403   offset = 0;
1404   str_cnt = 0;
1405   while (offset < (filesize - 1))
1406   {
1407     offset++;
1408     if (((data[offset] == '\n')) && (buf != &data[offset]))
1409     {
1410       data[offset] = '\0';
1411       str_cnt++;
1412       buf = &data[offset + 1];
1413     }
1414     else if ((data[offset] == '\n') || (data[offset] == '\0'))
1415       buf = &data[offset + 1];
1416   }
1417   *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1418   offset = 0;
1419   for (i = 0; i < str_cnt; i++)
1420   {
1421     (*strings)[i] = GNUNET_strdup (&data[offset]);
1422     offset += strlen ((*strings)[i]) + 1;
1423   }
1424   free (data);
1425   return str_cnt;
1426 }
1427
1428 /**
1429  * Main function that will be run by the scheduler.
1430  *
1431  * @param cls closure
1432  * @param args remaining command-line arguments
1433  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1434  * @param config configuration
1435  */
1436 static void
1437 run (void *cls, char *const *args, const char *cfgfile,
1438      const struct GNUNET_CONFIGURATION_Handle *config)
1439 {
1440   unsigned int nhost;
1441
1442   if (NULL == args[0])
1443   {
1444     fprintf (stderr, _("No hosts-file specified on command line. Exiting.\n"));
1445     return;
1446   }
1447   if (NULL == args[1])
1448   {
1449     fprintf (stderr, _("No policy directory specified on command line. Exiting.\n"));
1450     return;
1451   }
1452   num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
1453   if (0 == num_hosts)
1454   {
1455     fprintf (stderr, _("No hosts loaded. Need at least one host\n"));
1456     return;
1457   }
1458   for (nhost = 0; nhost < num_hosts; nhost++)
1459   {
1460     if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
1461     {
1462       fprintf (stderr, _("Host %s cannot start testbed\n"),
1463                          GNUNET_TESTBED_host_get_hostname (hosts[nhost]));
1464       break;
1465     }
1466   }
1467   if (num_hosts != nhost)
1468   {
1469     fprintf (stderr, _("Exiting\n"));
1470     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1471     return;
1472   }
1473   if (NULL == config)
1474   {
1475     fprintf (stderr, _("No configuration file given. Exiting\n"));
1476     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1477     return;
1478   }
1479   if ( (NULL != data_filename) &&
1480        (NULL == (data_file = 
1481                  GNUNET_DISK_file_open (data_filename,
1482                                         GNUNET_DISK_OPEN_READWRITE |
1483                                         GNUNET_DISK_OPEN_TRUNCATE |
1484                                         GNUNET_DISK_OPEN_CREATE,
1485                                         GNUNET_DISK_PERM_USER_READ |
1486                                         GNUNET_DISK_PERM_USER_WRITE))) )
1487     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1488                               "open",
1489                               data_filename);
1490   if (GNUNET_YES != GNUNET_DISK_directory_test (args[1]))
1491   {
1492     fprintf (stderr, _("Specified policies directory does not exist. Exiting.\n"));
1493     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1494     return;
1495   }
1496   policy_dir = args[1];
1497   if (GNUNET_YES != GNUNET_DISK_file_test (args[2]))
1498   {
1499     fprintf (stderr, _("No search strings file given. Exiting.\n"));
1500     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1501     return;
1502   }
1503   num_search_strings = load_search_strings (args[2], &search_strings);
1504   if (0 >= num_search_strings || NULL == search_strings)
1505   {
1506     fprintf (stderr, _("Error loading search strings. Exiting.\n"));
1507     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1508     return;
1509   }
1510   unsigned int i;
1511   for (i = 0; i < num_search_strings; i++)
1512     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "search string: %s\n", search_strings[i]);
1513   cfg = GNUNET_CONFIGURATION_dup (config);
1514   mc_proc =
1515       GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
1516                                        (hosts[0]),
1517                                        hosts[0],
1518                                        cfg,
1519                                        status_cb,
1520                                        NULL);
1521   abort_task =
1522       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1523                                     (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
1524                                     NULL);
1525 }
1526
1527
1528 /**
1529  * Main function.
1530  *
1531  * @param argc argument count
1532  * @param argv argument values
1533  * @return 0 on success
1534  */
1535 int
1536 main (int argc, char *const *argv)
1537 {
1538   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1539     {'d', "details", "FILENAME",
1540      gettext_noop ("name of the file for writing statistics"),
1541      1, &GNUNET_GETOPT_set_string, &data_filename},
1542     { 'n', "num-links", "COUNT",
1543       gettext_noop ("create COUNT number of random links"),
1544       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
1545     { 'e', "num-errors", "COUNT",
1546       gettext_noop ("tolerate COUNT number of continious timeout failures"),
1547       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails },
1548     { 't', "matching-timeout", "TIMEOUT",
1549       gettext_noop ("wait TIMEOUT seconds before considering a string match as failed"),
1550       GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout },
1551     { 's', "search-delay", "DELAY",
1552       gettext_noop ("wait DELAY minutes before starting string search"),
1553       GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
1554     GNUNET_GETOPT_OPTION_END
1555   };
1556   int ret;
1557
1558   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1559     return 2;
1560
1561   result = GNUNET_SYSERR;
1562   ret =
1563       GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
1564                           _("Profiler for regex/mesh"),
1565                           options, &run, NULL);
1566   if (GNUNET_OK != ret)
1567     return ret;
1568   if (GNUNET_OK != result)
1569     return 1;
1570   return 0;
1571 }