diff options
author | Omar Tarabai <tarabai@devegypt.com> | 2014-07-24 14:27:25 +0000 |
---|---|---|
committer | Omar Tarabai <tarabai@devegypt.com> | 2014-07-24 14:27:25 +0000 |
commit | eaeb4e371a1ad8d3a7b3b85ad54e99afdfc106d9 (patch) | |
tree | 025d5a6b999d94d586efdfd2e14d89d20c41bcfb /src/sensordashboard | |
parent | 2b1af57c696d62b6be7b179eabdb4b4a0c0d4b6d (diff) | |
download | gnunet-eaeb4e371a1ad8d3a7b3b85ad54e99afdfc106d9.tar.gz gnunet-eaeb4e371a1ad8d3a7b3b85ad54e99afdfc106d9.zip |
sensor: minor fixes
Diffstat (limited to 'src/sensordashboard')
-rw-r--r-- | src/sensordashboard/gnunet-service-sensordashboard.c | 106 |
1 files changed, 96 insertions, 10 deletions
diff --git a/src/sensordashboard/gnunet-service-sensordashboard.c b/src/sensordashboard/gnunet-service-sensordashboard.c index 26cc421fc..5cec7e636 100644 --- a/src/sensordashboard/gnunet-service-sensordashboard.c +++ b/src/sensordashboard/gnunet-service-sensordashboard.c | |||
@@ -59,6 +59,11 @@ struct ClientPeerContext | |||
59 | */ | 59 | */ |
60 | struct GNUNET_CADET_Channel *ch; | 60 | struct GNUNET_CADET_Channel *ch; |
61 | 61 | ||
62 | /** | ||
63 | * Are we in the process of destroying this context? | ||
64 | */ | ||
65 | int destroying; | ||
66 | |||
62 | }; | 67 | }; |
63 | 68 | ||
64 | 69 | ||
@@ -101,6 +106,7 @@ static struct ClientPeerContext *cp_tail; | |||
101 | static void | 106 | static void |
102 | destroy_clientpeer (struct ClientPeerContext *cp) | 107 | destroy_clientpeer (struct ClientPeerContext *cp) |
103 | { | 108 | { |
109 | cp->destroying = GNUNET_YES; | ||
104 | if (NULL != cp->ch) | 110 | if (NULL != cp->ch) |
105 | { | 111 | { |
106 | GNUNET_CADET_channel_destroy (cp->ch); | 112 | GNUNET_CADET_channel_destroy (cp->ch); |
@@ -109,6 +115,7 @@ destroy_clientpeer (struct ClientPeerContext *cp) | |||
109 | GNUNET_free (cp); | 115 | GNUNET_free (cp); |
110 | } | 116 | } |
111 | 117 | ||
118 | |||
112 | /** | 119 | /** |
113 | * Task run during shutdown. | 120 | * Task run during shutdown. |
114 | * | 121 | * |
@@ -138,9 +145,10 @@ cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
138 | peerstore = NULL; | 145 | peerstore = NULL; |
139 | } | 146 | } |
140 | GNUNET_SENSOR_destroy_sensors (sensors); | 147 | GNUNET_SENSOR_destroy_sensors (sensors); |
141 | GNUNET_SCHEDULER_shutdown(); | 148 | GNUNET_SCHEDULER_shutdown (); |
142 | } | 149 | } |
143 | 150 | ||
151 | |||
144 | /** | 152 | /** |
145 | * Function called whenever a channel is destroyed. Should clean up | 153 | * Function called whenever a channel is destroyed. Should clean up |
146 | * any associated state. | 154 | * any associated state. |
@@ -159,11 +167,14 @@ cadet_channel_destroyed (void *cls, | |||
159 | { | 167 | { |
160 | struct ClientPeerContext *cp = channel_ctx; | 168 | struct ClientPeerContext *cp = channel_ctx; |
161 | 169 | ||
170 | if (GNUNET_YES == cp->destroying) | ||
171 | return; | ||
162 | cp->ch = NULL; | 172 | cp->ch = NULL; |
163 | GNUNET_CONTAINER_DLL_remove (cp_head, cp_tail, cp); | 173 | GNUNET_CONTAINER_DLL_remove (cp_head, cp_tail, cp); |
164 | destroy_clientpeer (cp); | 174 | destroy_clientpeer (cp); |
165 | } | 175 | } |
166 | 176 | ||
177 | |||
167 | /** | 178 | /** |
168 | * Method called whenever another peer has added us to a channel | 179 | * Method called whenever another peer has added us to a channel |
169 | * the other peer initiated. | 180 | * the other peer initiated. |
@@ -193,10 +204,85 @@ cadet_channel_created (void *cls, | |||
193 | cp = GNUNET_new (struct ClientPeerContext); | 204 | cp = GNUNET_new (struct ClientPeerContext); |
194 | cp->peerid = *initiator; | 205 | cp->peerid = *initiator; |
195 | cp->ch = channel; | 206 | cp->ch = channel; |
207 | cp->destroying = GNUNET_NO; | ||
196 | GNUNET_CONTAINER_DLL_insert (cp_head, cp_tail, cp); | 208 | GNUNET_CONTAINER_DLL_insert (cp_head, cp_tail, cp); |
197 | return cp; | 209 | return cp; |
198 | } | 210 | } |
199 | 211 | ||
212 | |||
213 | /** | ||
214 | * Parses a sensor reading message struct | ||
215 | * | ||
216 | * @param msg message header received | ||
217 | * @param sensors multihashmap of loaded sensors | ||
218 | * @return sensor reading struct or NULL if error | ||
219 | */ | ||
220 | static struct GNUNET_SENSOR_Reading * | ||
221 | parse_reading_message (const struct GNUNET_MessageHeader *msg, | ||
222 | struct GNUNET_CONTAINER_MultiHashMap *sensors) | ||
223 | { | ||
224 | uint16_t msg_size; | ||
225 | struct GNUNET_SENSOR_ReadingMessage *rm; | ||
226 | uint16_t sensorname_size; | ||
227 | uint16_t value_size; | ||
228 | void *dummy; | ||
229 | char *sensorname; | ||
230 | struct GNUNET_HashCode key; | ||
231 | struct GNUNET_SENSOR_SensorInfo *sensor; | ||
232 | struct GNUNET_SENSOR_Reading *reading; | ||
233 | |||
234 | msg_size = ntohs (msg->size); | ||
235 | if (msg_size < sizeof (struct GNUNET_SENSOR_ReadingMessage)) | ||
236 | { | ||
237 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Invalid reading message size.\n"); | ||
238 | return NULL; | ||
239 | } | ||
240 | rm = (struct GNUNET_SENSOR_ReadingMessage *)msg; | ||
241 | sensorname_size = ntohs (rm->sensorname_size); | ||
242 | value_size = ntohs (rm->value_size); | ||
243 | if ((sizeof (struct GNUNET_SENSOR_ReadingMessage) | ||
244 | + sensorname_size + value_size) != msg_size) | ||
245 | { | ||
246 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Invalid reading message size.\n"); | ||
247 | return NULL; | ||
248 | } | ||
249 | dummy = &rm[1]; | ||
250 | sensorname = GNUNET_malloc (sensorname_size); | ||
251 | memcpy (sensorname, dummy, sensorname_size); | ||
252 | GNUNET_CRYPTO_hash(sensorname, sensorname_size, &key); | ||
253 | GNUNET_free (sensorname); | ||
254 | sensor = GNUNET_CONTAINER_multihashmap_get (sensors, &key); | ||
255 | if (NULL == sensor) | ||
256 | { | ||
257 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
258 | "Unknown sensor name in reading message.\n"); | ||
259 | return NULL; | ||
260 | } | ||
261 | if ((sensor->version_minor != ntohs (rm->sensorversion_minor)) || | ||
262 | (sensor->version_major != ntohs (rm->sensorversion_major))) | ||
263 | { | ||
264 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
265 | "Sensor version mismatch in reading message.\n"); | ||
266 | return NULL; | ||
267 | } | ||
268 | if (0 == strcmp (sensor->expected_datatype, "numeric") && | ||
269 | sizeof (double) != value_size) | ||
270 | { | ||
271 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
272 | "Invalid value size for a numerical sensor.\n"); | ||
273 | return NULL; | ||
274 | } | ||
275 | reading = GNUNET_new (struct GNUNET_SENSOR_Reading); | ||
276 | reading->sensor = sensor; | ||
277 | reading->timestamp = GNUNET_be64toh (rm->timestamp); | ||
278 | reading->value_size = value_size; | ||
279 | reading->value = GNUNET_malloc (value_size); | ||
280 | dummy += sensorname_size; | ||
281 | memcpy (reading->value, dummy, value_size); | ||
282 | return reading; | ||
283 | } | ||
284 | |||
285 | |||
200 | /** | 286 | /** |
201 | * Called with any sensor reading messages received from CADET. | 287 | * Called with any sensor reading messages received from CADET. |
202 | * | 288 | * |
@@ -217,15 +303,15 @@ handle_sensor_reading (void *cls, | |||
217 | void **channel_ctx, | 303 | void **channel_ctx, |
218 | const struct GNUNET_MessageHeader *message) | 304 | const struct GNUNET_MessageHeader *message) |
219 | { | 305 | { |
220 | struct GNUNET_PeerIdentity *peer = *channel_ctx; | 306 | struct ClientPeerContext *cp = *channel_ctx; |
221 | struct GNUNET_SENSOR_Reading *reading; | 307 | struct GNUNET_SENSOR_Reading *reading; |
222 | 308 | ||
223 | reading = GNUNET_SENSOR_parse_reading_message (message, sensors); | 309 | reading = parse_reading_message (message, sensors); |
224 | if (NULL == reading) | 310 | if (NULL == reading) |
225 | { | 311 | { |
226 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 312 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
227 | "Received an invalid sensor reading from peer `%s'\n", | 313 | "Received an invalid sensor reading from peer `%s'\n", |
228 | GNUNET_i2s (peer)); | 314 | GNUNET_i2s (&cp->peerid)); |
229 | return GNUNET_SYSERR; | 315 | return GNUNET_SYSERR; |
230 | } | 316 | } |
231 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 317 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
@@ -233,13 +319,13 @@ handle_sensor_reading (void *cls, | |||
233 | "# Sensor name: `%s'\n" | 319 | "# Sensor name: `%s'\n" |
234 | "# Timestamp: %" PRIu64 "\n" | 320 | "# Timestamp: %" PRIu64 "\n" |
235 | "# Value size: %" PRIu64 ".\n", | 321 | "# Value size: %" PRIu64 ".\n", |
236 | GNUNET_i2s (peer), | 322 | GNUNET_i2s (&cp->peerid), |
237 | reading->sensor->name, | 323 | reading->sensor->name, |
238 | reading->timestamp, | 324 | reading->timestamp, |
239 | reading->value_size); | 325 | reading->value_size); |
240 | GNUNET_PEERSTORE_store (peerstore, subsystem, peer, reading->sensor->name, | 326 | GNUNET_PEERSTORE_store (peerstore, subsystem, &cp->peerid, |
241 | reading->value, reading->value_size, | 327 | reading->sensor->name, reading->value, |
242 | GNUNET_TIME_UNIT_FOREVER_ABS, | 328 | reading->value_size, GNUNET_TIME_UNIT_FOREVER_ABS, |
243 | GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL); | 329 | GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL); |
244 | GNUNET_free (reading->value); | 330 | GNUNET_free (reading->value); |
245 | GNUNET_free (reading); | 331 | GNUNET_free (reading); |
@@ -286,7 +372,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, | |||
286 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 372 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
287 | { | 373 | { |
288 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { | 374 | static const struct GNUNET_SERVER_MessageHandler handlers[] = { |
289 | {NULL, NULL, 0, 0} | 375 | {NULL, NULL, 0, 0} |
290 | }; | 376 | }; |
291 | static struct GNUNET_CADET_MessageHandler cadet_handlers[] = { | 377 | static struct GNUNET_CADET_MessageHandler cadet_handlers[] = { |
292 | {&handle_sensor_reading, | 378 | {&handle_sensor_reading, |
@@ -309,7 +395,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, | |||
309 | &cadet_channel_destroyed, | 395 | &cadet_channel_destroyed, |
310 | cadet_handlers, | 396 | cadet_handlers, |
311 | cadet_ports); | 397 | cadet_ports); |
312 | if(NULL == cadet) | 398 | if (NULL == cadet) |
313 | { | 399 | { |
314 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 400 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
315 | _("Failed to connect to `%s' service.\n"), "CADET"); | 401 | _("Failed to connect to `%s' service.\n"), "CADET"); |