peerstore: towards profiler
[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  * TODO:
23  * - Run X peers
24  * - Rewrite interval time (optional)
25  * - Run 1 dashboard
26  * - Monitor dashboard records
27  * - Prompt for anomalies when ready:
28  *  -- Cut Y peers (remove their connections to other X-Y peers but not the connections among themselves)
29  */
30
31 /**
32  * @file sensor/gnunet-sensor-profiler.c
33  * @brief Profiler for the sensor service
34  * @author Omar Tarabai
35  */
36 #include "platform.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_testbed_service.h"
39 #include "gnunet_peerstore_service.h"
40 #include "gnunet_sensor_service.h"
41
42 /**
43  * Information about a single peer
44  */
45 struct PeerInfo
46 {
47
48   /**
49    * Peer Identity
50    */
51   struct GNUNET_PeerIdentity peer_id;
52
53   /**
54    * Testbed peer handle
55    */
56   struct GNUNET_TESTBED_Peer *testbed_peer;
57
58 };
59
60
61 /**
62  * Name of the configuration file used
63  */
64 static const char *cfg_filename = "gnunet-sensor-profiler.conf";
65
66 /**
67  * Directory to read sensor definitions from
68  */
69 static const char *sensor_src_dir = "sensors";
70
71 /**
72  * Directory to write new sensor definitions to
73  */
74 static const char *sensor_dst_dir = "/tmp/gnunet-sensor-profiler";
75
76 /**
77  * Return value of the program
78  */
79 static int ok = 1;
80
81 /**
82  * Number of peers to run (Option -p)
83  */
84 static unsigned int num_peers = 0;
85
86 /**
87  * Set sensors running interval to this value (Option -i)
88  */
89 static unsigned int sensors_interval = 0;
90
91 /**
92  * Array of peer info for all peers
93  */
94 static struct PeerInfo *all_peers_info;
95
96 /**
97  * Number of peers that we already collected and start their info
98  */
99 static int peers_known = 0;
100
101 /**
102  * TESTBED operation connecting us to peerstore service on collection point
103  */
104 static struct GNUNET_TESTBED_Operation *peerstore_op;
105
106 /**
107  * Handle to peerstore service on collection point
108  */
109 static struct GNUNET_PEERSTORE_Handle *peerstore;
110
111
112 /**
113  * Copy directory recursively
114  *
115  * @param src Path to source directory
116  * @param dst Destination directory, will be created if it does not exist
117  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
118  */
119 static int
120 copy_dir (const char *src, const char *dst);
121
122
123 /**
124  * Do clean up and shutdown scheduler
125  */
126 static void
127 do_shutdown ()                  // TODO: schedule timeout shutdown
128 {
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down.\n");
130   if (NULL != peerstore_op)
131   {
132     GNUNET_TESTBED_operation_done (peerstore_op);
133     peerstore_op = NULL;
134   }
135   if (NULL != all_peers_info)
136   {
137     GNUNET_free (all_peers_info);
138     all_peers_info = NULL;
139   }
140   GNUNET_SCHEDULER_shutdown ();
141 }
142
143
144 /**
145  * Function called with each file/folder inside a directory that is being copied.
146  *
147  * @param cls closure, destination directory
148  * @param filename complete filename (absolute path)
149  * @return #GNUNET_OK to continue to iterate.
150  *         #GNUNET_SYSERR to abort iteration with error
151  */
152 static int
153 copy_dir_scanner (void *cls, const char *filename)
154 {
155   char *dst_dir = cls;
156   char *dst;
157   int copy_result;
158
159   GNUNET_asprintf (&dst, "%s%s%s", dst_dir, DIR_SEPARATOR_STR,
160                    GNUNET_STRINGS_get_short_name (filename));
161   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Copying `%s' to `%s'.\n", filename,
162               dst);
163   if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_YES))
164     copy_result = copy_dir (filename, dst);
165   else
166   {
167     if (GNUNET_YES == GNUNET_DISK_file_test (dst))
168       GNUNET_DISK_directory_remove (dst);
169     copy_result = GNUNET_DISK_file_copy (filename, dst);
170   }
171   GNUNET_free (dst);
172   return copy_result;
173 }
174
175
176 /**
177  * Copy directory recursively
178  *
179  * @param src Path to source directory
180  * @param dst Destination directory, will be created if it does not exist
181  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
182  */
183 static int
184 copy_dir (const char *src, const char *dst)
185 {
186   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Copying directory `%s' to `%s'.\n", src,
187               dst);
188   if (GNUNET_YES != GNUNET_DISK_directory_test (src, GNUNET_YES))
189     return GNUNET_SYSERR;
190   if (GNUNET_OK != GNUNET_DISK_directory_create (dst))
191     return GNUNET_SYSERR;
192   if (GNUNET_SYSERR ==
193       GNUNET_DISK_directory_scan (src, &copy_dir_scanner, (char *) dst))
194     return GNUNET_SYSERR;
195   return GNUNET_OK;
196 }
197
198
199 /**
200  * Function called with each file/folder inside source sensor directory.
201  *
202  * @param cls closure (unused)
203  * @param filename complete filename (absolute path)
204  * @return #GNUNET_OK to continue to iterate.
205  */
206 static int
207 sensor_dir_scanner (void *cls, const char *filename)
208 {
209   const char *file_basename;
210   char *dst_path;
211   struct GNUNET_CONFIGURATION_Handle *sensor_cfg;
212
213   file_basename = GNUNET_STRINGS_get_short_name (filename);
214   GNUNET_asprintf (&dst_path, "%s%s%s", sensor_dst_dir, DIR_SEPARATOR_STR,
215                    file_basename);
216   if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_NO))
217   {
218     GNUNET_assert (GNUNET_OK == copy_dir (filename, dst_path));
219   }
220   else
221   {
222     sensor_cfg = GNUNET_CONFIGURATION_create ();
223     GNUNET_assert (GNUNET_OK ==
224                    GNUNET_CONFIGURATION_parse (sensor_cfg, filename));
225     GNUNET_CONFIGURATION_set_value_string (sensor_cfg, file_basename,
226                                            "COLLECTION_POINT",
227                                            GNUNET_i2s_full (&all_peers_info[0].
228                                                             peer_id));
229     if (sensors_interval > 0)
230     {
231       GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename,
232                                              "INTERVAL",
233                                              (unsigned long long int)
234                                              sensors_interval);
235     }
236     GNUNET_CONFIGURATION_write (sensor_cfg, dst_path);
237     GNUNET_CONFIGURATION_destroy (sensor_cfg);
238   }
239   GNUNET_free (dst_path);
240   return GNUNET_OK;
241 }
242
243
244 /**
245  * Load sensor definitions and rewrite them to tmp location.
246  * Add collection point peer id and change running interval if needed.
247  */
248 static void
249 rewrite_sensors ()
250 {
251   GNUNET_assert (GNUNET_YES ==
252                  GNUNET_DISK_directory_test (sensor_src_dir, GNUNET_YES));
253   GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (sensor_dst_dir));
254   GNUNET_DISK_directory_scan (sensor_src_dir, &sensor_dir_scanner, NULL);
255 }
256
257
258 /**
259  * Callback to be called when dashboard service is started
260  *
261  * @param cls the callback closure from functions generating an operation
262  * @param op the operation that has been finished
263  * @param emsg error message in case the operation has failed; will be NULL if
264  *          operation has executed successfully.
265  */
266 static void
267 dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op,
268                    const char *emsg)
269 {
270   if (NULL != emsg)
271   {
272     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
273     GNUNET_assert (0);
274   }
275   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Dashboard service started.\n");
276   //TODO:
277   GNUNET_TESTBED_operation_done (op);
278 }
279
280
281 /**
282  * Callback to be called when peerstore service connect operation is completed
283  *
284  * @param cls the callback closure from functions generating an operation
285  * @param op the operation that has been finished
286  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
287  * @param emsg error message in case the operation has failed; will be NULL if
288  *          operation has executed successfully.
289  */
290 static void
291 peerstore_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
292                       void *ca_result, const char *emsg)
293 {
294   if (NULL != emsg)
295   {
296     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
297     GNUNET_assert (0);
298   }
299   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peerstore service.\n");
300   //TODO
301 }
302
303
304 /**
305  * Adapter function called to establish a connection to peerstore service.
306  *
307  * @param cls closure
308  * @param cfg configuration of the peer to connect to; will be available until
309  *          GNUNET_TESTBED_operation_done() is called on the operation returned
310  *          from GNUNET_TESTBED_service_connect()
311  * @return service handle to return in 'op_result', NULL on error
312  */
313 static void *
314 peerstore_connect_adapter (void *cls,
315                            const struct GNUNET_CONFIGURATION_Handle *cfg)
316 {
317   peerstore = GNUNET_PEERSTORE_connect (cfg);
318   GNUNET_assert (NULL != peerstore);
319   return peerstore;
320 }
321
322
323 /**
324  * Adapter function called to destroy a connection to peerstore service.
325  *
326  * @param cls closure
327  * @param op_result service handle returned from the connect adapter
328  */
329 static void
330 peerstore_disconnect_adapter (void *cls, void *op_result)
331 {
332   GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO);
333   peerstore = NULL;
334   peerstore_op = NULL;
335 }
336
337
338 /**
339  * Callback to be called when sensor service is started
340  *
341  * @param cls the callback closure from functions generating an operation
342  * @param op the operation that has been finished
343  * @param emsg error message in case the operation has failed; will be NULL if
344  *          operation has executed successfully.
345  */
346 static void
347 sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op,
348                         const char *emsg)
349 {
350   struct PeerInfo *peer = cls;
351
352   if (NULL != emsg)
353   {
354     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
355     GNUNET_assert (0);
356   }
357   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sensor service started on peer `%s'.\n",
358               GNUNET_i2s (&peer->peer_id));
359   //TODO:
360   GNUNET_TESTBED_operation_done (op);
361 }
362
363
364 /**
365  * Callback to be called when the requested peer information is available
366  *
367  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
368  * @param op the operation this callback corresponds to
369  * @param pinfo the result; will be NULL if the operation has failed
370  * @param emsg error message if the operation has failed; will be NULL if the
371  *          operation is successfull
372  */
373 static void
374 peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
375               const struct GNUNET_TESTBED_PeerInformation *pinfo,
376               const char *emsg)
377 {
378   struct GNUNET_TESTBED_Peer *testbed_peer = cb_cls;
379   struct PeerInfo *peer = &all_peers_info[peers_known];
380
381   if (NULL != emsg)
382   {
383     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
384     GNUNET_assert (0);
385   }
386   peer->testbed_peer = testbed_peer;
387   GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id);
388   peers_known++;
389   if (1 == peers_known)         /* First peer is collection point */
390   {
391     /* Rewrite sensors */
392     rewrite_sensors ();
393     /* Start dashboard */
394     GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensordashboard",
395                                         &dashboard_started, NULL, 1);
396   }
397   /* Start sensor service on every peer */
398   GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensor",
399                                       &sensor_service_started, peer, 1);
400   if (num_peers == peers_known) /* Last peer */
401   {
402     /* Connect to peerstore on first peer (collection point) */
403     peerstore_op =
404         GNUNET_TESTBED_service_connect (NULL, all_peers_info[0].testbed_peer,
405                                         "peerstore", &peerstore_connect_cb,
406                                         NULL, &peerstore_connect_adapter,
407                                         &peerstore_disconnect_adapter, NULL);
408   }
409   GNUNET_TESTBED_operation_done (op);
410 }
411
412
413 /**
414  * Signature of a main function for a testcase.
415  *
416  * @param cls closure
417  * @param h the run handle
418  * @param num number of peers in 'peers'
419  * @param peers handle to peers run in the testbed.  NULL upon timeout (see
420  *          GNUNET_TESTBED_test_run()).
421  * @param links_succeeded the number of overlay link connection attempts that
422  *          succeeded
423  * @param links_failed the number of overlay link connection attempts that
424  *          failed
425  * @see GNUNET_TESTBED_test_run()
426  */
427 static void
428 test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num,
429              struct GNUNET_TESTBED_Peer **peers, unsigned int links_succeeded,
430              unsigned int links_failed)
431 {
432   int i;
433
434   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435               "%d peers started. %d links succeeded. %d links failed.\n",
436               num_peers, links_succeeded, links_failed);
437   GNUNET_assert (num == num_peers);
438   GNUNET_assert (0 == links_failed);
439   /* Collect peer information */
440   all_peers_info = GNUNET_new_array (num_peers, struct PeerInfo);
441
442   for (i = 0; i < num_peers; i++)
443   {
444     GNUNET_TESTBED_peer_get_information (peers[i],
445                                          GNUNET_TESTBED_PIT_CONFIGURATION,
446                                          &peer_info_cb, peers[i]);
447   }
448 }
449
450
451 /**
452  * Verify that the user passed correct CL args
453  *
454  * @return #GNUNET_OK if arguments are valid, #GNUNET_SYSERR otherwise
455  */
456 static int
457 verify_args ()
458 {
459   if (num_peers < 3)
460   {
461     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
462                 _
463                 ("Invalid or missing number of peers. Set at least 3 peers.\n"));
464     return GNUNET_SYSERR;
465   }
466   return GNUNET_OK;
467 }
468
469
470 /**
471  * Actual main function.
472  *
473  * @param cls unused
474  * @param args remaining args, unused
475  * @param cfgfile name of the configuration
476  * @param cfg configuration handle
477  */
478 static void
479 run (void *cls, char *const *args, const char *cf,
480      const struct GNUNET_CONFIGURATION_Handle *c)
481 {
482   struct GNUNET_CONFIGURATION_Handle *cfg;
483   double links;
484
485   if (GNUNET_OK != verify_args ())
486   {
487     do_shutdown ();
488     return;
489   }
490   cfg = GNUNET_CONFIGURATION_create ();
491   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_filename));
492   links = log (num_peers) * log (num_peers) * num_peers / 2;
493   GNUNET_CONFIGURATION_set_value_number ((struct GNUNET_CONFIGURATION_Handle *)
494                                          cfg, "TESTBED", "OVERLAY_RANDOM_LINKS",
495                                          (unsigned long long int) links);
496   GNUNET_TESTBED_run (NULL, cfg, num_peers, 0, NULL, NULL, &test_master, NULL);
497   GNUNET_CONFIGURATION_destroy (cfg);
498 }
499
500
501 /**
502  * Main function.
503  *
504  * @return 0 on success
505  */
506 int
507 main (int argc, char *const *argv)
508 {
509   static struct GNUNET_GETOPT_CommandLineOption options[] = {
510     {'p', "peers", "COUNT", gettext_noop ("Number of peers to run"), GNUNET_YES,
511      &GNUNET_GETOPT_set_uint, &num_peers},
512     {'i', "sensors-interval", "INTERVAL",
513      gettext_noop ("Change the interval or running sensors to given value"),
514      GNUNET_YES, &GNUNET_GETOPT_set_uint, &sensors_interval},
515     GNUNET_GETOPT_OPTION_END
516   };
517
518   return (GNUNET_OK ==
519           GNUNET_PROGRAM_run (argc, argv, "gnunet-sensor-profiler",
520                               gettext_noop ("Profiler for sensor service"),
521                               options, &run, NULL)) ? ok : 1;
522 }
523
524 /* end of gnunet-sensor-profiler.c */