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