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-analysis.c
23 * @brief sensor service analysis functionality
24 * @author Omar Tarabai
27 #include "gnunet_util_lib.h"
29 #include "gnunet_peerstore_service.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "sensor-analysis",__VA_ARGS__)
34 * Carries information about the analysis model
35 * corresponding to one sensor
41 * Pointer to sensor info structure
43 struct SensorInfo *sensor;
46 * Watcher of sensor values
48 struct GNUNET_PEERSTORE_WatchContext *wc;
55 static const struct GNUNET_CONFIGURATION_Handle *cfg;
60 static char *model_lib_name;
65 static struct GNUNET_SENSOR_ModelFunctions *model;
68 * Hashmap of loaded sensor definitions
70 static struct GNUNET_CONTAINER_MultiHashMap *sensors;
73 * Handle to peerstore service
75 static struct GNUNET_PEERSTORE_Handle *peerstore;
78 * Datatypes supported by the analysis component
80 static const char *analysis_datatypes[] = { "uint64", "double", NULL };
83 * MultiHashmap of all sensor models
85 static struct GNUNET_CONTAINER_MultiHashMap *sensor_models;
90 struct GNUNET_PeerIdentity peerid;
96 destroy_sensor_model (void *cls,
97 const struct GNUNET_HashCode *key,
100 struct SensorModel *sensor_model = value;
102 if (NULL == sensor_model)
104 LOG (GNUNET_ERROR_TYPE_DEBUG,
105 "Destroying sensor model for `%s'.\n",
106 sensor_model->sensor->name);
107 if (NULL != sensor_model->wc)
109 GNUNET_PEERSTORE_watch_cancel(sensor_model->wc);
110 sensor_model->wc = NULL;
116 * Stop the sensor analysis module
118 void SENSOR_analysis_stop()
121 LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor analysis module.\n");
124 GNUNET_break (NULL == GNUNET_PLUGIN_unload (model_lib_name, model));
125 GNUNET_free (model_lib_name);
126 model_lib_name = NULL;
128 if (NULL != sensor_models)
130 GNUNET_CONTAINER_multihashmap_iterate(sensor_models, &destroy_sensor_model, NULL);
131 GNUNET_CONTAINER_multihashmap_destroy(sensor_models);
132 sensor_models = NULL;
134 if (NULL != peerstore)
136 GNUNET_PEERSTORE_disconnect(peerstore);
145 sensor_watcher (void *cls,
146 struct GNUNET_PEERSTORE_Record *record,
149 LOG (GNUNET_ERROR_TYPE_DEBUG,
150 "Received a sensor value, will feed to sensor model.\n");
158 init_sensor_model (void *cls,
159 const struct GNUNET_HashCode *key,
162 struct SensorInfo *sensor = value;
163 struct SensorModel *sensor_model;
167 is_numeric = GNUNET_NO;
168 for (i = 0; NULL != analysis_datatypes[i]; i++)
170 if (0 == strcmp (analysis_datatypes[i], sensor->expected_datatype))
172 is_numeric = GNUNET_YES;
176 if (GNUNET_NO == is_numeric)
178 sensor_model = GNUNET_new(struct SensorModel);
179 sensor_model->sensor = sensor;
180 sensor_model->wc = GNUNET_PEERSTORE_watch(peerstore,
181 "sensor", &peerid, sensor->name,
182 &sensor_watcher, sensor_model);
183 GNUNET_CONTAINER_multihashmap_put(sensor_models, key,
184 sensor_model, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
185 LOG (GNUNET_ERROR_TYPE_DEBUG,
186 "Created sensor model for `%s'.\n", sensor->name);
191 * Start the sensor analysis module
193 * @param c our service configuration
194 * @param sensors_mhm multihashmap of loaded sensors
195 * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise
198 SENSOR_analysis_start(const struct GNUNET_CONFIGURATION_Handle *c,
199 struct GNUNET_CONTAINER_MultiHashMap *sensors_mhm)
205 GNUNET_CONFIGURATION_get_value_string (cfg, "sensor-analysis", "MODEL",
208 LOG (GNUNET_ERROR_TYPE_ERROR, _("Analysis model not defined in configuration.\n"));
209 return GNUNET_SYSERR;
211 GNUNET_asprintf (&model_lib_name, "libgnunet_plugin_sensor_model_%s", model_name);
212 model = GNUNET_PLUGIN_load(model_lib_name, (void *) cfg);
213 GNUNET_free(model_name);
216 LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not load analysis model `%s'.\n"), model_lib_name);
217 return GNUNET_SYSERR;
219 sensors = sensors_mhm;
222 LOG (GNUNET_ERROR_TYPE_ERROR, _("Tried to start analysis before loading sensors.\n"));
223 SENSOR_analysis_stop();
224 return GNUNET_SYSERR;
226 peerstore = GNUNET_PEERSTORE_connect(cfg);
227 if (NULL == peerstore)
229 LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not connect to peerstore service.\n"));
230 SENSOR_analysis_stop();
231 return GNUNET_SYSERR;
233 GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
234 sensor_models = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO);
235 GNUNET_CONTAINER_multihashmap_iterate(sensors, &init_sensor_model, NULL);
240 /* end of gnunet-service-sensor-analysis.c */