- verboser log, faster start
[oweals/gnunet.git] / src / regex / gnunet-regex-profiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 - 2013 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 regex/gnunet-regex-profiler.c
23  * @brief Regex profiler for testing distributed regex use.
24  * @author Bartlomiej Polot
25  * @author Maximilian 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_regex_lib.h"
35 #include "gnunet_arm_service.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_testbed_service.h"
38
39 /**
40  * DLL of operations
41  */
42 struct DLLOperation
43 {
44   /**
45    * The testbed operation handle
46    */
47   struct GNUNET_TESTBED_Operation *op;
48
49   /**
50    * Closure
51    */
52   void *cls;
53
54   /**
55    * The next pointer for DLL
56    */
57   struct DLLOperation *next;
58
59   /**
60    * The prev pointer for DLL
61    */
62   struct DLLOperation *prev;
63 };
64
65
66 /**
67  * Available states during profiling
68  */
69 enum State
70 {
71   /**
72    * Initial state
73    */
74   STATE_INIT = 0,
75
76   /**
77    * Starting slaves
78    */
79   STATE_SLAVES_STARTING,
80
81   /**
82    * Creating peers
83    */
84   STATE_PEERS_CREATING,
85
86   /**
87    * Starting peers
88    */
89   STATE_PEERS_STARTING,
90
91   /**
92    * Linking peers
93    */
94   STATE_PEERS_LINKING,
95
96   /**
97    * Matching strings against announced regexes
98    */
99   STATE_SEARCH_REGEX,
100
101   /**
102    * Destroying peers; we can do this as the controller takes care of stopping a
103    * peer if it is running
104    */
105   STATE_PEERS_DESTROYING
106 };
107
108
109 /**
110  * Peer handles.
111  */
112 struct RegexPeer
113 {
114   /**
115    * Peer id.
116    */
117   unsigned int id;
118
119   /**
120    * Peer configuration handle.
121    */
122   struct GNUNET_CONFIGURATION_Handle *cfg;
123
124   /**
125    * The actual testbed peer handle.
126    */
127   struct GNUNET_TESTBED_Peer *peer_handle;
128
129   /**
130    * Host on which the peer is running.
131    */
132   struct GNUNET_TESTBED_Host *host_handle;
133
134   /**
135    * Filename of the peer's policy file.
136    */
137   char *policy_file;
138
139   /**
140    * Peers search string.
141    */
142   const char *search_str;
143
144   /**
145    * Set to GNUNET_YES if the peer successfully matched the above
146    * search string. GNUNET_NO if the string could not be matched
147    * during the profiler run. GNUNET_SYSERR if the string matching
148    * timed out. Undefined if search_str is NULL
149    */
150   int search_str_matched;
151
152   /**
153    * Peer's ARM handle.
154    */
155   struct GNUNET_ARM_Handle *arm_handle;
156
157   /**
158    * Peer's DHT handle.
159    */
160   struct GNUNET_DHT_Handle *dht_handle;
161
162   /**
163    * Handle to a running regex search.
164    */
165    struct GNUNET_REGEX_search_handle *search_handle;
166
167   /**
168    * Testbed operation handle for the ARM and DHT services.
169    */
170   struct GNUNET_TESTBED_Operation *op_handle;
171
172   /**
173    * Peers's statistics handle.
174    */
175   struct GNUNET_STATISTICS_Handle *stats_handle;
176
177   /**
178    * Testbed operation handle for the statistics service.
179    */
180   struct GNUNET_TESTBED_Operation *stats_op_handle;
181
182   /**
183    * The starting time of a profiling step.
184    */
185   struct GNUNET_TIME_Absolute prof_start_time;
186 };
187
188
189 /**
190  * An array of hosts loaded from the hostkeys file
191  */
192 static struct GNUNET_TESTBED_Host **hosts;
193
194 /**
195  * Array of peer handles used to pass to
196  * GNUNET_TESTBED_overlay_configure_topology
197  */
198 static struct GNUNET_TESTBED_Peer **peer_handles;
199
200 /**
201  * The array of peers; we fill this as the peers are given to us by the testbed
202  */
203 static struct RegexPeer *peers;
204
205 /**
206  * Host registration handle
207  */
208 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
209
210 /**
211  * Handle to the master controller process
212  */
213 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
214
215 /**
216  * Handle to the master controller
217  */
218 static struct GNUNET_TESTBED_Controller *mc;
219
220 /**
221  * Handle to global configuration
222  */
223 static struct GNUNET_CONFIGURATION_Handle *cfg;
224
225 /**
226  * Head of the operations list
227  */
228 static struct DLLOperation *dll_op_head;
229
230 /**
231  * Tail of the operations list
232  */
233 static struct DLLOperation *dll_op_tail;
234
235 /**
236  * Peer linking - topology operation
237  */
238 static struct GNUNET_TESTBED_Operation *topology_op;
239
240 /**
241  * The handle for whether a host is habitable or not
242  */
243 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
244
245 /**
246  * Abort task identifier
247  */
248 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
249
250 /**
251  * Shutdown task identifier
252  */
253 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
254
255 /**
256  * Host registration task identifier
257  */
258 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
259
260 /**
261  * Global event mask for all testbed events
262  */
263 static uint64_t event_mask;
264
265 /**
266  * The starting time of a profiling step
267  */
268 static struct GNUNET_TIME_Absolute prof_start_time;
269
270 /**
271  * Duration profiling step has taken
272  */
273 static struct GNUNET_TIME_Relative prof_time;
274
275 /**
276  * Number of peers to be started by the profiler
277  */
278 static unsigned int num_peers;
279
280 /**
281  * Number of hosts in the hosts array
282  */
283 static unsigned int num_hosts;
284
285 /**
286  * Factor of number of links. num_links = num_peers * linking_factor.
287  */
288 static unsigned int linking_factor;
289
290 /**
291  * Number of random links to be established between peers
292  */
293 static unsigned int num_links;
294
295 /**
296  * Number of times we try overlay connect operations
297  */
298 static unsigned int retry_links;
299
300 /**
301  * Continuous failures during overlay connect operations
302  */
303 static unsigned int cont_fails;
304
305 /**
306  * Global testing status
307  */
308 static int result;
309
310 /**
311  * current state of profiling
312  */
313 enum State state;
314
315 /**
316  * Folder where policy files are stored.
317  */
318 static char * policy_dir;
319
320 /**
321  * Search strings.
322  */
323 static char **search_strings;
324
325 /**
326  * Number of search strings.
327  */
328 static int num_search_strings;
329
330 /**
331  * Number of peers found with search strings.
332  */
333 static unsigned int peers_found;
334
335 /**
336  * Search task identifier
337  */
338 static GNUNET_SCHEDULER_TaskIdentifier search_task;
339
340 /**
341  * Search timeout task identifier.
342  */
343 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
344
345 /**
346  * Search timeout in seconds.
347  */
348 static struct GNUNET_TIME_Relative search_timeout = { 60000 };
349
350 /**
351  * How long do we wait before starting the search?
352  * Default: 1 m.
353  */
354 static struct GNUNET_TIME_Relative search_delay = { 60000 };
355
356 /**
357  * File to log statistics to.
358  */
359 static struct GNUNET_DISK_FileHandle *data_file;
360
361 /**
362  * Filename to log statistics to.
363  */
364 static char *data_filename;
365
366 /**
367  * Maximal path compression length.
368  */
369 static unsigned int max_path_compression;
370
371 /**
372  * If we should distribute the search evenly throught all peers (each
373  * peer searches for a string) or if only one peer should search for
374  * all strings.
375  */
376 static int no_distributed_search;
377
378 /**
379  * Prefix used for regex announcing. We need to prefix the search
380  * strings with it, in order to find something.
381  */
382 static char * regex_prefix;
383
384
385 /******************************************************************************/
386 /******************************  DECLARATIONS  ********************************/
387 /******************************************************************************/
388
389
390 /**
391  * Search callback function.
392  *
393  * @param cls Closure provided in GNUNET_REGEX_search.
394  * @param id Peer providing a regex that matches the string.
395  * @param get_path Path of the get request.
396  * @param get_path_length Lenght of get_path.
397  * @param put_path Path of the put request.
398  * @param put_path_length Length of the put_path.
399  */
400 static void
401 regex_found_handler (void *cls,
402                      const struct GNUNET_PeerIdentity *id,
403                      const struct GNUNET_PeerIdentity *get_path,
404                      unsigned int get_path_length,
405                      const struct GNUNET_PeerIdentity *put_path,
406                      unsigned int put_path_length);
407
408
409 /**
410  * DHT connect callback.
411  *
412  * @param cls internal peer id.
413  * @param op operation handle.
414  * @param ca_result connect adapter result.
415  * @param emsg error message.
416  */
417 static void
418 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
419                 void *ca_result, const char *emsg);
420
421 /**
422  * DHT connect adapter.
423  *
424  * @param cls not used.
425  * @param cfg configuration handle.
426  *
427  * @return
428  */
429 static void *
430 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
431
432
433 /**
434  * Adapter function called to destroy a connection to
435  * the DHT service
436  *
437  * @param cls closure
438  * @param op_result service handle returned from the connect adapter
439  */
440 static void
441 dht_da (void *cls, void *op_result);
442
443
444 /**
445  * Function called by testbed once we are connected to stats
446  * service. Get the statistics for the services of interest.
447  *
448  * @param cls the 'struct RegexPeer' for which we connected to stats
449  * @param op connect operation handle
450  * @param ca_result handle to stats service
451  * @param emsg error message on failure
452  */
453 static void
454 stats_connect_cb (void *cls,
455                   struct GNUNET_TESTBED_Operation *op,
456                   void *ca_result,
457                   const char *emsg);
458
459
460 /**
461  * Task to collect all statistics from all peers, will shutdown the
462  * profiler, when done.
463  *
464  * @param cls NULL
465  * @param tc the task context
466  */
467 static void
468 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
469
470
471 /******************************************************************************/
472 /********************************  SHUTDOWN  **********************************/
473 /******************************************************************************/
474
475
476 /**
477  * Shutdown nicely
478  *
479  * @param cls NULL
480  * @param tc the task context
481  */
482 static void
483 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
484 {
485   struct DLLOperation *dll_op;
486   struct RegexPeer *peer;
487   unsigned int nhost;
488   unsigned int peer_cnt;
489   unsigned int search_str_cnt;
490   char output_buffer[512];
491   size_t size;
492
493   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
494   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
495     GNUNET_SCHEDULER_cancel (abort_task);
496   if (NULL != hc_handles)
497   {
498     for (nhost = 0; nhost < num_hosts; nhost++)
499       if (NULL != hc_handles[nhost])
500         GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[nhost]);
501     GNUNET_free (hc_handles);
502     hc_handles = NULL;
503   }
504   if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
505     GNUNET_SCHEDULER_cancel (register_hosts_task);
506
507   for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
508   {
509     peer = &peers[peer_cnt];
510
511     if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
512     {
513       prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
514       size =
515         GNUNET_snprintf (output_buffer,
516                          sizeof (output_buffer),
517                          "%p Search string not found: %s (%d)\n%p On peer: %u (%p)\n%p With policy file: %s\n%p After: %s\n",
518                          peer, peer->search_str, peer->search_str_matched,
519                          peer, peer->id, peer,
520                          peer, peer->policy_file,
521                          peer,
522                          GNUNET_STRINGS_relative_time_to_string (prof_time,
523                                                                  GNUNET_NO));
524       if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
525         GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
526     }
527
528     if (NULL != peers[peer_cnt].op_handle)
529       GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
530     if (NULL != peers[peer_cnt].stats_op_handle)
531       GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
532   }
533
534   if (NULL != data_file)
535     GNUNET_DISK_file_close (data_file);
536
537   for (search_str_cnt = 0;
538        search_str_cnt < num_search_strings && NULL != search_strings;
539        search_str_cnt++)
540   {
541     GNUNET_free_non_null (search_strings[search_str_cnt]);
542   }
543   GNUNET_free_non_null (search_strings);
544
545   if (NULL != reg_handle)
546     GNUNET_TESTBED_cancel_registration (reg_handle);
547   if (NULL != topology_op)
548     GNUNET_TESTBED_operation_done (topology_op);
549   for (nhost = 0; nhost < num_hosts; nhost++)
550     if (NULL != hosts[nhost])
551       GNUNET_TESTBED_host_destroy (hosts[nhost]);
552   GNUNET_free_non_null (hosts);
553
554   while (NULL != (dll_op = dll_op_head))
555   {
556     GNUNET_TESTBED_operation_done (dll_op->op);
557     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
558     GNUNET_free (dll_op);
559   }
560   if (NULL != mc)
561     GNUNET_TESTBED_controller_disconnect (mc);
562   if (NULL != mc_proc)
563     GNUNET_TESTBED_controller_stop (mc_proc);
564   if (NULL != cfg)
565     GNUNET_CONFIGURATION_destroy (cfg);
566
567   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
568 }
569
570
571 /**
572  * abort task to run on test timed out
573  *
574  * @param cls NULL
575  * @param tc the task context
576  */
577 static void
578 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
579 {
580   unsigned long i = (unsigned long) cls;
581
582   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting %lu...\n", i);
583   abort_task = GNUNET_SCHEDULER_NO_TASK;
584   result = GNUNET_SYSERR;
585   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
586     GNUNET_SCHEDULER_cancel (shutdown_task);
587   shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
588 }
589
590
591 /******************************************************************************/
592 /*********************  STATISTICS SERVICE CONNECTIONS  ***********************/
593 /******************************************************************************/
594
595 /**
596  * Adapter function called to establish a connection to
597  * statistics service.
598  *
599  * @param cls closure
600  * @param cfg configuration of the peer to connect to; will be available until
601  *          GNUNET_TESTBED_operation_done() is called on the operation returned
602  *          from GNUNET_TESTBED_service_connect()
603  * @return service handle to return in 'op_result', NULL on error
604  */
605 static void *
606 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
607 {
608   return GNUNET_STATISTICS_create ("<driver>", cfg);
609 }
610
611
612 /**
613  * Adapter function called to destroy a connection to
614  * statistics service.
615  *
616  * @param cls closure
617  * @param op_result service handle returned from the connect adapter
618  */
619 static void
620 stats_da (void *cls, void *op_result)
621 {
622   struct RegexPeer *peer = cls;
623
624   GNUNET_assert (op_result == peer->stats_handle);
625
626   GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
627   peer->stats_handle = NULL;
628 }
629
630
631 /**
632  * Process statistic values. Write all values to global 'data_file', if present.
633  *
634  * @param cls closure
635  * @param subsystem name of subsystem that created the statistic
636  * @param name the name of the datum
637  * @param value the current value
638  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
639  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
640  */
641 static int
642 stats_iterator (void *cls, const char *subsystem, const char *name,
643                 uint64_t value, int is_persistent)
644 {
645   struct RegexPeer *peer = cls;
646   char output_buffer[512];
647   size_t size;
648
649   if (NULL == data_file)
650   {
651     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
652                 "%p -> %s [%s]: %llu\n",
653                 peer, subsystem, name, value);
654     return GNUNET_OK;
655   }
656   size =
657     GNUNET_snprintf (output_buffer,
658                      sizeof (output_buffer),
659                      "%p [%s] %llu %s\n",
660                      peer,
661                      subsystem, value, name);
662   if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
663     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
664
665   return GNUNET_OK;
666 }
667
668
669 /**
670  * Stats callback. Finish the stats testbed operation and when all stats have
671  * been iterated, shutdown the profiler.
672  *
673  * @param cls closure
674  * @param success GNUNET_OK if statistics were
675  *        successfully obtained, GNUNET_SYSERR if not.
676  */
677 static void
678 stats_cb (void *cls,
679           int success)
680 {
681   static unsigned int peer_cnt;
682   struct RegexPeer *peer = cls;
683
684   if (GNUNET_OK != success)
685   {
686     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
687                 "Getting statistics for peer %u failed!\n",
688                 peer->id);
689     return;
690   }
691
692   GNUNET_assert (NULL != peer->stats_op_handle);
693
694   GNUNET_TESTBED_operation_done (peer->stats_op_handle);
695   peer->stats_op_handle = NULL;
696
697   peer_cnt++;
698   peer = &peers[peer_cnt];
699
700   if (peer_cnt == num_peers)
701   {
702     struct GNUNET_TIME_Relative delay = { 100 };
703     shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
704   }
705   else
706   {
707     peer->stats_op_handle =
708       GNUNET_TESTBED_service_connect (NULL,
709                                       peer->peer_handle,
710                                       "statistics",
711                                       &stats_connect_cb,
712                                       peer,
713                                       &stats_ca,
714                                       &stats_da,
715                                       peer);
716   }
717 }
718
719
720 /**
721  * Function called by testbed once we are connected to stats
722  * service. Get the statistics for the services of interest.
723  *
724  * @param cls the 'struct RegexPeer' for which we connected to stats
725  * @param op connect operation handle
726  * @param ca_result handle to stats service
727  * @param emsg error message on failure
728  */
729 static void
730 stats_connect_cb (void *cls,
731                   struct GNUNET_TESTBED_Operation *op,
732                   void *ca_result,
733                   const char *emsg)
734 {
735   struct RegexPeer *peer = cls;
736
737   if (NULL == ca_result || NULL != emsg)
738   {
739     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
740                 "Failed to connect to statistics service on peer %u: %s\n",
741                 peer->id, emsg);
742
743     peer->stats_handle = NULL;
744     return;
745   }
746
747   peer->stats_handle = ca_result;
748
749   if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
750                                      GNUNET_TIME_UNIT_FOREVER_REL,
751                                      &stats_cb,
752                                      &stats_iterator, peer))
753   {
754     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
755                 "Could not get statistics of peer %u!\n", peer->id);
756   }
757 }
758
759
760 /**
761  * Task to collect all statistics from all peers, will shutdown the
762  * profiler, when done.
763  *
764  * @param cls NULL
765  * @param tc the task context
766  */
767 static void
768 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
769 {
770   struct RegexPeer *peer = &peers[0];
771
772   GNUNET_assert (NULL != peer->peer_handle);
773
774   peer->stats_op_handle =
775     GNUNET_TESTBED_service_connect (NULL,
776                                     peer->peer_handle,
777                                     "statistics",
778                                     &stats_connect_cb,
779                                     peer,
780                                     &stats_ca,
781                                     &stats_da,
782                                     peer);
783 }
784
785
786 /******************************************************************************/
787 /************************  MESH SERVICE CONNECTIONS  **************************/
788 /******************************************************************************/
789
790 /**
791  * Method called when we've found a peer that announced a regex
792  * that matches our search string. Now get the statistics.
793  *
794  * @param cls Closure provided in GNUNET_REGEX_search.
795  * @param id Peer providing a regex that matches the string.
796  * @param get_path Path of the get request.
797  * @param get_path_length Lenght of get_path.
798  * @param put_path Path of the put request.
799  * @param put_path_length Length of the put_path.
800  */
801 static void
802 regex_found_handler (void *cls,
803                      const struct GNUNET_PeerIdentity *id,
804                      const struct GNUNET_PeerIdentity *get_path,
805                      unsigned int get_path_length,
806                      const struct GNUNET_PeerIdentity *put_path,
807                      unsigned int put_path_length)
808 {
809   struct RegexPeer *peer = cls;
810   char output_buffer[512];
811   size_t size;
812
813   if (GNUNET_YES == peer->search_str_matched)
814   {
815     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
816                 "String %s on peer %u already matched!\n",
817                 peer->search_str, peer->id);
818     return;
819   }
820
821   peers_found++;
822
823   if (NULL == id)
824   {
825     // FIXME not possible right now
826     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
827                 "String matching timed out for string %s on peer %u (%i/%i)\n",
828                 peer->search_str, peer->id, peers_found, num_search_strings);
829
830     printf ("String matching timed out for string %s on peer %u (%i/%i)\n",
831             peer->search_str, peer->id, peers_found, num_search_strings);
832
833     peer->search_str_matched = GNUNET_SYSERR;
834   }
835   else
836   {
837     prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
838     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
839                 "String %s successfully matched on peer %u after %s (%i/%i)\n",
840                 peer->search_str, peer->id, GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
841                 peers_found, num_search_strings);
842
843     printf ("String %s successfully matched on peer %u after %s (%i/%i)\n",
844             peer->search_str, peer->id, GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
845             peers_found, num_search_strings);
846     fflush (stdout);
847
848     peer->search_str_matched = GNUNET_YES;
849
850     if (NULL != data_file)
851     {
852       size =
853         GNUNET_snprintf (output_buffer,
854                          sizeof (output_buffer),
855                          "%p Peer: %u\n%p Host: %s\n%p Policy file: %s\n"
856                          "%p Search string: %s\n%p Search duration: %s\n\n",
857                          peer, peer->id,
858                          peer,
859                          GNUNET_TESTBED_host_get_hostname (peer->host_handle),
860                          peer, peer->policy_file,
861                          peer, peer->search_str,
862                          peer,
863                          GNUNET_STRINGS_relative_time_to_string (prof_time,
864                                                                  GNUNET_NO));
865
866       if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
867         GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
868     }
869   }
870
871   GNUNET_TESTBED_operation_done (peer->op_handle);
872   peer->op_handle = NULL;
873
874   if (peers_found == num_search_strings)
875   {
876     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
877     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
878                 "All strings successfully matched in %s\n",
879                 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
880     printf ("All strings successfully matched.\n");
881     fflush (stdout);
882
883     if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
884       GNUNET_SCHEDULER_cancel (search_timeout_task);
885
886     printf ("Collecting stats and shutting down.\n");
887     GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
888   }
889 }
890
891
892 /**
893  * Connect by string timeout task. This will cancel the profiler after the
894  * specified timeout 'search_timeout'.
895  *
896  * @param cls NULL
897  * @param tc the task context
898  */
899 static void
900 do_connect_by_string_timeout (void *cls,
901                               const struct GNUNET_SCHEDULER_TaskContext * tc)
902 {
903   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
904               "Finding matches to all strings did not succeed after %s.\n",
905               GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
906   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
907               "Found %i of %i strings\n", peers_found, num_search_strings);
908
909   printf ("Search timed out after %s. Collecting stats and shutting down.\n", 
910           GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
911   fflush (stdout);
912
913   GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
914 }
915
916
917 /**
918  * Connect by string task that is run to search for a string in the
919  * NFA. It first connects to the mesh service and when a connection is
920  * established it starts to search for the string.
921  *
922  * @param cls NULL
923  * @param tc the task context
924  */
925 static void
926 do_connect_by_string (void *cls,
927                       const struct GNUNET_SCHEDULER_TaskContext * tc)
928 {
929   printf ("Starting string search.\n");
930   fflush (stdout);
931
932   peers[0].search_str = search_strings[0];
933   peers[0].search_str_matched = GNUNET_NO;
934
935   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
936               "Searching for string \"%s\" on peer %d with file %s\n",
937               peers[0].search_str, 0, peers[0].policy_file);
938
939     /* First connect to mesh service, then search for string. Next
940        connect will be in mesh_connect_cb */
941     peers[0].op_handle =
942       GNUNET_TESTBED_service_connect (NULL,
943                                       peers[0].peer_handle,
944                                       "dht",
945                                       &dht_connect_cb,
946                                       &peers[0],
947                                       &dht_ca,
948                                       &dht_da,
949                                       &peers[0]);
950
951   search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout,
952                                                       &do_connect_by_string_timeout, NULL);
953 }
954
955 /**
956  * Start searching for the next string in the DHT.
957  *
958  * @param cls Index of the next peer in the peers array.
959  * @param tc TaskContext.
960  */
961 void
962 find_next_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
963 {
964   long next_p = (long) cls;
965
966   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
967     return;
968
969   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
970               "Searching for string \"%s\" on peer %d with file %s\n",
971               peers[next_p].search_str, next_p, peers[next_p].policy_file);
972
973   /* FIXME
974     * dont connect to a new dht for each peer, we might want to seach for n
975     * strings on m peers where n > m
976     */
977   peers[next_p].op_handle =
978     GNUNET_TESTBED_service_connect (NULL,
979                                     peers[next_p].peer_handle,
980                                     "dht",
981                                     &dht_connect_cb,
982                                     &peers[next_p],
983                                     &dht_ca,
984                                     &dht_da,
985                                     &peers[next_p]);
986 }
987
988
989 /**
990  * ARM connect adapter. Opens a connection to the ARM service.
991  *
992  * @param cls Closure (peer).
993  * @param cfg Configuration handle.
994  *
995  * @return
996  */
997 static void *
998 arm_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
999 {
1000   struct RegexPeer *peer = cls;
1001
1002   peer->arm_handle = GNUNET_ARM_connect (cfg, NULL);
1003
1004   return peer->arm_handle;
1005 }
1006
1007
1008 /**
1009  * Adapter function called to destroy a connection to the ARM service.
1010  *
1011  * @param cls Closure (peer).
1012  * @param op_result Service handle returned from the connect adapter.
1013  */
1014 static void
1015 arm_da (void *cls, void *op_result)
1016 {
1017   struct RegexPeer *peer = (struct RegexPeer *) cls;
1018
1019   GNUNET_assert (peer->arm_handle == op_result);
1020
1021   if (NULL != peer->arm_handle)
1022   {
1023     GNUNET_ARM_disconnect (peer->arm_handle);
1024     peer->arm_handle = NULL;
1025   }
1026 }
1027
1028
1029 /**
1030  * Start announcing the next regex in the DHT.
1031  *
1032  * @param cls Index of the next peer in the peers array.
1033  * @param tc TaskContext.
1034  */
1035 void
1036 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1037
1038
1039 /**
1040  * Callback function invoked when ARM peration is complete: deamon is started.
1041  *
1042  * @param cls Closure (RegexPeer).
1043  * @param result Outcome of the operation.
1044  */
1045 static void
1046 arm_start_cb (void *cls, enum GNUNET_ARM_ProcessStatus result)
1047 {
1048   struct RegexPeer *peer = (struct RegexPeer *) cls;
1049   static unsigned int peer_cnt;
1050   unsigned int next_p;
1051
1052   switch (result)
1053   {
1054       /**
1055        * Service is currently being started (due to client request).
1056        */
1057     case GNUNET_ARM_PROCESS_STARTING:
1058       GNUNET_TESTBED_operation_done (peer->op_handle);
1059       peer->op_handle = NULL;
1060
1061       if (peer_cnt < (num_peers - 1))
1062       {
1063         next_p = (++peer_cnt % num_peers);
1064         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
1065                                         GNUNET_TIME_UNIT_MILLISECONDS,
1066                                         200),
1067                                       &announce_next_regex,
1068                                       (void *) (long) next_p);
1069       }
1070       else
1071       {
1072         printf ("All daemons started. Waiting %s to start string searches\n",
1073                 GNUNET_STRINGS_relative_time_to_string (search_delay,
1074                                                         GNUNET_NO));
1075         fflush (stdout);
1076         GNUNET_SCHEDULER_add_delayed (search_delay,
1077                                       do_connect_by_string, 
1078                                       NULL);
1079       }
1080       break;
1081
1082       /**
1083        * Service name is unknown to ARM.
1084        */
1085     case GNUNET_ARM_PROCESS_UNKNOWN:
1086       /**
1087        * Service is now down (due to client request).
1088        */
1089     case GNUNET_ARM_PROCESS_DOWN:
1090       /**
1091        * Service is already running.
1092        */
1093     case GNUNET_ARM_PROCESS_ALREADY_RUNNING:
1094       /**
1095        * Service is already being stopped by some other client.
1096        */
1097     case GNUNET_ARM_PROCESS_ALREADY_STOPPING:
1098       /**
1099        * Service is already down (no action taken)
1100        */
1101     case GNUNET_ARM_PROCESS_ALREADY_DOWN: 
1102       /**
1103        * ARM is currently being shut down (no more process starts)
1104        */
1105     case GNUNET_ARM_PROCESS_SHUTDOWN:
1106       /**
1107        * Error in communication with ARM
1108        */
1109     case GNUNET_ARM_PROCESS_COMMUNICATION_ERROR:
1110       /**
1111        * Timeout in communication with ARM
1112        */
1113     case GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT:
1114       /**
1115       * Failure to perform operation
1116       */
1117     case GNUNET_ARM_PROCESS_FAILURE:
1118     default:
1119       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ARM returned %d\n", result);
1120       GNUNET_abort ();
1121   }
1122 }
1123
1124
1125 /**
1126  * ARM connect callback. Called when we are connected to the arm service for
1127  * the peer in 'cls'. If successfull we start the regex deamon to start
1128  * announcing the regex of this peer.
1129  *
1130  * @param cls internal peer id.
1131  * @param op operation handle.
1132  * @param ca_result connect adapter result.
1133  * @param emsg error message.
1134  */
1135 static void
1136 arm_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1137                 void *ca_result, const char *emsg)
1138 {
1139   struct RegexPeer *peer = (struct RegexPeer *) cls;
1140
1141   if (NULL != emsg || NULL == op || NULL == ca_result)
1142   {
1143     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ARM connect failed: %s\n", emsg);
1144     GNUNET_abort ();
1145   }
1146
1147   GNUNET_assert (NULL != peer->arm_handle);
1148   GNUNET_assert (peer->op_handle == op);
1149   GNUNET_assert (peer->arm_handle == ca_result);
1150
1151   GNUNET_ARM_start_service (ca_result, "regexprofiler",
1152                             GNUNET_OS_INHERIT_STD_NONE,
1153                             GNUNET_TIME_UNIT_FOREVER_REL,
1154                             &arm_start_cb,
1155                             peer);
1156 }
1157
1158
1159 /**
1160  * Task to start the daemons on each peer so that the regexes are announced
1161  * into the DHT.
1162  *
1163  * @param cls NULL
1164  * @param tc the task context
1165  */
1166 static void
1167 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1168 {
1169   printf ("Starting announce.\n");
1170   fflush (stdout);
1171
1172     /* First connect to arm service, then announce. Next
1173        announce will be in arm_connect_cb */
1174     peers[0].op_handle =
1175       GNUNET_TESTBED_service_connect (NULL,
1176                                       peers[0].peer_handle,
1177                                       "arm",
1178                                       &arm_connect_cb,
1179                                       &peers[0],
1180                                       &arm_ca,
1181                                       &arm_da,
1182                                       &peers[0]);
1183
1184 }
1185
1186
1187 /**
1188  * Start announcing the next regex in the DHT.
1189  *
1190  * @param cls Index of the next peer in the peers array.
1191  * @param tc TaskContext.
1192  */
1193 void
1194 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1195 {
1196   long next_p = (long) cls;
1197
1198   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1199     return;
1200
1201   printf ("Starting daemon %ld\n", next_p);
1202   fflush (stdout);
1203
1204   peers[next_p].op_handle =
1205     GNUNET_TESTBED_service_connect (NULL,
1206                                     peers[next_p].peer_handle,
1207                                     "arm",
1208                                     &arm_connect_cb,
1209                                     &peers[next_p],
1210                                     &arm_ca,
1211                                     &arm_da,
1212                                     &peers[next_p]);
1213 }
1214
1215
1216 /**
1217  * DHT connect callback. Called when we are connected to the dht service for
1218  * the peer in 'cls'. If successfull we connect to the stats service of this
1219  * peer and then try to match the search string of this peer.
1220  *
1221  * @param cls internal peer id.
1222  * @param op operation handle.
1223  * @param ca_result connect adapter result.
1224  * @param emsg error message.
1225  */
1226 static void
1227 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1228                 void *ca_result, const char *emsg)
1229 {
1230   struct RegexPeer *peer = (struct RegexPeer *) cls;
1231   static unsigned int peer_cnt;
1232   unsigned int next_p;
1233
1234   if (NULL != emsg || NULL == op || NULL == ca_result)
1235   {
1236     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1237     GNUNET_abort ();
1238   }
1239
1240   GNUNET_assert (NULL != peer->dht_handle);
1241   GNUNET_assert (peer->op_handle == op);
1242   GNUNET_assert (peer->dht_handle == ca_result);
1243
1244   peer->search_str_matched = GNUNET_NO;
1245   peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
1246                                              peer->search_str,
1247                                              &regex_found_handler, peer,
1248                                              NULL);
1249   peer->prof_start_time = GNUNET_TIME_absolute_get ();
1250
1251   if (peer_cnt < (num_search_strings - 1))
1252   {
1253     if (GNUNET_YES == no_distributed_search)
1254       next_p = 0;
1255     else
1256       next_p = (++peer_cnt % num_peers);
1257
1258     peers[next_p].search_str = search_strings[next_p];
1259     peers[next_p].search_str_matched = GNUNET_NO;
1260
1261     /* Don't start all searches at once */
1262     /* TODO add some intelligence to the timeout */
1263     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1264                                   &find_next_string,
1265                                   (void *) (long) next_p);
1266   }
1267 }
1268
1269
1270 /**
1271  * DHT connect adapter. Opens a connection to the dht service.
1272  *
1273  * @param cls Closure (peer).
1274  * @param cfg Configuration handle.
1275  *
1276  * @return
1277  */
1278 static void *
1279 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1280 {
1281   struct RegexPeer *peer = cls;
1282
1283   peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1284
1285   return peer->dht_handle;
1286 }
1287
1288
1289 /**
1290  * Adapter function called to destroy a connection to the dht service.
1291  *
1292  * @param cls Closure (peer).
1293  * @param op_result Service handle returned from the connect adapter.
1294  */
1295 static void
1296 dht_da (void *cls, void *op_result)
1297 {
1298   struct RegexPeer *peer = (struct RegexPeer *) cls;
1299
1300   GNUNET_assert (peer->dht_handle == op_result);
1301
1302   if (NULL != peer->search_handle)
1303   {
1304     GNUNET_REGEX_search_cancel (peer->search_handle);
1305     peer->search_handle = NULL;
1306   }
1307
1308   if (NULL != peer->dht_handle)
1309   {
1310     GNUNET_DHT_disconnect (peer->dht_handle);
1311     peer->dht_handle = NULL;
1312   }
1313 }
1314
1315
1316 /******************************************************************************/
1317 /***************************  TESTBED PEER SETUP  *****************************/
1318 /******************************************************************************/
1319
1320
1321 /**
1322  * Configure the peer overlay topology.
1323  *
1324  * @param cls NULL
1325  * @param tc the task context
1326  */
1327 static void
1328 do_configure_topology (void *cls,
1329                        const struct GNUNET_SCHEDULER_TaskContext * tc)
1330 {
1331   /*
1332     if (0 == linking_factor)
1333     linking_factor = 1;
1334     num_links = linking_factor * num_peers;
1335   */
1336   /* num_links = num_peers - 1; */
1337   num_links = linking_factor;
1338
1339   /* Do overlay connect */
1340   prof_start_time = GNUNET_TIME_absolute_get ();
1341   topology_op =
1342     GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
1343                                                NULL,
1344                                                NULL,
1345                                                NULL,
1346                                                GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
1347                                                num_links,
1348                                                GNUNET_TESTBED_TOPOLOGY_RETRY_CNT,
1349                                                (unsigned int) 0,
1350                                                GNUNET_TESTBED_TOPOLOGY_OPTION_END);
1351   if (NULL == topology_op)
1352   {
1353     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1354                 "Cannot create topology, op handle was NULL\n");
1355     GNUNET_assert (0);
1356   }
1357 }
1358
1359
1360 /**
1361  * Functions of this signature are called when a peer has been successfully
1362  * started or stopped.
1363  *
1364  * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
1365  * @param emsg NULL on success; otherwise an error description
1366  */
1367 static void
1368 peer_churn_cb (void *cls, const char *emsg)
1369 {
1370   struct DLLOperation *dll_op = cls;
1371   struct GNUNET_TESTBED_Operation *op;
1372   static unsigned int started_peers;
1373   unsigned int peer_cnt;
1374
1375   op = dll_op->op;
1376   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1377   GNUNET_free (dll_op);
1378   if (NULL != emsg)
1379   {
1380     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1381          _("An operation has failed while starting peers: %s\n"), emsg);
1382     GNUNET_TESTBED_operation_done (op);
1383     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1384       GNUNET_SCHEDULER_cancel (abort_task);
1385     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1386     return;
1387   }
1388   GNUNET_TESTBED_operation_done (op);
1389   if (++started_peers == num_peers)
1390   {
1391     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1392     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1393                 "All peers started successfully in %s\n",
1394                 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1395     result = GNUNET_OK;
1396
1397     peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
1398     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1399       peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
1400
1401     state = STATE_PEERS_LINKING;
1402     GNUNET_SCHEDULER_add_now (&do_configure_topology, NULL);
1403   }
1404 }
1405
1406
1407 /**
1408  * Functions of this signature are called when a peer has been successfully
1409  * created
1410  *
1411  * @param cls the closure from GNUNET_TESTBED_peer_create()
1412  * @param peer the handle for the created peer; NULL on any error during
1413  *          creation
1414  * @param emsg NULL if peer is not NULL; else MAY contain the error description
1415  */
1416 static void
1417 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
1418 {
1419   struct DLLOperation *dll_op = cls;
1420   struct RegexPeer *peer_ptr;
1421   static unsigned int created_peers;
1422   unsigned int peer_cnt;
1423
1424   if (NULL != emsg)
1425   {
1426     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1427          _("Creating a peer failed. Error: %s\n"), emsg);
1428     GNUNET_TESTBED_operation_done (dll_op->op);
1429     GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1430     GNUNET_free (dll_op);
1431     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1432       GNUNET_SCHEDULER_cancel (abort_task);
1433     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1434     return;
1435   }
1436
1437   peer_ptr = dll_op->cls;
1438   GNUNET_assert (NULL == peer_ptr->peer_handle);
1439   GNUNET_CONFIGURATION_destroy (peer_ptr->cfg);
1440   peer_ptr->cfg = NULL;
1441   peer_ptr->peer_handle = peer;
1442   GNUNET_TESTBED_operation_done (dll_op->op);
1443   GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1444   GNUNET_free (dll_op);
1445
1446   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
1447               peer_ptr->id,
1448               GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
1449
1450   if (++created_peers == num_peers)
1451   {
1452     prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1453     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1454                 "All peers created successfully in %s\n",
1455                 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1456     /* Now peers are to be started */
1457     state = STATE_PEERS_STARTING;
1458     prof_start_time = GNUNET_TIME_absolute_get ();
1459     for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1460     {
1461       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1462       dll_op->op = GNUNET_TESTBED_peer_start (dll_op,
1463                                               peers[peer_cnt].peer_handle,
1464                                               &peer_churn_cb, dll_op);
1465       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1466     }
1467   }
1468 }
1469
1470
1471 /**
1472  * Function called with a filename for each file in the policy directory. Create
1473  * a peer for each filename and update the peer's configuration to include the
1474  * max_path_compression specified as a command line argument as well as the
1475  * policy_file for this peer. The gnunet-service-regexprofiler service is
1476  * automatically started on this peer. The service reads the configurration and
1477  * announces the regexes stored in the policy file 'filename'.
1478  *
1479  * @param cls closure
1480  * @param filename complete filename (absolute path)
1481  * @return GNUNET_OK to continue to iterate,
1482  *  GNUNET_SYSERR to abort iteration with error!
1483  */
1484 static int
1485 policy_filename_cb (void *cls, const char *filename)
1486 {
1487   static unsigned int peer_cnt;
1488   struct DLLOperation *dll_op;
1489   struct RegexPeer *peer = &peers[peer_cnt];
1490
1491   GNUNET_assert (NULL != peer);
1492
1493   peer->policy_file = GNUNET_strdup (filename);
1494
1495   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Creating peer %i on host %s for policy file %s\n",
1496               peer->id,
1497               GNUNET_TESTBED_host_get_hostname (peer->host_handle),
1498               filename);
1499
1500   /* Set configuration options specific for this peer
1501      (max_path_compression and policy_file */
1502   peer->cfg = GNUNET_CONFIGURATION_dup (cfg);
1503   GNUNET_CONFIGURATION_set_value_number (peer->cfg, "REGEXPROFILER",
1504                                          "MAX_PATH_COMPRESSION",
1505                                          (unsigned long long)max_path_compression);
1506   GNUNET_CONFIGURATION_set_value_string (peer->cfg, "REGEXPROFILER",
1507                                          "POLICY_FILE", filename);
1508
1509   dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1510   dll_op->cls = &peers[peer_cnt];
1511   dll_op->op = GNUNET_TESTBED_peer_create (mc,
1512                                            peer->host_handle,
1513                                            peer->cfg,
1514                                            &peer_create_cb,
1515                                            dll_op);
1516   GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1517
1518   peer_cnt++;
1519
1520   return GNUNET_OK;
1521 }
1522
1523
1524 /**
1525  * Controller event callback.
1526  *
1527  * @param cls NULL
1528  * @param event the controller event
1529  */
1530 static void
1531 controller_event_cb (void *cls,
1532                      const struct GNUNET_TESTBED_EventInformation *event)
1533 {
1534   struct DLLOperation *dll_op;
1535   struct GNUNET_TESTBED_Operation *op;
1536   int ret;
1537
1538   switch (state)
1539   {
1540   case STATE_SLAVES_STARTING:
1541     switch (event->type)
1542     {
1543     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1544       {
1545         static unsigned int slaves_started;
1546         unsigned int peer_cnt;
1547
1548         dll_op = event->details.operation_finished.op_cls;
1549         GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1550         GNUNET_free (dll_op);
1551         op = event->details.operation_finished.operation;
1552         if (NULL != event->details.operation_finished.emsg)
1553         {
1554           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1555                _("An operation has failed while starting slaves\n"));
1556           GNUNET_TESTBED_operation_done (op);
1557           if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1558             GNUNET_SCHEDULER_cancel (abort_task);
1559           abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1560           return;
1561         }
1562         GNUNET_TESTBED_operation_done (op);
1563         /* Proceed to start peers */
1564         if (++slaves_started == num_hosts - 1)
1565         {
1566           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1567                       "All slaves started successfully\n");
1568
1569           state = STATE_PEERS_CREATING;
1570           prof_start_time = GNUNET_TIME_absolute_get ();
1571
1572           if (-1 == (ret = GNUNET_DISK_directory_scan (policy_dir,
1573                                                        NULL,
1574                                                        NULL)))
1575           {
1576             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1577                         _("No files found in `%s'\n"),
1578                         policy_dir);
1579             GNUNET_SCHEDULER_shutdown ();
1580             return;
1581           }
1582           num_peers = (unsigned int) ret;
1583           peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1584
1585           /* Initialize peers */
1586           for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1587           {
1588             struct RegexPeer *peer = &peers[peer_cnt];
1589             peer->id = peer_cnt;
1590             peer->policy_file = NULL;
1591             /* Do not start peers on hosts[0] (master controller) */
1592             peer->host_handle = hosts[1 + (peer_cnt % (num_hosts -1))];
1593             peer->dht_handle = NULL;
1594             peer->search_handle = NULL;
1595             peer->stats_handle = NULL;
1596             peer->stats_op_handle = NULL;
1597             peer->search_str = NULL;
1598             peer->search_str_matched = GNUNET_NO;
1599           }
1600
1601           GNUNET_DISK_directory_scan (policy_dir,
1602                                       &policy_filename_cb,
1603                                       NULL);
1604         }
1605       }
1606       break;
1607     default:
1608       GNUNET_assert (0);
1609     }
1610     break;
1611   case STATE_PEERS_STARTING:
1612     switch (event->type)
1613     {
1614     case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1615       /* Control reaches here when peer start fails */
1616     case GNUNET_TESTBED_ET_PEER_START:
1617       /* we handle peer starts in peer_churn_cb */
1618       break;
1619     default:
1620       GNUNET_assert (0);
1621     }
1622     break;
1623   case STATE_PEERS_LINKING:
1624    switch (event->type)
1625    {
1626      static unsigned int established_links;
1627    case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1628      /* Control reaches here when a peer linking operation fails */
1629      if (NULL != event->details.operation_finished.emsg)
1630      {
1631        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1632                    _("An operation has failed while linking\n"));
1633        printf ("F%u ", retry_links);
1634        fflush (stdout);
1635        retry_links++;
1636      }
1637      /* We do no retries, consider this link as established */
1638      /* break; */
1639    case GNUNET_TESTBED_ET_CONNECT:
1640    {
1641      char output_buffer[512];
1642      size_t size;
1643
1644      if (0 == established_links)
1645        printf ("Establishing links .");
1646      else
1647      {
1648        printf (".");
1649        fflush (stdout);
1650      }
1651      if (++established_links == num_links)
1652      {
1653        fflush (stdout);
1654        prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1655        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1656                    "%u links established in %s\n",
1657                    num_links,
1658                    GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1659        prof_time = GNUNET_TIME_relative_divide(prof_time, num_links);
1660        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1661                    "Average of %s per connection\n",
1662                    GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1663        result = GNUNET_OK;
1664        GNUNET_free (peer_handles);
1665
1666        if (NULL != data_file)
1667        {
1668          size =
1669            GNUNET_snprintf (output_buffer,
1670                             sizeof (output_buffer),
1671                             "# of peers: %u\n# of links established: %u\n"
1672                             "Time to establish links: %s\nLinking failures: %u\n"
1673                             "path compression length: %u\n# of search strings: %u\n",
1674                             num_peers,
1675                             (established_links - cont_fails),
1676                             GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
1677                             cont_fails,
1678                             max_path_compression,
1679                             num_search_strings);
1680
1681          if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
1682            GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
1683        }
1684
1685        printf ("\nWaiting %s before starting to announce.\n",
1686                GNUNET_STRINGS_relative_time_to_string (search_delay, GNUNET_NO));
1687        fflush (stdout);
1688
1689        state = STATE_SEARCH_REGEX;
1690
1691        search_task = GNUNET_SCHEDULER_add_delayed (search_delay,
1692                                                    &do_announce, NULL);
1693      }
1694    }
1695    break;
1696    default:
1697      GNUNET_assert (0);
1698    }
1699    break;
1700   case STATE_SEARCH_REGEX:
1701   {
1702     /* Handled in service connect callback */
1703     break;
1704   }
1705   default:
1706     switch (state)
1707     {
1708     case STATE_PEERS_CREATING:
1709       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create peer\n");
1710       break;
1711     default:
1712       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1713                   "Unexpected controller_cb with state %i!\n", state);
1714     }
1715     GNUNET_assert (0);
1716   }
1717 }
1718
1719
1720 /**
1721  * Task to register all hosts available in the global host list.
1722  *
1723  * @param cls NULL
1724  * @param tc the scheduler task context
1725  */
1726 static void
1727 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1728
1729
1730 /**
1731  * Callback which will be called to after a host registration succeeded or failed
1732  *
1733  * @param cls the closure
1734  * @param emsg the error message; NULL if host registration is successful
1735  */
1736 static void
1737 host_registration_completion (void *cls, const char *emsg)
1738 {
1739   reg_handle = NULL;
1740   if (NULL != emsg)
1741   {
1742     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1743                 _("Host registration failed for a host. Error: %s\n"), emsg);
1744     if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1745       GNUNET_SCHEDULER_cancel (abort_task);
1746     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1747     return;
1748   }
1749   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
1750 }
1751
1752
1753 /**
1754  * Task to register all hosts available in the global host list.
1755  *
1756  * @param cls NULL
1757  * @param tc the scheduler task context
1758  */
1759 static void
1760 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1761 {
1762   struct DLLOperation *dll_op;
1763   static unsigned int reg_host;
1764   unsigned int slave;
1765
1766   register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1767   if (reg_host == num_hosts - 1)
1768   {
1769     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1770                 "All hosts successfully registered\n");
1771     /* Start slaves */
1772     state = STATE_SLAVES_STARTING;
1773     for (slave = 1; slave < num_hosts; slave++)
1774     {
1775       dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1776       dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
1777                                                    mc,
1778                                                    hosts[slave],
1779                                                    hosts[0],
1780                                                    cfg,
1781                                                    GNUNET_YES);
1782       GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1783     }
1784     return;
1785   }
1786   reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
1787                                              host_registration_completion,
1788                                              NULL);
1789 }
1790
1791
1792 /**
1793  * Callback to signal successfull startup of the controller process.
1794  *
1795  * @param cls the closure from GNUNET_TESTBED_controller_start()
1796  * @param config the configuration with which the controller has been started;
1797  *          NULL if status is not GNUNET_OK
1798  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1799  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1800  */
1801 static void
1802 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
1803 {
1804   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1805     GNUNET_SCHEDULER_cancel (abort_task);
1806   if (GNUNET_OK != status)
1807   {
1808     mc_proc = NULL;
1809     printf("Oh, dear!\n");
1810     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1811     return;
1812   }
1813   event_mask = 0;
1814   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1815   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1816   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1817   event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1818   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1819   mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
1820                                           &controller_event_cb, NULL);
1821   if (NULL == mc)
1822   {
1823     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1824                 _("Unable to connect to master controller -- Check config\n"));
1825     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1826     return;
1827   }
1828   register_hosts_task = GNUNET_SCHEDULER_add_now (&register_hosts, NULL);
1829   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1830                                              &do_abort, (void*) __LINE__);
1831 }
1832
1833
1834 /**
1835  * Load search strings from given filename. One search string per line.
1836  *
1837  * @param filename filename of the file containing the search strings.
1838  * @param strings set of strings loaded from file. Caller needs to free this
1839  *                if number returned is greater than zero.
1840  * @param limit upper limit on the number of strings read from the file
1841  * @return number of strings found in the file. GNUNET_SYSERR on error.
1842  */
1843 static int
1844 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1845 {
1846   char *data;
1847   char *buf;
1848   uint64_t filesize;
1849   unsigned int offset;
1850   int str_cnt;
1851   unsigned int i;
1852
1853   if (NULL == filename)
1854   {
1855     return GNUNET_SYSERR;
1856   }
1857
1858   if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1859   {
1860     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1861                 "Could not find search strings file %s\n", filename);
1862     return GNUNET_SYSERR;
1863   }
1864   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1865     filesize = 0;
1866   if (0 == filesize)
1867   {
1868     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1869     return GNUNET_SYSERR;
1870   }
1871   data = GNUNET_malloc (filesize);
1872   if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1873   {
1874     GNUNET_free (data);
1875     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1876          filename);
1877     return GNUNET_SYSERR;
1878   }
1879   buf = data;
1880   offset = 0;
1881   str_cnt = 0;
1882   while (offset < (filesize - 1) && str_cnt < limit)
1883   {
1884     offset++;
1885     if (((data[offset] == '\n')) && (buf != &data[offset]))
1886     {
1887       data[offset] = '\0';
1888       str_cnt++;
1889       buf = &data[offset + 1];
1890     }
1891     else if ((data[offset] == '\n') || (data[offset] == '\0'))
1892       buf = &data[offset + 1];
1893   }
1894   *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1895   offset = 0;
1896   for (i = 0; i < str_cnt; i++)
1897   {
1898     GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1899     offset += strlen (&data[offset]) + 1;
1900   }
1901   GNUNET_free (data);
1902   return str_cnt;
1903 }
1904
1905
1906 /**
1907  * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
1908  * inform whether the given host is habitable or not. The Handle returned by
1909  * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
1910  *
1911  * @param cls NULL
1912  * @param host the host whose status is being reported; will be NULL if the host
1913  *          given to GNUNET_TESTBED_is_host_habitable() is NULL
1914  * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
1915  */
1916 static void 
1917 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host, int status)
1918 {
1919   struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handle = cls;
1920   static unsigned int hosts_checked;
1921
1922   *hc_handle = NULL;
1923   if (GNUNET_NO == status)
1924   {
1925     if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1926       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1927                   GNUNET_TESTBED_host_get_hostname (host));
1928     else
1929       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Testbed cannot be started on localhost\n"));
1930     GNUNET_SCHEDULER_cancel (abort_task);
1931     abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1932     return;
1933   }
1934   hosts_checked++;
1935   /* printf (_("\rChecked %u hosts"), hosts_checked); */
1936   /* fflush (stdout); */
1937   if (hosts_checked < num_hosts)
1938     return;
1939   /* printf (_("\nAll hosts can start testbed. Creating peers\n")); */
1940   GNUNET_free (hc_handles);
1941   hc_handles = NULL;
1942   mc_proc = 
1943       GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
1944                                        (hosts[0]),
1945                                        hosts[0],
1946                                        cfg,
1947                                        status_cb,
1948                                        NULL);
1949 }
1950
1951
1952 /**
1953  * Main function that will be run by the scheduler.
1954  *
1955  * @param cls closure
1956  * @param args remaining command-line arguments
1957  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1958  * @param config configuration
1959  */
1960 static void
1961 run (void *cls, char *const *args, const char *cfgfile,
1962      const struct GNUNET_CONFIGURATION_Handle *config)
1963 {
1964   unsigned int nhost;
1965   unsigned int nsearchstrs;
1966
1967   if (NULL == args[0])
1968   {
1969     fprintf (stderr, _("No hosts-file specified on command line. Exiting.\n"));
1970     return;
1971   }
1972   if (NULL == args[1])
1973   {
1974     fprintf (stderr, _("No policy directory specified on command line. Exiting.\n"));
1975     return;
1976   }
1977   num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
1978   if (0 == num_hosts)
1979   {
1980     fprintf (stderr, _("No hosts loaded. Need at least one host\n"));
1981     return;
1982   }
1983   printf (_("Checking whether given hosts can start testbed. Please wait\n"));
1984   hc_handles = GNUNET_malloc (sizeof (struct
1985                                       GNUNET_TESTBED_HostHabitableCheckHandle *) 
1986                               * num_hosts);
1987   for (nhost = 0; nhost < num_hosts; nhost++)
1988   {
1989     hc_handles[nhost] = GNUNET_TESTBED_is_host_habitable (hosts[nhost], config,
1990                                                           &host_habitable_cb,
1991                                                           &hc_handles[nhost]);
1992     if (NULL == hc_handles[nhost])
1993     {
1994       int i;
1995
1996       GNUNET_break (0);
1997       for (i = 0; i <= nhost; i++)
1998         if (NULL != hc_handles[i])
1999           GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[i]);
2000       GNUNET_free (hc_handles);
2001       hc_handles = NULL;
2002       break;
2003     }
2004   }
2005   if (num_hosts != nhost)
2006   {
2007     fprintf (stderr, _("Exiting\n"));
2008     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2009     return;
2010   }
2011   if (NULL == config)
2012   {
2013     fprintf (stderr, _("No configuration file given. Exiting\n"));
2014     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2015     return;
2016   }
2017
2018   if (GNUNET_OK !=
2019       GNUNET_CONFIGURATION_get_value_string (config, "REGEXPROFILER", "REGEX_PREFIX",
2020                                              &regex_prefix))
2021   {
2022     fprintf (stderr, _("Configuration option (regex_prefix) missing. Exiting\n"));
2023     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2024     return;
2025   }
2026
2027   if ( (NULL != data_filename) &&
2028        (NULL == (data_file =
2029                  GNUNET_DISK_file_open (data_filename,
2030                                         GNUNET_DISK_OPEN_READWRITE |
2031                                         GNUNET_DISK_OPEN_TRUNCATE |
2032                                         GNUNET_DISK_OPEN_CREATE,
2033                                         GNUNET_DISK_PERM_USER_READ |
2034                                         GNUNET_DISK_PERM_USER_WRITE))) )
2035     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2036                               "open",
2037                               data_filename);
2038   if (GNUNET_YES != GNUNET_DISK_directory_test (args[1], GNUNET_YES))
2039   {
2040     fprintf (stderr, _("Specified policies directory does not exist. Exiting.\n"));
2041     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2042     return;
2043   }
2044   policy_dir = args[1];
2045   if (GNUNET_YES != GNUNET_DISK_file_test (args[2]))
2046   {
2047     fprintf (stderr, _("No search strings file given. Exiting.\n"));
2048     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2049     return;
2050   }
2051   nsearchstrs = load_search_strings (args[2], &search_strings, num_search_strings);
2052   if (num_search_strings != nsearchstrs)
2053   {
2054     num_search_strings = nsearchstrs;
2055     fprintf (stderr, _("Error loading search strings. Given file does not contain enough strings. Exiting.\n"));
2056     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2057     return;
2058   }
2059   if (0 >= num_search_strings || NULL == search_strings)
2060   {
2061     fprintf (stderr, _("Error loading search strings. Exiting.\n"));
2062     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2063     return;
2064   }
2065   unsigned int i;
2066   for (i = 0; i < num_search_strings; i++)
2067     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "search string: %s\n", search_strings[i]);
2068   cfg = GNUNET_CONFIGURATION_dup (config);
2069   abort_task =
2070       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2071                                     (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
2072                                     (void*) __LINE__);
2073 }
2074
2075
2076 /**
2077  * Main function.
2078  *
2079  * @param argc argument count
2080  * @param argv argument values
2081  * @return 0 on success
2082  */
2083 int
2084 main (int argc, char *const *argv)
2085 {
2086   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2087     {'d', "details", "FILENAME",
2088      gettext_noop ("name of the file for writing statistics"),
2089      1, &GNUNET_GETOPT_set_string, &data_filename},
2090     {'n', "num-links", "COUNT",
2091       gettext_noop ("create COUNT number of random links between peers"),
2092       GNUNET_YES, &GNUNET_GETOPT_set_uint, &linking_factor },
2093     {'t', "matching-timeout", "TIMEOUT",
2094       gettext_noop ("wait TIMEOUT before considering a string match as failed"),
2095       GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout },
2096     {'s', "search-delay", "DELAY",
2097       gettext_noop ("wait DELAY before starting string search"),
2098       GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
2099     {'a', "num-search-strings", "COUNT",
2100       gettext_noop ("number of search strings to read from search strings file"),
2101       GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
2102     {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
2103      gettext_noop ("maximum path compression length"),
2104      1, &GNUNET_GETOPT_set_uint, &max_path_compression},
2105     {'i', "no-distributed-search", "",
2106      gettext_noop ("if this option is set, only one peer is responsible for searching all strings"),
2107      0, &GNUNET_GETOPT_set_one, &no_distributed_search},
2108     GNUNET_GETOPT_OPTION_END
2109   };
2110   int ret;
2111
2112   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2113     return 2;
2114
2115   result = GNUNET_SYSERR;
2116   ret =
2117       GNUNET_PROGRAM_run (argc, argv,
2118                           "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
2119                           _("Profiler for regex"),
2120                           options, &run, NULL);
2121
2122   if (GNUNET_OK != ret)
2123     return ret;
2124   if (GNUNET_OK != result)
2125     return 1;
2126   return 0;
2127 }