added some fixme
[oweals/gnunet.git] / src / sensor / gnunet-service-sensor.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-service-sensor.c
23  * @brief sensor service implementation
24  * @author Omar Tarabai
25  */
26 #include <inttypes.h>
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "sensor.h"
30 #include "gnunet_statistics_service.h"
31
32 //TODO: GNUNET_SERVER_receive_done() ?
33
34 /**
35  * Minimum sensor execution interval (in seconds)
36  */
37 #define MIN_INTERVAL 30
38
39 /**
40  * Structure containing sensor definition
41  */
42 struct SensorInfo
43 {
44
45   /*
46    * Sensor name
47    */
48   char *name;
49
50   /*
51    * Path to definition file
52    */
53   char *def_file;
54
55   /*
56    * First part of version number
57    */
58   uint16_t version_major;
59
60   /*
61    * Second part of version number
62    */
63   uint16_t version_minor;
64
65   /*
66    * Sensor description
67    */
68   char *description;
69
70   /*
71    * Sensor currently enabled
72    */
73   int enabled;
74
75   /*
76    * Category under which the sensor falls (e.g. tcp, datastore)
77    */
78   char *category;
79
80   /*
81    * When does the sensor become active
82    */
83   struct GNUNET_TIME_Absolute *start_time;
84
85   /*
86    * When does the sensor expire
87    */
88   struct GNUNET_TIME_Absolute *end_time;
89
90   /*
91    * Time interval to collect sensor information (e.g. every 1 min)
92    */
93   struct GNUNET_TIME_Relative interval;
94
95   /*
96    * Lifetime of an information sample after which it is deleted from storage
97    */
98   struct GNUNET_TIME_Relative lifetime;
99
100   /*
101    * A set of required peer capabilities for the sensor to collect meaningful information (e.g. ipv6)
102    */
103   char *capabilities;
104
105   /*
106    * Either "gnunet-statistics" or external "process"
107    */
108   char *source;
109
110   /*
111    * Name of the GNUnet service that is the source for the gnunet-statistics entry
112    */
113   char *gnunet_stat_service;
114
115   /*
116    * Name of the gnunet-statistics entry
117    */
118   char *gnunet_stat_name;
119
120   /**
121    * Handle to statistics get request (OR GNUNET_SCHEDULER_NO_TASK)
122    */
123   struct GNUNET_STATISTICS_GetHandle *gnunet_stat_get_handle;
124
125   /*
126    * Name of the external process to be executed
127    */
128   char *ext_process;
129
130   /*
131    * Arguments to be passed to the external process
132    */
133   char *ext_args;
134
135   /*
136    * The output datatype to be expected
137    */
138   char *expected_datatype;
139
140   /*
141    * Peer-identity of peer running collection point
142    */
143   struct GNUNET_PeerIdentity *collection_point;
144
145   /*
146    * Time interval to send sensor information to collection point (e.g. every 30 mins)
147    */
148   struct GNUNET_TIME_Relative *collection_interval;
149
150   /*
151    * Flag specifying if value is to be communicated to the p2p network
152    */
153   int p2p_report;
154
155   /*
156    * Time interval to communicate value to the p2p network
157    */
158   struct GNUNET_TIME_Relative *p2p_interval;
159
160   /*
161    * Execution task (OR GNUNET_SCHEDULER_NO_TASK)
162    */
163   GNUNET_SCHEDULER_TaskIdentifier execution_task;
164
165   /*
166    * Is the sensor being executed
167    */
168   int running;
169
170 };
171
172 /**
173  * Our configuration.
174  */
175 static const struct GNUNET_CONFIGURATION_Handle *cfg;
176
177 /**
178  * Hashmap of loaded sensor definitions
179  */
180 struct GNUNET_CONTAINER_MultiHashMap *sensors;
181
182 /**
183  * Supported sources of sensor information
184  */
185 static const char *sources[] = { "gnunet-statistics", "process", NULL };
186
187 /**
188  * Supported datatypes of sensor information
189  */
190 static const char *datatypes[] = { "uint64", "double", "string", NULL };
191
192 /**
193  * Handle to statistics service
194  */
195 struct GNUNET_STATISTICS_Handle *statistics;
196
197 //TODO: logging macro that includes sensor info
198
199 /**
200  * Remove sensor execution from scheduler
201  *
202  * @param cls unused
203  * @param key hash of sensor name, key to hashmap
204  * @param value a 'struct SensorInfo *'
205  * @return #GNUNET_YES if we should continue to
206  *         iterate,
207  *         #GNUNET_NO if not.
208  */
209 int unschedule_sensor(void *cls,
210     const struct GNUNET_HashCode *key, void *value)
211 {
212   struct SensorInfo *sensorinfo = value;
213
214   if(NULL != sensorinfo->gnunet_stat_get_handle)
215   {
216     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Canceling a statistics get request for sensor `%s'\n", sensorinfo->name);
217     GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle);
218   }
219   if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
220   {
221     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unscheduling sensor `%s'\n", sensorinfo->name);
222     GNUNET_SCHEDULER_cancel(sensorinfo->execution_task);
223     sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK;
224   }
225   return GNUNET_YES;
226 }
227
228 /**
229  * Task run during shutdown.
230  *
231  * @param cls unused
232  * @param tc unused
233  */
234 static void
235 shutdown_task (void *cls,
236                const struct GNUNET_SCHEDULER_TaskContext *tc)
237 {
238   GNUNET_CONTAINER_multihashmap_iterate(sensors, &unschedule_sensor, NULL);
239   /* Free sensor information */
240   if(NULL != statistics)
241     GNUNET_STATISTICS_destroy(statistics, GNUNET_YES);
242   /* Destroy sensor hashmap */
243   GNUNET_SCHEDULER_shutdown();
244 }
245
246
247 /**
248  * A client disconnected.  Remove all of its data structure entries.
249  *
250  * @param cls closure, NULL
251  * @param client identification of the client
252  */
253 static void
254 handle_client_disconnect (void *cls,
255                           struct GNUNET_SERVER_Client
256                           * client)
257 {
258 }
259
260 /**
261  * Parses a version number string into major and minor
262  *
263  * @param version full version string
264  * @param major pointer to parsed major value
265  * @param minor pointer to parsed minor value
266  * @return #GNUNET_OK if parsing went ok, #GNUNET_SYSERROR in case of error
267  */
268 static int
269 version_parse(char *version, uint16_t *major, uint16_t *minor)
270 {
271   int majorval = 0;
272   int minorval = 0;
273
274   for(; isdigit(*version); version++)
275   {
276     majorval *= 10;
277     majorval += *version - '0';
278   }
279   if(*version != '.')
280     return GNUNET_SYSERR;
281   version++;
282   for(; isdigit(*version); version++)
283   {
284     minorval *= 10;
285     minorval += *version - '0';
286   }
287   if(*version != 0)
288     return GNUNET_SYSERR;
289   *major = majorval;
290   *minor = minorval;
291
292   return GNUNET_OK;
293 }
294
295 /**
296  * Load sensor definition from configuration
297  *
298  * @param cfg configuration handle
299  * @param sectionname configuration section containing definition
300  */
301 static struct SensorInfo *
302 load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
303 {
304   struct SensorInfo *sensor;
305   char *version_str;
306   char *starttime_str;
307   char *endtime_str;
308   unsigned long long time_sec;
309
310   sensor = GNUNET_new(struct SensorInfo);
311   //name
312   sensor->name = GNUNET_strdup(sectionname);
313   //version
314   if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "VERSION", &version_str))
315   {
316     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor version\n"));
317     GNUNET_free(sensor);
318     return NULL;
319   }
320   if(GNUNET_OK != version_parse(version_str, &(sensor->version_major), &(sensor->version_minor)))
321   {
322     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid sensor version number, format should be major.minor\n"));
323     GNUNET_free(sensor);
324     GNUNET_free(version_str);
325     return NULL;
326   }
327   GNUNET_free(version_str);
328   //description
329   GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "DESCRIPTION", &sensor->description);
330   //category
331   if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "CATEGORY", &sensor->category) ||
332         NULL == sensor->category)
333   {
334     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor category\n"));
335     GNUNET_free(sensor);
336     return NULL;
337   }
338   //enabled
339   if(GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "ENABLED"))
340     sensor->enabled = GNUNET_NO;
341   else
342     sensor->enabled = GNUNET_YES;
343   //start time
344   sensor->start_time = NULL;
345   if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "START_TIME", &starttime_str))
346   {
347     GNUNET_STRINGS_fancy_time_to_absolute(starttime_str, sensor->start_time);
348     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Start time loaded: `%s'. Parsed: %d\n", starttime_str, (NULL != sensor->start_time));
349     GNUNET_free(starttime_str);
350   }
351   //end time
352   sensor->end_time = NULL;
353   if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "END_TIME", &endtime_str))
354   {
355     GNUNET_STRINGS_fancy_time_to_absolute(endtime_str, sensor->end_time);
356     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "End time loaded: `%s'. Parsed: %d\n", endtime_str, (NULL != sensor->end_time));
357     GNUNET_free(endtime_str);
358   }
359   //interval
360   if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "INTERVAL", &time_sec))
361   {
362     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor run interval\n"));
363     GNUNET_free(sensor);
364     return NULL;
365   }
366   if(time_sec < MIN_INTERVAL)
367   {
368     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Sensor run interval too low (%" PRIu64 " < %d)\n"),
369         time_sec, MIN_INTERVAL);
370     GNUNET_free(sensor);
371     return NULL;
372   }
373   sensor->interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
374   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Interval loaded: %" PRIu64 "\n", sensor->interval.rel_value_us);
375   //lifetime
376   if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "LIFETIME", &time_sec))
377   {
378     sensor->lifetime = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
379     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Lifetime loaded: %" PRIu64 "\n", sensor->lifetime.rel_value_us);
380   }
381   else
382     sensor->lifetime = GNUNET_TIME_UNIT_FOREVER_REL;
383   //capabilities TODO
384   //source
385   if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "SOURCE", sources, (const char **)&sensor->source))
386   {
387     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor source\n"));
388     GNUNET_free(sensor);
389     return NULL;
390   }
391   if(sources[0] == sensor->source) //gnunet-statistics
392   {
393     if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_SERVICE", &sensor->gnunet_stat_service) ||
394         GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_NAME", &sensor->gnunet_stat_name))
395     {
396       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor gnunet-statistics source information\n"));
397       GNUNET_free(sensor);
398       return NULL;
399     }
400     sensor->gnunet_stat_get_handle = NULL;
401   }
402   else if(sources[1] == sensor->source) //process
403   {
404     if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_PROCESS", &sensor->ext_process))
405     {
406       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor process name\n"));
407       GNUNET_free(sensor);
408       return NULL;
409     }
410     GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_ARGS", &sensor->ext_args);
411   }
412   //expected datatype
413   if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "EXPECTED_DATATYPE", datatypes, (const char **)&sensor->expected_datatype))
414   {
415     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor expected datatype\n"));
416     GNUNET_free(sensor);
417     return NULL;
418   }
419   if(sources[0] == sensor->source && datatypes[0] != sensor->expected_datatype)
420   {
421     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid expected datatype, gnunet-statistics returns uint64 values\n"));
422     GNUNET_free(sensor);
423     return NULL;
424   }
425   //TODO: reporting mechanism
426   //execution task
427   sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
428   //running
429   sensor->running = GNUNET_NO;
430
431   return sensor;
432 }
433
434 /**
435  * Load sensor definition from file
436  *
437  * @param filename full path to file containing sensor definition
438  */
439 static struct SensorInfo *
440 load_sensor_from_file(const char *filename)
441 {
442   struct GNUNET_CONFIGURATION_Handle *sensorcfg;
443   const char *filebasename;
444   struct SensorInfo *sensor;
445
446   //test file
447   if(GNUNET_YES != GNUNET_DISK_file_test(filename))
448   {
449     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to access sensor file: %s\n"), filename);
450     return NULL;
451   }
452   //load file as configuration
453   sensorcfg = GNUNET_CONFIGURATION_create();
454   if(GNUNET_SYSERR == GNUNET_CONFIGURATION_parse(sensorcfg, filename))
455   {
456     GNUNET_CONFIGURATION_destroy(sensorcfg);
457     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to load sensor definition: %s\n"), filename);
458     return NULL;
459   }
460   //configuration section should be the same as filename
461   filebasename = GNUNET_STRINGS_get_short_name(filename);
462   sensor = load_sensor_from_cfg(sensorcfg, filebasename);
463   sensor->def_file = GNUNET_strdup(filename);
464
465   GNUNET_CONFIGURATION_destroy(sensorcfg);
466
467   return sensor;
468 }
469
470 /**
471  * Compares version numbers of two sensors
472  *
473  * @param s1 first sensor
474  * @param s2 second sensor
475  * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2
476  */
477 static int
478 sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2)
479 {
480   if(s1->version_major == s2->version_major)
481     return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor);
482   else
483     return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major);
484 }
485
486 /**
487  * Adds a new sensor to given hashmap.
488  * If the same name exist, compares versions and update if old.
489  *
490  * @param sensor Sensor structure to add
491  * @param map Hashmap to add to
492  * @return #GNUNET_YES if added, #GNUNET_NO if not added which is not necessarily an error
493  */
494 static int
495 add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHashMap *map)
496 {
497   struct GNUNET_HashCode key;
498   struct SensorInfo *existing;
499
500   GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name), &key);
501   existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
502   if(NULL != existing) //sensor with same name already exists
503   {
504     if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist
505     {
506       GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Sensor `%s' already exists with same or newer version\n"), sensor->name);
507       return GNUNET_NO;
508     }
509     else
510     {
511       GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
512       GNUNET_free(existing);
513       GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Upgrading sensor `%s' to a newer version\n"), sensor->name);
514     }
515   }
516   if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
517   {
518     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error adding new sensor `%s' to global hashmap, this should not happen\n"), sensor->name);
519     return GNUNET_NO;
520   }
521
522   return GNUNET_YES;
523 }
524
525 /**
526  * Iterating over files in sensors directory
527  *
528  * @param cls closure
529  * @param filename complete filename (absolute path)
530  * @return #GNUNET_OK to continue to iterate,
531  *  #GNUNET_NO to stop iteration with no error,
532  *  #GNUNET_SYSERR to abort iteration with error!
533  */
534 static int
535 reload_sensors_dir_cb(void *cls, const char *filename)
536 {
537   struct SensorInfo *sensor;
538
539   sensor = load_sensor_from_file(filename);
540   if(NULL == sensor)
541   {
542     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error loading sensor from file: %s\n"), filename);
543     return GNUNET_OK;
544   }
545   if(GNUNET_YES == add_sensor_to_hashmap(sensor, sensors))
546     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Sensor `%s' added to global hashmap\n"), sensor->name);
547   else
548     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, ("Could not add sensor `%s' to global hashmap\n"), sensor->name);
549
550   return GNUNET_OK;
551 }
552
553 /*
554  * Get path to the directory containing the sensor definition files
555  *
556  * @return sensor files directory
557  */
558 static char *
559 get_sensor_dir()
560 {
561   char* datadir;
562   char* sensordir;
563
564   datadir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
565   GNUNET_asprintf(&sensordir, "%ssensors%s",
566       datadir, DIR_SEPARATOR_STR);
567
568   return sensordir;
569 }
570
571 /**
572  * Reads sensor definitions from data files
573  *
574  */
575 static void
576 reload_sensors()
577 {
578   char* sensordir;
579   int filesfound;
580
581   sensordir = get_sensor_dir();
582   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Reloading sensor definitions from directory `%s'\n"), sensordir);
583   GNUNET_assert(GNUNET_YES == GNUNET_DISK_directory_test(sensordir, GNUNET_YES));
584
585   //read all files in sensors directory
586   filesfound = GNUNET_DISK_directory_scan(sensordir, &reload_sensors_dir_cb, NULL);
587   GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Loaded %d/%d sensors from directory `%s'\n"),
588       GNUNET_CONTAINER_multihashmap_size(sensors), filesfound, sensordir);
589 }
590
591 /**
592  * Creates a structure with basic sensor info to be sent to a client
593  *
594  * @param sensor sensor information
595  * @return message ready to be sent to client
596  */
597 static struct SensorInfoMessage *
598 create_sensor_info_msg(struct SensorInfo *sensor)
599 {
600   struct SensorInfoMessage *msg;
601   uint16_t len;
602   size_t name_len;
603   size_t desc_len;
604   char *str_ptr;
605
606   name_len = strlen(sensor->name);
607   if(NULL == sensor->description)
608     desc_len = 0;
609   else
610     /* FIXME strlen + 1 */
611     desc_len = strlen(sensor->description);
612   len = 0;
613   len += sizeof(struct SensorInfoMessage);
614   len += name_len;
615   len += desc_len;
616   msg = GNUNET_malloc(len);
617   msg->header.size = htons(len);
618   msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
619   msg->name_len = htons(name_len);
620   msg->description_len = htons(desc_len);
621   msg->version_major = htons(sensor->version_major);
622   msg->version_minor = htons(sensor->version_minor);
623   str_ptr = (char*) &msg[1];
624   memcpy(str_ptr, sensor->name, name_len);
625   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
626         name_len, name_len, str_ptr);
627   str_ptr += name_len;
628   memcpy(str_ptr, sensor->description, desc_len);
629   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
630           desc_len, desc_len, str_ptr);
631
632   return msg;
633 }
634
635 /**
636  * Handle GET SENSOR message.
637  *
638  * @param cls closure
639  * @param client identification of the client
640  * @param message the actual message
641  */
642 static void
643 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
644             const struct GNUNET_MessageHeader *message)
645 {
646   struct GNUNET_SERVER_TransmitContext *tc;
647   char *sensorname;
648   size_t sensorname_len;
649   struct GNUNET_HashCode key;
650   struct SensorInfo *sensorinfo;
651   struct SensorInfoMessage *msg;
652
653   sensorname = (char *)&message[1];
654   sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
655   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
656               "GET SENSOR", sensorname_len, sensorname_len, sensorname);
657   tc = GNUNET_SERVER_transmit_context_create (client);
658   GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
659   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
660   sensorinfo = (struct SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
661   if(NULL != sensorinfo)
662   {
663     msg = create_sensor_info_msg(sensorinfo);
664     GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
665     GNUNET_free(msg);
666   }
667   else
668     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
669         sensorname_len, sensorname);
670   GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
671   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
672 }
673
674 /**
675  * Iterator for sensors and adds them to transmit context
676  *
677  * @param cls a 'struct GNUNET_SERVER_TransmitContext *'
678  * @param key hash of sensor name, key to hashmap
679  * @param value a 'struct SensorInfo *'
680  */
681 int add_sensor_to_tc(void *cls,
682     const struct GNUNET_HashCode *key, void *value)
683 {
684   struct GNUNET_SERVER_TransmitContext *tc = cls;
685   struct SensorInfo *sensorinfo = value;
686   struct SensorInfoMessage *msg;
687
688   msg = create_sensor_info_msg(sensorinfo);
689   GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
690
691   GNUNET_free(msg);
692
693   return GNUNET_YES;
694 }
695
696 /**
697  * Handle GET ALL SENSORS message.
698  *
699  * @param cls closure
700  * @param client identification of the client
701  * @param message the actual message
702  */
703 static void
704 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
705             const struct GNUNET_MessageHeader *message)
706 {
707   struct GNUNET_SERVER_TransmitContext *tc;
708
709   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
710                 "GET ALL SENSOR");
711   tc = GNUNET_SERVER_transmit_context_create (client);
712   GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
713   GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
714   GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
715 }
716
717 /**
718  * Do a series of checks to determine if sensor should execute
719  *
720  * @return #GNUNET_YES / #GNUNET_NO
721  */
722 static int
723 should_run_sensor(struct SensorInfo *sensorinfo)
724 {
725   //FIXME: some checks should disable the sensor (e.g. expired)
726   struct GNUNET_TIME_Absolute now;
727
728   if(GNUNET_NO == sensorinfo->enabled)
729   {
730     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
731     return GNUNET_NO;
732   }
733   now = GNUNET_TIME_absolute_get();
734   if(NULL != sensorinfo->start_time
735       && now.abs_value_us < sensorinfo->start_time->abs_value_us)
736   {
737     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n", sensorinfo->name);
738     return GNUNET_NO;
739   }
740   if(NULL != sensorinfo->end_time
741       && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
742   {
743     GNUNET_log(GNUNET_ERROR_TYPE_INFO, "End time for sensor `%s' passed, will not run\n", sensorinfo->name);
744     return GNUNET_NO;
745   }
746   return GNUNET_YES;
747 }
748
749 /**
750  * Callback function to process statistic values
751  *
752  * @param cls 'struct SensorInfo *'
753  * @param subsystem name of subsystem that created the statistic
754  * @param name the name of the datum
755  * @param value the current value
756  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
757  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
758  */
759 int sensor_statistics_iterator (void *cls,
760     const char *subsystem,
761     const char *name,
762     uint64_t value,
763     int is_persistent)
764 {
765   struct SensorInfo *sensorinfo = cls;
766
767   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %" PRIu64 "\n", sensorinfo->name, value);
768   return GNUNET_OK;
769 }
770
771 /**
772  * Continuation called after sensor gets all gnunet statistics values
773  *
774  * @param cls 'struct SensorInfo *'
775  * @param success #GNUNET_OK if statistics were
776  *        successfully obtained, #GNUNET_SYSERR if not.
777  */
778 void end_sensor_run_stat (void *cls, int success)
779 {
780   struct SensorInfo *sensorinfo = cls;
781
782   sensorinfo->gnunet_stat_get_handle = NULL;
783   sensorinfo->running = GNUNET_NO;
784 }
785
786 /**
787  * Callback for output of executed sensor process
788  *
789  * @param cls 'struct SensorInfo *'
790  * @param line line of output from a command, NULL for the end
791  */
792 void sensor_process_callback (void *cls, const char *line)
793 {
794   struct SensorInfo *sensorinfo = cls;
795
796   if(NULL == line)
797   {
798     sensorinfo->running = GNUNET_NO;
799     return;
800   }
801   GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n", sensorinfo->name, line);
802 }
803
804 /**
805  * Actual execution of a sensor
806  *
807  * @param cls 'struct SensorInfo'
808  * @param tc unsed
809  */
810 void
811 sensor_run (void *cls,
812     const struct GNUNET_SCHEDULER_TaskContext * tc)
813 {
814   struct SensorInfo *sensorinfo = cls;
815   int check_result;
816   char *sensors_dir;
817   char *process_path;
818
819   sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
820   if(GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
821   {
822     GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sensor `%s' running for too long, will try again next interval\n", sensorinfo->name);
823     return;
824   }
825   if(GNUNET_NO == should_run_sensor(sensorinfo))
826     return;
827   sensorinfo->running = GNUNET_YES;
828   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
829   if(sources[0] == sensorinfo->source) //gnunet-statistics
830   {
831     if(NULL == statistics)
832     {
833       statistics = GNUNET_STATISTICS_create("sensor", cfg);
834     }
835     sensorinfo->gnunet_stat_get_handle = GNUNET_STATISTICS_get(statistics,
836         sensorinfo->gnunet_stat_service,
837         sensorinfo->gnunet_stat_name,
838         sensorinfo->interval, //try to get values only for the interval of the sensor
839         &end_sensor_run_stat,
840         &sensor_statistics_iterator,
841         sensorinfo);
842   }
843   else if(sources[1] == sensorinfo->source)
844   {
845     //FIXME: break execution if process is a path
846     //check if the process exists in $PATH
847     process_path = GNUNET_strdup(sensorinfo->ext_process);
848     check_result =
849         GNUNET_OS_check_helper_binary(sensorinfo->ext_process, GNUNET_NO, NULL); //search in $PATH
850     if(GNUNET_SYSERR == check_result)
851     {
852       //search in sensor directory
853       sensors_dir = get_sensor_dir();
854       GNUNET_free(process_path);
855       GNUNET_asprintf(&process_path, "%s%s-files%s%s",
856           sensors_dir,
857           sensorinfo->name,
858           DIR_SEPARATOR_STR,
859           sensorinfo->ext_process);
860       GNUNET_free(sensors_dir);
861       check_result =
862         GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
863     }
864     if(GNUNET_SYSERR == check_result)
865     {
866       GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n",
867           sensorinfo->name,
868           sensorinfo->ext_process);
869       //FIXME: disable sensor here?
870       sensorinfo->running = GNUNET_NO;
871       GNUNET_free(process_path);
872       return;
873     }
874     GNUNET_OS_command_run(&sensor_process_callback,
875         sensorinfo,
876         GNUNET_TIME_UNIT_FOREVER_REL,
877         process_path,
878         sensorinfo->ext_process,
879         sensorinfo->ext_args,
880         NULL);
881     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name);
882   }
883   else
884   {
885     sensorinfo->running = GNUNET_NO;
886     GNUNET_break(0); //shouldn't happen
887   }
888 }
889
890 /**
891  * Starts the execution of a sensor
892  *
893  * @param cls unused
894  * @param key hash of sensor name, key to hashmap (unused)
895  * @param value a 'struct SensorInfo *'
896  * @return #GNUNET_YES if we should continue to
897  *         iterate,
898  *         #GNUNET_NO if not.
899  */
900 int schedule_sensor(void *cls,
901     const struct GNUNET_HashCode *key, void *value)
902 {
903   struct SensorInfo *sensorinfo = value;
904
905   if(GNUNET_NO == should_run_sensor(sensorinfo))
906     return GNUNET_YES;
907   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
908       sensorinfo->name, sensorinfo->interval.rel_value_us);
909   if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
910   {
911     GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' execution task already set, this should not happen\n", sensorinfo->name);
912     return GNUNET_NO;
913   }
914   sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
915   return GNUNET_YES;
916 }
917
918 /**
919  * Starts the execution of all enabled sensors
920  *
921  */
922 static void
923 schedule_all_sensors()
924 {
925   GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
926 }
927
928 /**
929  * Process statistics requests.
930  *
931  * @param cls closure
932  * @param server the initialized server
933  * @param c configuration to use
934  */
935 static void
936 run (void *cls,
937      struct GNUNET_SERVER_Handle *server,
938      const struct GNUNET_CONFIGURATION_Handle *c)
939 {
940   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
941     {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
942      0},
943     {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
944      sizeof (struct GNUNET_MessageHeader)},
945     {NULL, NULL, 0, 0}
946   };
947
948   cfg = c;
949   sensors = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
950   reload_sensors();
951   schedule_all_sensors();
952   GNUNET_SERVER_add_handlers (server, handlers);
953   GNUNET_SERVER_disconnect_notify (server, 
954                                    &handle_client_disconnect,
955                                    NULL);
956   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
957                                 &shutdown_task,
958                                 NULL);
959 }
960
961
962 /**
963  * The main function for the sensor service.
964  *
965  * @param argc number of arguments from the command line
966  * @param argv command line arguments
967  * @return 0 ok, 1 on error
968  */
969 int
970 main (int argc, char *const *argv)
971 {
972   return (GNUNET_OK ==
973           GNUNET_SERVICE_run (argc,
974                               argv,
975                               "sensor",
976                               GNUNET_SERVICE_OPTION_NONE,
977                               &run, NULL)) ? 0 : 1;
978 }
979
980 /* end of gnunet-service-sensor.c */