2 This file is part of GNUnet.
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.
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.
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.
22 * @file sensor/gnunet-service-sensor.c
23 * @brief sensor service implementation
24 * @author Omar Tarabai
28 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_peerstore_service.h"
34 * Minimum sensor execution interval (in seconds)
36 #define MIN_INTERVAL 30
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
44 * Hashmap of loaded sensor definitions
46 static struct GNUNET_CONTAINER_MultiHashMap *sensors;
49 * Supported sources of sensor information
51 static const char *sources[] = { "gnunet-statistics", "process", NULL };
54 * Supported datatypes of sensor information
56 static const char *datatypes[] = { "numeric", "string", NULL };
59 * Handle to statistics service
61 struct GNUNET_STATISTICS_Handle *statistics;
64 * Handle to peerstore service
66 struct GNUNET_PEERSTORE_Handle *peerstore;
71 char *subsystem = "sensor";
76 struct GNUNET_PeerIdentity peerid;
79 * Remove sensor execution from scheduler
82 * @param key hash of sensor name, key to hashmap
83 * @param value a 'struct SensorInfo *'
84 * @return #GNUNET_YES if we should continue to
88 static int destroy_sensor(void *cls,
89 const struct GNUNET_HashCode *key, void *value)
91 struct SensorInfo *sensorinfo = value;
93 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor `%s'\n", sensorinfo->name);
94 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
96 GNUNET_SCHEDULER_cancel(sensorinfo->execution_task);
97 sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK;
99 if(NULL != sensorinfo->gnunet_stat_get_handle)
101 GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle);
102 sensorinfo->gnunet_stat_get_handle = NULL;
104 if(NULL != sensorinfo->ext_cmd)
106 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
107 sensorinfo->ext_cmd = NULL;
109 if(NULL != sensorinfo->cfg)
110 GNUNET_CONFIGURATION_destroy(sensorinfo->cfg);
111 if(NULL != sensorinfo->name)
112 GNUNET_free(sensorinfo->name);
113 if(NULL != sensorinfo->def_file)
114 GNUNET_free(sensorinfo->def_file);
115 if(NULL != sensorinfo->description)
116 GNUNET_free(sensorinfo->description);
117 if(NULL != sensorinfo->category)
118 GNUNET_free(sensorinfo->category);
119 if(NULL != sensorinfo->capabilities)
120 GNUNET_free(sensorinfo->capabilities);
121 if(NULL != sensorinfo->gnunet_stat_service)
122 GNUNET_free(sensorinfo->gnunet_stat_service);
123 if(NULL != sensorinfo->gnunet_stat_name)
124 GNUNET_free(sensorinfo->gnunet_stat_name);
125 if(NULL != sensorinfo->ext_process)
126 GNUNET_free(sensorinfo->ext_process);
127 if(NULL != sensorinfo->ext_args)
128 GNUNET_free(sensorinfo->ext_args);
129 GNUNET_free(sensorinfo);
135 * Sensor will not run again unless
136 * explicitly enabled or reloaded
138 * @param sensor sensor information
140 static void set_sensor_enabled(struct SensorInfo *sensor, int state)
142 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
143 "Sensor `%s': Setting enabled to %d.\n",
144 sensor->name, state);
145 sensor->enabled = GNUNET_NO;
146 GNUNET_assert(NULL != sensor->cfg);
147 GNUNET_CONFIGURATION_set_value_string(sensor->cfg, sensor->name, "ENABLED",
148 (GNUNET_YES == state)?"YES":"NO");
149 GNUNET_CONFIGURATION_write(sensor->cfg, sensor->def_file);
153 * Task run during shutdown.
159 shutdown_task (void *cls,
160 const struct GNUNET_SCHEDULER_TaskContext *tc)
162 SENSOR_reporting_stop();
163 SENSOR_analysis_stop();
164 GNUNET_CONTAINER_multihashmap_iterate(sensors, &destroy_sensor, NULL);
165 GNUNET_CONTAINER_multihashmap_destroy(sensors);
166 if(NULL != statistics)
168 GNUNET_STATISTICS_destroy(statistics, GNUNET_YES);
171 if(NULL != peerstore)
173 GNUNET_PEERSTORE_disconnect(peerstore);
176 GNUNET_SCHEDULER_shutdown();
181 * A client disconnected. Remove all of its data structure entries.
183 * @param cls closure, NULL
184 * @param client identification of the client
187 handle_client_disconnect (void *cls,
188 struct GNUNET_SERVER_Client
194 * Parses a version number string into major and minor
196 * @param version full version string
197 * @param major pointer to parsed major value
198 * @param minor pointer to parsed minor value
199 * @return #GNUNET_OK if parsing went ok, #GNUNET_SYSERROR in case of error
202 version_parse(char *version, uint16_t *major, uint16_t *minor)
207 for(; isdigit(*version); version++)
210 majorval += *version - '0';
213 return GNUNET_SYSERR;
215 for(; isdigit(*version); version++)
218 minorval += *version - '0';
221 return GNUNET_SYSERR;
229 * Load sensor definition from configuration
231 * @param cfg configuration handle
232 * @param sectionname configuration section containing definition
234 static struct SensorInfo *
235 load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
237 struct SensorInfo *sensor;
241 unsigned long long time_sec;
243 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
245 sensor = GNUNET_new(struct SensorInfo);
247 sensor->name = GNUNET_strdup(sectionname);
249 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "VERSION", &version_str))
251 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor version\n"));
255 if(GNUNET_OK != version_parse(version_str, &(sensor->version_major), &(sensor->version_minor)))
257 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid sensor version number, format should be major.minor\n"));
259 GNUNET_free(version_str);
262 GNUNET_free(version_str);
264 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "DESCRIPTION", &sensor->description);
266 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "CATEGORY", &sensor->category) ||
267 NULL == sensor->category)
269 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor category\n"));
274 if(GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "ENABLED"))
275 sensor->enabled = GNUNET_NO;
277 sensor->enabled = GNUNET_YES;
279 sensor->start_time = NULL;
280 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "START_TIME", &starttime_str))
282 GNUNET_STRINGS_fancy_time_to_absolute(starttime_str, sensor->start_time);
283 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Start time loaded: `%s'. Parsed: %d\n", starttime_str, (NULL != sensor->start_time));
284 GNUNET_free(starttime_str);
287 sensor->end_time = NULL;
288 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "END_TIME", &endtime_str))
290 GNUNET_STRINGS_fancy_time_to_absolute(endtime_str, sensor->end_time);
291 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "End time loaded: `%s'. Parsed: %d\n", endtime_str, (NULL != sensor->end_time));
292 GNUNET_free(endtime_str);
295 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "INTERVAL", &time_sec))
297 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor run interval\n"));
301 if(time_sec < MIN_INTERVAL)
303 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Sensor run interval too low (%" PRIu64 " < %d)\n"),
304 time_sec, MIN_INTERVAL);
308 sensor->interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
310 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "LIFETIME", &time_sec))
312 sensor->lifetime = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
313 if (sensor->lifetime.rel_value_us < sensor->interval.rel_value_us)
314 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
315 "Lifetime of sensor data is preferred to be higher than interval for sensor `%s'.\n",
319 sensor->lifetime = sensor->interval;
322 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "SOURCE", sources, (const char **)&sensor->source))
324 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor source\n"));
328 if(sources[0] == sensor->source) //gnunet-statistics
330 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_SERVICE", &sensor->gnunet_stat_service) ||
331 GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_NAME", &sensor->gnunet_stat_name))
333 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor gnunet-statistics source information\n"));
337 sensor->gnunet_stat_get_handle = NULL;
339 else if(sources[1] == sensor->source) //process
341 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_PROCESS", &sensor->ext_process))
343 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor process name\n"));
347 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_ARGS", &sensor->ext_args);
350 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "EXPECTED_DATATYPE", datatypes, (const char **)&sensor->expected_datatype))
352 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor expected datatype\n"));
356 if(sources[0] == sensor->source && datatypes[0] != sensor->expected_datatype)
358 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid expected datatype, gnunet-statistics returns uint64 values\n"));
362 //reporting mechanism
363 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "COLLECTION_POINT", &dummy))
365 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "COLLECTION_INTERVAL", &time_sec))
367 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor collection interval\n"));
371 sensor->collection_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
372 if (GNUNET_OK == GNUNET_CRYPTO_eddsa_public_key_from_string(dummy, strlen(dummy), &public_key))
374 sensor->collection_point = GNUNET_new(struct GNUNET_PeerIdentity);
375 sensor->collection_point->public_key = public_key;
379 sensor->p2p_report = GNUNET_NO;
380 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "P2P_REPORT"))
382 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "P2P_INTERVAL", &time_sec))
384 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor p2p reporting interval\n"));
388 sensor->p2p_interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
389 sensor->p2p_report = GNUNET_YES;
393 sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
395 sensor->running = GNUNET_NO;
401 * Load sensor definition from file
403 * @param filename full path to file containing sensor definition
405 static struct SensorInfo *
406 load_sensor_from_file(const char *filename)
408 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
409 const char *filebasename;
410 struct SensorInfo *sensor;
413 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
415 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to access sensor file: %s\n"), filename);
418 //load file as configuration
419 sensorcfg = GNUNET_CONFIGURATION_create();
420 if(GNUNET_SYSERR == GNUNET_CONFIGURATION_parse(sensorcfg, filename))
422 GNUNET_CONFIGURATION_destroy(sensorcfg);
423 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to load sensor definition: %s\n"), filename);
426 //configuration section should be the same as filename
427 filebasename = GNUNET_STRINGS_get_short_name(filename);
428 sensor = load_sensor_from_cfg(sensorcfg, filebasename);
431 GNUNET_CONFIGURATION_destroy(sensorcfg);
434 sensor->def_file = GNUNET_strdup(filename);
435 sensor->cfg = sensorcfg;
441 * Compares version numbers of two sensors
443 * @param s1 first sensor
444 * @param s2 second sensor
445 * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2
448 sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2)
450 if(s1->version_major == s2->version_major)
451 return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor);
453 return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major);
457 * Adds a new sensor to given hashmap.
458 * If the same name exist, compares versions and update if old.
460 * @param sensor Sensor structure to add
461 * @param map Hashmap to add to
462 * @return #GNUNET_YES if added, #GNUNET_NO if not added which is not necessarily an error
465 add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHashMap *map)
467 struct GNUNET_HashCode key;
468 struct SensorInfo *existing;
470 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name), &key);
471 existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
472 if(NULL != existing) //sensor with same name already exists
474 if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist
476 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Sensor `%s' already exists with same or newer version\n"), sensor->name);
481 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
482 GNUNET_free(existing);
483 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Upgrading sensor `%s' to a newer version\n", sensor->name);
486 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
488 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error adding new sensor `%s' to global hashmap, this should not happen\n"), sensor->name);
496 * Iterating over files in sensors directory
499 * @param filename complete filename (absolute path)
500 * @return #GNUNET_OK to continue to iterate
503 reload_sensors_dir_cb(void *cls, const char *filename)
505 struct SensorInfo *sensor;
507 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
509 sensor = load_sensor_from_file(filename);
512 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error loading sensor from file: %s\n"), filename);
515 if(GNUNET_YES == add_sensor_to_hashmap(sensor, sensors))
516 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Sensor `%s' added to global hashmap\n"), sensor->name);
518 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, ("Could not add sensor `%s' to global hashmap\n"), sensor->name);
524 * Get path to the directory containing the sensor definition files
526 * @return sensor files directory
534 datadir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
535 GNUNET_asprintf(&sensordir, "%ssensors%s",
536 datadir, DIR_SEPARATOR_STR);
537 GNUNET_free(datadir);
543 * Reads sensor definitions from data files
551 sensordir = get_sensor_dir();
552 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Reloading sensor definitions from directory `%s'\n"), sensordir);
553 GNUNET_assert(GNUNET_YES == GNUNET_DISK_directory_test(sensordir, GNUNET_YES));
555 //read all files in sensors directory
556 GNUNET_DISK_directory_scan(sensordir, &reload_sensors_dir_cb, NULL);
557 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Loaded %d sensors from directory `%s'\n"),
558 GNUNET_CONTAINER_multihashmap_size(sensors), sensordir);
559 GNUNET_free(sensordir);
563 * Creates a structure with basic sensor info to be sent to a client
565 * @param sensor sensor information
566 * @return message ready to be sent to client
568 static struct SensorInfoMessage *
569 create_sensor_info_msg(struct SensorInfo *sensor)
571 struct SensorInfoMessage *msg;
577 name_len = strlen(sensor->name);
578 if(NULL == sensor->description)
581 desc_len = strlen(sensor->description) + 1;
583 len += sizeof(struct SensorInfoMessage);
586 msg = GNUNET_malloc(len);
587 msg->header.size = htons(len);
588 msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
589 msg->name_len = htons(name_len);
590 msg->description_len = htons(desc_len);
591 msg->version_major = htons(sensor->version_major);
592 msg->version_minor = htons(sensor->version_minor);
593 str_ptr = (char*) &msg[1];
594 memcpy(str_ptr, sensor->name, name_len);
595 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
596 name_len, name_len, str_ptr);
598 memcpy(str_ptr, sensor->description, desc_len);
599 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
600 desc_len, desc_len, str_ptr);
606 * Handle GET SENSOR message.
609 * @param client identification of the client
610 * @param message the actual message
613 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
614 const struct GNUNET_MessageHeader *message)
616 struct GNUNET_SERVER_TransmitContext *tc;
618 size_t sensorname_len;
619 struct GNUNET_HashCode key;
620 struct SensorInfo *sensorinfo;
621 struct SensorInfoMessage *msg;
623 sensorname = (char *)&message[1];
624 sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
625 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
626 "GET SENSOR", sensorname_len, sensorname_len, sensorname);
627 tc = GNUNET_SERVER_transmit_context_create (client);
628 GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
630 sensorinfo = (struct SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
631 if(NULL != sensorinfo)
633 msg = create_sensor_info_msg(sensorinfo);
634 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
638 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
639 sensorname_len, sensorname);
640 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
641 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
645 * Iterator for sensors and adds them to transmit context
647 * @param cls a 'struct GNUNET_SERVER_TransmitContext *'
648 * @param key hash of sensor name, key to hashmap
649 * @param value a 'struct SensorInfo *'
651 int add_sensor_to_tc(void *cls,
652 const struct GNUNET_HashCode *key, void *value)
654 struct GNUNET_SERVER_TransmitContext *tc = cls;
655 struct SensorInfo *sensorinfo = value;
656 struct SensorInfoMessage *msg;
658 msg = create_sensor_info_msg(sensorinfo);
659 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
667 * Handle GET ALL SENSORS message.
670 * @param client identification of the client
671 * @param message the actual message
674 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
675 const struct GNUNET_MessageHeader *message)
677 struct GNUNET_SERVER_TransmitContext *tc;
679 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
681 tc = GNUNET_SERVER_transmit_context_create (client);
682 GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
683 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
684 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
688 * Do a series of checks to determine if sensor should execute
690 * @return #GNUNET_YES / #GNUNET_NO
693 should_run_sensor(struct SensorInfo *sensorinfo)
695 struct GNUNET_TIME_Absolute now;
697 if(GNUNET_NO == sensorinfo->enabled)
699 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
702 now = GNUNET_TIME_absolute_get();
703 if(NULL != sensorinfo->start_time
704 && now.abs_value_us < sensorinfo->start_time->abs_value_us)
706 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n", sensorinfo->name);
709 if(NULL != sensorinfo->end_time
710 && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
712 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n", sensorinfo->name);
713 set_sensor_enabled(sensorinfo, GNUNET_NO);
720 * Callback function to process statistic values
722 * @param cls 'struct SensorInfo *'
723 * @param subsystem name of subsystem that created the statistic
724 * @param name the name of the datum
725 * @param value the current value
726 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
727 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
729 int sensor_statistics_iterator (void *cls,
735 struct SensorInfo *sensorinfo = cls;
736 double dvalue = (double)value;
737 struct GNUNET_TIME_Absolute expiry;
739 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %" PRIu64 "\n", sensorinfo->name, value);
740 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->lifetime);
741 GNUNET_PEERSTORE_store(peerstore,
748 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
751 return GNUNET_SYSERR; /* We only want one value */
755 * Continuation called after sensor gets all gnunet statistics values
757 * @param cls 'struct SensorInfo *'
758 * @param success #GNUNET_OK if statistics were
759 * successfully obtained, #GNUNET_SYSERR if not.
761 void end_sensor_run_stat (void *cls, int success)
763 struct SensorInfo *sensorinfo = cls;
765 sensorinfo->gnunet_stat_get_handle = NULL;
766 sensorinfo->running = GNUNET_NO;
770 * Tries to parse a received sensor value to its
773 * @param value the string value received, should be null terminated
774 * @param sensor sensor information struct
775 * @param ret pointer to parsed value
776 * @return size of new parsed value, 0 for error
779 parse_sensor_value (const char *value, struct SensorInfo* sensor, void **ret)
787 if(0 == strcmp("numeric", sensor->expected_datatype))
789 dval = GNUNET_new(double);
790 *dval = strtod(value, &endptr);
794 return sizeof(double);
796 if(0 == strcmp("string", sensor->expected_datatype))
798 *ret = GNUNET_strdup(value);
799 return strlen(value) + 1;
801 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
802 _("Unknown value type expected by sensor, this should not happen.\n"));
807 * Callback for output of executed sensor process
809 * @param cls 'struct SensorInfo *'
810 * @param line line of output from a command, NULL for the end
812 void sensor_process_callback (void *cls, const char *line)
814 struct SensorInfo *sensorinfo = cls;
817 struct GNUNET_TIME_Absolute expiry;
821 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
822 sensorinfo->ext_cmd = NULL;
823 sensorinfo->running = GNUNET_NO;
824 sensorinfo->ext_cmd_value_received = GNUNET_NO;
827 if(GNUNET_YES == sensorinfo->ext_cmd_value_received)
828 return; /* We only want one *valid* value */
829 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n", sensorinfo->name, line);
830 valsize = parse_sensor_value(line, sensorinfo, &value);
831 if (valsize == 0) /* invalid value, FIXME: should we disable the sensor now? */
833 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
834 _("Received an invalid value for sensor `%s': %s\n"),
835 sensorinfo->name, line);
839 sensorinfo->ext_cmd_value_received = GNUNET_YES;
840 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->lifetime);
841 GNUNET_PEERSTORE_store(peerstore,
848 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
855 * Checks if the given file is a path
857 * @return #GNUNET_YES / #GNUNET_NO
860 is_path(char *filename)
865 filename_len = strlen(filename);
866 for(i = 0; i < filename_len; i++)
868 if(DIR_SEPARATOR == filename[i])
875 * Actual execution of a sensor
877 * @param cls 'struct SensorInfo'
881 sensor_run (void *cls,
882 const struct GNUNET_SCHEDULER_TaskContext * tc)
884 struct SensorInfo *sensorinfo = cls;
889 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
890 if(GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
892 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sensor `%s' running for too long, will try again next interval\n", sensorinfo->name);
895 if(GNUNET_NO == should_run_sensor(sensorinfo))
897 sensorinfo->running = GNUNET_YES;
898 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
899 if(sources[0] == sensorinfo->source) //gnunet-statistics
901 sensorinfo->gnunet_stat_get_handle = GNUNET_STATISTICS_get(statistics,
902 sensorinfo->gnunet_stat_service,
903 sensorinfo->gnunet_stat_name,
904 sensorinfo->interval, //try to get values only for the interval of the sensor
905 &end_sensor_run_stat,
906 &sensor_statistics_iterator,
909 else if(sources[1] == sensorinfo->source)
911 if(GNUNET_YES == is_path(sensorinfo->ext_process))
913 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
914 _("Sensor `%s': External process should not be a path, disabling sensor.\n"),
916 set_sensor_enabled(sensorinfo, GNUNET_NO);
919 //check if the process exists in $PATH
920 process_path = GNUNET_strdup(sensorinfo->ext_process);
922 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
923 if(GNUNET_SYSERR == check_result)
925 //search in sensor directory
926 sensors_dir = get_sensor_dir();
927 GNUNET_free(process_path);
928 GNUNET_asprintf(&process_path, "%s%s-files%s%s",
932 sensorinfo->ext_process);
933 GNUNET_free(sensors_dir);
935 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
937 if(GNUNET_SYSERR == check_result)
939 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
940 _("Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n"),
942 sensorinfo->ext_process);
943 set_sensor_enabled(sensorinfo, GNUNET_NO);
944 sensorinfo->running = GNUNET_NO;
945 GNUNET_free(process_path);
948 sensorinfo->ext_cmd_value_received = GNUNET_NO;
949 sensorinfo->ext_cmd = GNUNET_OS_command_run(&sensor_process_callback,
951 GNUNET_TIME_UNIT_FOREVER_REL,
953 sensorinfo->ext_process,
954 sensorinfo->ext_args,
956 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name);
957 GNUNET_free(process_path);
961 sensorinfo->running = GNUNET_NO;
962 GNUNET_break(0); //shouldn't happen
967 * Starts the execution of a sensor
970 * @param key hash of sensor name, key to hashmap (unused)
971 * @param value a 'struct SensorInfo *'
972 * @return #GNUNET_YES if we should continue to
976 int schedule_sensor(void *cls,
977 const struct GNUNET_HashCode *key, void *value)
979 struct SensorInfo *sensorinfo = value;
981 if(GNUNET_NO == should_run_sensor(sensorinfo))
983 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
984 sensorinfo->name, sensorinfo->interval.rel_value_us);
985 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
987 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
988 _("Sensor `%s' execution task already set, this should not happen\n"), sensorinfo->name);
991 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
996 * Starts the execution of all enabled sensors
1000 schedule_all_sensors()
1002 GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
1006 * Process statistics requests.
1008 * @param cls closure
1009 * @param server the initialized server
1010 * @param c configuration to use
1014 struct GNUNET_SERVER_Handle *server,
1015 const struct GNUNET_CONFIGURATION_Handle *c)
1017 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1018 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
1020 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
1021 sizeof (struct GNUNET_MessageHeader)},
1026 sensors = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
1028 schedule_all_sensors();
1029 SENSOR_analysis_start(c, sensors);
1030 SENSOR_reporting_start(c, sensors);
1031 statistics = GNUNET_STATISTICS_create("sensor", cfg);
1032 GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
1033 peerstore = GNUNET_PEERSTORE_connect(cfg);
1034 GNUNET_SERVER_add_handlers (server, handlers);
1035 GNUNET_SERVER_disconnect_notify (server,
1036 &handle_client_disconnect,
1038 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1045 * The main function for the sensor service.
1047 * @param argc number of arguments from the command line
1048 * @param argv command line arguments
1049 * @return 0 ok, 1 on error
1052 main (int argc, char *const *argv)
1054 return (GNUNET_OK ==
1055 GNUNET_SERVICE_run (argc,
1058 GNUNET_SERVICE_OPTION_NONE,
1059 &run, NULL)) ? 0 : 1;
1062 /* end of gnunet-service-sensor.c */