diff options
Diffstat (limited to 'src/sensor')
-rw-r--r-- | src/sensor/Makefile.am | 3 | ||||
-rw-r--r-- | src/sensor/gnunet-service-sensor-analysis.c | 204 | ||||
-rw-r--r-- | src/sensor/gnunet-service-sensor.c | 154 | ||||
-rw-r--r-- | src/sensor/gnunet_sensor_model_plugin.h | 62 |
4 files changed, 269 insertions, 154 deletions
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 = \ | |||
32 | $(GN_LIBINTL) | 32 | $(GN_LIBINTL) |
33 | 33 | ||
34 | gnunet_service_sensor_SOURCES = \ | 34 | gnunet_service_sensor_SOURCES = \ |
35 | gnunet-service-sensor.c | 35 | gnunet-service-sensor.c \ |
36 | gnunet-service-sensor-analysis.c | ||
36 | gnunet_service_sensor_LDADD = \ | 37 | gnunet_service_sensor_LDADD = \ |
37 | $(top_builddir)/src/util/libgnunetutil.la \ | 38 | $(top_builddir)/src/util/libgnunetutil.la \ |
38 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ | 39 | $(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 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file sensor/gnunet-service-sensor-analysis.c | ||
23 | * @brief sensor service analysis functionality | ||
24 | * @author Omar Tarabai | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "sensor.h" | ||
29 | #include "gnunet_peerstore_service.h" | ||
30 | |||
31 | #define LOG(kind,...) GNUNET_log_from (kind, "sensor-analysis",__VA_ARGS__) | ||
32 | |||
33 | /* | ||
34 | * Carries information about the analysis model | ||
35 | * corresponding to one sensor | ||
36 | */ | ||
37 | struct SensorModel | ||
38 | { | ||
39 | |||
40 | /* | ||
41 | * Pointer to sensor info structure | ||
42 | */ | ||
43 | struct SensorInfo *sensor; | ||
44 | |||
45 | /* | ||
46 | * Watcher of sensor values | ||
47 | */ | ||
48 | struct GNUNET_PEERSTORE_WatchContext *wc; | ||
49 | |||
50 | }; | ||
51 | |||
52 | /** | ||
53 | * Our configuration. | ||
54 | */ | ||
55 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
56 | |||
57 | /* | ||
58 | * Model library name | ||
59 | */ | ||
60 | static char *model_lib_name; | ||
61 | |||
62 | /* | ||
63 | * Model handle | ||
64 | */ | ||
65 | static struct GNUNET_SENSOR_ModelFunctions *model; | ||
66 | |||
67 | /** | ||
68 | * Hashmap of loaded sensor definitions | ||
69 | */ | ||
70 | static struct GNUNET_CONTAINER_MultiHashMap *sensors; | ||
71 | |||
72 | /* | ||
73 | * Handle to peerstore service | ||
74 | */ | ||
75 | static struct GNUNET_PEERSTORE_Handle *peerstore; | ||
76 | |||
77 | /* | ||
78 | * Datatypes supported by the analysis component | ||
79 | */ | ||
80 | static const char *analysis_datatypes[] = { "uint64", "double", NULL }; | ||
81 | |||
82 | /* | ||
83 | * MultiHashmap of all sensor models | ||
84 | */ | ||
85 | static struct GNUNET_CONTAINER_MultiHashMap *sensor_models; | ||
86 | |||
87 | /* | ||
88 | * TODO: document | ||
89 | */ | ||
90 | void SENSOR_analysis_stop() | ||
91 | { | ||
92 | if (NULL != model) | ||
93 | { | ||
94 | GNUNET_break (NULL == GNUNET_PLUGIN_unload (model_lib_name, model)); | ||
95 | GNUNET_free (model_lib_name); | ||
96 | model_lib_name = NULL; | ||
97 | } | ||
98 | if (NULL != peerstore) | ||
99 | { | ||
100 | GNUNET_PEERSTORE_disconnect(peerstore); | ||
101 | peerstore = NULL; | ||
102 | } | ||
103 | if (NULL != sensor_models) | ||
104 | { | ||
105 | /* TODO: iterate over sensor models and destroy */ | ||
106 | GNUNET_CONTAINER_multihashmap_destroy(sensor_models); | ||
107 | sensor_models = NULL; | ||
108 | } | ||
109 | } | ||
110 | |||
111 | /* | ||
112 | * TODO: document | ||
113 | */ | ||
114 | static int | ||
115 | sensor_watcher (void *cls, | ||
116 | struct GNUNET_PEERSTORE_Record *record, | ||
117 | char *emsg) | ||
118 | { | ||
119 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
120 | "Received a sensor value, will feed to sensor model.\n"); | ||
121 | return GNUNET_YES; | ||
122 | } | ||
123 | |||
124 | /* | ||
125 | * TODO: document | ||
126 | */ | ||
127 | static int | ||
128 | init_sensor_model (void *cls, | ||
129 | const struct GNUNET_HashCode *key, | ||
130 | void *value) | ||
131 | { | ||
132 | struct SensorInfo *sensor = value; | ||
133 | struct SensorModel *sensor_model; | ||
134 | int is_numeric; | ||
135 | int i; | ||
136 | |||
137 | is_numeric = GNUNET_NO; | ||
138 | for (i = 0; NULL != analysis_datatypes[i]; i++) | ||
139 | { | ||
140 | if (0 == strcasecmp (analysis_datatypes[i], sensor->expected_datatype)) | ||
141 | { | ||
142 | is_numeric = GNUNET_YES; | ||
143 | break; | ||
144 | } | ||
145 | } | ||
146 | if (GNUNET_NO == is_numeric) | ||
147 | return GNUNET_YES; | ||
148 | sensor_model = GNUNET_new(struct SensorModel); | ||
149 | sensor_model->wc = GNUNET_PEERSTORE_watch(peerstore, | ||
150 | "sensor", NULL, sensor->name, | ||
151 | &sensor_watcher, sensor_model); | ||
152 | GNUNET_CONTAINER_multihashmap_put(sensor_models, key, | ||
153 | sensor_model, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
154 | return GNUNET_YES; | ||
155 | } | ||
156 | |||
157 | /* | ||
158 | * TODO: document | ||
159 | * | ||
160 | * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise | ||
161 | */ | ||
162 | int | ||
163 | SENSOR_analysis_start(const struct GNUNET_CONFIGURATION_Handle *c, | ||
164 | struct GNUNET_CONTAINER_MultiHashMap *sensors_mhm) | ||
165 | { | ||
166 | char *model_name; | ||
167 | |||
168 | cfg = c; | ||
169 | if (GNUNET_OK != | ||
170 | GNUNET_CONFIGURATION_get_value_string (cfg, "sensor-analysis", "MODEL", | ||
171 | &model_name)) | ||
172 | { | ||
173 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Analysis model not defined in configuration.\n")); | ||
174 | return GNUNET_SYSERR; | ||
175 | } | ||
176 | GNUNET_asprintf (&model_lib_name, "libgnunet_plugin_sensor_model_%s", model_name); | ||
177 | model = GNUNET_PLUGIN_load(model_lib_name, (void *) cfg); | ||
178 | GNUNET_free(model_name); | ||
179 | if(NULL == model) | ||
180 | { | ||
181 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not load analysis model `%s'.\n"), model_lib_name); | ||
182 | return GNUNET_SYSERR; | ||
183 | } | ||
184 | sensors = sensors_mhm; | ||
185 | if (NULL == sensors) | ||
186 | { | ||
187 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Tried to start analysis before loading sensors.\n")); | ||
188 | SENSOR_analysis_stop(); | ||
189 | return GNUNET_SYSERR; | ||
190 | } | ||
191 | peerstore = GNUNET_PEERSTORE_connect(cfg); | ||
192 | if (NULL == peerstore) | ||
193 | { | ||
194 | LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not connect to peerstore service.\n")); | ||
195 | SENSOR_analysis_stop(); | ||
196 | return GNUNET_SYSERR; | ||
197 | } | ||
198 | sensor_models = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO); | ||
199 | GNUNET_CONTAINER_multihashmap_iterate(sensors, &init_sensor_model, NULL); | ||
200 | |||
201 | return GNUNET_OK; | ||
202 | } | ||
203 | |||
204 | /* 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 | |||
@@ -36,158 +36,6 @@ | |||
36 | #define MIN_INTERVAL 30 | 36 | #define MIN_INTERVAL 30 |
37 | 37 | ||
38 | /** | 38 | /** |
39 | * Structure containing sensor definition | ||
40 | */ | ||
41 | struct SensorInfo | ||
42 | { | ||
43 | |||
44 | /** | ||
45 | * The configuration handle | ||
46 | * carrying sensor information | ||
47 | */ | ||
48 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
49 | |||
50 | /* | ||
51 | * Sensor name | ||
52 | */ | ||
53 | char *name; | ||
54 | |||
55 | /* | ||
56 | * Path to definition file | ||
57 | */ | ||
58 | char *def_file; | ||
59 | |||
60 | /* | ||
61 | * First part of version number | ||
62 | */ | ||
63 | uint16_t version_major; | ||
64 | |||
65 | /* | ||
66 | * Second part of version number | ||
67 | */ | ||
68 | uint16_t version_minor; | ||
69 | |||
70 | /* | ||
71 | * Sensor description | ||
72 | */ | ||
73 | char *description; | ||
74 | |||
75 | /* | ||
76 | * Sensor currently enabled | ||
77 | */ | ||
78 | int enabled; | ||
79 | |||
80 | /* | ||
81 | * Category under which the sensor falls (e.g. tcp, datastore) | ||
82 | */ | ||
83 | char *category; | ||
84 | |||
85 | /* | ||
86 | * When does the sensor become active | ||
87 | */ | ||
88 | struct GNUNET_TIME_Absolute *start_time; | ||
89 | |||
90 | /* | ||
91 | * When does the sensor expire | ||
92 | */ | ||
93 | struct GNUNET_TIME_Absolute *end_time; | ||
94 | |||
95 | /* | ||
96 | * Time interval to collect sensor information (e.g. every 1 min) | ||
97 | */ | ||
98 | struct GNUNET_TIME_Relative interval; | ||
99 | |||
100 | /* | ||
101 | * Lifetime of an information sample after which it is deleted from storage | ||
102 | * If not supplied, will default to the interval value | ||
103 | */ | ||
104 | struct GNUNET_TIME_Relative lifetime; | ||
105 | |||
106 | /* | ||
107 | * A set of required peer capabilities for the sensor to collect meaningful information (e.g. ipv6) | ||
108 | */ | ||
109 | char *capabilities; | ||
110 | |||
111 | /* | ||
112 | * Either "gnunet-statistics" or external "process" | ||
113 | */ | ||
114 | char *source; | ||
115 | |||
116 | /* | ||
117 | * Name of the GNUnet service that is the source for the gnunet-statistics entry | ||
118 | */ | ||
119 | char *gnunet_stat_service; | ||
120 | |||
121 | /* | ||
122 | * Name of the gnunet-statistics entry | ||
123 | */ | ||
124 | char *gnunet_stat_name; | ||
125 | |||
126 | /** | ||
127 | * Handle to statistics get request (OR GNUNET_SCHEDULER_NO_TASK) | ||
128 | */ | ||
129 | struct GNUNET_STATISTICS_GetHandle *gnunet_stat_get_handle; | ||
130 | |||
131 | /* | ||
132 | * Name of the external process to be executed | ||
133 | */ | ||
134 | char *ext_process; | ||
135 | |||
136 | /* | ||
137 | * Arguments to be passed to the external process | ||
138 | */ | ||
139 | char *ext_args; | ||
140 | |||
141 | /* | ||
142 | * Handle to the external process | ||
143 | */ | ||
144 | struct GNUNET_OS_CommandHandle *ext_cmd; | ||
145 | |||
146 | /* | ||
147 | * Did we already receive a value | ||
148 | * from the currently running external | ||
149 | * proccess ? #GNUNET_YES / #GNUNET_NO | ||
150 | */ | ||
151 | int ext_cmd_value_received; | ||
152 | |||
153 | /* | ||
154 | * The output datatype to be expected | ||
155 | */ | ||
156 | char *expected_datatype; | ||
157 | |||
158 | /* | ||
159 | * Peer-identity of peer running collection point | ||
160 | */ | ||
161 | struct GNUNET_PeerIdentity *collection_point; | ||
162 | |||
163 | /* | ||
164 | * Time interval to send sensor information to collection point (e.g. every 30 mins) | ||
165 | */ | ||
166 | struct GNUNET_TIME_Relative *collection_interval; | ||
167 | |||
168 | /* | ||
169 | * Flag specifying if value is to be communicated to the p2p network | ||
170 | */ | ||
171 | int p2p_report; | ||
172 | |||
173 | /* | ||
174 | * Time interval to communicate value to the p2p network | ||
175 | */ | ||
176 | struct GNUNET_TIME_Relative *p2p_interval; | ||
177 | |||
178 | /* | ||
179 | * Execution task (OR GNUNET_SCHEDULER_NO_TASK) | ||
180 | */ | ||
181 | GNUNET_SCHEDULER_TaskIdentifier execution_task; | ||
182 | |||
183 | /* | ||
184 | * Is the sensor being executed | ||
185 | */ | ||
186 | int running; | ||
187 | |||
188 | }; | ||
189 | |||
190 | /** | ||
191 | * Our configuration. | 39 | * Our configuration. |
192 | */ | 40 | */ |
193 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | 41 | static const struct GNUNET_CONFIGURATION_Handle *cfg; |
@@ -195,7 +43,7 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg; | |||
195 | /** | 43 | /** |
196 | * Hashmap of loaded sensor definitions | 44 | * Hashmap of loaded sensor definitions |
197 | */ | 45 | */ |
198 | struct GNUNET_CONTAINER_MultiHashMap *sensors; | 46 | static struct GNUNET_CONTAINER_MultiHashMap *sensors; |
199 | 47 | ||
200 | /** | 48 | /** |
201 | * Supported sources of sensor information | 49 | * 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 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2012, 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file sensor/gnunet_sensor_model_plugin.h | ||
23 | * @brief plugin API for sensor analysis models | ||
24 | * @author Omar Tarabai | ||
25 | */ | ||
26 | #ifndef GNUNET_SENSOR_MODEL_PLUGIN_H | ||
27 | #define GNUNET_SENSOR_MODEL_PLUGIN_H | ||
28 | |||
29 | #include "gnunet_util_lib.h" | ||
30 | |||
31 | #ifdef __cplusplus | ||
32 | extern "C" | ||
33 | { | ||
34 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
35 | } | ||
36 | #endif | ||
37 | #endif | ||
38 | |||
39 | |||
40 | /** | ||
41 | * API for a sensor analysis model | ||
42 | */ | ||
43 | struct GNUNET_SENSOR_ModelFunctions | ||
44 | { | ||
45 | |||
46 | /** | ||
47 | * Closure to pass to all plugin functions. | ||
48 | */ | ||
49 | void *cls; | ||
50 | |||
51 | }; | ||
52 | |||
53 | |||
54 | #if 0 /* keep Emacsens' auto-indent happy */ | ||
55 | { | ||
56 | #endif | ||
57 | #ifdef __cplusplus | ||
58 | } | ||
59 | #endif | ||
60 | |||
61 | /* end of gnunet_sensor_model_plugin.h */ | ||
62 | #endif | ||