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"
32 * Structure containing sensor definition
43 * Path to definition file
48 * First part of version number
50 uint16_t version_major;
53 * Second part of version number
55 uint16_t version_minor;
63 * Sensor currently enabled
68 * Category under which the sensor falls (e.g. tcp, datastore)
73 * When does the sensor become active
75 struct GNUNET_TIME_Absolute *start_time;
78 * When does the sensor expire
80 struct GNUNET_TIME_Absolute *end_time;
83 * Time interval to collect sensor information (e.g. every 1 min)
85 struct GNUNET_TIME_Relative *interval;
88 * Lifetime of an information sample after which it is deleted from storage
90 struct GNUNET_TIME_Relative *lifetime;
93 * A set of required peer capabilities for the sensor to collect meaningful information (e.g. ipv6)
98 * Either "gnunet-statistics" or external "process"
103 * Name of the GNUnet service that is the source for the gnunet-statistics entry
105 char *gnunet_stat_service;
108 * Name of the gnunet-statistics entry
110 char *gnunet_stat_name;
113 * Name of the external process to be executed
118 * Arguments to be passed to the external process
123 * The output datatype to be expected
125 char *expected_datatype;
128 * Peer-identity of peer running collection point
130 struct GNUNET_PeerIdentity *collection_point;
133 * Time interval to send sensor information to collection point (e.g. every 30 mins)
135 struct GNUNET_TIME_Relative *collection_interval;
138 * Flag specifying if value is to be communicated to the p2p network
143 * Time interval to communicate value to the p2p network
145 struct GNUNET_TIME_Relative *p2p_interval;
152 static const struct GNUNET_CONFIGURATION_Handle *cfg;
155 * Hashmap of loaded sensor definitions
157 struct GNUNET_CONTAINER_MultiHashMap *sensors;
160 * Task run during shutdown.
166 shutdown_task (void *cls,
167 const struct GNUNET_SCHEDULER_TaskContext *tc)
169 GNUNET_SCHEDULER_shutdown();
174 * A client disconnected. Remove all of its data structure entries.
176 * @param cls closure, NULL
177 * @param client identification of the client
180 handle_client_disconnect (void *cls,
181 struct GNUNET_SERVER_Client
187 * Parses a version number string into major and minor
189 * @param version full version string
190 * @param major pointer to parsed major value
191 * @param minor pointer to parsed minor value
192 * @return #GNUNET_OK if parsing went ok, #GNUNET_SYSERROR in case of error
195 version_parse(char *version, uint16_t *major, uint16_t *minor)
200 for(; isdigit(*version); version++)
203 majorval += *version - '0';
206 return GNUNET_SYSERR;
208 for(; isdigit(*version); version++)
211 minorval += *version - '0';
214 return GNUNET_SYSERR;
222 * Load sensor definition from configuration
224 * @param cfg configuration handle
225 * @param sectionname configuration section containing definition
227 static struct SensorInfo *
228 load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
230 struct SensorInfo *sensor;
234 unsigned long long interval_sec;
235 struct GNUNET_TIME_Relative interval;
237 sensor = GNUNET_new(struct SensorInfo);
239 sensor->name = GNUNET_strdup(sectionname);
241 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "VERSION", &version_str))
243 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor version\n"));
247 if(GNUNET_OK != version_parse(version_str, &(sensor->version_major), &(sensor->version_minor)))
249 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid sensor version number, format should be major.minor\n"));
251 GNUNET_free(version_str);
254 GNUNET_free(version_str);
256 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "DESCRIPTION", &sensor->description);
258 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "CATEGORY", &sensor->category) ||
259 NULL == sensor->category)
261 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor category\n"));
266 if(GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "ENABLED"))
267 sensor->enabled = GNUNET_NO;
269 sensor->enabled = GNUNET_YES;
271 sensor->start_time = NULL;
272 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "START_TIME", &starttime_str))
274 GNUNET_STRINGS_fancy_time_to_absolute(starttime_str, sensor->start_time);
275 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Start time loaded: `%s'. Parsed: %d\n", starttime_str, (NULL != sensor->start_time));
276 GNUNET_free(starttime_str);
279 sensor->end_time = NULL;
280 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "END_TIME", &endtime_str))
282 GNUNET_STRINGS_fancy_time_to_absolute(endtime_str, sensor->end_time);
283 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "End time loaded: `%s'. Parsed: %d\n", endtime_str, (NULL != sensor->end_time));
284 GNUNET_free(endtime_str);
287 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "INTERVAL", &interval_sec))
289 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor run interval\n"));
293 interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, interval_sec);
294 sensor->interval = &interval;
295 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Interval loaded: %" PRIu64 "\n", sensor->interval->rel_value_us);
301 * Load sensor definition from file
303 * @param filename full path to file containing sensor definition
305 static struct SensorInfo *
306 load_sensor_from_file(const char *filename)
308 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
309 const char *filebasename;
310 struct SensorInfo *sensor;
313 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
315 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to access sensor file: %s\n"), filename);
318 //load file as configuration
319 sensorcfg = GNUNET_CONFIGURATION_create();
320 if(GNUNET_SYSERR == GNUNET_CONFIGURATION_parse(sensorcfg, filename))
322 GNUNET_CONFIGURATION_destroy(sensorcfg);
323 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to load sensor definition: %s\n"), filename);
326 //configuration section should be the same as filename
327 filebasename = GNUNET_STRINGS_get_short_name(filename);
328 sensor = load_sensor_from_cfg(sensorcfg, filebasename);
329 sensor->def_file = GNUNET_strdup(filename);
331 GNUNET_CONFIGURATION_destroy(sensorcfg);
337 * Compares version numbers of two sensors
339 * @param s1 first sensor
340 * @param s2 second sensor
341 * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2
344 sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2)
346 if(s1->version_major == s2->version_major)
347 return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor);
349 return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major);
353 * Adds a new sensor to given hashmap.
354 * If the same name exist, compares versions and update if old.
356 * @param sensor Sensor structure to add
357 * @param map Hashmap to add to
358 * @return #GNUNET_YES if added, #GNUNET_NO if not added which is not necessarily an error
361 add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHashMap *map)
363 struct GNUNET_HashCode key;
364 struct SensorInfo *existing;
366 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name), &key);
367 existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
368 if(NULL != existing) //sensor with same name already exists
370 if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist
372 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Sensor `%s' already exists with same or newer version\n"), sensor->name);
377 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
378 GNUNET_free(existing);
379 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Upgrading sensor `%s' to a newer version\n"), sensor->name);
382 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
384 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error adding new sensor `%s' to global hashmap, this should not happen\n"), sensor->name);
392 * Iterating over files in sensors directory
395 * @param filename complete filename (absolute path)
396 * @return #GNUNET_OK to continue to iterate,
397 * #GNUNET_NO to stop iteration with no error,
398 * #GNUNET_SYSERR to abort iteration with error!
401 reload_sensors_dir_cb(void *cls, const char *filename)
403 struct SensorInfo *sensor;
405 sensor = load_sensor_from_file(filename);
408 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error loading sensor from file: %s\n"), filename);
411 if(GNUNET_YES == add_sensor_to_hashmap(sensor, sensors))
412 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Sensor `%s' added to global hashmap\n"), sensor->name);
414 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, ("Could not add sensor `%s' to global hashmap\n"), sensor->name);
420 * Get path to the directory containing the sensor definition files
422 * @return sensor files directory
430 datadir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
431 GNUNET_asprintf(&sensordir, "%ssensors%s",
432 datadir, DIR_SEPARATOR_STR);
438 * Reads sensor definitions from data files
447 sensordir = get_sensor_dir();
448 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Reloading sensor definitions from directory `%s'\n"), sensordir);
449 GNUNET_assert(GNUNET_YES == GNUNET_DISK_directory_test(sensordir, GNUNET_YES));
451 //read all files in sensors directory
452 filesfound = GNUNET_DISK_directory_scan(sensordir, &reload_sensors_dir_cb, NULL);
453 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Loaded %d/%d sensors from directory `%s'\n"),
454 GNUNET_CONTAINER_multihashmap_size(sensors), filesfound, sensordir);
458 * Creates a structure with basic sensor info to be sent to a client
460 * @param sensor sensor information
461 * @return message ready to be sent to client
463 static struct SensorInfoMessage *
464 create_sensor_info_msg(struct SensorInfo *sensor)
466 struct SensorInfoMessage *msg;
472 name_len = strlen(sensor->name);
473 if(NULL == sensor->description)
476 desc_len = strlen(sensor->description);
478 len += sizeof(struct SensorInfoMessage);
481 msg = GNUNET_malloc(len);
482 msg->header.size = htons(len);
483 msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
484 msg->name_len = htons(name_len);
485 msg->description_len = htons(desc_len);
486 msg->version_major = htons(sensor->version_major);
487 msg->version_minor = htons(sensor->version_minor);
488 str_ptr = (char*) &msg[1];
489 memcpy(str_ptr, sensor->name, name_len);
490 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
491 name_len, name_len, str_ptr);
493 memcpy(str_ptr, sensor->description, desc_len);
494 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
495 desc_len, desc_len, str_ptr);
501 * Handle GET SENSOR message.
504 * @param client identification of the client
505 * @param message the actual message
508 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
509 const struct GNUNET_MessageHeader *message)
511 struct GNUNET_SERVER_TransmitContext *tc;
513 size_t sensorname_len;
514 struct GNUNET_HashCode key;
515 struct SensorInfo *sensorinfo;
516 struct SensorInfoMessage *msg;
518 sensorname = (char *)&message[1];
519 sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
520 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
521 "GET SENSOR", sensorname_len, sensorname_len, sensorname);
522 tc = GNUNET_SERVER_transmit_context_create (client);
523 GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
525 sensorinfo = (struct SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
526 if(NULL != sensorinfo)
528 msg = create_sensor_info_msg(sensorinfo);
529 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
533 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
534 sensorname_len, sensorname);
535 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
536 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
540 * Iterator for sensors and adds them to transmit context
542 * @param cls a 'struct GNUNET_SERVER_TransmitContext *'
543 * @param key hash of sensor name, key to hashmap
544 * @param value a 'struct SensorInfo *'
546 int add_sensor_to_tc(void *cls,
547 const struct GNUNET_HashCode *key, void *value)
549 struct GNUNET_SERVER_TransmitContext *tc = cls;
550 struct SensorInfo *sensorinfo = value;
551 struct SensorInfoMessage *msg;
553 msg = create_sensor_info_msg(sensorinfo);
554 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
562 * Handle GET ALL SENSORS message.
565 * @param client identification of the client
566 * @param message the actual message
569 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
570 const struct GNUNET_MessageHeader *message)
572 struct GNUNET_SERVER_TransmitContext *tc;
574 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
576 tc = GNUNET_SERVER_transmit_context_create (client);
577 GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
578 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
579 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
583 * Do a series of checks to determine if sensor should execute
585 * @return #GNUNET_YES / #GNUNET_NO
588 should_run_sensor(struct SensorInfo *sensorinfo)
590 struct GNUNET_TIME_Absolute now;
592 if(GNUNET_NO == sensorinfo->enabled)
594 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n");
597 now = GNUNET_TIME_absolute_get();
598 if(NULL != sensorinfo->start_time
599 && now.abs_value_us < sensorinfo->start_time->abs_value_us)
601 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n");
604 if(NULL != sensorinfo->end_time
605 && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
607 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "End time for sensor `%s' passed, will not run\n");
614 * Actual execution of a sensor
616 * @param cls 'struct SensorInfo'
620 run_sensor (void *cls,
621 const struct GNUNET_SCHEDULER_TaskContext * tc)
623 struct SensorInfo *sensorinfo = cls;
625 if(GNUNET_NO == should_run_sensor(sensorinfo))
627 //GNUNET_SCHEDULER_add_delayed(*sensorinfo->interval, &run_sensor, sensorinfo);
628 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
632 * Starts the execution of a sensor
635 * @param key hash of sensor name, key to hashmap
636 * @param value a 'struct SensorInfo *'
637 * @return #GNUNET_YES if we should continue to
641 int schedule_sensor(void *cls,
642 const struct GNUNET_HashCode *key, void *value)
644 struct SensorInfo *sensorinfo = value;
646 if(GNUNET_NO == should_run_sensor(sensorinfo))
648 GNUNET_SCHEDULER_add_delayed(*sensorinfo->interval, &run_sensor, sensorinfo);
653 * Starts the execution of all enabled sensors
657 schedule_all_sensors()
659 GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
663 * Process statistics requests.
666 * @param server the initialized server
667 * @param c configuration to use
671 struct GNUNET_SERVER_Handle *server,
672 const struct GNUNET_CONFIGURATION_Handle *c)
674 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
675 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
677 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
678 sizeof (struct GNUNET_MessageHeader)},
683 sensors = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
685 schedule_all_sensors();
686 GNUNET_SERVER_add_handlers (server, handlers);
687 GNUNET_SERVER_disconnect_notify (server,
688 &handle_client_disconnect,
690 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
697 * The main function for the sensor service.
699 * @param argc number of arguments from the command line
700 * @param argv command line arguments
701 * @return 0 ok, 1 on error
704 main (int argc, char *const *argv)
707 GNUNET_SERVICE_run (argc,
710 GNUNET_SERVICE_OPTION_NONE,
711 &run, NULL)) ? 0 : 1;
714 /* end of gnunet-service-sensor.c */