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