diff options
Diffstat (limited to 'src/sensor/sensor_api.c')
-rw-r--r-- | src/sensor/sensor_api.c | 139 |
1 files changed, 131 insertions, 8 deletions
diff --git a/src/sensor/sensor_api.c b/src/sensor/sensor_api.c index 9a18427d0..628873f91 100644 --- a/src/sensor/sensor_api.c +++ b/src/sensor/sensor_api.c | |||
@@ -46,16 +46,26 @@ struct GNUNET_SENSOR_Handle | |||
46 | struct GNUNET_CLIENT_Connection *client; | 46 | struct GNUNET_CLIENT_Connection *client; |
47 | 47 | ||
48 | /** | 48 | /** |
49 | * Head of iterator DLL. | 49 | * Head of iteration requests DLL. |
50 | */ | 50 | */ |
51 | struct GNUNET_SENSOR_IterateContext *ic_head; | 51 | struct GNUNET_SENSOR_IterateContext *ic_head; |
52 | 52 | ||
53 | /** | 53 | /** |
54 | * Tail of iterator DLL. | 54 | * Tail of iteration requests DLL. |
55 | */ | 55 | */ |
56 | struct GNUNET_SENSOR_IterateContext *ic_tail; | 56 | struct GNUNET_SENSOR_IterateContext *ic_tail; |
57 | 57 | ||
58 | /** | 58 | /** |
59 | * Head of force anomaly requests | ||
60 | */ | ||
61 | struct GNUNET_SENSOR_ForceAnomalyContext *fa_head; | ||
62 | |||
63 | /** | ||
64 | * Tail of force anomaly requests | ||
65 | */ | ||
66 | struct GNUNET_SENSOR_ForceAnomalyContext *fa_tail; | ||
67 | |||
68 | /** | ||
59 | * Message queue used to send data to service | 69 | * Message queue used to send data to service |
60 | */ | 70 | */ |
61 | struct GNUNET_MQ_Handle *mq; | 71 | struct GNUNET_MQ_Handle *mq; |
@@ -115,6 +125,44 @@ struct GNUNET_SENSOR_IterateContext | |||
115 | 125 | ||
116 | }; | 126 | }; |
117 | 127 | ||
128 | /** | ||
129 | * Context of a force anomaly request | ||
130 | */ | ||
131 | struct GNUNET_SENSOR_ForceAnomalyContext | ||
132 | { | ||
133 | |||
134 | /** | ||
135 | * DLL | ||
136 | */ | ||
137 | struct GNUNET_SENSOR_ForceAnomalyContext *next; | ||
138 | |||
139 | /** | ||
140 | * DLL | ||
141 | */ | ||
142 | struct GNUNET_SENSOR_ForceAnomalyContext *prev; | ||
143 | |||
144 | /** | ||
145 | * Handle to the SENSOR service. | ||
146 | */ | ||
147 | struct GNUNET_SENSOR_Handle *h; | ||
148 | |||
149 | /** | ||
150 | * Envelope containing iterate request. | ||
151 | */ | ||
152 | struct GNUNET_MQ_Envelope *ev; | ||
153 | |||
154 | /** | ||
155 | * User continuation function | ||
156 | */ | ||
157 | GNUNET_SENSOR_Continuation cont; | ||
158 | |||
159 | /** | ||
160 | * Closure for cont | ||
161 | */ | ||
162 | void *cont_cls; | ||
163 | |||
164 | }; | ||
165 | |||
118 | 166 | ||
119 | /** | 167 | /** |
120 | * Notifier of an error encountered by MQ. | 168 | * Notifier of an error encountered by MQ. |
@@ -222,7 +270,8 @@ handle_sensor_info (void *cls, const struct GNUNET_MessageHeader *msg) | |||
222 | 270 | ||
223 | 271 | ||
224 | /** | 272 | /** |
225 | * Disconnect from the sensor service | 273 | * Disconnect from the sensor service. |
274 | * Please disconnect only when all requests sent are complete or canceled. | ||
226 | * | 275 | * |
227 | * @param h handle to disconnect | 276 | * @param h handle to disconnect |
228 | */ | 277 | */ |
@@ -230,16 +279,34 @@ void | |||
230 | GNUNET_SENSOR_disconnect (struct GNUNET_SENSOR_Handle *h) | 279 | GNUNET_SENSOR_disconnect (struct GNUNET_SENSOR_Handle *h) |
231 | { | 280 | { |
232 | struct GNUNET_SENSOR_IterateContext *ic; | 281 | struct GNUNET_SENSOR_IterateContext *ic; |
282 | GNUNET_SENSOR_SensorIterateCB ic_callback; | ||
283 | void *ic_callback_cls; | ||
284 | struct GNUNET_SENSOR_ForceAnomalyContext *fa; | ||
285 | GNUNET_SENSOR_Continuation fa_cont; | ||
286 | void *fa_cont_cls; | ||
233 | 287 | ||
234 | ic = h->ic_head; | 288 | ic = h->ic_head; |
235 | while (NULL != ic) | 289 | while (NULL != ic) |
236 | { | 290 | { |
237 | if (NULL != ic->callback) | 291 | ic_callback = ic->callback; |
238 | ic->callback (ic->callback_cls, NULL, | 292 | ic_callback_cls = ic->callback_cls; |
239 | _("Iterate request canceled due to disconnection.\n")); | ||
240 | GNUNET_SENSOR_iterate_cancel (ic); | 293 | GNUNET_SENSOR_iterate_cancel (ic); |
294 | if (NULL != ic_callback) | ||
295 | ic_callback (ic_callback_cls, NULL, | ||
296 | _("Iterate request canceled due to disconnection.\n")); | ||
241 | ic = h->ic_head; | 297 | ic = h->ic_head; |
242 | } | 298 | } |
299 | fa = h->fa_head; | ||
300 | while (NULL != fa) | ||
301 | { | ||
302 | fa_cont = fa->cont; | ||
303 | fa_cont_cls = fa->cont_cls; | ||
304 | GNUNET_SENSOR_force_anomaly_cancel (fa); | ||
305 | if (NULL != fa_cont) | ||
306 | fa_cont (fa_cont_cls, | ||
307 | _("Force anomaly request canceled due to disconnection.\n")); | ||
308 | fa = h->fa_head; | ||
309 | } | ||
243 | if (NULL != h->mq) | 310 | if (NULL != h->mq) |
244 | { | 311 | { |
245 | GNUNET_MQ_destroy (h->mq); | 312 | GNUNET_MQ_destroy (h->mq); |
@@ -414,6 +481,49 @@ GNUNET_SENSOR_iterate (struct GNUNET_SENSOR_Handle *h, | |||
414 | 481 | ||
415 | 482 | ||
416 | /** | 483 | /** |
484 | * Callback from MQ when the request has already been sent to the service. | ||
485 | * Now it can not be canelled. | ||
486 | * | ||
487 | * @param cls closure | ||
488 | */ | ||
489 | static void | ||
490 | force_anomaly_sent (void *cls) | ||
491 | { | ||
492 | struct GNUNET_SENSOR_ForceAnomalyContext *fa = cls; | ||
493 | GNUNET_SENSOR_Continuation cont; | ||
494 | void *cont_cls; | ||
495 | |||
496 | fa->ev = NULL; | ||
497 | cont = fa->cont; | ||
498 | cont_cls = fa->cont_cls; | ||
499 | GNUNET_SENSOR_force_anomaly_cancel (fa); | ||
500 | if (NULL != cont) | ||
501 | cont (cont_cls, NULL); | ||
502 | } | ||
503 | |||
504 | |||
505 | /** | ||
506 | * Cancel a force anomaly request. | ||
507 | * | ||
508 | * @param fa Force anomaly context returned by GNUNET_SENSOR_force_anomaly() | ||
509 | */ | ||
510 | void | ||
511 | GNUNET_SENSOR_force_anomaly_cancel (struct GNUNET_SENSOR_ForceAnomalyContext | ||
512 | *fa) | ||
513 | { | ||
514 | struct GNUNET_SENSOR_Handle *h = fa->h; | ||
515 | |||
516 | if (NULL != fa->ev) | ||
517 | { | ||
518 | GNUNET_MQ_send_cancel (fa->ev); | ||
519 | fa->ev = NULL; | ||
520 | } | ||
521 | GNUNET_CONTAINER_DLL_remove (h->fa_head, h->fa_tail, fa); | ||
522 | GNUNET_free (fa); | ||
523 | } | ||
524 | |||
525 | |||
526 | /** | ||
417 | * Force an anomaly status change on a given sensor. If the sensor reporting | 527 | * Force an anomaly status change on a given sensor. If the sensor reporting |
418 | * module is running, this will trigger the usual reporting logic, therefore, | 528 | * module is running, this will trigger the usual reporting logic, therefore, |
419 | * please only use this in a test environment. | 529 | * please only use this in a test environment. |
@@ -424,19 +534,32 @@ GNUNET_SENSOR_iterate (struct GNUNET_SENSOR_Handle *h, | |||
424 | * @param h Service handle | 534 | * @param h Service handle |
425 | * @param sensor_name Sensor name to set the anomaly status | 535 | * @param sensor_name Sensor name to set the anomaly status |
426 | * @param anomalous The desired status: #GNUNET_YES / #GNUNET_NO | 536 | * @param anomalous The desired status: #GNUNET_YES / #GNUNET_NO |
537 | * @param cont Continuation function to be called after the request is sent | ||
538 | * @param cont_cls Closure for cont | ||
427 | */ | 539 | */ |
428 | void | 540 | struct GNUNET_SENSOR_ForceAnomalyContext * |
429 | GNUNET_SENSOR_force_anomaly (struct GNUNET_SENSOR_Handle *h, char *sensor_name, | 541 | GNUNET_SENSOR_force_anomaly (struct GNUNET_SENSOR_Handle *h, char *sensor_name, |
430 | int anomalous) | 542 | int anomalous, GNUNET_SENSOR_Continuation cont, |
543 | void *cont_cls) | ||
431 | { | 544 | { |
432 | struct ForceAnomalyMessage *msg; | 545 | struct ForceAnomalyMessage *msg; |
433 | struct GNUNET_MQ_Envelope *ev; | 546 | struct GNUNET_MQ_Envelope *ev; |
547 | struct GNUNET_SENSOR_ForceAnomalyContext *fa; | ||
434 | 548 | ||
435 | ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_FORCE); | 549 | ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SENSOR_ANOMALY_FORCE); |
436 | GNUNET_CRYPTO_hash (sensor_name, strlen (sensor_name) + 1, | 550 | GNUNET_CRYPTO_hash (sensor_name, strlen (sensor_name) + 1, |
437 | &msg->sensor_name_hash); | 551 | &msg->sensor_name_hash); |
438 | msg->anomalous = htons (anomalous); | 552 | msg->anomalous = htons (anomalous); |
439 | GNUNET_MQ_send (h->mq, ev); | 553 | GNUNET_MQ_send (h->mq, ev); |
554 | fa = GNUNET_new (struct GNUNET_SENSOR_ForceAnomalyContext); | ||
555 | |||
556 | fa->h = h; | ||
557 | fa->cont = cont; | ||
558 | fa->cont_cls = cont_cls; | ||
559 | fa->ev = ev; | ||
560 | GNUNET_CONTAINER_DLL_insert_tail (h->fa_head, h->fa_tail, fa); | ||
561 | GNUNET_MQ_notify_sent (ev, &force_anomaly_sent, fa); | ||
562 | return fa; | ||
440 | } | 563 | } |
441 | 564 | ||
442 | 565 | ||