From df0aabbb2fa9adc12b2882ce63e4587b9b4c2f27 Mon Sep 17 00:00:00 2001 From: Omar Tarabai Date: Wed, 25 Jun 2014 19:29:54 +0000 Subject: towards sensor analysis functionality --- src/include/gnunet_sensor_service.h | 154 ++++++++++++++++++++- src/sensor/Makefile.am | 3 +- src/sensor/gnunet-service-sensor-analysis.c | 204 ++++++++++++++++++++++++++++ src/sensor/gnunet-service-sensor.c | 154 +-------------------- src/sensor/gnunet_sensor_model_plugin.h | 62 +++++++++ 5 files changed, 422 insertions(+), 155 deletions(-) create mode 100644 src/sensor/gnunet-service-sensor-analysis.c create mode 100644 src/sensor/gnunet_sensor_model_plugin.h (limited to 'src') diff --git a/src/include/gnunet_sensor_service.h b/src/include/gnunet_sensor_service.h index 5a4aa82c2..cd840dd75 100644 --- a/src/include/gnunet_sensor_service.h +++ b/src/include/gnunet_sensor_service.h @@ -43,10 +43,162 @@ extern "C" */ struct GNUNET_SENSOR_Handle; +/** + * Structure containing sensor definition + */ +struct SensorInfo +{ + + /** + * The configuration handle + * carrying sensor information + */ + struct GNUNET_CONFIGURATION_Handle *cfg; + + /* + * Sensor name + */ + char *name; + + /* + * Path to definition file + */ + char *def_file; + + /* + * First part of version number + */ + uint16_t version_major; + + /* + * Second part of version number + */ + uint16_t version_minor; + + /* + * Sensor description + */ + char *description; + + /* + * Sensor currently enabled + */ + int enabled; + + /* + * Category under which the sensor falls (e.g. tcp, datastore) + */ + char *category; + + /* + * When does the sensor become active + */ + struct GNUNET_TIME_Absolute *start_time; + + /* + * When does the sensor expire + */ + struct GNUNET_TIME_Absolute *end_time; + + /* + * Time interval to collect sensor information (e.g. every 1 min) + */ + struct GNUNET_TIME_Relative interval; + + /* + * Lifetime of an information sample after which it is deleted from storage + * If not supplied, will default to the interval value + */ + struct GNUNET_TIME_Relative lifetime; + + /* + * A set of required peer capabilities for the sensor to collect meaningful information (e.g. ipv6) + */ + char *capabilities; + + /* + * Either "gnunet-statistics" or external "process" + */ + char *source; + + /* + * Name of the GNUnet service that is the source for the gnunet-statistics entry + */ + char *gnunet_stat_service; + + /* + * Name of the gnunet-statistics entry + */ + char *gnunet_stat_name; + + /** + * Handle to statistics get request (OR GNUNET_SCHEDULER_NO_TASK) + */ + struct GNUNET_STATISTICS_GetHandle *gnunet_stat_get_handle; + + /* + * Name of the external process to be executed + */ + char *ext_process; + + /* + * Arguments to be passed to the external process + */ + char *ext_args; + + /* + * Handle to the external process + */ + struct GNUNET_OS_CommandHandle *ext_cmd; + + /* + * Did we already receive a value + * from the currently running external + * proccess ? #GNUNET_YES / #GNUNET_NO + */ + int ext_cmd_value_received; + + /* + * The output datatype to be expected + */ + char *expected_datatype; + + /* + * Peer-identity of peer running collection point + */ + struct GNUNET_PeerIdentity *collection_point; + + /* + * Time interval to send sensor information to collection point (e.g. every 30 mins) + */ + struct GNUNET_TIME_Relative *collection_interval; + + /* + * Flag specifying if value is to be communicated to the p2p network + */ + int p2p_report; + + /* + * Time interval to communicate value to the p2p network + */ + struct GNUNET_TIME_Relative *p2p_interval; + + /* + * Execution task (OR GNUNET_SCHEDULER_NO_TASK) + */ + GNUNET_SCHEDULER_TaskIdentifier execution_task; + + /* + * Is the sensor being executed + */ + int running; + +}; + /** * Structure containing brief info about sensor */ -struct SensorInfoShort //FIXME: rename +struct SensorInfoShort { /* diff --git a/src/sensor/Makefile.am b/src/sensor/Makefile.am index b6c55f5e6..ca6760c29 100644 --- a/src/sensor/Makefile.am +++ b/src/sensor/Makefile.am @@ -32,7 +32,8 @@ gnunet_sensor_LDADD = \ $(GN_LIBINTL) gnunet_service_sensor_SOURCES = \ - gnunet-service-sensor.c + gnunet-service-sensor.c \ + gnunet-service-sensor-analysis.c gnunet_service_sensor_LDADD = \ $(top_builddir)/src/util/libgnunetutil.la \ $(top_builddir)/src/statistics/libgnunetstatistics.la \ diff --git a/src/sensor/gnunet-service-sensor-analysis.c b/src/sensor/gnunet-service-sensor-analysis.c new file mode 100644 index 000000000..8ab486094 --- /dev/null +++ b/src/sensor/gnunet-service-sensor-analysis.c @@ -0,0 +1,204 @@ +/* + This file is part of GNUnet. + (C) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/** + * @file sensor/gnunet-service-sensor-analysis.c + * @brief sensor service analysis functionality + * @author Omar Tarabai + */ +#include "platform.h" +#include "gnunet_util_lib.h" +#include "sensor.h" +#include "gnunet_peerstore_service.h" + +#define LOG(kind,...) GNUNET_log_from (kind, "sensor-analysis",__VA_ARGS__) + +/* + * Carries information about the analysis model + * corresponding to one sensor + */ +struct SensorModel +{ + + /* + * Pointer to sensor info structure + */ + struct SensorInfo *sensor; + + /* + * Watcher of sensor values + */ + struct GNUNET_PEERSTORE_WatchContext *wc; + +}; + +/** + * Our configuration. + */ +static const struct GNUNET_CONFIGURATION_Handle *cfg; + +/* + * Model library name + */ +static char *model_lib_name; + +/* + * Model handle + */ +static struct GNUNET_SENSOR_ModelFunctions *model; + +/** + * Hashmap of loaded sensor definitions + */ +static struct GNUNET_CONTAINER_MultiHashMap *sensors; + +/* + * Handle to peerstore service + */ +static struct GNUNET_PEERSTORE_Handle *peerstore; + +/* + * Datatypes supported by the analysis component + */ +static const char *analysis_datatypes[] = { "uint64", "double", NULL }; + +/* + * MultiHashmap of all sensor models + */ +static struct GNUNET_CONTAINER_MultiHashMap *sensor_models; + +/* + * TODO: document + */ +void SENSOR_analysis_stop() +{ + if (NULL != model) + { + GNUNET_break (NULL == GNUNET_PLUGIN_unload (model_lib_name, model)); + GNUNET_free (model_lib_name); + model_lib_name = NULL; + } + if (NULL != peerstore) + { + GNUNET_PEERSTORE_disconnect(peerstore); + peerstore = NULL; + } + if (NULL != sensor_models) + { + /* TODO: iterate over sensor models and destroy */ + GNUNET_CONTAINER_multihashmap_destroy(sensor_models); + sensor_models = NULL; + } +} + +/* + * TODO: document + */ +static int +sensor_watcher (void *cls, + struct GNUNET_PEERSTORE_Record *record, + char *emsg) +{ + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Received a sensor value, will feed to sensor model.\n"); + return GNUNET_YES; +} + +/* + * TODO: document + */ +static int +init_sensor_model (void *cls, + const struct GNUNET_HashCode *key, + void *value) +{ + struct SensorInfo *sensor = value; + struct SensorModel *sensor_model; + int is_numeric; + int i; + + is_numeric = GNUNET_NO; + for (i = 0; NULL != analysis_datatypes[i]; i++) + { + if (0 == strcasecmp (analysis_datatypes[i], sensor->expected_datatype)) + { + is_numeric = GNUNET_YES; + break; + } + } + if (GNUNET_NO == is_numeric) + return GNUNET_YES; + sensor_model = GNUNET_new(struct SensorModel); + sensor_model->wc = GNUNET_PEERSTORE_watch(peerstore, + "sensor", NULL, sensor->name, + &sensor_watcher, sensor_model); + GNUNET_CONTAINER_multihashmap_put(sensor_models, key, + sensor_model, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); + return GNUNET_YES; +} + +/* + * TODO: document + * + * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise + */ +int +SENSOR_analysis_start(const struct GNUNET_CONFIGURATION_Handle *c, + struct GNUNET_CONTAINER_MultiHashMap *sensors_mhm) +{ + char *model_name; + + cfg = c; + if (GNUNET_OK != + GNUNET_CONFIGURATION_get_value_string (cfg, "sensor-analysis", "MODEL", + &model_name)) + { + LOG (GNUNET_ERROR_TYPE_ERROR, _("Analysis model not defined in configuration.\n")); + return GNUNET_SYSERR; + } + GNUNET_asprintf (&model_lib_name, "libgnunet_plugin_sensor_model_%s", model_name); + model = GNUNET_PLUGIN_load(model_lib_name, (void *) cfg); + GNUNET_free(model_name); + if(NULL == model) + { + LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not load analysis model `%s'.\n"), model_lib_name); + return GNUNET_SYSERR; + } + sensors = sensors_mhm; + if (NULL == sensors) + { + LOG (GNUNET_ERROR_TYPE_ERROR, _("Tried to start analysis before loading sensors.\n")); + SENSOR_analysis_stop(); + return GNUNET_SYSERR; + } + peerstore = GNUNET_PEERSTORE_connect(cfg); + if (NULL == peerstore) + { + LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not connect to peerstore service.\n")); + SENSOR_analysis_stop(); + return GNUNET_SYSERR; + } + sensor_models = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO); + GNUNET_CONTAINER_multihashmap_iterate(sensors, &init_sensor_model, NULL); + + return GNUNET_OK; +} + +/* end of gnunet-service-sensor-analysis.c */ diff --git a/src/sensor/gnunet-service-sensor.c b/src/sensor/gnunet-service-sensor.c index f1c49683e..96856c3bf 100644 --- a/src/sensor/gnunet-service-sensor.c +++ b/src/sensor/gnunet-service-sensor.c @@ -35,158 +35,6 @@ */ #define MIN_INTERVAL 30 -/** - * Structure containing sensor definition - */ -struct SensorInfo -{ - - /** - * The configuration handle - * carrying sensor information - */ - struct GNUNET_CONFIGURATION_Handle *cfg; - - /* - * Sensor name - */ - char *name; - - /* - * Path to definition file - */ - char *def_file; - - /* - * First part of version number - */ - uint16_t version_major; - - /* - * Second part of version number - */ - uint16_t version_minor; - - /* - * Sensor description - */ - char *description; - - /* - * Sensor currently enabled - */ - int enabled; - - /* - * Category under which the sensor falls (e.g. tcp, datastore) - */ - char *category; - - /* - * When does the sensor become active - */ - struct GNUNET_TIME_Absolute *start_time; - - /* - * When does the sensor expire - */ - struct GNUNET_TIME_Absolute *end_time; - - /* - * Time interval to collect sensor information (e.g. every 1 min) - */ - struct GNUNET_TIME_Relative interval; - - /* - * Lifetime of an information sample after which it is deleted from storage - * If not supplied, will default to the interval value - */ - struct GNUNET_TIME_Relative lifetime; - - /* - * A set of required peer capabilities for the sensor to collect meaningful information (e.g. ipv6) - */ - char *capabilities; - - /* - * Either "gnunet-statistics" or external "process" - */ - char *source; - - /* - * Name of the GNUnet service that is the source for the gnunet-statistics entry - */ - char *gnunet_stat_service; - - /* - * Name of the gnunet-statistics entry - */ - char *gnunet_stat_name; - - /** - * Handle to statistics get request (OR GNUNET_SCHEDULER_NO_TASK) - */ - struct GNUNET_STATISTICS_GetHandle *gnunet_stat_get_handle; - - /* - * Name of the external process to be executed - */ - char *ext_process; - - /* - * Arguments to be passed to the external process - */ - char *ext_args; - - /* - * Handle to the external process - */ - struct GNUNET_OS_CommandHandle *ext_cmd; - - /* - * Did we already receive a value - * from the currently running external - * proccess ? #GNUNET_YES / #GNUNET_NO - */ - int ext_cmd_value_received; - - /* - * The output datatype to be expected - */ - char *expected_datatype; - - /* - * Peer-identity of peer running collection point - */ - struct GNUNET_PeerIdentity *collection_point; - - /* - * Time interval to send sensor information to collection point (e.g. every 30 mins) - */ - struct GNUNET_TIME_Relative *collection_interval; - - /* - * Flag specifying if value is to be communicated to the p2p network - */ - int p2p_report; - - /* - * Time interval to communicate value to the p2p network - */ - struct GNUNET_TIME_Relative *p2p_interval; - - /* - * Execution task (OR GNUNET_SCHEDULER_NO_TASK) - */ - GNUNET_SCHEDULER_TaskIdentifier execution_task; - - /* - * Is the sensor being executed - */ - int running; - -}; - /** * Our configuration. */ @@ -195,7 +43,7 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg; /** * Hashmap of loaded sensor definitions */ -struct GNUNET_CONTAINER_MultiHashMap *sensors; +static struct GNUNET_CONTAINER_MultiHashMap *sensors; /** * Supported sources of sensor information diff --git a/src/sensor/gnunet_sensor_model_plugin.h b/src/sensor/gnunet_sensor_model_plugin.h new file mode 100644 index 000000000..374c776f5 --- /dev/null +++ b/src/sensor/gnunet_sensor_model_plugin.h @@ -0,0 +1,62 @@ +/* + This file is part of GNUnet + (C) 2012, 2013 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +/** + * @file sensor/gnunet_sensor_model_plugin.h + * @brief plugin API for sensor analysis models + * @author Omar Tarabai + */ +#ifndef GNUNET_SENSOR_MODEL_PLUGIN_H +#define GNUNET_SENSOR_MODEL_PLUGIN_H + +#include "gnunet_util_lib.h" + +#ifdef __cplusplus +extern "C" +{ +#if 0 /* keep Emacsens' auto-indent happy */ +} +#endif +#endif + + +/** + * API for a sensor analysis model + */ +struct GNUNET_SENSOR_ModelFunctions +{ + + /** + * Closure to pass to all plugin functions. + */ + void *cls; + +}; + + +#if 0 /* keep Emacsens' auto-indent happy */ +{ +#endif +#ifdef __cplusplus +} +#endif + +/* end of gnunet_sensor_model_plugin.h */ +#endif -- cgit v1.2.3