aboutsummaryrefslogtreecommitdiff
path: root/src/peerstore/peerstore_api.c
diff options
context:
space:
mode:
authorOmar Tarabai <tarabai@devegypt.com>2014-06-06 11:55:14 +0000
committerOmar Tarabai <tarabai@devegypt.com>2014-06-06 11:55:14 +0000
commitbdfda4dd17faf721d82bfee411dcaf2777012a9f (patch)
treeda228559f7992fe39e29e7cd4b908ec89a8d5e7e /src/peerstore/peerstore_api.c
parentf92f3f8a717f1a3b332bec4f162ad8da237f2e89 (diff)
downloadgnunet-bdfda4dd17faf721d82bfee411dcaf2777012a9f.tar.gz
gnunet-bdfda4dd17faf721d82bfee411dcaf2777012a9f.zip
using PEERSTORE in SENSOR + fixes in PEERSTORE
Diffstat (limited to 'src/peerstore/peerstore_api.c')
-rw-r--r--src/peerstore/peerstore_api.c89
1 files changed, 84 insertions, 5 deletions
diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c
index 2b1cc6a1d..b53bc2f1a 100644
--- a/src/peerstore/peerstore_api.c
+++ b/src/peerstore/peerstore_api.c
@@ -245,6 +245,27 @@ static void
245reconnect (struct GNUNET_PEERSTORE_Handle *h); 245reconnect (struct GNUNET_PEERSTORE_Handle *h);
246 246
247/** 247/**
248 * Callback after MQ envelope is sent
249 *
250 * @param cls a 'struct GNUNET_PEERSTORE_WatchContext *'
251 */
252void watch_request_sent (void *cls);
253
254/**
255 * Callback after MQ envelope is sent
256 *
257 * @param cls a 'struct GNUNET_PEERSTORE_IterateContext *'
258 */
259void iterate_request_sent (void *cls);
260
261/**
262 * Callback after MQ envelope is sent
263 *
264 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
265 */
266void store_request_sent (void *cls);
267
268/**
248 * MQ message handlers 269 * MQ message handlers
249 */ 270 */
250static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { 271static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
@@ -268,6 +289,28 @@ handle_client_error (void *cls, enum GNUNET_MQ_Error error)
268} 289}
269 290
270/** 291/**
292 * Iterator over previous watches to resend them
293 */
294int rewatch_it(void *cls,
295 const struct GNUNET_HashCode *key,
296 void *value)
297{
298 struct GNUNET_PEERSTORE_Handle *h = cls;
299 struct GNUNET_PEERSTORE_WatchContext *wc = value;
300 struct StoreKeyHashMessage *hm;
301
302 if(GNUNET_YES == wc->request_sent)
303 { /* Envelope gone, create new one. */
304 wc->ev = GNUNET_MQ_msg(hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH);
305 hm->keyhash = wc->keyhash;
306 wc->request_sent = GNUNET_NO;
307 }
308 GNUNET_MQ_notify_sent(wc->ev, &watch_request_sent, wc);
309 GNUNET_MQ_send(h->mq, wc->ev);
310 return GNUNET_YES;
311}
312
313/**
271 * Close the existing connection to PEERSTORE and reconnect. 314 * Close the existing connection to PEERSTORE and reconnect.
272 * 315 *
273 * @param h handle to the service 316 * @param h handle to the service
@@ -275,6 +318,11 @@ handle_client_error (void *cls, enum GNUNET_MQ_Error error)
275static void 318static void
276reconnect (struct GNUNET_PEERSTORE_Handle *h) 319reconnect (struct GNUNET_PEERSTORE_Handle *h)
277{ 320{
321 struct GNUNET_PEERSTORE_IterateContext *ic;
322 GNUNET_PEERSTORE_Processor icb;
323 void *icb_cls;
324 struct GNUNET_PEERSTORE_StoreContext *sc;
325
278 LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n"); 326 LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
279 if (NULL != h->mq) 327 if (NULL != h->mq)
280 { 328 {
@@ -287,13 +335,43 @@ reconnect (struct GNUNET_PEERSTORE_Handle *h)
287 h->client = NULL; 335 h->client = NULL;
288 } 336 }
289 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg); 337 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
290 //FIXME: retry connecting if fails again (client == NULL) 338 GNUNET_assert(NULL != h->client);
291 h->mq = GNUNET_MQ_queue_for_connection_client(h->client, 339 h->mq = GNUNET_MQ_queue_for_connection_client(h->client,
292 mq_handlers, 340 mq_handlers,
293 &handle_client_error, 341 &handle_client_error,
294 h); 342 h);
295 //FIXME: resend pending requests after reconnecting 343 LOG(GNUNET_ERROR_TYPE_DEBUG,
296 344 "Resending pending requests after reconnect.\n");
345 if (NULL != h->watches)
346 {
347 GNUNET_CONTAINER_multihashmap_iterate(h->watches,
348 &rewatch_it, h);
349 }
350 ic = h->iterate_head;
351 while (NULL != ic)
352 {
353 if (GNUNET_YES == ic->request_sent)
354 {
355 icb = ic->callback;
356 icb_cls = ic->callback_cls;
357 GNUNET_PEERSTORE_iterate_cancel(ic);
358 if(NULL != icb)
359 icb(icb_cls, NULL,_("Iteration canceled due to reconnection."));
360 }
361 else
362 {
363 GNUNET_MQ_notify_sent(ic->ev, &iterate_request_sent, ic);
364 GNUNET_MQ_send(h->mq, ic->ev);
365 }
366 ic = ic->next;
367 }
368 sc = h->store_head;
369 while (NULL != sc)
370 {
371 GNUNET_MQ_notify_sent(sc->ev, &store_request_sent, sc);
372 GNUNET_MQ_send(h->mq, sc->ev);
373 sc = sc->next;
374 }
297} 375}
298 376
299/** 377/**
@@ -336,6 +414,7 @@ GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
336void 414void
337GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h) 415GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
338{ 416{
417 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting.\n");
339 if(NULL != h->watches) 418 if(NULL != h->watches)
340 { 419 {
341 GNUNET_CONTAINER_multihashmap_destroy(h->watches); 420 GNUNET_CONTAINER_multihashmap_destroy(h->watches);
@@ -442,7 +521,7 @@ GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
442 sc->cont = cont; 521 sc->cont = cont;
443 sc->cont_cls = cont_cls; 522 sc->cont_cls = cont_cls;
444 sc->h = h; 523 sc->h = h;
445 GNUNET_CONTAINER_DLL_insert(h->store_head, h->store_tail, sc); 524 GNUNET_CONTAINER_DLL_insert_tail(h->store_head, h->store_tail, sc);
446 GNUNET_MQ_notify_sent(ev, &store_request_sent, sc); 525 GNUNET_MQ_notify_sent(ev, &store_request_sent, sc);
447 GNUNET_MQ_send(h->mq, ev); 526 GNUNET_MQ_send(h->mq, ev);
448 return sc; 527 return sc;
@@ -604,7 +683,7 @@ GNUNET_PEERSTORE_iterate (struct GNUNET_PEERSTORE_Handle *h,
604 ic->ev = ev; 683 ic->ev = ev;
605 ic->h = h; 684 ic->h = h;
606 ic->request_sent = GNUNET_NO; 685 ic->request_sent = GNUNET_NO;
607 GNUNET_CONTAINER_DLL_insert(h->iterate_head, h->iterate_tail, ic); 686 GNUNET_CONTAINER_DLL_insert_tail(h->iterate_head, h->iterate_tail, ic);
608 LOG(GNUNET_ERROR_TYPE_DEBUG, 687 LOG(GNUNET_ERROR_TYPE_DEBUG,
609 "Sending an iterate request for sub system `%s'\n", sub_system); 688 "Sending an iterate request for sub system `%s'\n", sub_system);
610 GNUNET_MQ_notify_sent(ev, &iterate_request_sent, ic); 689 GNUNET_MQ_notify_sent(ev, &iterate_request_sent, ic);