aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmar Tarabai <tarabai@devegypt.com>2014-07-02 12:51:48 +0000
committerOmar Tarabai <tarabai@devegypt.com>2014-07-02 12:51:48 +0000
commit0255e3026f4553ecfe5098d7f96c05ffca982a52 (patch)
tree0c27cee84382d1f568e64f84d03b4627b4d3e617
parent13c92b1966accce020a6a5d3f90b252c55a4ad2f (diff)
downloadgnunet-0255e3026f4553ecfe5098d7f96c05ffca982a52.tar.gz
gnunet-0255e3026f4553ecfe5098d7f96c05ffca982a52.zip
sensor: completed reporting to collection point
-rw-r--r--src/include/gnunet_sensor_service.h39
-rw-r--r--src/include/gnunet_sensordashboard_service.h48
-rw-r--r--src/sensor/gnunet-service-sensor-reporting.c185
-rw-r--r--src/sensordashboard/gnunet-service-sensordashboard.c1
-rw-r--r--src/sensordashboard/sensordashboard.h30
5 files changed, 263 insertions, 40 deletions
diff --git a/src/include/gnunet_sensor_service.h b/src/include/gnunet_sensor_service.h
index 19dcbbb75..1bfed345a 100644
--- a/src/include/gnunet_sensor_service.h
+++ b/src/include/gnunet_sensor_service.h
@@ -223,6 +223,45 @@ struct SensorInfoShort
223 223
224}; 224};
225 225
226GNUNET_NETWORK_STRUCT_BEGIN
227
228/**
229 * Used to communicate sensor readings to
230 * collection points (SENSORDASHBAORD service)
231 */
232struct GNUNET_SENSOR_Reading
233{
234
235 /**
236 * Size of the sensor name value, allocated
237 * at position 0 after this struct
238 */
239 size_t sensorname_size;
240
241 /**
242 * First part of sensor version number
243 */
244 uint16_t sensorversion_major;
245
246 /**
247 * Second part of sensor version number
248 */
249 uint16_t sensorversion_minor;
250
251 /**
252 * Timestamp of recorded reading
253 */
254 uint64_t timestamp;
255
256 /**
257 * Size of reading value, allocation
258 * at poistion 1 after this struct
259 */
260 size_t value_size;
261
262};
263GNUNET_NETWORK_STRUCT_END
264
226/** 265/**
227 * Type of an iterator over sensor definitions. 266 * Type of an iterator over sensor definitions.
228 * 267 *
diff --git a/src/include/gnunet_sensordashboard_service.h b/src/include/gnunet_sensordashboard_service.h
new file mode 100644
index 000000000..b3d94e4df
--- /dev/null
+++ b/src/include/gnunet_sensordashboard_service.h
@@ -0,0 +1,48 @@
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 include/gnunet_sensordashboard_service.h
23 * @brief API to the sensordashboard service
24 * @author Omar Tarabai
25 */
26#ifndef GNUNET_SENSORDASHBOARD_SERVICE_H
27#define GNUNET_SENSORDASHBOARD_SERVICE_H
28
29#include "platform.h"
30#include "gnunet_util_lib.h"
31
32#ifdef __cplusplus
33extern "C"
34{
35#if 0 /* keep Emacsens' auto-indent happy */
36}
37#endif
38#endif
39
40
41#if 0 /* keep Emacsens' auto-indent happy */
42{
43#endif
44#ifdef __cplusplus
45}
46#endif
47
48#endif
diff --git a/src/sensor/gnunet-service-sensor-reporting.c b/src/sensor/gnunet-service-sensor-reporting.c
index 952d7b842..6c8a05490 100644
--- a/src/sensor/gnunet-service-sensor-reporting.c
+++ b/src/sensor/gnunet-service-sensor-reporting.c
@@ -75,11 +75,9 @@ struct ReportingContext
75 size_t last_value_size; 75 size_t last_value_size;
76 76
77 /** 77 /**
78 * Incremented with every lock request 78 * Timestamp of last value reading
79 * (e.g. to send last value).
80 * Change @last_value only when @value_lock = 0
81 */ 79 */
82 int value_lock; 80 uint64_t timestamp;
83 81
84}; 82};
85 83
@@ -115,6 +113,22 @@ struct CadetChannelContext
115 */ 113 */
116 int sending; 114 int sending;
117 115
116 /**
117 * Pointer to a pending message to be sent over the channel
118 */
119 void *pending_msg;
120
121 /**
122 * Size of @pending_msg
123 */
124 size_t pending_msg_size;
125
126 /**
127 * Handle to CADET tranmission request in case we are sending
128 * (sending == GNUNET_YES)
129 */
130 struct GNUNET_CADET_TransmitHandle *th;
131
118}; 132};
119 133
120/** 134/**
@@ -183,14 +197,44 @@ destroy_reporting_context (struct ReportingContext *rc)
183} 197}
184 198
185/** 199/**
200 * Destroy a CADET channel context struct
201 */
202static void
203destroy_cadet_channel_context (struct CadetChannelContext *cc)
204{
205 if (NULL != cc->th)
206 {
207 GNUNET_CADET_notify_transmit_ready_cancel (cc->th);
208 cc->th = NULL;
209 }
210 if (NULL != cc->pending_msg)
211 {
212 GNUNET_free (cc->pending_msg);
213 cc->pending_msg = NULL;
214 }
215 if (NULL != cc->c)
216 {
217 GNUNET_CADET_channel_destroy (cc->c);
218 cc->c = NULL;
219 }
220 GNUNET_free (cc);
221}
222
223/**
186 * Stop sensor reporting module 224 * Stop sensor reporting module
187 */ 225 */
188void SENSOR_reporting_stop () 226void SENSOR_reporting_stop ()
189{ 227{
190 struct ReportingContext *rc; 228 struct ReportingContext *rc;
229 struct CadetChannelContext *cc;
191 230
192 LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor reporting module.\n"); 231 LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor reporting module.\n");
193 /* TODO: destroy cadet channels */ 232 while (NULL != cc_head)
233 {
234 cc = cc_head;
235 GNUNET_CONTAINER_DLL_remove (cc_head, cc_tail, cc);
236 destroy_cadet_channel_context (cc);
237 }
194 while (NULL != rc_head) 238 while (NULL != rc_head)
195 { 239 {
196 rc = rc_head; 240 rc = rc_head;
@@ -216,7 +260,7 @@ void SENSOR_reporting_stop ()
216 * @param pid Peer Identity 260 * @param pid Peer Identity
217 * @return Context of established cadet channel 261 * @return Context of established cadet channel
218 */ 262 */
219struct CadetChannelContext * 263static struct CadetChannelContext *
220get_cadet_channel (struct GNUNET_PeerIdentity pid) 264get_cadet_channel (struct GNUNET_PeerIdentity pid)
221{ 265{
222 struct CadetChannelContext *cc; 266 struct CadetChannelContext *cc;
@@ -241,6 +285,40 @@ get_cadet_channel (struct GNUNET_PeerIdentity pid)
241} 285}
242 286
243/** 287/**
288 * Construct a reading message ready to be sent over CADET channel
289 *
290 * @param rc reporting context to read data from
291 * @param msg used to return the created message structure
292 * @return size of created message
293 */
294static size_t
295construct_reading_message (struct ReportingContext *rc,
296 struct GNUNET_SENSOR_Reading **msg)
297{
298 struct GNUNET_SENSOR_Reading *ret;
299 size_t sensorname_size;
300 size_t total_size;
301 void *dummy;
302
303 sensorname_size = strlen (rc->sensor->name) + 1;
304 total_size = sizeof(struct GNUNET_SENSOR_Reading) +
305 sensorname_size +
306 rc->last_value_size;
307 ret = GNUNET_malloc (total_size);
308 ret->sensorname_size = sensorname_size;
309 ret->sensorversion_major = rc->sensor->version_major;
310 ret->sensorversion_minor = rc->sensor->version_minor;
311 ret->timestamp = rc->timestamp;
312 ret->value_size = rc->last_value_size;
313 dummy = &ret[1];
314 memcpy (dummy, rc->sensor->name, sensorname_size);
315 dummy += sensorname_size;
316 memcpy (dummy, rc->last_value, rc->last_value_size);
317 *msg = ret;
318 return total_size;
319}
320
321/**
244 * Function called to notify a client about the connection begin ready 322 * Function called to notify a client about the connection begin ready
245 * to queue more data. @a buf will be NULL and @a size zero if the 323 * to queue more data. @a buf will be NULL and @a size zero if the
246 * connection was closed for writing in the meantime. 324 * connection was closed for writing in the meantime.
@@ -250,13 +328,27 @@ get_cadet_channel (struct GNUNET_PeerIdentity pid)
250 * @param buf where the callee should write the message 328 * @param buf where the callee should write the message
251 * @return number of bytes written to @a buf 329 * @return number of bytes written to @a buf
252 */ 330 */
253size_t 331static size_t
254do_report_collection_point (void *cls, size_t size, void *buf) 332do_report_collection_point (void *cls, size_t size, void *buf)
255{ 333{
256 /* TODO: check error from CADET */ 334 struct CadetChannelContext *cc = cls;
257 /* TODO: do transfer */ 335 size_t written = 0;
258 /* TODO: cc->sending, rc->value_lock */ 336
259 return 0; 337 LOG (GNUNET_ERROR_TYPE_DEBUG, "Copying to CADET transmit buffer.\n");
338 cc->sending = GNUNET_NO;
339 if (NULL == buf || size != cc->pending_msg_size)
340 {
341 LOG (GNUNET_ERROR_TYPE_WARNING,
342 "CADET failed to transmit message to collection point, discarding.");
343 }
344 else
345 {
346 memcpy (buf, cc->pending_msg, cc->pending_msg_size);
347 written = cc->pending_msg_size;
348 }
349 GNUNET_free (cc->pending_msg);
350 cc->pending_msg_size = 0;
351 return written;
260} 352}
261 353
262/** 354/**
@@ -265,38 +357,49 @@ do_report_collection_point (void *cls, size_t size, void *buf)
265 * @param cls closure, a 'struct CollectionReportingContext *' 357 * @param cls closure, a 'struct CollectionReportingContext *'
266 * @param tc unused 358 * @param tc unused
267 */ 359 */
268void report_collection_point 360static void report_collection_point
269(void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc) 361(void *cls, const struct GNUNET_SCHEDULER_TaskContext* tc)
270{ 362{
271 struct ReportingContext *rc = cls; 363 struct ReportingContext *rc = cls;
272 struct SensorInfo *sensor = rc->sensor; 364 struct SensorInfo *sensor = rc->sensor;
273 struct CadetChannelContext *cc; 365 struct CadetChannelContext *cc;
366 struct GNUNET_SENSOR_Reading *msg;
367 size_t msg_size;
274 368
275 rc->cp_task = GNUNET_SCHEDULER_NO_TASK; 369 rc->cp_task = GNUNET_SCHEDULER_NO_TASK;
370 if (0 == rc->last_value_size) /* Did not receive a sensor value yet */
371 {
372 LOG (GNUNET_ERROR_TYPE_WARNING, "Did not receive a value from `%s' "
373 "to report yet.\n", rc->sensor->name);
374 rc->cp_task = GNUNET_SCHEDULER_add_delayed (sensor->collection_interval,
375 &report_collection_point, rc);
376 return;
377 }
378 LOG (GNUNET_ERROR_TYPE_DEBUG, "Now trying to report last seen value of `%s' "
379 "to collection point.\n", rc->sensor->name);
276 GNUNET_assert (NULL != sensor->collection_point); 380 GNUNET_assert (NULL != sensor->collection_point);
277 cc = get_cadet_channel (*sensor->collection_point); 381 cc = get_cadet_channel (*sensor->collection_point);
278 if (GNUNET_YES == cc->sending) 382 if (GNUNET_YES == cc->sending)
279 { 383 {
280 LOG (GNUNET_ERROR_TYPE_DEBUG, 384 LOG (GNUNET_ERROR_TYPE_DEBUG,
281 "Cadet channel to collection point busy, trying again on next interval."); 385 "Cadet channel to collection point busy, "
282 rc->cp_task = 386 "trying again for sensor `%s' on next interval.\n", rc->sensor->name);
283 GNUNET_SCHEDULER_add_delayed (sensor->collection_interval, 387 rc->cp_task = GNUNET_SCHEDULER_add_delayed (sensor->collection_interval,
284 &report_collection_point, 388 &report_collection_point, rc);
285 rc);
286 return; 389 return;
287 } 390 }
391 msg_size = construct_reading_message (rc, &msg);
288 cc->sending = GNUNET_YES; 392 cc->sending = GNUNET_YES;
289 rc->value_lock ++; 393 cc->pending_msg = msg;
290 /* TODO: construct message */ 394 cc->pending_msg_size = msg_size;
291 /* TODO: if constructed message is added to cc, no need for rc->value_lock */ 395 cc->th = GNUNET_CADET_notify_transmit_ready (cc->c,
292 GNUNET_CADET_notify_transmit_ready (cc->c,
293 GNUNET_YES, 396 GNUNET_YES,
294 sensor->collection_interval, 397 sensor->collection_interval,
295 rc->last_value_size, /* FIXME: size of constructed message */ 398 msg_size,
296 &do_report_collection_point, 399 &do_report_collection_point,
297 rc); 400 cc);
298 /* TODO */ 401 rc->cp_task = GNUNET_SCHEDULER_add_delayed (sensor->collection_interval,
299 /* TODO: reschedule reporting */ 402 &report_collection_point, rc);
300} 403}
301 404
302/* 405/*
@@ -309,16 +412,10 @@ sensor_watch_cb (void *cls,
309{ 412{
310 struct ReportingContext *rc = cls; 413 struct ReportingContext *rc = cls;
311 414
415 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a sensor `%s' watch value, "
416 "updating notification last_value.\n", rc->sensor->name);
312 if (NULL != emsg) 417 if (NULL != emsg)
313 return GNUNET_YES; 418 return GNUNET_YES;
314 if (rc->value_lock > 0)
315 {
316 LOG (GNUNET_ERROR_TYPE_DEBUG,
317 "Did not update reporting context of sensor `%s'"
318 " because value is locked for sending.",
319 rc->sensor->name);
320 return GNUNET_YES;
321 }
322 if (NULL != rc->last_value) 419 if (NULL != rc->last_value)
323 { 420 {
324 GNUNET_free (rc->last_value); 421 GNUNET_free (rc->last_value);
@@ -327,6 +424,7 @@ sensor_watch_cb (void *cls,
327 rc->last_value = GNUNET_malloc(record->value_size); 424 rc->last_value = GNUNET_malloc(record->value_size);
328 memcpy (rc->last_value, record->value, record->value_size); 425 memcpy (rc->last_value, record->value, record->value_size);
329 rc->last_value_size = record->value_size; 426 rc->last_value_size = record->value_size;
427 rc->timestamp = GNUNET_TIME_absolute_get().abs_value_us;
330 return GNUNET_YES; 428 return GNUNET_YES;
331} 429}
332 430
@@ -354,7 +452,6 @@ init_sensor_reporting (void *cls,
354 rc->sensor = sensor; 452 rc->sensor = sensor;
355 rc->last_value = NULL; 453 rc->last_value = NULL;
356 rc->last_value_size = 0; 454 rc->last_value_size = 0;
357 rc->value_lock = 0;
358 rc->wc = GNUNET_PEERSTORE_watch(peerstore, 455 rc->wc = GNUNET_PEERSTORE_watch(peerstore,
359 "sensor", 456 "sensor",
360 &mypeerid, 457 &mypeerid,
@@ -364,9 +461,11 @@ init_sensor_reporting (void *cls,
364 if (NULL != sensor->collection_point) 461 if (NULL != sensor->collection_point)
365 { 462 {
366 LOG (GNUNET_ERROR_TYPE_INFO, 463 LOG (GNUNET_ERROR_TYPE_INFO,
367 "Will start reporting sensor `%s' values to collection point `%s' every %s.\n", 464 "Will start reporting sensor `%s' values to "
465 "collection point `%s' every %s.\n",
368 sensor->name, GNUNET_i2s_full(sensor->collection_point), 466 sensor->name, GNUNET_i2s_full(sensor->collection_point),
369 GNUNET_STRINGS_relative_time_to_string(sensor->collection_interval, GNUNET_YES)); 467 GNUNET_STRINGS_relative_time_to_string(sensor->collection_interval,
468 GNUNET_YES));
370 rc->cp_task = 469 rc->cp_task =
371 GNUNET_SCHEDULER_add_delayed (sensor->collection_interval, 470 GNUNET_SCHEDULER_add_delayed (sensor->collection_interval,
372 &report_collection_point, 471 &report_collection_point,
@@ -377,7 +476,8 @@ init_sensor_reporting (void *cls,
377 LOG (GNUNET_ERROR_TYPE_INFO, 476 LOG (GNUNET_ERROR_TYPE_INFO,
378 "Will start reporting sensor `%s' values to p2p network every %s.\n", 477 "Will start reporting sensor `%s' values to p2p network every %s.\n",
379 sensor->name, 478 sensor->name,
380 GNUNET_STRINGS_relative_time_to_string(sensor->p2p_interval, GNUNET_YES)); 479 GNUNET_STRINGS_relative_time_to_string(sensor->p2p_interval,
480 GNUNET_YES));
381 } 481 }
382 GNUNET_CONTAINER_DLL_insert (rc_head, rc_tail, rc); 482 GNUNET_CONTAINER_DLL_insert (rc_head, rc_tail, rc);
383 return GNUNET_YES; 483 return GNUNET_YES;
@@ -400,8 +500,12 @@ static void cadet_channel_destroyed (void *cls,
400{ 500{
401 struct CadetChannelContext *cc = channel_ctx; 501 struct CadetChannelContext *cc = channel_ctx;
402 502
503 LOG (GNUNET_ERROR_TYPE_DEBUG,
504 "Received a `channel destroyed' notification from CADET, "
505 "cleaning up.\n");
403 GNUNET_CONTAINER_DLL_remove (cc_head, cc_tail, cc); 506 GNUNET_CONTAINER_DLL_remove (cc_head, cc_tail, cc);
404 GNUNET_free (cc); 507 cc->c = NULL;
508 destroy_cadet_channel_context (cc);
405} 509}
406 510
407/** 511/**
@@ -419,10 +523,9 @@ SENSOR_reporting_start (const struct GNUNET_CONFIGURATION_Handle *c,
419 {NULL, 0, 0} 523 {NULL, 0, 0}
420 }; 524 };
421 525
526 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting sensor reporting module.\n");
422 GNUNET_assert(NULL != sensors); 527 GNUNET_assert(NULL != sensors);
423 cfg = c; 528 cfg = c;
424 GNUNET_CRYPTO_get_peer_identity(cfg, &mypeerid);
425 GNUNET_CONTAINER_multihashmap_iterate(sensors, &init_sensor_reporting, NULL);
426 peerstore = GNUNET_PEERSTORE_connect(cfg); 529 peerstore = GNUNET_PEERSTORE_connect(cfg);
427 if (NULL == peerstore) 530 if (NULL == peerstore)
428 { 531 {
@@ -444,6 +547,8 @@ SENSOR_reporting_start (const struct GNUNET_CONFIGURATION_Handle *c,
444 SENSOR_reporting_stop (); 547 SENSOR_reporting_stop ();
445 return GNUNET_SYSERR; 548 return GNUNET_SYSERR;
446 } 549 }
550 GNUNET_CRYPTO_get_peer_identity(cfg, &mypeerid);
551 GNUNET_CONTAINER_multihashmap_iterate(sensors, &init_sensor_reporting, NULL);
447 552
448 return GNUNET_OK; 553 return GNUNET_OK;
449} 554}
diff --git a/src/sensordashboard/gnunet-service-sensordashboard.c b/src/sensordashboard/gnunet-service-sensordashboard.c
index b3fbf5d9d..5f451844e 100644
--- a/src/sensordashboard/gnunet-service-sensordashboard.c
+++ b/src/sensordashboard/gnunet-service-sensordashboard.c
@@ -26,6 +26,7 @@
26#include "platform.h" 26#include "platform.h"
27#include "gnunet_util_lib.h" 27#include "gnunet_util_lib.h"
28#include "gnunet_applications.h" 28#include "gnunet_applications.h"
29#include "sensordashboard.h"
29#include "gnunet_cadet_service.h" 30#include "gnunet_cadet_service.h"
30 31
31static struct GNUNET_CADET_Handle *cadet; 32static struct GNUNET_CADET_Handle *cadet;
diff --git a/src/sensordashboard/sensordashboard.h b/src/sensordashboard/sensordashboard.h
new file mode 100644
index 000000000..c7758e26e
--- /dev/null
+++ b/src/sensordashboard/sensordashboard.h
@@ -0,0 +1,30 @@
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 * @file sensordashboard/sensordashboard.h
22 * @brief IPC messages and private service declarations
23 * @author Omar Tarabai
24 */
25
26#include "gnunet_sensordashboard_service.h"
27
28GNUNET_NETWORK_STRUCT_BEGIN
29
30GNUNET_NETWORK_STRUCT_END