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