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;
148 * Execution task (OR GNUNET_SCHEDULER_NO_TASK)
150 GNUNET_SCHEDULER_TaskIdentifier execution_task;
157 static const struct GNUNET_CONFIGURATION_Handle *cfg;
160 * Hashmap of loaded sensor definitions
162 struct GNUNET_CONTAINER_MultiHashMap *sensors;
165 * Remove sensor execution from scheduler
168 * @param key hash of sensor name, key to hashmap
169 * @param value a 'struct SensorInfo *'
170 * @return #GNUNET_YES if we should continue to
174 int unschedule_sensor(void *cls,
175 const struct GNUNET_HashCode *key, void *value)
177 struct SensorInfo *sensorinfo = value;
179 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
181 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unscheduling sensor `%s'\n", sensorinfo->name);
182 GNUNET_SCHEDULER_cancel(sensorinfo->execution_task);
188 * Task run during shutdown.
194 shutdown_task (void *cls,
195 const struct GNUNET_SCHEDULER_TaskContext *tc)
197 GNUNET_CONTAINER_multihashmap_iterate(sensors, &unschedule_sensor, NULL);
198 GNUNET_SCHEDULER_shutdown();
203 * A client disconnected. Remove all of its data structure entries.
205 * @param cls closure, NULL
206 * @param client identification of the client
209 handle_client_disconnect (void *cls,
210 struct GNUNET_SERVER_Client
216 * Parses a version number string into major and minor
218 * @param version full version string
219 * @param major pointer to parsed major value
220 * @param minor pointer to parsed minor value
221 * @return #GNUNET_OK if parsing went ok, #GNUNET_SYSERROR in case of error
224 version_parse(char *version, uint16_t *major, uint16_t *minor)
229 for(; isdigit(*version); version++)
232 majorval += *version - '0';
235 return GNUNET_SYSERR;
237 for(; isdigit(*version); version++)
240 minorval += *version - '0';
243 return GNUNET_SYSERR;
251 * Load sensor definition from configuration
253 * @param cfg configuration handle
254 * @param sectionname configuration section containing definition
256 static struct SensorInfo *
257 load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
259 struct SensorInfo *sensor;
263 unsigned long long interval_sec;
265 sensor = GNUNET_new(struct SensorInfo);
267 sensor->name = GNUNET_strdup(sectionname);
269 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "VERSION", &version_str))
271 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor version\n"));
275 if(GNUNET_OK != version_parse(version_str, &(sensor->version_major), &(sensor->version_minor)))
277 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid sensor version number, format should be major.minor\n"));
279 GNUNET_free(version_str);
282 GNUNET_free(version_str);
284 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "DESCRIPTION", &sensor->description);
286 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "CATEGORY", &sensor->category) ||
287 NULL == sensor->category)
289 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor category\n"));
294 if(GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "ENABLED"))
295 sensor->enabled = GNUNET_NO;
297 sensor->enabled = GNUNET_YES;
299 sensor->start_time = NULL;
300 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "START_TIME", &starttime_str))
302 GNUNET_STRINGS_fancy_time_to_absolute(starttime_str, sensor->start_time);
303 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Start time loaded: `%s'. Parsed: %d\n", starttime_str, (NULL != sensor->start_time));
304 GNUNET_free(starttime_str);
307 sensor->end_time = NULL;
308 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "END_TIME", &endtime_str))
310 GNUNET_STRINGS_fancy_time_to_absolute(endtime_str, sensor->end_time);
311 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "End time loaded: `%s'. Parsed: %d\n", endtime_str, (NULL != sensor->end_time));
312 GNUNET_free(endtime_str);
315 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "INTERVAL", &interval_sec))
317 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor run interval\n"));
321 sensor->interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, interval_sec);
322 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Interval loaded: %" PRIu64 "\n", sensor->interval.rel_value_us);
324 sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
330 * Load sensor definition from file
332 * @param filename full path to file containing sensor definition
334 static struct SensorInfo *
335 load_sensor_from_file(const char *filename)
337 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
338 const char *filebasename;
339 struct SensorInfo *sensor;
342 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
344 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to access sensor file: %s\n"), filename);
347 //load file as configuration
348 sensorcfg = GNUNET_CONFIGURATION_create();
349 if(GNUNET_SYSERR == GNUNET_CONFIGURATION_parse(sensorcfg, filename))
351 GNUNET_CONFIGURATION_destroy(sensorcfg);
352 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to load sensor definition: %s\n"), filename);
355 //configuration section should be the same as filename
356 filebasename = GNUNET_STRINGS_get_short_name(filename);
357 sensor = load_sensor_from_cfg(sensorcfg, filebasename);
358 sensor->def_file = GNUNET_strdup(filename);
360 GNUNET_CONFIGURATION_destroy(sensorcfg);
366 * Compares version numbers of two sensors
368 * @param s1 first sensor
369 * @param s2 second sensor
370 * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2
373 sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2)
375 if(s1->version_major == s2->version_major)
376 return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor);
378 return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major);
382 * Adds a new sensor to given hashmap.
383 * If the same name exist, compares versions and update if old.
385 * @param sensor Sensor structure to add
386 * @param map Hashmap to add to
387 * @return #GNUNET_YES if added, #GNUNET_NO if not added which is not necessarily an error
390 add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHashMap *map)
392 struct GNUNET_HashCode key;
393 struct SensorInfo *existing;
395 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name), &key);
396 existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
397 if(NULL != existing) //sensor with same name already exists
399 if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist
401 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Sensor `%s' already exists with same or newer version\n"), sensor->name);
406 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
407 GNUNET_free(existing);
408 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Upgrading sensor `%s' to a newer version\n"), sensor->name);
411 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
413 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error adding new sensor `%s' to global hashmap, this should not happen\n"), sensor->name);
421 * Iterating over files in sensors directory
424 * @param filename complete filename (absolute path)
425 * @return #GNUNET_OK to continue to iterate,
426 * #GNUNET_NO to stop iteration with no error,
427 * #GNUNET_SYSERR to abort iteration with error!
430 reload_sensors_dir_cb(void *cls, const char *filename)
432 struct SensorInfo *sensor;
434 sensor = load_sensor_from_file(filename);
437 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error loading sensor from file: %s\n"), filename);
440 if(GNUNET_YES == add_sensor_to_hashmap(sensor, sensors))
441 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Sensor `%s' added to global hashmap\n"), sensor->name);
443 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, ("Could not add sensor `%s' to global hashmap\n"), sensor->name);
449 * Get path to the directory containing the sensor definition files
451 * @return sensor files directory
459 datadir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
460 GNUNET_asprintf(&sensordir, "%ssensors%s",
461 datadir, DIR_SEPARATOR_STR);
467 * Reads sensor definitions from data files
476 sensordir = get_sensor_dir();
477 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Reloading sensor definitions from directory `%s'\n"), sensordir);
478 GNUNET_assert(GNUNET_YES == GNUNET_DISK_directory_test(sensordir, GNUNET_YES));
480 //read all files in sensors directory
481 filesfound = GNUNET_DISK_directory_scan(sensordir, &reload_sensors_dir_cb, NULL);
482 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Loaded %d/%d sensors from directory `%s'\n"),
483 GNUNET_CONTAINER_multihashmap_size(sensors), filesfound, sensordir);
487 * Creates a structure with basic sensor info to be sent to a client
489 * @param sensor sensor information
490 * @return message ready to be sent to client
492 static struct SensorInfoMessage *
493 create_sensor_info_msg(struct SensorInfo *sensor)
495 struct SensorInfoMessage *msg;
501 name_len = strlen(sensor->name);
502 if(NULL == sensor->description)
505 desc_len = strlen(sensor->description);
507 len += sizeof(struct SensorInfoMessage);
510 msg = GNUNET_malloc(len);
511 msg->header.size = htons(len);
512 msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
513 msg->name_len = htons(name_len);
514 msg->description_len = htons(desc_len);
515 msg->version_major = htons(sensor->version_major);
516 msg->version_minor = htons(sensor->version_minor);
517 str_ptr = (char*) &msg[1];
518 memcpy(str_ptr, sensor->name, name_len);
519 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
520 name_len, name_len, str_ptr);
522 memcpy(str_ptr, sensor->description, desc_len);
523 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
524 desc_len, desc_len, str_ptr);
530 * Handle GET SENSOR message.
533 * @param client identification of the client
534 * @param message the actual message
537 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
538 const struct GNUNET_MessageHeader *message)
540 struct GNUNET_SERVER_TransmitContext *tc;
542 size_t sensorname_len;
543 struct GNUNET_HashCode key;
544 struct SensorInfo *sensorinfo;
545 struct SensorInfoMessage *msg;
547 sensorname = (char *)&message[1];
548 sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
549 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
550 "GET SENSOR", sensorname_len, sensorname_len, sensorname);
551 tc = GNUNET_SERVER_transmit_context_create (client);
552 GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
554 sensorinfo = (struct SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
555 if(NULL != sensorinfo)
557 msg = create_sensor_info_msg(sensorinfo);
558 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
562 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
563 sensorname_len, sensorname);
564 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
565 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
569 * Iterator for sensors and adds them to transmit context
571 * @param cls a 'struct GNUNET_SERVER_TransmitContext *'
572 * @param key hash of sensor name, key to hashmap
573 * @param value a 'struct SensorInfo *'
575 int add_sensor_to_tc(void *cls,
576 const struct GNUNET_HashCode *key, void *value)
578 struct GNUNET_SERVER_TransmitContext *tc = cls;
579 struct SensorInfo *sensorinfo = value;
580 struct SensorInfoMessage *msg;
582 msg = create_sensor_info_msg(sensorinfo);
583 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
591 * Handle GET ALL SENSORS message.
594 * @param client identification of the client
595 * @param message the actual message
598 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
599 const struct GNUNET_MessageHeader *message)
601 struct GNUNET_SERVER_TransmitContext *tc;
603 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
605 tc = GNUNET_SERVER_transmit_context_create (client);
606 GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
607 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
608 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
612 * Do a series of checks to determine if sensor should execute
614 * @return #GNUNET_YES / #GNUNET_NO
617 should_run_sensor(struct SensorInfo *sensorinfo)
619 struct GNUNET_TIME_Absolute now;
621 if(GNUNET_NO == sensorinfo->enabled)
623 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
626 now = GNUNET_TIME_absolute_get();
627 if(NULL != sensorinfo->start_time
628 && now.abs_value_us < sensorinfo->start_time->abs_value_us)
630 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n", sensorinfo->name);
633 if(NULL != sensorinfo->end_time
634 && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
636 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "End time for sensor `%s' passed, will not run\n", sensorinfo->name);
643 * Actual execution of a sensor
645 * @param cls 'struct SensorInfo'
649 run_sensor (void *cls,
650 const struct GNUNET_SCHEDULER_TaskContext * tc)
652 struct SensorInfo *sensorinfo = cls;
654 sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK;
655 if(GNUNET_NO == should_run_sensor(sensorinfo))
657 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &run_sensor, sensorinfo);
658 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
662 * Starts the execution of a sensor
665 * @param key hash of sensor name, key to hashmap
666 * @param value a 'struct SensorInfo *'
667 * @return #GNUNET_YES if we should continue to
671 int schedule_sensor(void *cls,
672 const struct GNUNET_HashCode *key, void *value)
674 struct SensorInfo *sensorinfo = value;
676 if(GNUNET_NO == should_run_sensor(sensorinfo))
678 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
679 sensorinfo->name, sensorinfo->interval.rel_value_us);
680 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
682 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' execution task already set, this should not happen\n", sensorinfo->name);
685 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &run_sensor, sensorinfo);
690 * Starts the execution of all enabled sensors
694 schedule_all_sensors()
696 GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
700 * Process statistics requests.
703 * @param server the initialized server
704 * @param c configuration to use
708 struct GNUNET_SERVER_Handle *server,
709 const struct GNUNET_CONFIGURATION_Handle *c)
711 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
712 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
714 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
715 sizeof (struct GNUNET_MessageHeader)},
720 sensors = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
722 schedule_all_sensors();
723 GNUNET_SERVER_add_handlers (server, handlers);
724 GNUNET_SERVER_disconnect_notify (server,
725 &handle_client_disconnect,
727 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
734 * The main function for the sensor service.
736 * @param argc number of arguments from the command line
737 * @param argv command line arguments
738 * @return 0 ok, 1 on error
741 main (int argc, char *const *argv)
744 GNUNET_SERVICE_run (argc,
747 GNUNET_SERVICE_OPTION_NONE,
748 &run, NULL)) ? 0 : 1;
751 /* end of gnunet-service-sensor.c */