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"
36 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39 * Hashmap of loaded sensor definitions
41 static struct GNUNET_CONTAINER_MultiHashMap *sensors;
44 * Handle to statistics service
46 struct GNUNET_STATISTICS_Handle *statistics;
49 * Handle to peerstore service
51 struct GNUNET_PEERSTORE_Handle *peerstore;
56 char *subsystem = "sensor";
61 struct GNUNET_PeerIdentity peerid;
65 * Resets the service by stopping components, reloading sensors and starting
66 * components. This is needed when we receive new sensor updates.
73 * Change the state of the sensor.
74 * Write the change to file to make it persistent.
76 * @param sensor sensor info struct
77 * @param state new enabled state: #GNUNET_YES / #GNUNET_NO
80 set_sensor_enabled (struct GNUNET_SENSOR_SensorInfo *sensor, int state)
82 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
83 "Sensor `%s': Setting enabled to %d.\n",
85 sensor->enabled = GNUNET_NO;
86 GNUNET_assert (NULL != sensor->cfg);
87 GNUNET_CONFIGURATION_set_value_string (sensor->cfg,
90 (GNUNET_YES == state) ? "YES" : "NO");
91 GNUNET_CONFIGURATION_write (sensor->cfg, sensor->def_file);
96 * Stops components and destroys sensors
101 SENSOR_update_stop ();
102 SENSOR_reporting_stop ();
103 SENSOR_analysis_stop ();
104 GNUNET_SENSOR_destroy_sensors (sensors);
109 * Task run during shutdown.
115 shutdown_task (void *cls,
116 const struct GNUNET_SCHEDULER_TaskContext *tc)
119 if (NULL != statistics)
121 GNUNET_STATISTICS_destroy (statistics, GNUNET_YES);
124 if (NULL != peerstore)
126 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_YES);
129 GNUNET_SCHEDULER_shutdown ();
134 * A client disconnected. Remove all of its data structure entries.
136 * @param cls closure, NULL
137 * @param client identification of the client
140 handle_client_disconnect (void *cls,
141 struct GNUNET_SERVER_Client *client)
146 * Creates a structure with basic sensor info to be sent to a client.
148 * @param sensor sensor information
149 * @return message ready to be sent to client
151 static struct SensorInfoMessage *
152 create_sensor_info_msg (struct GNUNET_SENSOR_SensorInfo *sensor)
154 struct SensorInfoMessage *msg;
160 name_len = strlen(sensor->name);
161 if(NULL == sensor->description)
164 desc_len = strlen(sensor->description) + 1;
166 len += sizeof(struct SensorInfoMessage);
169 msg = GNUNET_malloc(len);
170 msg->header.size = htons(len);
171 msg->header.type = htons(GNUNET_MESSAGE_TYPE_SENSOR_INFO);
172 msg->name_len = htons(name_len);
173 msg->description_len = htons(desc_len);
174 msg->version_major = htons(sensor->version_major);
175 msg->version_minor = htons(sensor->version_minor);
176 str_ptr = (char*) &msg[1];
177 memcpy(str_ptr, sensor->name, name_len);
178 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor name (%d): %.*s\n",
179 name_len, name_len, str_ptr);
181 memcpy(str_ptr, sensor->description, desc_len);
182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending sensor description (%d): %.*s\n",
183 desc_len, desc_len, str_ptr);
189 * Handle GET SENSOR message.
192 * @param client identification of the client
193 * @param message the actual message
196 handle_get_sensor (void *cls,
197 struct GNUNET_SERVER_Client *client,
198 const struct GNUNET_MessageHeader *message)
200 struct GNUNET_SERVER_TransmitContext *tc;
202 size_t sensorname_len;
203 struct GNUNET_HashCode key;
204 struct GNUNET_SENSOR_SensorInfo *sensorinfo;
205 struct SensorInfoMessage *msg;
207 sensorname = (char *)&message[1];
208 sensorname_len = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
209 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received for sensor (%d) `%.*s'\n",
210 "GET SENSOR", sensorname_len, sensorname_len, sensorname);
211 tc = GNUNET_SERVER_transmit_context_create (client);
212 GNUNET_CRYPTO_hash(sensorname, sensorname_len, &key);
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created key hash for requested sensor\n");
214 sensorinfo = (struct GNUNET_SENSOR_SensorInfo *)GNUNET_CONTAINER_multihashmap_get(sensors, &key);
215 if(NULL != sensorinfo)
217 msg = create_sensor_info_msg(sensorinfo);
218 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
222 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Requested sensor `%.*s' was not found\n",
223 sensorname_len, sensorname);
224 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
225 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
229 * Iterator for sensors and adds them to transmit context
231 * @param cls a `struct GNUNET_SERVER_TransmitContext *`
232 * @param key hash of sensor name, key to hashmap
233 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
236 add_sensor_to_tc (void *cls,
237 const struct GNUNET_HashCode *key,
240 struct GNUNET_SERVER_TransmitContext *tc = cls;
241 struct GNUNET_SENSOR_SensorInfo *sensorinfo = value;
242 struct SensorInfoMessage *msg;
244 msg = create_sensor_info_msg(sensorinfo);
245 GNUNET_SERVER_transmit_context_append_message(tc, (struct GNUNET_MessageHeader *)msg);
253 * Handle GET ALL SENSORS message.
256 * @param client identification of the client
257 * @param message the actual message
260 handle_get_all_sensors (void *cls, struct GNUNET_SERVER_Client *client,
261 const struct GNUNET_MessageHeader *message)
263 struct GNUNET_SERVER_TransmitContext *tc;
265 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "`%s' message received.\n",
267 tc = GNUNET_SERVER_transmit_context_create (client);
268 GNUNET_CONTAINER_multihashmap_iterate(sensors, &add_sensor_to_tc, tc);
269 GNUNET_SERVER_transmit_context_append_data(tc, NULL, 0, GNUNET_MESSAGE_TYPE_SENSOR_END);
270 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
274 * Do a series of checks to determine if sensor should execute
276 * @return #GNUNET_YES / #GNUNET_NO
279 should_run_sensor(struct GNUNET_SENSOR_SensorInfo *sensorinfo)
281 struct GNUNET_TIME_Absolute now;
283 if(GNUNET_NO == sensorinfo->enabled)
285 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' is disabled, will not run\n", sensorinfo->name);
288 now = GNUNET_TIME_absolute_get();
289 if(NULL != sensorinfo->start_time
290 && now.abs_value_us < sensorinfo->start_time->abs_value_us)
292 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start time for sensor `%s' not reached yet, will not run\n", sensorinfo->name);
295 if(NULL != sensorinfo->end_time
296 && now.abs_value_us >= sensorinfo->end_time->abs_value_us)
298 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Sensor `%s' expired, disabling.\n", sensorinfo->name);
299 set_sensor_enabled(sensorinfo, GNUNET_NO);
306 * Callback function to process statistic values
308 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
309 * @param subsystem name of subsystem that created the statistic
310 * @param name the name of the datum
311 * @param value the current value
312 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
313 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
316 sensor_statistics_iterator (void *cls,
322 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
323 double dvalue = (double)value;
324 struct GNUNET_TIME_Absolute expiry;
326 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %" PRIu64 "\n", sensorinfo->name, value);
327 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->lifetime);
328 GNUNET_PEERSTORE_store(peerstore,
335 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
338 return GNUNET_SYSERR; /* We only want one value */
342 * Continuation called after sensor gets all gnunet statistics values
344 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
345 * @param success #GNUNET_OK if statistics were
346 * successfully obtained, #GNUNET_SYSERR if not.
349 end_sensor_run_stat (void *cls, int success)
351 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
353 sensorinfo->gnunet_stat_get_handle = NULL;
354 sensorinfo->running = GNUNET_NO;
358 * Tries to parse a received sensor value to its
361 * @param value the string value received, should be null terminated
362 * @param sensor sensor information struct
363 * @param ret pointer to parsed value
364 * @return size of new parsed value, 0 for error
367 parse_sensor_value (const char *value,
368 struct GNUNET_SENSOR_SensorInfo *sensor,
377 if(0 == strcmp("numeric", sensor->expected_datatype))
379 dval = GNUNET_new(double);
380 *dval = strtod(value, &endptr);
384 return sizeof(double);
386 if(0 == strcmp("string", sensor->expected_datatype))
388 *ret = GNUNET_strdup(value);
389 return strlen(value) + 1;
391 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
392 _("Unknown value type expected by sensor, this should not happen.\n"));
397 * Callback for output of executed sensor process
399 * @param cls `struct GNUNET_SENSOR_SensorInfo *`
400 * @param line line of output from a command, NULL for the end
403 sensor_process_callback (void *cls, const char *line)
405 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
408 struct GNUNET_TIME_Absolute expiry;
412 GNUNET_OS_command_stop(sensorinfo->ext_cmd);
413 sensorinfo->ext_cmd = NULL;
414 sensorinfo->running = GNUNET_NO;
415 sensorinfo->ext_cmd_value_received = GNUNET_NO;
418 if(GNUNET_YES == sensorinfo->ext_cmd_value_received)
419 return; /* We only want one *valid* value */
420 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Received a value for sensor `%s': %s\n", sensorinfo->name, line);
421 valsize = parse_sensor_value(line, sensorinfo, &value);
422 if (valsize == 0) /* invalid value, FIXME: should we disable the sensor now? */
424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
425 _("Received an invalid value for sensor `%s': %s\n"),
426 sensorinfo->name, line);
430 sensorinfo->ext_cmd_value_received = GNUNET_YES;
431 expiry = GNUNET_TIME_relative_to_absolute(sensorinfo->lifetime);
432 GNUNET_PEERSTORE_store(peerstore,
439 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
447 * Checks if the given file is a path
449 * @return #GNUNET_YES / #GNUNET_NO
452 is_path(char *filename)
457 filename_len = strlen(filename);
458 for(i = 0; i < filename_len; i++)
460 if(DIR_SEPARATOR == filename[i])
467 * Actual execution of a sensor
469 * @param cls 'struct SensorInfo'
473 sensor_run (void *cls,
474 const struct GNUNET_SCHEDULER_TaskContext * tc)
476 struct GNUNET_SENSOR_SensorInfo *sensorinfo = cls;
481 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
482 if(GNUNET_YES == sensorinfo->running) //FIXME: should we try to kill?
484 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sensor `%s' running for too long, will try again next interval\n", sensorinfo->name);
487 if(GNUNET_NO == should_run_sensor(sensorinfo))
489 sensorinfo->running = GNUNET_YES;
490 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Starting the execution of sensor `%s'\n", sensorinfo->name);
491 if(0 == strcmp ("gnunet-statistics", sensorinfo->source))
493 sensorinfo->gnunet_stat_get_handle = GNUNET_STATISTICS_get(statistics,
494 sensorinfo->gnunet_stat_service,
495 sensorinfo->gnunet_stat_name,
496 sensorinfo->interval, //try to get values only for the interval of the sensor
497 &end_sensor_run_stat,
498 &sensor_statistics_iterator,
501 else if(0 == strcmp ("process", sensorinfo->source))
503 if(GNUNET_YES == is_path(sensorinfo->ext_process))
505 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
506 _("Sensor `%s': External process should not be a path, disabling sensor.\n"),
508 set_sensor_enabled(sensorinfo, GNUNET_NO);
511 //check if the process exists in $PATH
512 process_path = GNUNET_strdup(sensorinfo->ext_process);
514 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
515 if(GNUNET_SYSERR == check_result)
517 //search in sensor directory
518 sensors_dir = GNUNET_SENSOR_get_sensor_dir ();
519 GNUNET_free(process_path);
520 GNUNET_asprintf(&process_path,
525 sensorinfo->ext_process);
526 GNUNET_free(sensors_dir);
528 GNUNET_OS_check_helper_binary(process_path, GNUNET_NO, NULL);
530 if(GNUNET_SYSERR == check_result)
532 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
533 _("Sensor `%s' process `%s' problem: binary doesn't exist or not executable\n"),
535 sensorinfo->ext_process);
536 set_sensor_enabled(sensorinfo, GNUNET_NO);
537 sensorinfo->running = GNUNET_NO;
538 GNUNET_free(process_path);
541 sensorinfo->ext_cmd_value_received = GNUNET_NO;
542 sensorinfo->ext_cmd = GNUNET_OS_command_run(&sensor_process_callback,
544 GNUNET_TIME_UNIT_FOREVER_REL,
546 sensorinfo->ext_process,
547 sensorinfo->ext_args,
549 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Process started for sensor `%s'\n", sensorinfo->name);
550 GNUNET_free(process_path);
554 sensorinfo->running = GNUNET_NO;
555 GNUNET_break(0); //shouldn't happen
560 * Starts the execution of a sensor
563 * @param key hash of sensor name, key to hashmap (unused)
564 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
565 * @return #GNUNET_YES if we should continue to
570 schedule_sensor (void *cls, const struct GNUNET_HashCode *key, void *value)
572 struct GNUNET_SENSOR_SensorInfo *sensorinfo = value;
574 if(GNUNET_NO == should_run_sensor(sensorinfo))
576 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling sensor `%s' to run after %" PRIu64 " microseconds\n",
577 sensorinfo->name, sensorinfo->interval.rel_value_us);
578 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task)
580 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
581 _("Sensor `%s' execution task already set, this should not happen\n"), sensorinfo->name);
584 sensorinfo->execution_task = GNUNET_SCHEDULER_add_delayed(sensorinfo->interval, &sensor_run, sensorinfo);
589 * Starts the execution of all enabled sensors
593 schedule_all_sensors()
595 GNUNET_CONTAINER_multihashmap_iterate(sensors, &schedule_sensor, NULL);
600 * Loads sensors and starts different service components
605 sensors = GNUNET_SENSOR_load_all_sensors ();
606 schedule_all_sensors();
607 SENSOR_analysis_start(cfg, sensors);
608 SENSOR_reporting_start(cfg, sensors);
609 SENSOR_update_start (cfg, sensors, &reset);
614 * Process statistics requests.
617 * @param server the initialized server
618 * @param c configuration to use
622 struct GNUNET_SERVER_Handle *server,
623 const struct GNUNET_CONFIGURATION_Handle *c)
625 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
626 {&handle_get_sensor, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GET,
628 {&handle_get_all_sensors, NULL, GNUNET_MESSAGE_TYPE_SENSOR_GETALL,
629 sizeof (struct GNUNET_MessageHeader)},
634 statistics = GNUNET_STATISTICS_create("sensor", cfg);
635 GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
636 peerstore = GNUNET_PEERSTORE_connect(cfg);
637 GNUNET_SERVER_add_handlers (server, handlers);
638 GNUNET_SERVER_disconnect_notify (server,
639 &handle_client_disconnect,
641 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
649 * Resets the service by stopping components, reloading sensors and starting
650 * components. This is needed when we receive new sensor updates.
661 * The main function for the sensor service.
663 * @param argc number of arguments from the command line
664 * @param argv command line arguments
665 * @return 0 ok, 1 on error
668 main (int argc, char *const *argv)
671 GNUNET_SERVICE_run (argc,
674 GNUNET_SERVICE_OPTION_NONE,
675 &run, NULL)) ? 0 : 1;
678 /* end of gnunet-service-sensor.c */