aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOmar Tarabai <tarabai@devegypt.com>2014-07-24 11:55:36 +0000
committerOmar Tarabai <tarabai@devegypt.com>2014-07-24 11:55:36 +0000
commit9e77b7fcdecbf00a51ff056b3041fd5fd260e2b4 (patch)
treebba38852510ccf6c9c34194d859b5d246339c55b /src
parent3c2e1baac2588ae2783f560e1aa6b459aa9ee6f5 (diff)
downloadgnunet-9e77b7fcdecbf00a51ff056b3041fd5fd260e2b4.tar.gz
gnunet-9e77b7fcdecbf00a51ff056b3041fd5fd260e2b4.zip
sensor: towards update functionality
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_protocols.h10
-rw-r--r--src/sensor/gnunet-service-sensor-update.c51
-rw-r--r--src/sensordashboard/gnunet-service-sensordashboard.c181
3 files changed, 202 insertions, 40 deletions
diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h
index cff78eea6..9b0f16507 100644
--- a/src/include/gnunet_protocols.h
+++ b/src/include/gnunet_protocols.h
@@ -2428,6 +2428,16 @@ extern "C"
2428 */ 2428 */
2429#define GNUNET_MESSAGE_TYPE_SENSOR_READING 804 2429#define GNUNET_MESSAGE_TYPE_SENSOR_READING 804
2430 2430
2431/**
2432 * Request for sensor list from update point
2433 */
2434#define GNUNET_MESSAGE_TYPE_SENSOR_LIST_REQ 805
2435
2436/**
2437 * Sensor list sent from update point to requesting peer
2438 */
2439#define GNUNET_MESSAGE_TYPE_SENSOR_LIST 806
2440
2431 2441
2432/******************************************************************************* 2442/*******************************************************************************
2433 * PEERSTORE message types 2443 * PEERSTORE message types
diff --git a/src/sensor/gnunet-service-sensor-update.c b/src/sensor/gnunet-service-sensor-update.c
index e8c294923..0accd3e05 100644
--- a/src/sensor/gnunet-service-sensor-update.c
+++ b/src/sensor/gnunet-service-sensor-update.c
@@ -74,6 +74,11 @@ struct UpdatePoint
74 */ 74 */
75 struct GNUNET_CADET_TransmitHandle *sensor_list_req_th; 75 struct GNUNET_CADET_TransmitHandle *sensor_list_req_th;
76 76
77 /**
78 * Are we waiting for a sensor list?
79 */
80 int expecting_sensor_list;
81
77}; 82};
78 83
79 84
@@ -132,6 +137,7 @@ SENSOR_update_stop ()
132{ 137{
133 struct UpdatePoint *up; 138 struct UpdatePoint *up;
134 139
140 up_default = NULL;
135 up = up_head; 141 up = up_head;
136 while (NULL != up) 142 while (NULL != up)
137 { 143 {
@@ -178,10 +184,22 @@ fail ()
178static size_t 184static size_t
179do_send_sensor_list_req (void *cls, size_t size, void *buf) 185do_send_sensor_list_req (void *cls, size_t size, void *buf)
180{ 186{
181 up_default->sensor_list_req_th = NULL; 187 struct GNUNET_MessageHeader *msg;
182 //TODO 188 size_t msg_size;
183 189
184 return 0; //FIXME 190 up_default->sensor_list_req_th = NULL;
191 if (NULL == buf)
192 {
193 fail ();
194 return 0;
195 }
196 msg = GNUNET_new (struct GNUNET_MessageHeader);
197 msg_size = sizeof (struct GNUNET_MessageHeader);
198 msg->size = htons (msg_size);
199 msg->type = htons (GNUNET_MESSAGE_TYPE_SENSOR_LIST_REQ);
200 memcpy (buf, msg, msg_size);
201 up_default->expecting_sensor_list = GNUNET_YES;
202 return msg_size;
185} 203}
186 204
187 205
@@ -221,7 +239,6 @@ check_for_updates (void *cls,
221 GNUNET_TIME_UNIT_FOREVER_REL, 239 GNUNET_TIME_UNIT_FOREVER_REL,
222 sizeof (struct GNUNET_MessageHeader), 240 sizeof (struct GNUNET_MessageHeader),
223 &do_send_sensor_list_req, NULL); 241 &do_send_sensor_list_req, NULL);
224 //TODO
225} 242}
226 243
227 244
@@ -278,6 +295,7 @@ load_update_points ()
278 up->peer_id.public_key = public_key; 295 up->peer_id.public_key = public_key;
279 up->ch = NULL; 296 up->ch = NULL;
280 up->sensor_list_req_th = NULL; 297 up->sensor_list_req_th = NULL;
298 up->expecting_sensor_list = GNUNET_NO;
281 GNUNET_CONTAINER_DLL_insert (up_head, up_tail, up); 299 GNUNET_CONTAINER_DLL_insert (up_head, up_tail, up);
282 LOG (GNUNET_ERROR_TYPE_DEBUG, 300 LOG (GNUNET_ERROR_TYPE_DEBUG,
283 "Loaded update point `%s'.\n", 301 "Loaded update point `%s'.\n",
@@ -288,6 +306,30 @@ load_update_points ()
288 306
289 307
290/** 308/**
309 * Handler of a sensor list message received from an update point.
310 *
311 * @param cls Closure (unused).
312 * @param channel Connection to the other end.
313 * @param channel_ctx Place to store local state associated with the channel.
314 * @param message The actual message.
315 * @return #GNUNET_OK to keep the channel open,
316 * #GNUNET_SYSERR to close it (signal serious error).
317 */
318static int
319handle_sensor_list (void *cls,
320 struct GNUNET_CADET_Channel *channel,
321 void **channel_ctx,
322 const struct GNUNET_MessageHeader *message)
323{
324 GNUNET_assert (*channel_ctx == up_default);
325 GNUNET_assert (GNUNET_YES == up_default->expecting_sensor_list);
326 up_default->expecting_sensor_list = GNUNET_NO;
327 //TODO
328 return GNUNET_OK;
329}
330
331
332/**
291 * Start the sensor update module 333 * Start the sensor update module
292 * 334 *
293 * @param c our service configuration 335 * @param c our service configuration
@@ -299,6 +341,7 @@ SENSOR_update_start (const struct GNUNET_CONFIGURATION_Handle *c,
299 struct GNUNET_CONTAINER_MultiHashMap *sensors) 341 struct GNUNET_CONTAINER_MultiHashMap *sensors)
300{ 342{
301 static struct GNUNET_CADET_MessageHandler cadet_handlers[] = { 343 static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
344 {&handle_sensor_list, GNUNET_MESSAGE_TYPE_SENSOR_LIST, 0},
302 {NULL, 0, 0} 345 {NULL, 0, 0}
303 }; 346 };
304 347
diff --git a/src/sensordashboard/gnunet-service-sensordashboard.c b/src/sensordashboard/gnunet-service-sensordashboard.c
index e93a8cbfd..26cc421fc 100644
--- a/src/sensordashboard/gnunet-service-sensordashboard.c
+++ b/src/sensordashboard/gnunet-service-sensordashboard.c
@@ -32,6 +32,36 @@
32#include "gnunet_sensor_util_lib.h" 32#include "gnunet_sensor_util_lib.h"
33#include "gnunet_peerstore_service.h" 33#include "gnunet_peerstore_service.h"
34 34
35
36/**
37 * Context of a connected client peer
38 */
39struct ClientPeerContext
40{
41
42 /**
43 * DLL
44 */
45 struct ClientPeerContext *prev;
46
47 /*
48 * DLL
49 */
50 struct ClientPeerContext *next;
51
52 /**
53 * GNUnet Peer identity
54 */
55 struct GNUNET_PeerIdentity peerid;
56
57 /**
58 * Handle to the cadet channel
59 */
60 struct GNUNET_CADET_Channel *ch;
61
62};
63
64
35/** 65/**
36 * Handle to CADET service 66 * Handle to CADET service
37 */ 67 */
@@ -53,6 +83,33 @@ static struct GNUNET_PEERSTORE_Handle *peerstore;
53static char *subsystem = "sensordashboard"; 83static char *subsystem = "sensordashboard";
54 84
55/** 85/**
86 * Head of a DLL of all connected client peers
87 */
88static struct ClientPeerContext *cp_head;
89
90/**
91 * Tail of a DLL of all connected client peers
92 */
93static struct ClientPeerContext *cp_tail;
94
95
96/**
97 * Destroy a given client peer context
98 *
99 * @param cp client peer context
100 */
101static void
102destroy_clientpeer (struct ClientPeerContext *cp)
103{
104 if (NULL != cp->ch)
105 {
106 GNUNET_CADET_channel_destroy (cp->ch);
107 cp->ch = NULL;
108 }
109 GNUNET_free (cp);
110}
111
112/**
56 * Task run during shutdown. 113 * Task run during shutdown.
57 * 114 *
58 * @param cls unused 115 * @param cls unused
@@ -61,9 +118,18 @@ static char *subsystem = "sensordashboard";
61static void 118static void
62cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 119cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
63{ 120{
121 struct ClientPeerContext *cp;
122
123 cp = cp_head;
124 while (NULL != cp)
125 {
126 GNUNET_CONTAINER_DLL_remove (cp_head, cp_tail, cp);
127 destroy_clientpeer (cp);
128 cp = cp_head;
129 }
64 if (NULL != cadet) 130 if (NULL != cadet)
65 { 131 {
66 GNUNET_CADET_disconnect(cadet); 132 GNUNET_CADET_disconnect (cadet);
67 cadet = NULL; 133 cadet = NULL;
68 } 134 }
69 if (NULL != peerstore) 135 if (NULL != peerstore)
@@ -86,13 +152,16 @@ cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
86 * @param channel_ctx place where local state associated 152 * @param channel_ctx place where local state associated
87 * with the channel is stored 153 * with the channel is stored
88 */ 154 */
89static void cadet_channel_destroyed (void *cls, 155static void
90 const struct GNUNET_CADET_Channel *channel, 156cadet_channel_destroyed (void *cls,
91 void *channel_ctx) 157 const struct GNUNET_CADET_Channel *channel,
158 void *channel_ctx)
92{ 159{
93 struct GNUNET_PeerIdentity *peer = channel_ctx; 160 struct ClientPeerContext *cp = channel_ctx;
94 161
95 GNUNET_free (peer); 162 cp->ch = NULL;
163 GNUNET_CONTAINER_DLL_remove (cp_head, cp_tail, cp);
164 destroy_clientpeer (cp);
96} 165}
97 166
98/** 167/**
@@ -113,16 +182,19 @@ static void cadet_channel_destroyed (void *cls,
113 * @return initial channel context for the channel 182 * @return initial channel context for the channel
114 * (can be NULL -- that's not an error) 183 * (can be NULL -- that's not an error)
115 */ 184 */
116static void *cadet_channel_created (void *cls, 185static void *
117 struct GNUNET_CADET_Channel *channel, 186cadet_channel_created (void *cls,
118 const struct GNUNET_PeerIdentity *initiator, 187 struct GNUNET_CADET_Channel *channel,
119 uint32_t port, enum GNUNET_CADET_ChannelOption options) 188 const struct GNUNET_PeerIdentity *initiator,
189 uint32_t port, enum GNUNET_CADET_ChannelOption options)
120{ 190{
121 struct GNUNET_PeerIdentity *peer; 191 struct ClientPeerContext *cp;
122 192
123 peer = GNUNET_new (struct GNUNET_PeerIdentity); 193 cp = GNUNET_new (struct ClientPeerContext);
124 memcpy (peer, initiator, sizeof (struct GNUNET_PeerIdentity)); 194 cp->peerid = *initiator;
125 return peer; 195 cp->ch = channel;
196 GNUNET_CONTAINER_DLL_insert (cp_head, cp_tail, cp);
197 return cp;
126} 198}
127 199
128/** 200/**
@@ -139,8 +211,11 @@ static void *cadet_channel_created (void *cls,
139 * @return #GNUNET_OK to keep the channel open, 211 * @return #GNUNET_OK to keep the channel open,
140 * #GNUNET_SYSERR to close it (signal serious error). 212 * #GNUNET_SYSERR to close it (signal serious error).
141 */ 213 */
142int sensor_reading_receiver (void *cls, struct GNUNET_CADET_Channel *channel, 214static int
143 void **channel_ctx, const struct GNUNET_MessageHeader *message) 215handle_sensor_reading (void *cls,
216 struct GNUNET_CADET_Channel *channel,
217 void **channel_ctx,
218 const struct GNUNET_MessageHeader *message)
144{ 219{
145 struct GNUNET_PeerIdentity *peer = *channel_ctx; 220 struct GNUNET_PeerIdentity *peer = *channel_ctx;
146 struct GNUNET_SENSOR_Reading *reading; 221 struct GNUNET_SENSOR_Reading *reading;
@@ -149,27 +224,56 @@ int sensor_reading_receiver (void *cls, struct GNUNET_CADET_Channel *channel,
149 if (NULL == reading) 224 if (NULL == reading)
150 { 225 {
151 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
152 "Received an invalid sensor reading from peer `%s'\n", 227 "Received an invalid sensor reading from peer `%s'\n",
153 GNUNET_i2s (peer)); 228 GNUNET_i2s (peer));
154 return GNUNET_SYSERR; 229 return GNUNET_SYSERR;
155 } 230 }
156 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 231 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
157 "Received a sensor reading from peer `%s':\n" 232 "Received a sensor reading from peer `%s':\n"
158 "# Sensor name: `%s'\n" 233 "# Sensor name: `%s'\n"
159 "# Timestamp: %" PRIu64 "\n" 234 "# Timestamp: %" PRIu64 "\n"
160 "# Value size: %" PRIu64 ".\n", 235 "# Value size: %" PRIu64 ".\n",
161 GNUNET_i2s (peer), 236 GNUNET_i2s (peer),
162 reading->sensor->name, 237 reading->sensor->name,
163 reading->timestamp, 238 reading->timestamp,
164 reading->value_size); 239 reading->value_size);
165 GNUNET_PEERSTORE_store (peerstore, subsystem, peer, reading->sensor->name, 240 GNUNET_PEERSTORE_store (peerstore, subsystem, peer, reading->sensor->name,
166 reading->value, reading->value_size, GNUNET_TIME_UNIT_FOREVER_ABS, 241 reading->value, reading->value_size,
167 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL); 242 GNUNET_TIME_UNIT_FOREVER_ABS,
243 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, NULL, NULL);
168 GNUNET_free (reading->value); 244 GNUNET_free (reading->value);
169 GNUNET_free (reading); 245 GNUNET_free (reading);
246 GNUNET_CADET_receive_done (channel);
170 return GNUNET_OK; 247 return GNUNET_OK;
171} 248}
172 249
250
251/**
252 * Called with any sensor list request received.
253 *
254 * Each time the function must call #GNUNET_CADET_receive_done on the channel
255 * in order to receive the next message. This doesn't need to be immediate:
256 * can be delayed if some processing is done on the message.
257 *
258 * @param cls Closure (set from #GNUNET_CADET_connect).
259 * @param channel Connection to the other end.
260 * @param channel_ctx Place to store local state associated with the channel.
261 * @param message The actual message.
262 * @return #GNUNET_OK to keep the channel open,
263 * #GNUNET_SYSERR to close it (signal serious error).
264 */
265static int
266handle_sensor_list_req (void *cls,
267 struct GNUNET_CADET_Channel *channel,
268 void **channel_ctx,
269 const struct GNUNET_MessageHeader *message)
270{
271 //TODO
272 GNUNET_CADET_receive_done (channel);
273 return GNUNET_OK;
274}
275
276
173/** 277/**
174 * Process sensordashboard requests. 278 * Process sensordashboard requests.
175 * 279 *
@@ -185,25 +289,30 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
185 {NULL, NULL, 0, 0} 289 {NULL, NULL, 0, 0}
186 }; 290 };
187 static struct GNUNET_CADET_MessageHandler cadet_handlers[] = { 291 static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
188 {&sensor_reading_receiver, GNUNET_MESSAGE_TYPE_SENSOR_READING, 0}, 292 {&handle_sensor_reading,
293 GNUNET_MESSAGE_TYPE_SENSOR_READING, 0},
294 {&handle_sensor_list_req,
295 GNUNET_MESSAGE_TYPE_SENSOR_LIST_REQ,
296 sizeof (struct GNUNET_MessageHeader)},
189 {NULL, 0, 0} 297 {NULL, 0, 0}
190 }; 298 };
191 static uint32_t cadet_ports[] = { 299 static uint32_t cadet_ports[] = {
192 GNUNET_APPLICATION_TYPE_SENSORDASHBOARD, 300 GNUNET_APPLICATION_TYPE_SENSORDASHBOARD,
301 GNUNET_APPLICATION_TYPE_SENSORUPDATE,
193 GNUNET_APPLICATION_TYPE_END 302 GNUNET_APPLICATION_TYPE_END
194 }; 303 };
195 sensors = GNUNET_SENSOR_load_all_sensors (); 304 sensors = GNUNET_SENSOR_load_all_sensors ();
196 GNUNET_assert (NULL != sensors); 305 GNUNET_assert (NULL != sensors);
197 cadet = GNUNET_CADET_connect(cfg, 306 cadet = GNUNET_CADET_connect(cfg,
198 NULL, 307 NULL,
199 &cadet_channel_created, 308 &cadet_channel_created,
200 &cadet_channel_destroyed, 309 &cadet_channel_destroyed,
201 cadet_handlers, 310 cadet_handlers,
202 cadet_ports); 311 cadet_ports);
203 if(NULL == cadet) 312 if(NULL == cadet)
204 { 313 {
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 _("Failed to connect to `%s' service.\n"), "CADET"); 315 _("Failed to connect to `%s' service.\n"), "CADET");
207 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL); 316 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
208 return; 317 return;
209 } 318 }
@@ -211,7 +320,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
211 if (NULL == peerstore) 320 if (NULL == peerstore)
212 { 321 {
213 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214 _("Failed to connect to `%s' service.\n"), "PEERSTORE"); 323 _("Failed to connect to `%s' service.\n"), "PEERSTORE");
215 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL); 324 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
216 return; 325 return;
217 } 326 }