aboutsummaryrefslogtreecommitdiff
path: root/src/peerinfo/peerinfo_api.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-05-03 11:27:14 +0000
committerChristian Grothoff <christian@grothoff.org>2012-05-03 11:27:14 +0000
commit31fa7ff21510bad20198b2e6d2b93d62d7624394 (patch)
tree13f4cbacee4a266b6908a5fef47fa2382785b56c /src/peerinfo/peerinfo_api.c
parent214038fae7d60dc443e823009c1fc942d0560aa4 (diff)
downloadgnunet-31fa7ff21510bad20198b2e6d2b93d62d7624394.tar.gz
gnunet-31fa7ff21510bad20198b2e6d2b93d62d7624394.zip
-enable lm building
Diffstat (limited to 'src/peerinfo/peerinfo_api.c')
-rw-r--r--src/peerinfo/peerinfo_api.c136
1 files changed, 66 insertions, 70 deletions
diff --git a/src/peerinfo/peerinfo_api.c b/src/peerinfo/peerinfo_api.c
index 4564df727..f70b8fde1 100644
--- a/src/peerinfo/peerinfo_api.c
+++ b/src/peerinfo/peerinfo_api.c
@@ -33,35 +33,28 @@
33 33
34#define LOG(kind,...) GNUNET_log_from (kind, "peerinfo-api",__VA_ARGS__) 34#define LOG(kind,...) GNUNET_log_from (kind, "peerinfo-api",__VA_ARGS__)
35 35
36/**
37 * Function to call after transmission has succeeded.
38 *
39 * @param cls closure
40 * @param success GNUNET_OK if transmission worked, GNUNET_SYSERR on error
41 */
42typedef void (*TransmissionContinuation) (void *cls, int success);
43
44 36
45/** 37/**
46 * Entry in the transmission queue to PEERINFO service. 38 * Entry in the transmission queue to PEERINFO service. We use
39 * the same structure for queueing 'iteration' requests and
40 * actual 'add' messages.
47 */ 41 */
48struct TransmissionQueueEntry 42struct GNUNET_PEERINFO_AddContext
49{ 43{
50 /** 44 /**
51 * This is a linked list. 45 * This is a linked list.
52 */ 46 */
53 struct TransmissionQueueEntry *next; 47 struct GNUNET_PEERINFO_AddContext *next;
54 48
55 /** 49 /**
56 * This is a linked list. 50 * This is a linked list.
57 */ 51 */
58 struct TransmissionQueueEntry *prev; 52 struct GNUNET_PEERINFO_AddContext *prev;
59 53
60 /** 54 /**
61 * Function to call after request has been transmitted, or NULL (in which 55 * Function to call after request has been transmitted, or NULL.
62 * case we must consider sending the next entry immediately).
63 */ 56 */
64 TransmissionContinuation cont; 57 GNUNET_PEERINFO_Continuation cont;
65 58
66 /** 59 /**
67 * Closure for 'cont'. 60 * Closure for 'cont'.
@@ -110,7 +103,7 @@ struct GNUNET_PEERINFO_IteratorContext
110 /** 103 /**
111 * Our entry in the transmission queue. 104 * Our entry in the transmission queue.
112 */ 105 */
113 struct TransmissionQueueEntry *tqe; 106 struct GNUNET_PEERINFO_AddContext *ac;
114 107
115 /** 108 /**
116 * Task responsible for timeout. 109 * Task responsible for timeout.
@@ -147,12 +140,12 @@ struct GNUNET_PEERINFO_Handle
147 /** 140 /**
148 * Head of transmission queue. 141 * Head of transmission queue.
149 */ 142 */
150 struct TransmissionQueueEntry *tq_head; 143 struct GNUNET_PEERINFO_AddContext *ac_head;
151 144
152 /** 145 /**
153 * Tail of transmission queue. 146 * Tail of transmission queue.
154 */ 147 */
155 struct TransmissionQueueEntry *tq_tail; 148 struct GNUNET_PEERINFO_AddContext *ac_tail;
156 149
157 /** 150 /**
158 * Handle for the current transmission request, or NULL if none is pending. 151 * Handle for the current transmission request, or NULL if none is pending.
@@ -214,7 +207,7 @@ GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
214void 207void
215GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h) 208GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
216{ 209{
217 struct TransmissionQueueEntry *tqe; 210 struct GNUNET_PEERINFO_AddContext *ac;
218 struct GNUNET_PEERINFO_IteratorContext *ic; 211 struct GNUNET_PEERINFO_IteratorContext *ic;
219 212
220 while (NULL != (ic = h->ic_head)) 213 while (NULL != (ic = h->ic_head))
@@ -223,12 +216,12 @@ GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
223 ic->in_receive = GNUNET_NO; 216 ic->in_receive = GNUNET_NO;
224 GNUNET_PEERINFO_iterate_cancel (ic); 217 GNUNET_PEERINFO_iterate_cancel (ic);
225 } 218 }
226 while (NULL != (tqe = h->tq_head)) 219 while (NULL != (ac = h->ac_head))
227 { 220 {
228 GNUNET_CONTAINER_DLL_remove (h->tq_head, h->tq_tail, tqe); 221 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
229 if (tqe->cont != NULL) 222 if (NULL != ac->cont)
230 tqe->cont (tqe->cont_cls, GNUNET_SYSERR); 223 ac->cont (ac->cont_cls, _("aborted due to explicit disconnect request"));
231 GNUNET_free (tqe); 224 GNUNET_free (ac);
232 } 225 }
233 if (NULL != h->th) 226 if (NULL != h->th)
234 { 227 {
@@ -333,25 +326,25 @@ static size_t
333do_transmit (void *cls, size_t size, void *buf) 326do_transmit (void *cls, size_t size, void *buf)
334{ 327{
335 struct GNUNET_PEERINFO_Handle *h = cls; 328 struct GNUNET_PEERINFO_Handle *h = cls;
336 struct TransmissionQueueEntry *tqe = h->tq_head; 329 struct GNUNET_PEERINFO_AddContext *ac = h->ac_head;
337 size_t ret; 330 size_t ret;
338 331
339 h->th = NULL; 332 h->th = NULL;
340 if (NULL == tqe) 333 if (NULL == ac)
341 return 0; /* request was cancelled in the meantime */ 334 return 0; /* request was cancelled in the meantime */
342 if (NULL == buf) 335 if (NULL == buf)
343 { 336 {
344 /* peerinfo service died */ 337 /* peerinfo service died */
345 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, 338 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
346 "Failed to transmit message to `%s' service.\n", "PEERINFO"); 339 "Failed to transmit message to `%s' service.\n", "PEERINFO");
347 GNUNET_CONTAINER_DLL_remove (h->tq_head, h->tq_tail, tqe); 340 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
348 reconnect (h); 341 reconnect (h);
349 if (NULL != tqe->cont) 342 if (NULL != ac->cont)
350 tqe->cont (tqe->cont_cls, GNUNET_SYSERR); 343 ac->cont (ac->cont_cls, _("failed to transmit request (service down?)"));
351 GNUNET_free (tqe); 344 GNUNET_free (ac);
352 return 0; 345 return 0;
353 } 346 }
354 ret = tqe->size; 347 ret = ac->size;
355 if (size < ret) 348 if (size < ret)
356 { 349 {
357 /* change in head of queue (i.e. cancel + add), try again */ 350 /* change in head of queue (i.e. cancel + add), try again */
@@ -360,13 +353,12 @@ do_transmit (void *cls, size_t size, void *buf)
360 } 353 }
361 LOG (GNUNET_ERROR_TYPE_DEBUG, 354 LOG (GNUNET_ERROR_TYPE_DEBUG,
362 "Transmitting request of size %u to `%s' service.\n", ret, "PEERINFO"); 355 "Transmitting request of size %u to `%s' service.\n", ret, "PEERINFO");
363 memcpy (buf, &tqe[1], ret); 356 memcpy (buf, &ac[1], ret);
364 GNUNET_CONTAINER_DLL_remove (h->tq_head, h->tq_tail, tqe); 357 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
365 if (NULL != tqe->cont) 358 trigger_transmit (h);
366 tqe->cont (tqe->cont_cls, GNUNET_OK); 359 if (NULL != ac->cont)
367 else 360 ac->cont (ac->cont_cls, NULL);
368 trigger_transmit (h); 361 GNUNET_free (ac);
369 GNUNET_free (tqe);
370 return ret; 362 return ret;
371} 363}
372 364
@@ -380,9 +372,9 @@ do_transmit (void *cls, size_t size, void *buf)
380static void 372static void
381trigger_transmit (struct GNUNET_PEERINFO_Handle *h) 373trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
382{ 374{
383 struct TransmissionQueueEntry *tqe; 375 struct GNUNET_PEERINFO_AddContext *ac;
384 376
385 if (NULL == (tqe = h->tq_head)) 377 if (NULL == (ac = h->ac_head))
386 return; /* no requests queued */ 378 return; /* no requests queued */
387 if (NULL != h->th) 379 if (NULL != h->th)
388 return; /* request already pending */ 380 return; /* request already pending */
@@ -395,7 +387,7 @@ trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
395 return; 387 return;
396 } 388 }
397 h->th = 389 h->th =
398 GNUNET_CLIENT_notify_transmit_ready (h->client, tqe->size, 390 GNUNET_CLIENT_notify_transmit_ready (h->client, ac->size,
399 GNUNET_TIME_UNIT_FOREVER_REL, 391 GNUNET_TIME_UNIT_FOREVER_REL,
400 GNUNET_YES, 392 GNUNET_YES,
401 &do_transmit, h); 393 &do_transmit, h);
@@ -413,23 +405,28 @@ trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
413 * @param h handle to the peerinfo service 405 * @param h handle to the peerinfo service
414 * @param hello the verified (!) HELLO message 406 * @param hello the verified (!) HELLO message
415 */ 407 */
416void 408struct GNUNET_PEERINFO_AddContext *
417GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h, 409GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
418 const struct GNUNET_HELLO_Message *hello) 410 const struct GNUNET_HELLO_Message *hello,
411 GNUNET_PEERINFO_Continuation cont,
412 void *cont_cls)
419{ 413{
420 uint16_t hs = GNUNET_HELLO_size (hello); 414 uint16_t hs = GNUNET_HELLO_size (hello);
421 struct TransmissionQueueEntry *tqe; 415 struct GNUNET_PEERINFO_AddContext *ac;
422 struct GNUNET_PeerIdentity peer; 416 struct GNUNET_PeerIdentity peer;
423 417
424 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer)); 418 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
425 LOG (GNUNET_ERROR_TYPE_DEBUG, 419 LOG (GNUNET_ERROR_TYPE_DEBUG,
426 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n", 420 "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
427 GNUNET_i2s (&peer), hs, "HELLO"); 421 GNUNET_i2s (&peer), hs, "HELLO");
428 tqe = GNUNET_malloc (sizeof (struct TransmissionQueueEntry) + hs); 422 ac = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) + hs);
429 tqe->size = hs; 423 ac->size = hs;
430 memcpy (&tqe[1], hello, hs); 424 ac->cont = cont;
431 GNUNET_CONTAINER_DLL_insert_after (h->tq_head, h->tq_tail, h->tq_tail, tqe); 425 ac->cont_cls = cont_cls;
426 memcpy (&ac[1], hello, hs);
427 GNUNET_CONTAINER_DLL_insert_after (h->ac_head, h->ac_tail, h->ac_tail, ac);
432 trigger_transmit (h); 428 trigger_transmit (h);
429 return ac;
433} 430}
434 431
435 432
@@ -528,26 +525,25 @@ peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
528 * the results (or handle transmission error). 525 * the results (or handle transmission error).
529 * 526 *
530 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext' 527 * @param cls the 'struct GNUNET_PEERINFO_IteratorContext'
531 * @param transmit_success GNUNET_OK if transmission worked 528 * @param emsg error message, NULL if transmission worked
532 */ 529 */
533static void 530static void
534iterator_start_receive (void *cls, int transmit_success) 531iterator_start_receive (void *cls, const char *emsg)
535{ 532{
536 struct GNUNET_PEERINFO_IteratorContext *ic = cls; 533 struct GNUNET_PEERINFO_IteratorContext *ic = cls;
537 struct GNUNET_PEERINFO_Handle *h = ic->h; 534 struct GNUNET_PEERINFO_Handle *h = ic->h;
538 GNUNET_PEERINFO_Processor cb; 535 GNUNET_PEERINFO_Processor cb;
539 void *cb_cls; 536 void *cb_cls;
540 537
541 ic->tqe = NULL; 538 ic->ac = NULL;
542 if (GNUNET_OK != transmit_success) 539 if (NULL != emsg)
543 { 540 {
544 cb = ic->callback; 541 cb = ic->callback;
545 cb_cls = ic->callback_cls; 542 cb_cls = ic->callback_cls;
546 GNUNET_PEERINFO_iterate_cancel (ic); 543 GNUNET_PEERINFO_iterate_cancel (ic);
547 reconnect (h); 544 reconnect (h);
548 if (NULL != cb) 545 if (NULL != cb)
549 cb (cb_cls, NULL, NULL, 546 cb (cb_cls, NULL, NULL, emsg);
550 _("Failed to transmit iteration request to `PEERINFO' service"));
551 return; 547 return;
552 } 548 }
553 LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for response from `%s' service.\n", 549 LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for response from `%s' service.\n",
@@ -607,17 +603,17 @@ GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
607 struct GNUNET_MessageHeader *lapm; 603 struct GNUNET_MessageHeader *lapm;
608 struct ListPeerMessage *lpm; 604 struct ListPeerMessage *lpm;
609 struct GNUNET_PEERINFO_IteratorContext *ic; 605 struct GNUNET_PEERINFO_IteratorContext *ic;
610 struct TransmissionQueueEntry *tqe; 606 struct GNUNET_PEERINFO_AddContext *ac;
611 607
612 if (NULL == peer) 608 if (NULL == peer)
613 { 609 {
614 LOG (GNUNET_ERROR_TYPE_DEBUG, 610 LOG (GNUNET_ERROR_TYPE_DEBUG,
615 "Requesting list of peers from PEERINFO service\n"); 611 "Requesting list of peers from PEERINFO service\n");
616 tqe = 612 ac =
617 GNUNET_malloc (sizeof (struct TransmissionQueueEntry) + 613 GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
618 sizeof (struct GNUNET_MessageHeader)); 614 sizeof (struct GNUNET_MessageHeader));
619 tqe->size = sizeof (struct GNUNET_MessageHeader); 615 ac->size = sizeof (struct GNUNET_MessageHeader);
620 lapm = (struct GNUNET_MessageHeader *) &tqe[1]; 616 lapm = (struct GNUNET_MessageHeader *) &ac[1];
621 lapm->size = htons (sizeof (struct GNUNET_MessageHeader)); 617 lapm->size = htons (sizeof (struct GNUNET_MessageHeader));
622 lapm->type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL); 618 lapm->type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
623 } 619 }
@@ -626,26 +622,26 @@ GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
626 LOG (GNUNET_ERROR_TYPE_DEBUG, 622 LOG (GNUNET_ERROR_TYPE_DEBUG,
627 "Requesting information on peer `%4s' from PEERINFO service\n", 623 "Requesting information on peer `%4s' from PEERINFO service\n",
628 GNUNET_i2s (peer)); 624 GNUNET_i2s (peer));
629 tqe = 625 ac =
630 GNUNET_malloc (sizeof (struct TransmissionQueueEntry) + 626 GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
631 sizeof (struct ListPeerMessage)); 627 sizeof (struct ListPeerMessage));
632 tqe->size = sizeof (struct ListPeerMessage); 628 ac->size = sizeof (struct ListPeerMessage);
633 lpm = (struct ListPeerMessage *) &tqe[1]; 629 lpm = (struct ListPeerMessage *) &ac[1];
634 lpm->header.size = htons (sizeof (struct ListPeerMessage)); 630 lpm->header.size = htons (sizeof (struct ListPeerMessage));
635 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET); 631 lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
636 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity)); 632 memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
637 } 633 }
638 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext)); 634 ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext));
639 ic->h = h; 635 ic->h = h;
640 ic->tqe = tqe; 636 ic->ac = ac;
641 ic->callback = callback; 637 ic->callback = callback;
642 ic->callback_cls = callback_cls; 638 ic->callback_cls = callback_cls;
643 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout); 639 ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
644 ic->timeout_task = 640 ic->timeout_task =
645 GNUNET_SCHEDULER_add_delayed (timeout, &signal_timeout, ic); 641 GNUNET_SCHEDULER_add_delayed (timeout, &signal_timeout, ic);
646 tqe->cont = &iterator_start_receive; 642 ac->cont = &iterator_start_receive;
647 tqe->cont_cls = ic; 643 ac->cont_cls = ic;
648 GNUNET_CONTAINER_DLL_insert_after (h->tq_head, h->tq_tail, h->tq_tail, tqe); 644 GNUNET_CONTAINER_DLL_insert_after (h->ac_head, h->ac_tail, h->ac_tail, ac);
649 GNUNET_CONTAINER_DLL_insert (h->ic_head, 645 GNUNET_CONTAINER_DLL_insert (h->ic_head,
650 h->ic_tail, 646 h->ic_tail,
651 ic); 647 ic);
@@ -676,10 +672,10 @@ GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
676 ic->callback = NULL; 672 ic->callback = NULL;
677 if (GNUNET_YES == ic->in_receive) 673 if (GNUNET_YES == ic->in_receive)
678 return; /* need to finish processing */ 674 return; /* need to finish processing */
679 if (NULL != ic->tqe) 675 if (NULL != ic->ac)
680 { 676 {
681 GNUNET_CONTAINER_DLL_remove (h->tq_head, h->tq_tail, ic->tqe); 677 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ic->ac);
682 GNUNET_free (ic->tqe); 678 GNUNET_free (ic->ac);
683 } 679 }
684 GNUNET_free (ic); 680 GNUNET_free (ic);
685} 681}