-keep track of messages passed to mq
[oweals/gnunet.git] / src / sensor / gnunet-sensor-profiler.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file sensor/gnunet-sensor-profiler.c
23  * @brief Profiler for the sensor service
24  * @author Omar Tarabai
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_peerstore_service.h"
30 #include "gnunet_sensor_service.h"
31 #include "gnunet_sensor_util_lib.h"
32 #include "gnunet_transport_service.h"
33
34 /**
35  * Time to wait for the peer to startup completely
36  */
37 #define PEER_STARTUP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
38
39 /**
40  * Information about a single peer
41  */
42 struct PeerInfo
43 {
44
45   /**
46    * Peer Identity
47    */
48   struct GNUNET_PeerIdentity peer_id;
49
50   /**
51    * Testbed peer handle
52    */
53   struct GNUNET_TESTBED_Peer *testbed_peer;
54
55   /**
56    * Index of this peer within our list
57    */
58   int index;
59
60   /**
61    * TESTBED operation used to connect to statistics service
62    */
63   struct GNUNET_TESTBED_Operation *statistics_op;
64
65   /**
66    * Handle to the peer's statistics service
67    */
68   struct GNUNET_STATISTICS_Handle *statistics;
69
70 };
71
72 /**
73  * Name of the configuration file used
74  */
75 static const char *cfg_filename = "gnunet-sensor-profiler.conf";
76
77 /**
78  * Directory to read sensor definitions from
79  */
80 static const char *sensor_src_dir = "sensors";
81
82 /**
83  * Directory to write new sensor definitions to
84  */
85 static const char *sensor_dst_dir = "/tmp/gnunet-sensor-profiler";
86
87 /**
88  * Scheduled task to shutdown
89  */
90 static struct GNUNET_SCHEDULER_Task * shutdown_task = NULL;
91
92 /**
93  * GNUnet configuration
94  */
95 static struct GNUNET_CONFIGURATION_Handle *cfg;
96
97 /**
98  * Number of peers to run (Option -p)
99  */
100 static unsigned int num_peers = 0;
101
102 /**
103  * Set sensors running interval to this value (Option -i)
104  */
105 static unsigned int sensors_interval = 0;
106
107 /**
108  * Path to topology file (Option -t)
109  */
110 static char *topology_file;
111
112 /**
113  * Number of peers to simulate anomalies on (Option -a)
114  */
115 static unsigned int anomalous_peers = 0;
116
117 /**
118  * Array of peer info for all peers
119  */
120 static struct PeerInfo *all_peers_info;
121
122 /**
123  * Number of peers that we already collected and start their info
124  */
125 static int peers_known = 0;
126
127 /**
128  * TESTBED operation connecting us to peerstore service on collection point
129  */
130 static struct GNUNET_TESTBED_Operation *peerstore_op;
131
132 /**
133  * Handle to peerstore service on collection point
134  */
135 static struct GNUNET_PEERSTORE_Handle *peerstore;
136
137 /**
138  * Dashboard service on collection point started?
139  */
140 static int dashboard_service_started = GNUNET_NO;
141
142 /**
143  * Number of peers started the sensor service successfully
144  */
145 static int sensor_services_started = 0;
146
147 /**
148  * Array of sensor names to be used for watching peerstore records
149  */
150 static char **sensor_names;
151
152 /**
153  * Size of 'sensor_names' array
154  */
155 static unsigned int sensor_names_size = 0;
156
157 /**
158  * Task run after any waiting period
159  */
160 static struct GNUNET_SCHEDULER_Task * delayed_task = NULL;
161
162
163 /**
164  * Copy directory recursively
165  *
166  * @param src Path to source directory
167  * @param dst Destination directory, will be created if it does not exist
168  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
169  */
170 static int
171 copy_dir (const char *src, const char *dst);
172
173
174 /**
175  * Do clean up and shutdown scheduler
176  */
177 static void
178 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
179 {
180   int i;
181
182   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down.\n");
183   if (NULL != delayed_task)
184   {
185     GNUNET_SCHEDULER_cancel (delayed_task);
186     delayed_task = NULL;
187   }
188   for (i = 0; i < num_peers; i++)
189   {
190     if (NULL != all_peers_info[i].statistics_op)
191     {
192       GNUNET_TESTBED_operation_done (all_peers_info[i].statistics_op);
193       all_peers_info[i].statistics_op = NULL;
194     }
195   }
196   if (NULL != peerstore_op)
197   {
198     GNUNET_TESTBED_operation_done (peerstore_op);
199     peerstore_op = NULL;
200   }
201   if (NULL != all_peers_info)
202   {
203     GNUNET_free (all_peers_info);
204     all_peers_info = NULL;
205   }
206   if (NULL != cfg)
207   {
208     GNUNET_CONFIGURATION_destroy (cfg);
209     cfg = NULL;
210   }
211   if (NULL != sensor_names)
212   {
213     for (i = 0; i < sensor_names_size; i++)
214       GNUNET_free (sensor_names[i]);
215     GNUNET_array_grow (sensor_names, sensor_names_size, 0);
216   }
217   GNUNET_SCHEDULER_shutdown ();
218 }
219
220
221 /**
222  * Function called with each file/folder inside a directory that is being copied.
223  *
224  * @param cls closure, destination directory
225  * @param filename complete filename (absolute path)
226  * @return #GNUNET_OK to continue to iterate.
227  *         #GNUNET_SYSERR to abort iteration with error
228  */
229 static int
230 copy_dir_scanner (void *cls, const char *filename)
231 {
232   char *dst_dir = cls;
233   char *dst;
234   int copy_result;
235
236   GNUNET_asprintf (&dst, "%s%s%s", dst_dir, DIR_SEPARATOR_STR,
237                    GNUNET_STRINGS_get_short_name (filename));
238   if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_YES))
239     copy_result = copy_dir (filename, dst);
240   else
241   {
242     if (GNUNET_YES == GNUNET_DISK_file_test (dst))
243       GNUNET_DISK_directory_remove (dst);
244     copy_result = GNUNET_DISK_file_copy (filename, dst);
245     if (GNUNET_OK == copy_result)
246       GNUNET_DISK_fix_permissions (dst, GNUNET_NO, GNUNET_NO);
247   }
248   GNUNET_free (dst);
249   return copy_result;
250 }
251
252
253 /**
254  * Copy directory recursively
255  *
256  * @param src Path to source directory
257  * @param dst Destination directory, will be created if it does not exist
258  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
259  */
260 static int
261 copy_dir (const char *src, const char *dst)
262 {
263   if (GNUNET_YES != GNUNET_DISK_directory_test (src, GNUNET_YES))
264     return GNUNET_SYSERR;
265   if (GNUNET_OK != GNUNET_DISK_directory_create (dst))
266     return GNUNET_SYSERR;
267   if (GNUNET_SYSERR ==
268       GNUNET_DISK_directory_scan (src, &copy_dir_scanner, (char *) dst))
269     return GNUNET_SYSERR;
270   return GNUNET_OK;
271 }
272
273
274 /**
275  * Function called with each file/folder inside source sensor directory.
276  *
277  * @param cls closure (unused)
278  * @param filename complete filename (absolute path)
279  * @return #GNUNET_OK to continue to iterate.
280  */
281 static int
282 sensor_dir_scanner (void *cls, const char *filename)
283 {
284   const char *file_basename;
285   char *dst_path;
286   struct GNUNET_CONFIGURATION_Handle *sensor_cfg;
287   char *sensor_name;
288
289   file_basename = GNUNET_STRINGS_get_short_name (filename);
290   GNUNET_asprintf (&dst_path, "%s%s%s", sensor_dst_dir, DIR_SEPARATOR_STR,
291                    file_basename);
292   if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_NO))
293   {
294     GNUNET_assert (GNUNET_OK == copy_dir (filename, dst_path));
295   }
296   else
297   {
298     sensor_name = GNUNET_strdup (file_basename);
299     GNUNET_array_append (sensor_names, sensor_names_size, sensor_name);
300     sensor_cfg = GNUNET_CONFIGURATION_create ();
301     GNUNET_assert (GNUNET_OK ==
302                    GNUNET_CONFIGURATION_parse (sensor_cfg, filename));
303     GNUNET_CONFIGURATION_set_value_string (sensor_cfg, file_basename,
304                                            "COLLECTION_POINT",
305                                            GNUNET_i2s_full (&all_peers_info
306                                                             [0].peer_id));
307     if (sensors_interval > 0)
308     {
309       GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename,
310                                              "INTERVAL",
311                                              (unsigned long long int)
312                                              sensors_interval);
313     }
314     GNUNET_CONFIGURATION_write (sensor_cfg, dst_path);
315     GNUNET_CONFIGURATION_destroy (sensor_cfg);
316   }
317   GNUNET_free (dst_path);
318   return GNUNET_OK;
319 }
320
321
322 /**
323  * Load sensor definitions and rewrite them to tmp location.
324  * Add collection point peer id and change running interval if needed.
325  */
326 static void
327 rewrite_sensors ()
328 {
329   GNUNET_assert (GNUNET_YES ==
330                  GNUNET_DISK_directory_test (sensor_src_dir, GNUNET_YES));
331   GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (sensor_dst_dir));
332   GNUNET_DISK_directory_scan (sensor_src_dir, &sensor_dir_scanner, NULL);
333 }
334
335
336 /**
337  * Callback to be called when dashboard service is started
338  *
339  * @param cls the callback closure from functions generating an operation
340  * @param op the operation that has been finished
341  * @param emsg error message in case the operation has failed; will be NULL if
342  *          operation has executed successfully.
343  */
344 static void
345 dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op,
346                    const char *emsg)
347 {
348   if (NULL != emsg)
349   {
350     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
351     GNUNET_assert (0);
352   }
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Dashboard service started.\n");
354   GNUNET_TESTBED_operation_done (op);
355   dashboard_service_started = GNUNET_YES;
356 }
357
358
359 /**
360  * Function called by PEERSTORE for each matching record.
361  *
362  * @param cls closure
363  * @param record peerstore record information
364  * @param emsg error message, or NULL if no errors
365  * @return #GNUNET_YES to continue iterating, #GNUNET_NO to stop
366  */
367 static int
368 peerstore_watch_cb (void *cls,
369                     const struct GNUNET_PEERSTORE_Record *record,
370                     const char *emsg)
371 {
372   struct PeerInfo *peer = cls;
373   struct GNUNET_SENSOR_DashboardAnomalyEntry *anomaly;
374
375   if (NULL != emsg)
376   {
377     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
378     GNUNET_assert (0);
379   }
380   GNUNET_assert (record->value_size ==
381                  sizeof (struct GNUNET_SENSOR_DashboardAnomalyEntry));
382   anomaly = record->value;
383   GNUNET_assert (0 ==
384                  GNUNET_CRYPTO_cmp_peer_identity (&peer->peer_id,
385                                                   record->peer));
386   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387               "Anomaly report:{'peerid': '%s'," "'peer': %d," "'sensor': '%s',"
388               "'anomalous': %d," "'neighbors': %f}\n",
389               GNUNET_i2s (&peer->peer_id), peer->index, record->key,
390               anomaly->anomalous, anomaly->anomalous_neighbors);
391   return GNUNET_YES;
392 }
393
394
395 /**
396  * Callback to be called when peerstore service connect operation is completed
397  *
398  * @param cls the callback closure from functions generating an operation
399  * @param op the operation that has been finished
400  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
401  * @param emsg error message in case the operation has failed; will be NULL if
402  *          operation has executed successfully.
403  */
404 static void
405 peerstore_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
406                       void *ca_result, const char *emsg)
407 {
408   int i;
409   int j;
410   struct PeerInfo *peer;
411
412   if (NULL != emsg)
413   {
414     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
415     GNUNET_assert (0);
416   }
417   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peerstore service.\n");
418   /* Watch for anomaly reports from other peers */
419   for (i = 0; i < num_peers; i++)
420   {
421     peer = &all_peers_info[i];
422     for (j = 0; j < sensor_names_size; j++)
423     {
424       GNUNET_PEERSTORE_watch (peerstore, "sensordashboard-anomalies",
425                               &peer->peer_id, sensor_names[j],
426                               &peerstore_watch_cb, peer);
427     }
428   }
429 }
430
431
432 /**
433  * Adapter function called to establish a connection to peerstore service.
434  *
435  * @param cls closure
436  * @param cfg configuration of the peer to connect to; will be available until
437  *          GNUNET_TESTBED_operation_done() is called on the operation returned
438  *          from GNUNET_TESTBED_service_connect()
439  * @return service handle to return in 'op_result', NULL on error
440  */
441 static void *
442 peerstore_connect_adapter (void *cls,
443                            const struct GNUNET_CONFIGURATION_Handle *cfg)
444 {
445   peerstore = GNUNET_PEERSTORE_connect (cfg);
446   GNUNET_assert (NULL != peerstore);
447   return peerstore;
448 }
449
450
451 /**
452  * Adapter function called to destroy a connection to peerstore service.
453  *
454  * @param cls closure
455  * @param op_result service handle returned from the connect adapter
456  */
457 static void
458 peerstore_disconnect_adapter (void *cls, void *op_result)
459 {
460   GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO);
461   peerstore = NULL;
462   peerstore_op = NULL;
463 }
464
465
466 /**
467  * Callback to be called when statistics service connect operation is completed
468  *
469  * @param cls the callback closure from functions generating an operation
470  * @param op the operation that has been finished
471  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
472  * @param emsg error message in case the operation has failed; will be NULL if
473  *          operation has executed successfully.
474  */
475 static void
476 statistics_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
477                       void *ca_result, const char *emsg)
478 {
479   struct PeerInfo *peer = cls;
480
481   if (NULL != emsg)
482   {
483     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
484     GNUNET_assert (0);
485   }
486   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487       "Connected to statistics service on peer `%s'.\n", GNUNET_i2s (&peer->peer_id));
488   GNUNET_STATISTICS_set (peer->statistics, "# peers connected", 0, GNUNET_NO);
489 }
490
491
492 /**
493  * Adapter function called to establish a connection to statistics service.
494  *
495  * @param cls closure
496  * @param cfg configuration of the peer to connect to; will be available until
497  *          GNUNET_TESTBED_operation_done() is called on the operation returned
498  *          from GNUNET_TESTBED_service_connect()
499  * @return service handle to return in 'op_result', NULL on error
500  */
501 static void *
502 statistics_connect_adapter (void *cls,
503                            const struct GNUNET_CONFIGURATION_Handle *cfg)
504 {
505   struct PeerInfo *peer = cls;
506
507   peer->statistics = GNUNET_STATISTICS_create ("core", cfg);
508   GNUNET_assert (NULL != peer->statistics);
509   return peer->statistics;
510 }
511
512
513 /**
514  * Adapter function called to destroy a connection to statistics service.
515  *
516  * @param cls closure
517  * @param op_result service handle returned from the connect adapter
518  */
519 static void
520 statistics_disconnect_adapter (void *cls, void *op_result)
521 {
522   struct PeerInfo *peer = cls;
523
524   GNUNET_STATISTICS_destroy (peer->statistics, GNUNET_NO);
525   peer->statistics = NULL;
526 }
527
528
529 /**
530  * This function is called after the estimated training period is over.
531  */
532 static void
533 simulate_anomalies (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
534 {
535   int i;
536   uint32_t an_peer;
537   struct GNUNET_TIME_Relative shutdown_delay;
538
539   delayed_task = NULL;
540   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541               "Training period over, simulating anomalies now.\n");
542   GNUNET_assert (anomalous_peers <= num_peers);
543   for (i = 0; i < anomalous_peers; i++)
544   {
545     an_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
546     if (NULL != all_peers_info[an_peer].statistics_op)
547     {
548       i--;
549       continue;
550     }
551     all_peers_info[an_peer].statistics_op =
552     GNUNET_TESTBED_service_connect (NULL, all_peers_info[an_peer].testbed_peer,
553                                     "statistics", &statistics_connect_cb,
554                                     &all_peers_info[an_peer], &statistics_connect_adapter,
555                                     &statistics_disconnect_adapter, &all_peers_info[an_peer]);
556   }
557   shutdown_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, num_peers * 6);
558   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down in %s\n",
559       GNUNET_STRINGS_relative_time_to_string (shutdown_delay, GNUNET_NO));
560   GNUNET_SCHEDULER_cancel (shutdown_task);
561   shutdown_task = GNUNET_SCHEDULER_add_delayed (shutdown_delay, &do_shutdown, NULL);
562 }
563
564
565 /**
566  * This function is called after a delay which ensures that all peers are
567  * properly initialized
568  */
569 static void
570 peers_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
571 {
572   unsigned long long int training_points;
573   struct GNUNET_TIME_Relative training_period;
574
575   delayed_task = NULL;
576   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers are ready.\n");
577   GNUNET_assert (GNUNET_OK ==
578                  GNUNET_CONFIGURATION_get_value_number (cfg,
579                                                         "sensor-model-gaussian",
580                                                         "TRAINING_WINDOW",
581                                                         &training_points));
582   training_period =
583       GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_multiply
584                                      (GNUNET_TIME_UNIT_SECONDS,
585                                       (sensors_interval ==
586                                        0) ? 60 : sensors_interval),
587                                      training_points);
588   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589               "Sleeping for a training period of %s.\n",
590               GNUNET_STRINGS_relative_time_to_string (training_period,
591                                                       GNUNET_NO));
592   delayed_task =
593       GNUNET_SCHEDULER_add_delayed (training_period, &simulate_anomalies, NULL);
594 }
595
596
597 /**
598  * Callback to be called when sensor service is started
599  *
600  * @param cls the callback closure from functions generating an operation
601  * @param op the operation that has been finished
602  * @param emsg error message in case the operation has failed; will be NULL if
603  *          operation has executed successfully.
604  */
605 static void
606 sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op,
607                         const char *emsg)
608 {
609   struct PeerInfo *peer = cls;
610
611   if (NULL != emsg)
612   {
613     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
614     GNUNET_assert (0);
615   }
616   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sensor service started on peer `%s'.\n",
617               GNUNET_i2s (&peer->peer_id));
618   GNUNET_TESTBED_operation_done (op);
619   sensor_services_started++;
620   if (sensor_services_started == num_peers)
621   {
622     delayed_task =
623         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
624                                       (PEER_STARTUP_TIME, num_peers),
625                                       &peers_ready, NULL);
626   }
627 }
628
629
630 /**
631  * Callback to be called when the requested peer information is available
632  *
633  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
634  * @param op the operation this callback corresponds to
635  * @param pinfo the result; will be NULL if the operation has failed
636  * @param emsg error message if the operation has failed; will be NULL if the
637  *          operation is successfull
638  */
639 static void
640 peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
641               const struct GNUNET_TESTBED_PeerInformation *pinfo,
642               const char *emsg)
643 {
644   struct GNUNET_TESTBED_Peer *testbed_peer = cb_cls;
645   struct PeerInfo *peer = &all_peers_info[peers_known];
646
647   if (NULL != emsg)
648   {
649     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
650     GNUNET_assert (0);
651   }
652   peer->testbed_peer = testbed_peer;
653   GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id);
654   peer->index = peers_known;
655   peers_known++;
656   if (1 == peers_known)         /* First peer is collection point */
657   {
658     /* Rewrite sensors */
659     rewrite_sensors ();
660     /* Start dashboard */
661     GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensordashboard",
662                                         &dashboard_started, NULL, 1);
663   }
664   /* Start sensor service on every peer */
665   GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensor",
666                                       &sensor_service_started, peer, 1);
667   if (num_peers == peers_known) /* Last peer */
668   {
669     /* Connect to peerstore on first peer (collection point) */
670     peerstore_op =
671         GNUNET_TESTBED_service_connect (NULL, all_peers_info[0].testbed_peer,
672                                         "peerstore", &peerstore_connect_cb,
673                                         NULL, &peerstore_connect_adapter,
674                                         &peerstore_disconnect_adapter, NULL);
675   }
676   GNUNET_TESTBED_operation_done (op);
677 }
678
679
680 /**
681  * Signature of a main function for a testcase.
682  *
683  * @param cls closure
684  * @param h the run handle
685  * @param num number of peers in 'peers'
686  * @param peers handle to peers run in the testbed.  NULL upon timeout (see
687  *          GNUNET_TESTBED_test_run()).
688  * @param links_succeeded the number of overlay link connection attempts that
689  *          succeeded
690  * @param links_failed the number of overlay link connection attempts that
691  *          failed
692  * @see GNUNET_TESTBED_test_run()
693  */
694 static void
695 test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num,
696              struct GNUNET_TESTBED_Peer **peers, unsigned int links_succeeded,
697              unsigned int links_failed)
698 {
699   int i;
700
701   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702               "%d peers started. %d links succeeded. %d links failed.\n",
703               num_peers, links_succeeded, links_failed);
704   GNUNET_assert (num == num_peers);
705   /* Collect peer information */
706   all_peers_info = GNUNET_new_array (num_peers, struct PeerInfo);
707
708   for (i = 0; i < num_peers; i++)
709   {
710     GNUNET_TESTBED_peer_get_information (peers[i],
711                                          GNUNET_TESTBED_PIT_CONFIGURATION,
712                                          &peer_info_cb, peers[i]);
713   }
714 }
715
716
717 /**
718  * Verify that the user passed correct CL args
719  *
720  * @return #GNUNET_OK if arguments are valid, #GNUNET_SYSERR otherwise
721  */
722 static int
723 verify_args ()
724 {
725   if (num_peers < 2)
726   {
727     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
728                 _
729                 ("Invalid or missing number of peers. Set at least 2 peers.\n"));
730     return GNUNET_SYSERR;
731   }
732   if (NULL == topology_file ||
733       GNUNET_YES != GNUNET_DISK_file_test (topology_file))
734   {
735     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
736                 _("Missing or invalid topology file.\n"));
737     return GNUNET_SYSERR;
738   }
739   return GNUNET_OK;
740 }
741
742
743 /**
744  * Actual main function.
745  *
746  * @param cls unused
747  * @param args remaining args, unused
748  * @param cfgfile name of the configuration
749  * @param cfg configuration handle
750  */
751 static void
752 run (void *cls, char *const *args, const char *cf,
753      const struct GNUNET_CONFIGURATION_Handle *c)
754 {
755   if (GNUNET_OK != verify_args ())
756   {
757     do_shutdown (NULL, NULL);
758     return;
759   }
760   cfg = GNUNET_CONFIGURATION_create ();
761   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_filename));
762   GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle *)
763                                          cfg, "TESTBED",
764                                          "OVERLAY_TOPOLOGY_FILE",
765                                          topology_file);
766   shutdown_task =
767       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
768                                     NULL);
769   GNUNET_TESTBED_run (NULL, cfg, num_peers, 0, NULL, NULL, &test_master, NULL);
770 }
771
772
773 /**
774  * Main function.
775  *
776  * @return 0 on success
777  */
778 int
779 main (int argc, char *const *argv)
780 {
781   static struct GNUNET_GETOPT_CommandLineOption options[] = {
782     {'p', "peers", "COUNT", gettext_noop ("Number of peers to run"), GNUNET_YES,
783      &GNUNET_GETOPT_set_uint, &num_peers},
784     {'t', "topology-file", "FILEPATH", gettext_noop ("Path to topology file"),
785      GNUNET_YES, &GNUNET_GETOPT_set_filename, &topology_file},
786     {'i', "sensors-interval", "INTERVAL",
787      gettext_noop ("Change the interval of running sensors to given value"),
788      GNUNET_YES, &GNUNET_GETOPT_set_uint, &sensors_interval},
789     {'a', "anomalous-peers", "COUNT",
790      gettext_noop ("Number of peers to simulate anomalies on"), GNUNET_YES,
791      &GNUNET_GETOPT_set_uint, &anomalous_peers},
792     GNUNET_GETOPT_OPTION_END
793   };
794
795   return (GNUNET_OK ==
796           GNUNET_PROGRAM_run (argc, argv, "gnunet-sensor-profiler",
797                               gettext_noop ("Profiler for sensor service"),
798                               options, &run, NULL)) ? 0 : 1;
799 }
800
801 /* end of gnunet-sensor-profiler.c */