sensor: profiler update
[oweals/gnunet.git] / src / sensor / gnunet-sensor-profiler.c
1 /*
2      This file is part of GNUnet.
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier shutdown_task = GNUNET_SCHEDULER_NO_TASK;
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 GNUNET_SCHEDULER_TaskIdentifier delayed_task = GNUNET_SCHEDULER_NO_TASK;
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 (GNUNET_SCHEDULER_NO_TASK != delayed_task)
184   {
185     GNUNET_SCHEDULER_cancel (delayed_task);
186     delayed_task = GNUNET_SCHEDULER_NO_TASK;
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, struct GNUNET_PEERSTORE_Record *record,
369                     char *emsg)
370 {
371   struct PeerInfo *peer = cls;
372   struct GNUNET_SENSOR_DashboardAnomalyEntry *anomaly;
373
374   if (NULL != emsg)
375   {
376     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
377     GNUNET_assert (0);
378   }
379   GNUNET_assert (record->value_size ==
380                  sizeof (struct GNUNET_SENSOR_DashboardAnomalyEntry));
381   anomaly = record->value;
382   GNUNET_assert (0 ==
383                  GNUNET_CRYPTO_cmp_peer_identity (&peer->peer_id,
384                                                   record->peer));
385   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386               "Anomaly report:{'peerid': '%s'," "'peer': %d," "'sensor': '%s',"
387               "'anomalous': %d," "'neighbors': %f}\n",
388               GNUNET_i2s (&peer->peer_id), peer->index, record->key,
389               anomaly->anomalous, anomaly->anomalous_neighbors);
390   return GNUNET_YES;
391 }
392
393
394 /**
395  * Callback to be called when peerstore service connect operation is completed
396  *
397  * @param cls the callback closure from functions generating an operation
398  * @param op the operation that has been finished
399  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
400  * @param emsg error message in case the operation has failed; will be NULL if
401  *          operation has executed successfully.
402  */
403 static void
404 peerstore_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
405                       void *ca_result, const char *emsg)
406 {
407   int i;
408   int j;
409   struct PeerInfo *peer;
410
411   if (NULL != emsg)
412   {
413     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
414     GNUNET_assert (0);
415   }
416   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peerstore service.\n");
417   /* Watch for anomaly reports from other peers */
418   for (i = 0; i < num_peers; i++)
419   {
420     peer = &all_peers_info[i];
421     for (j = 0; j < sensor_names_size; j++)
422     {
423       GNUNET_PEERSTORE_watch (peerstore, "sensordashboard-anomalies",
424                               &peer->peer_id, sensor_names[j],
425                               &peerstore_watch_cb, peer);
426     }
427   }
428 }
429
430
431 /**
432  * Adapter function called to establish a connection to peerstore service.
433  *
434  * @param cls closure
435  * @param cfg configuration of the peer to connect to; will be available until
436  *          GNUNET_TESTBED_operation_done() is called on the operation returned
437  *          from GNUNET_TESTBED_service_connect()
438  * @return service handle to return in 'op_result', NULL on error
439  */
440 static void *
441 peerstore_connect_adapter (void *cls,
442                            const struct GNUNET_CONFIGURATION_Handle *cfg)
443 {
444   peerstore = GNUNET_PEERSTORE_connect (cfg);
445   GNUNET_assert (NULL != peerstore);
446   return peerstore;
447 }
448
449
450 /**
451  * Adapter function called to destroy a connection to peerstore service.
452  *
453  * @param cls closure
454  * @param op_result service handle returned from the connect adapter
455  */
456 static void
457 peerstore_disconnect_adapter (void *cls, void *op_result)
458 {
459   GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO);
460   peerstore = NULL;
461   peerstore_op = NULL;
462 }
463
464
465 /**
466  * Callback to be called when statistics service connect operation is completed
467  *
468  * @param cls the callback closure from functions generating an operation
469  * @param op the operation that has been finished
470  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
471  * @param emsg error message in case the operation has failed; will be NULL if
472  *          operation has executed successfully.
473  */
474 static void
475 statistics_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
476                       void *ca_result, const char *emsg)
477 {
478   struct PeerInfo *peer = cls;
479
480   if (NULL != emsg)
481   {
482     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
483     GNUNET_assert (0);
484   }
485   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486       "Connected to statistics service on peer `%s'.\n", GNUNET_i2s (&peer->peer_id));
487   GNUNET_STATISTICS_set (peer->statistics, "# peers connected", 0, GNUNET_NO);
488 }
489
490
491 /**
492  * Adapter function called to establish a connection to statistics service.
493  *
494  * @param cls closure
495  * @param cfg configuration of the peer to connect to; will be available until
496  *          GNUNET_TESTBED_operation_done() is called on the operation returned
497  *          from GNUNET_TESTBED_service_connect()
498  * @return service handle to return in 'op_result', NULL on error
499  */
500 static void *
501 statistics_connect_adapter (void *cls,
502                            const struct GNUNET_CONFIGURATION_Handle *cfg)
503 {
504   struct PeerInfo *peer = cls;
505
506   peer->statistics = GNUNET_STATISTICS_create ("core", cfg);
507   GNUNET_assert (NULL != peer->statistics);
508   return peer->statistics;
509 }
510
511
512 /**
513  * Adapter function called to destroy a connection to statistics service.
514  *
515  * @param cls closure
516  * @param op_result service handle returned from the connect adapter
517  */
518 static void
519 statistics_disconnect_adapter (void *cls, void *op_result)
520 {
521   struct PeerInfo *peer = cls;
522
523   GNUNET_STATISTICS_destroy (peer->statistics, GNUNET_NO);
524   peer->statistics = NULL;
525 }
526
527
528 /**
529  * This function is called after the estimated training period is over.
530  */
531 static void
532 simulate_anomalies (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
533 {
534   int i;
535   uint32_t an_peer;
536   struct GNUNET_TIME_Relative shutdown_delay;
537
538   delayed_task = GNUNET_SCHEDULER_NO_TASK;
539   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540               "Training period over, simulating anomalies now.\n");
541   GNUNET_assert (anomalous_peers <= num_peers);
542   for (i = 0; i < anomalous_peers; i++)
543   {
544     an_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
545     if (NULL != all_peers_info[an_peer].statistics_op)
546     {
547       i--;
548       continue;
549     }
550     all_peers_info[an_peer].statistics_op =
551     GNUNET_TESTBED_service_connect (NULL, all_peers_info[an_peer].testbed_peer,
552                                     "statistics", &statistics_connect_cb,
553                                     &all_peers_info[an_peer], &statistics_connect_adapter,
554                                     &statistics_disconnect_adapter, &all_peers_info[an_peer]);
555   }
556   shutdown_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, num_peers * 6);
557   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down in %s\n",
558       GNUNET_STRINGS_relative_time_to_string (shutdown_delay, GNUNET_NO));
559   GNUNET_SCHEDULER_cancel (shutdown_task);
560   shutdown_task = GNUNET_SCHEDULER_add_delayed (shutdown_delay, &do_shutdown, NULL);
561 }
562
563
564 /**
565  * This function is called after a delay which ensures that all peers are
566  * properly initialized
567  */
568 static void
569 peers_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
570 {
571   unsigned long long int training_points;
572   struct GNUNET_TIME_Relative training_period;
573
574   delayed_task = GNUNET_SCHEDULER_NO_TASK;
575   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers are ready.\n");
576   GNUNET_assert (GNUNET_OK ==
577                  GNUNET_CONFIGURATION_get_value_number (cfg,
578                                                         "sensor-model-gaussian",
579                                                         "TRAINING_WINDOW",
580                                                         &training_points));
581   training_period =
582       GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_multiply
583                                      (GNUNET_TIME_UNIT_SECONDS,
584                                       (sensors_interval ==
585                                        0) ? 60 : sensors_interval),
586                                      training_points);
587   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588               "Sleeping for a training period of %s.\n",
589               GNUNET_STRINGS_relative_time_to_string (training_period,
590                                                       GNUNET_NO));
591   delayed_task =
592       GNUNET_SCHEDULER_add_delayed (training_period, &simulate_anomalies, NULL);
593 }
594
595
596 /**
597  * Callback to be called when sensor service is started
598  *
599  * @param cls the callback closure from functions generating an operation
600  * @param op the operation that has been finished
601  * @param emsg error message in case the operation has failed; will be NULL if
602  *          operation has executed successfully.
603  */
604 static void
605 sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op,
606                         const char *emsg)
607 {
608   struct PeerInfo *peer = cls;
609
610   if (NULL != emsg)
611   {
612     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
613     GNUNET_assert (0);
614   }
615   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sensor service started on peer `%s'.\n",
616               GNUNET_i2s (&peer->peer_id));
617   GNUNET_TESTBED_operation_done (op);
618   sensor_services_started++;
619   if (sensor_services_started == num_peers)
620   {
621     delayed_task =
622         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
623                                       (PEER_STARTUP_TIME, num_peers),
624                                       &peers_ready, NULL);
625   }
626 }
627
628
629 /**
630  * Callback to be called when the requested peer information is available
631  *
632  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
633  * @param op the operation this callback corresponds to
634  * @param pinfo the result; will be NULL if the operation has failed
635  * @param emsg error message if the operation has failed; will be NULL if the
636  *          operation is successfull
637  */
638 static void
639 peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
640               const struct GNUNET_TESTBED_PeerInformation *pinfo,
641               const char *emsg)
642 {
643   struct GNUNET_TESTBED_Peer *testbed_peer = cb_cls;
644   struct PeerInfo *peer = &all_peers_info[peers_known];
645
646   if (NULL != emsg)
647   {
648     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
649     GNUNET_assert (0);
650   }
651   peer->testbed_peer = testbed_peer;
652   GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id);
653   peer->index = peers_known;
654   peers_known++;
655   if (1 == peers_known)         /* First peer is collection point */
656   {
657     /* Rewrite sensors */
658     rewrite_sensors ();
659     /* Start dashboard */
660     GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensordashboard",
661                                         &dashboard_started, NULL, 1);
662   }
663   /* Start sensor service on every peer */
664   GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensor",
665                                       &sensor_service_started, peer, 1);
666   if (num_peers == peers_known) /* Last peer */
667   {
668     /* Connect to peerstore on first peer (collection point) */
669     peerstore_op =
670         GNUNET_TESTBED_service_connect (NULL, all_peers_info[0].testbed_peer,
671                                         "peerstore", &peerstore_connect_cb,
672                                         NULL, &peerstore_connect_adapter,
673                                         &peerstore_disconnect_adapter, NULL);
674   }
675   GNUNET_TESTBED_operation_done (op);
676 }
677
678
679 /**
680  * Signature of a main function for a testcase.
681  *
682  * @param cls closure
683  * @param h the run handle
684  * @param num number of peers in 'peers'
685  * @param peers handle to peers run in the testbed.  NULL upon timeout (see
686  *          GNUNET_TESTBED_test_run()).
687  * @param links_succeeded the number of overlay link connection attempts that
688  *          succeeded
689  * @param links_failed the number of overlay link connection attempts that
690  *          failed
691  * @see GNUNET_TESTBED_test_run()
692  */
693 static void
694 test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num,
695              struct GNUNET_TESTBED_Peer **peers, unsigned int links_succeeded,
696              unsigned int links_failed)
697 {
698   int i;
699
700   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
701               "%d peers started. %d links succeeded. %d links failed.\n",
702               num_peers, links_succeeded, links_failed);
703   GNUNET_assert (num == num_peers);
704   /* Collect peer information */
705   all_peers_info = GNUNET_new_array (num_peers, struct PeerInfo);
706
707   for (i = 0; i < num_peers; i++)
708   {
709     GNUNET_TESTBED_peer_get_information (peers[i],
710                                          GNUNET_TESTBED_PIT_CONFIGURATION,
711                                          &peer_info_cb, peers[i]);
712   }
713 }
714
715
716 /**
717  * Verify that the user passed correct CL args
718  *
719  * @return #GNUNET_OK if arguments are valid, #GNUNET_SYSERR otherwise
720  */
721 static int
722 verify_args ()
723 {
724   if (num_peers < 2)
725   {
726     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
727                 _
728                 ("Invalid or missing number of peers. Set at least 2 peers.\n"));
729     return GNUNET_SYSERR;
730   }
731   if (NULL == topology_file ||
732       GNUNET_YES != GNUNET_DISK_file_test (topology_file))
733   {
734     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
735                 _("Missing or invalid topology file.\n"));
736     return GNUNET_SYSERR;
737   }
738   return GNUNET_OK;
739 }
740
741
742 /**
743  * Actual main function.
744  *
745  * @param cls unused
746  * @param args remaining args, unused
747  * @param cfgfile name of the configuration
748  * @param cfg configuration handle
749  */
750 static void
751 run (void *cls, char *const *args, const char *cf,
752      const struct GNUNET_CONFIGURATION_Handle *c)
753 {
754   if (GNUNET_OK != verify_args ())
755   {
756     do_shutdown (NULL, NULL);
757     return;
758   }
759   cfg = GNUNET_CONFIGURATION_create ();
760   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_filename));
761   GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle *)
762                                          cfg, "TESTBED",
763                                          "OVERLAY_TOPOLOGY_FILE",
764                                          topology_file);
765   shutdown_task =
766       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
767                                     NULL);
768   GNUNET_TESTBED_run (NULL, cfg, num_peers, 0, NULL, NULL, &test_master, NULL);
769 }
770
771
772 /**
773  * Main function.
774  *
775  * @return 0 on success
776  */
777 int
778 main (int argc, char *const *argv)
779 {
780   static struct GNUNET_GETOPT_CommandLineOption options[] = {
781     {'p', "peers", "COUNT", gettext_noop ("Number of peers to run"), GNUNET_YES,
782      &GNUNET_GETOPT_set_uint, &num_peers},
783     {'t', "topology-file", "FILEPATH", gettext_noop ("Path to topology file"),
784      GNUNET_YES, &GNUNET_GETOPT_set_filename, &topology_file},
785     {'i', "sensors-interval", "INTERVAL",
786      gettext_noop ("Change the interval of running sensors to given value"),
787      GNUNET_YES, &GNUNET_GETOPT_set_uint, &sensors_interval},
788     {'a', "anomalous-peers", "COUNT",
789      gettext_noop ("Number of peers to simulate anomalies on"), GNUNET_YES,
790      &GNUNET_GETOPT_set_uint, &anomalous_peers},
791     GNUNET_GETOPT_OPTION_END
792   };
793
794   return (GNUNET_OK ==
795           GNUNET_PROGRAM_run (argc, argv, "gnunet-sensor-profiler",
796                               gettext_noop ("Profiler for sensor service"),
797                               options, &run, NULL)) ? 0 : 1;
798 }
799
800 /* end of gnunet-sensor-profiler.c */