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"
33 * Minimum sensor execution interval (in seconds)
35 #define MIN_INTERVAL 30
38 * Structure containing sensor definition
49 * Path to definition file
54 * First part of version number
56 uint16_t version_major;
59 * Second part of version number
61 uint16_t version_minor;
69 * Sensor currently enabled
74 * Category under which the sensor falls (e.g. tcp, datastore)
79 * When does the sensor become active
81 struct GNUNET_TIME_Absolute *start_time;
84 * When does the sensor expire
86 struct GNUNET_TIME_Absolute *end_time;
89 * Time interval to collect sensor information (e.g. every 1 min)
91 struct GNUNET_TIME_Relative interval;
94 * Lifetime of an information sample after which it is deleted from storage
96 struct GNUNET_TIME_Relative lifetime;
99 * A set of required peer capabilities for the sensor to collect meaningful information (e.g. ipv6)
104 * Either "gnunet-statistics" or external "process"
109 * Name of the GNUnet service that is the source for the gnunet-statistics entry
111 char *gnunet_stat_service;
114 * Name of the gnunet-statistics entry
116 char *gnunet_stat_name;
119 * Handle to statistics get request (OR GNUNET_SCHEDULER_NO_TASK)
121 struct GNUNET_STATISTICS_GetHandle *gnunet_stat_get_handle;
124 * Name of the external process to be executed
129 * Arguments to be passed to the external process
134 * The output datatype to be expected
136 char *expected_datatype;
139 * Peer-identity of peer running collection point
141 struct GNUNET_PeerIdentity *collection_point;
144 * Time interval to send sensor information to collection point (e.g. every 30 mins)
146 struct GNUNET_TIME_Relative *collection_interval;
149 * Flag specifying if value is to be communicated to the p2p network
154 * Time interval to communicate value to the p2p network
156 struct GNUNET_TIME_Relative *p2p_interval;
159 * Execution task (OR GNUNET_SCHEDULER_NO_TASK)
161 GNUNET_SCHEDULER_TaskIdentifier execution_task;
164 * Is the sensor being executed
173 static const struct GNUNET_CONFIGURATION_Handle *cfg;
176 * Hashmap of loaded sensor definitions
178 struct GNUNET_CONTAINER_MultiHashMap *sensors;
181 * Supported sources of sensor information
183 static const char *sources[] = { "gnunet-statistics", "process", NULL };
186 * Supported datatypes of sensor information
188 static const char *datatypes[] = { "uint64", "double", "string", NULL };
191 * Handle to statistics service
193 struct GNUNET_STATISTICS_Handle *statistics;
196 * Remove sensor execution from scheduler
199 * @param key hash of sensor name, key to hashmap
200 * @param value a 'struct SensorInfo *'
201 * @return #GNUNET_YES if we should continue to
205 int destroy_sensor(void *cls,
206 const struct GNUNET_HashCode *key, void *value)
208 struct SensorInfo *sensorinfo = value;
210 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor `%s'\n", sensorinfo->name);
211 if(NULL != sensorinfo->gnunet_stat_get_handle)
213 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Canceling a statistics get request for sensor `%s'\n", sensorinfo->name);
214 GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle);
216 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
218 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Unscheduling sensor `%s'\n", sensorinfo->name);
219 GNUNET_SCHEDULER_cancel(sensorinfo->execution_task);
220 sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK;
222 if(NULL != sensorinfo->name)
223 GNUNET_free(sensorinfo->name);
224 if(NULL != sensorinfo->def_file)
225 GNUNET_free(sensorinfo->def_file);
226 if(NULL != sensorinfo->description)
227 GNUNET_free(sensorinfo->description);
228 if(NULL != sensorinfo->category)
229 GNUNET_free(sensorinfo->category);
230 if(NULL != sensorinfo->capabilities)
231 GNUNET_free(sensorinfo->capabilities);
232 if(NULL != sensorinfo->gnunet_stat_service)
233 GNUNET_free(sensorinfo->gnunet_stat_service);
234 if(NULL != sensorinfo->gnunet_stat_name)
235 GNUNET_free(sensorinfo->gnunet_stat_name);
236 if(NULL != sensorinfo->ext_process)
237 GNUNET_free(sensorinfo->ext_process);
238 if(NULL != sensorinfo->ext_args)
239 GNUNET_free(sensorinfo->ext_args);
240 GNUNET_free(sensorinfo);
245 * Task run during shutdown.
251 shutdown_task (void *cls,
252 const struct GNUNET_SCHEDULER_TaskContext *tc)
254 GNUNET_CONTAINER_multihashmap_iterate(sensors, &destroy_sensor, NULL);
255 GNUNET_CONTAINER_multihashmap_destroy(sensors);
256 if(NULL != statistics)
257 GNUNET_STATISTICS_destroy(statistics, GNUNET_YES);
258 GNUNET_SCHEDULER_shutdown();
263 * A client disconnected. Remove all of its data structure entries.
265 * @param cls closure, NULL
266 * @param client identification of the client
269 handle_client_disconnect (void *cls,
270 struct GNUNET_SERVER_Client
276 * Parses a version number string into major and minor
278 * @param version full version string
279 * @param major pointer to parsed major value
280 * @param minor pointer to parsed minor value
281 * @return #GNUNET_OK if parsing went ok, #GNUNET_SYSERROR in case of error
284 version_parse(char *version, uint16_t *major, uint16_t *minor)
289 for(; isdigit(*version); version++)
292 majorval += *version - '0';
295 return GNUNET_SYSERR;
297 for(; isdigit(*version); version++)
300 minorval += *version - '0';
303 return GNUNET_SYSERR;
311 * Load sensor definition from configuration
313 * @param cfg configuration handle
314 * @param sectionname configuration section containing definition
316 static struct SensorInfo *
317 load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
319 struct SensorInfo *sensor;
323 unsigned long long time_sec;
325 sensor = GNUNET_new(struct SensorInfo);
327 sensor->name = GNUNET_strdup(sectionname);
329 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "VERSION", &version_str))
331 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor version\n"));
335 if(GNUNET_OK != version_parse(version_str, &(sensor->version_major), &(sensor->version_minor)))
337 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid sensor version number, format should be major.minor\n"));
339 GNUNET_free(version_str);
342 GNUNET_free(version_str);
344 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "DESCRIPTION", &sensor->description);
346 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "CATEGORY", &sensor->category) ||
347 NULL == sensor->category)
349 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor category\n"));
354 if(GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno(cfg, sectionname, "ENABLED"))
355 sensor->enabled = GNUNET_NO;
357 sensor->enabled = GNUNET_YES;
359 sensor->start_time = NULL;
360 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "START_TIME", &starttime_str))
362 GNUNET_STRINGS_fancy_time_to_absolute(starttime_str, sensor->start_time);
363 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Start time loaded: `%s'. Parsed: %d\n", starttime_str, (NULL != sensor->start_time));
364 GNUNET_free(starttime_str);
367 sensor->end_time = NULL;
368 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "END_TIME", &endtime_str))
370 GNUNET_STRINGS_fancy_time_to_absolute(endtime_str, sensor->end_time);
371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "End time loaded: `%s'. Parsed: %d\n", endtime_str, (NULL != sensor->end_time));
372 GNUNET_free(endtime_str);
375 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "INTERVAL", &time_sec))
377 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor run interval\n"));
381 if(time_sec < MIN_INTERVAL)
383 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Sensor run interval too low (%" PRIu64 " < %d)\n"),
384 time_sec, MIN_INTERVAL);
388 sensor->interval = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
390 if(GNUNET_OK == GNUNET_CONFIGURATION_get_value_number(cfg, sectionname, "LIFETIME", &time_sec))
392 sensor->lifetime = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, time_sec);
395 sensor->lifetime = GNUNET_TIME_UNIT_FOREVER_REL;
398 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "SOURCE", sources, (const char **)&sensor->source))
400 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor source\n"));
404 if(sources[0] == sensor->source) //gnunet-statistics
406 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_SERVICE", &sensor->gnunet_stat_service) ||
407 GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "GNUNET_STAT_NAME", &sensor->gnunet_stat_name))
409 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor gnunet-statistics source information\n"));
413 sensor->gnunet_stat_get_handle = NULL;
415 else if(sources[1] == sensor->source) //process
417 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_PROCESS", &sensor->ext_process))
419 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor process name\n"));
423 GNUNET_CONFIGURATION_get_value_string(cfg, sectionname, "EXT_ARGS", &sensor->ext_args);
426 if(GNUNET_OK != GNUNET_CONFIGURATION_get_value_choice(cfg, sectionname, "EXPECTED_DATATYPE", datatypes, (const char **)&sensor->expected_datatype))
428 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error reading sensor expected datatype\n"));
432 if(sources[0] == sensor->source && datatypes[0] != sensor->expected_datatype)
434 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Invalid expected datatype, gnunet-statistics returns uint64 values\n"));
438 //TODO: reporting mechanism
440 sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
442 sensor->running = GNUNET_NO;
448 * Load sensor definition from file
450 * @param filename full path to file containing sensor definition
452 static struct SensorInfo *
453 load_sensor_from_file(const char *filename)
455 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
456 const char *filebasename;
457 struct SensorInfo *sensor;
460 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
462 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to access sensor file: %s\n"), filename);
465 //load file as configuration
466 sensorcfg = GNUNET_CONFIGURATION_create();
467 if(GNUNET_SYSERR == GNUNET_CONFIGURATION_parse(sensorcfg, filename))
469 GNUNET_CONFIGURATION_destroy(sensorcfg);
470 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Failed to load sensor definition: %s\n"), filename);
473 //configuration section should be the same as filename
474 filebasename = GNUNET_STRINGS_get_short_name(filename);
475 sensor = load_sensor_from_cfg(sensorcfg, filebasename);
476 sensor->def_file = GNUNET_strdup(filename);
478 GNUNET_CONFIGURATION_destroy(sensorcfg);
484 * Compares version numbers of two sensors
486 * @param s1 first sensor
487 * @param s2 second sensor
488 * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2
491 sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2)
493 if(s1->version_major == s2->version_major)
494 return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor);
496 return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major);
500 * Adds a new sensor to given hashmap.
501 * If the same name exist, compares versions and update if old.
503 * @param sensor Sensor structure to add
504 * @param map Hashmap to add to
505 * @return #GNUNET_YES if added, #GNUNET_NO if not added which is not necessarily an error
508 add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHashMap *map)
510 struct GNUNET_HashCode key;
511 struct SensorInfo *existing;
513 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name), &key);
514 existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
515 if(NULL != existing) //sensor with same name already exists
517 if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist
519 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Sensor `%s' already exists with same or newer version\n"), sensor->name);
524 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
525 GNUNET_free(existing);
526 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Upgrading sensor `%s' to a newer version\n"), sensor->name);
529 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
531 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error adding new sensor `%s' to global hashmap, this should not happen\n"), sensor->name);
539 * Iterating over files in sensors directory
542 * @param filename complete filename (absolute path)
543 * @return #GNUNET_OK to continue to iterate
546 reload_sensors_dir_cb(void *cls, const char *filename)
548 struct SensorInfo *sensor;
550 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
552 sensor = load_sensor_from_file(filename);
555 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error loading sensor from file: %s\n"), filename);
558 if(GNUNET_YES == add_sensor_to_hashmap(sensor, sensors))
559 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Sensor `%s' added to global hashmap\n"), sensor->name);
561 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, ("Could not add sensor `%s' to global hashmap\n"), sensor->name);
567 * Get path to the directory containing the sensor definition files
569 * @return sensor files directory
577 datadir = GNUNET_OS_installation_get_path(GNUNET_OS_IPK_DATADIR);
578 GNUNET_asprintf(&sensordir, "%ssensors%s",
579 datadir, DIR_SEPARATOR_STR);
580 GNUNET_free(datadir);
586 * Reads sensor definitions from data files
594 sensordir = get_sensor_dir();
595 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Reloading sensor definitions from directory `%s'\n"), sensordir);
596 GNUNET_assert(GNUNET_YES == GNUNET_DISK_directory_test(sensordir, GNUNET_YES));
598 //read all files in sensors directory
599 GNUNET_DISK_directory_scan(sensordir, &reload_sensors_dir_cb, NULL);
600 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Loaded %d sensors from directory `%s'\n"),
601 GNUNET_CONTAINER_multihashmap_size(sensors), sensordir);
602 GNUNET_free(sensordir);
606 * Creates a structure with basic sensor info to be sent to a client
608 * @param sensor sensor information
609 * @return message ready to be sent to client
611 static struct SensorInfoMessage *
612 create_sensor_info_msg(struct SensorInfo *sensor)
614 struct SensorInfoMessage *msg;
620 name_len = strlen(sensor->name);
621 if(NULL == sensor->description)
624 desc_len = strlen(sensor->description) + 1;
626 len += sizeof(struct SensorInfoMessage);
629 msg = GNUNET_malloc(len);
630 msg->header.size = htons(len);
631 msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
632 msg->name_len = htons(name_len);
633 msg->description_len = htons(desc_len);
634 msg->version_major = htons(sensor->version_major);
635 msg->version_minor = htons(sensor->version_minor);
636 str_ptr = (char*) &msg[1];
637 memcpy(str_ptr, sensor->name, name_len);
638 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
639 name_len, name_len, str_ptr);
641 memcpy(str_ptr, sensor->description, desc_len);
642 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
643 desc_len, desc_len, str_ptr);
649 * Handle GET SENSOR message.
652 * @param client identification of the client
653 * @param message the actual message
656 handle_get_sensor (void *cls, struct GNUNET_SERVER_Client *client,
657 const struct GNUNET_MessageHeader *message)
659 struct GNUNET_SERVER_TransmitContext *tc;
661 size_t sensorname_len;
662 struct GNUNET_HashCode key;
663 struct SensorInfo *sensorinfo;
664 struct SensorInfoMessage *msg;
666 sensorname = (char *)&message[1];
667 sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
668 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
669 "GET SENSOR", sensorname_len, sensorname_len, sensorname);
670 tc = GNUNET_SERVER_transmit_context_create (client);
671 GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
673 sensorinfo = (struct SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
674 if(NULL != sensorinfo)
676 msg = create_sensor_info_msg(sensorinfo);
677 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
681 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
682 sensorname_len, sensorname);
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 * Iterator for sensors and adds them to transmit context
690 * @param cls a 'struct GNUNET_SERVER_TransmitContext *'
691 * @param key hash of sensor name, key to hashmap
692 * @param value a 'struct SensorInfo *'
694 int add_sensor_to_tc(void *cls,
695 const struct GNUNET_HashCode *key, void *value)
697 struct GNUNET_SERVER_TransmitContext *tc = cls;
698 struct SensorInfo *sensorinfo = value;
699 struct SensorInfoMessage *msg;
701 msg = create_sensor_info_msg(sensorinfo);
702 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
710 * Handle GET ALL SENSORS message.
713 * @param client identification of the client
714 * @param message the actual message
717 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
718 const struct GNUNET_MessageHeader *message)
720 struct GNUNET_SERVER_TransmitContext *tc;
722 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
724 tc = GNUNET_SERVER_transmit_context_create (client);
725 GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
726 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
727 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
731 * Do a series of checks to determine if sensor should execute
733 * @return #GNUNET_YES / #GNUNET_NO
736 should_run_sensor(struct SensorInfo *sensorinfo)
738 //FIXME: some checks should disable the sensor (e.g. expired)
739 struct GNUNET_TIME_Absolute now;
741 if(GNUNET_NO == sensorinfo->enabled)
743 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
746 now = GNUNET_TIME_absolute_get();
747 if(NULL != sensorinfo->start_time
748 && now.abs_value_us < sensorinfo->start_time->abs_value_us)
750 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n", sensorinfo->name);
753 if(NULL != sensorinfo->end_time
754 && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
756 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "End time for sensor `%s' passed, will not run\n", sensorinfo->name);
763 * Callback function to process statistic values
765 * @param cls 'struct SensorInfo *'
766 * @param subsystem name of subsystem that created the statistic
767 * @param name the name of the datum
768 * @param value the current value
769 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
770 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
772 int sensor_statistics_iterator (void *cls,
773 const char *subsystem,
778 struct SensorInfo *sensorinfo = cls;
780 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %" PRIu64 "\n", sensorinfo->name, value);
785 * Continuation called after sensor gets all gnunet statistics values
787 * @param cls 'struct SensorInfo *'
788 * @param success #GNUNET_OK if statistics were
789 * successfully obtained, #GNUNET_SYSERR if not.
791 void end_sensor_run_stat (void *cls, int success)
793 struct SensorInfo *sensorinfo = cls;
795 sensorinfo->gnunet_stat_get_handle = NULL;
796 sensorinfo->running = GNUNET_NO;
800 * Callback for output of executed sensor process
802 * @param cls 'struct SensorInfo *'
803 * @param line line of output from a command, NULL for the end
805 void sensor_process_callback (void *cls, const char *line)
807 struct SensorInfo *sensorinfo = cls;
811 sensorinfo->running = GNUNET_NO;
814 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n", sensorinfo->name, line);
818 * Actual execution of a sensor
820 * @param cls 'struct SensorInfo'
824 sensor_run (void *cls,
825 const struct GNUNET_SCHEDULER_TaskContext * tc)
827 struct SensorInfo *sensorinfo = cls;
832 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
833 if(GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
835 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sensor `%s' running for too long, will try again next interval\n", sensorinfo->name);
838 if(GNUNET_NO == should_run_sensor(sensorinfo))
840 sensorinfo->running = GNUNET_YES;
841 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
842 if(sources[0] == sensorinfo->source) //gnunet-statistics
844 if(NULL == statistics)
846 statistics = GNUNET_STATISTICS_create("sensor", cfg);
848 sensorinfo->gnunet_stat_get_handle = GNUNET_STATISTICS_get(statistics,
849 sensorinfo->gnunet_stat_service,
850 sensorinfo->gnunet_stat_name,
851 sensorinfo->interval, //try to get values only for the interval of the sensor
852 &end_sensor_run_stat,
853 &sensor_statistics_iterator,
856 else if(sources[1] == sensorinfo->source)
858 //FIXME: break execution if process is a path
859 //check if the process exists in $PATH
860 process_path = GNUNET_strdup(sensorinfo->ext_process);
862 GNUNET_OS_check_helper_binary(sensorinfo->ext_process, GNUNET_NO, NULL); //search in $PATH
863 if(GNUNET_SYSERR == check_result)
865 //search in sensor directory
866 sensors_dir = get_sensor_dir();
867 GNUNET_free(process_path);
868 GNUNET_asprintf(&process_path, "%s%s-files%s%s",
872 sensorinfo->ext_process);
873 GNUNET_free(sensors_dir);
875 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
877 if(GNUNET_SYSERR == check_result)
879 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n",
881 sensorinfo->ext_process);
882 //FIXME: disable sensor here?
883 sensorinfo->running = GNUNET_NO;
884 GNUNET_free(process_path);
887 GNUNET_OS_command_run(&sensor_process_callback,
889 GNUNET_TIME_UNIT_FOREVER_REL,
891 sensorinfo->ext_process,
892 sensorinfo->ext_args,
894 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name);
898 sensorinfo->running = GNUNET_NO;
899 GNUNET_break(0); //shouldn't happen
904 * Starts the execution of a sensor
907 * @param key hash of sensor name, key to hashmap (unused)
908 * @param value a 'struct SensorInfo *'
909 * @return #GNUNET_YES if we should continue to
913 int schedule_sensor(void *cls,
914 const struct GNUNET_HashCode *key, void *value)
916 struct SensorInfo *sensorinfo = value;
918 if(GNUNET_NO == should_run_sensor(sensorinfo))
920 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
921 sensorinfo->name, sensorinfo->interval.rel_value_us);
922 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
924 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sensor `%s' execution task already set, this should not happen\n", sensorinfo->name);
927 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
932 * Starts the execution of all enabled sensors
936 schedule_all_sensors()
938 GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
942 * Process statistics requests.
945 * @param server the initialized server
946 * @param c configuration to use
950 struct GNUNET_SERVER_Handle *server,
951 const struct GNUNET_CONFIGURATION_Handle *c)
953 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
954 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
956 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
957 sizeof (struct GNUNET_MessageHeader)},
962 sensors = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
964 schedule_all_sensors();
965 GNUNET_SERVER_add_handlers (server, handlers);
966 GNUNET_SERVER_disconnect_notify (server,
967 &handle_client_disconnect,
969 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
976 * The main function for the sensor service.
978 * @param argc number of arguments from the command line
979 * @param argv command line arguments
980 * @return 0 ok, 1 on error
983 main (int argc, char *const *argv)
986 GNUNET_SERVICE_run (argc,
989 GNUNET_SERVICE_OPTION_NONE,
990 &run, NULL)) ? 0 : 1;
993 /* end of gnunet-service-sensor.c */