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