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