diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-04-21 19:52:54 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-04-21 19:52:54 +0000 |
commit | 7a6bb78122b5ec18dc9887f8d636db29cfa14781 (patch) | |
tree | d9ed2125ef18d191fa818b06192a84664eb05f9f /src/peerinfo/peerinfo_api.c | |
parent | 41e3996bf81ed9e91a62eb1a5a778c993ce35c93 (diff) | |
download | gnunet-7a6bb78122b5ec18dc9887f8d636db29cfa14781.tar.gz gnunet-7a6bb78122b5ec18dc9887f8d636db29cfa14781.zip |
new API implementation done
Diffstat (limited to 'src/peerinfo/peerinfo_api.c')
-rw-r--r-- | src/peerinfo/peerinfo_api.c | 115 |
1 files changed, 97 insertions, 18 deletions
diff --git a/src/peerinfo/peerinfo_api.c b/src/peerinfo/peerinfo_api.c index 15832ca80..5392d1696 100644 --- a/src/peerinfo/peerinfo_api.c +++ b/src/peerinfo/peerinfo_api.c | |||
@@ -22,12 +22,6 @@ | |||
22 | * @file peerinfo/peerinfo_api.c | 22 | * @file peerinfo/peerinfo_api.c |
23 | * @brief API to access peerinfo service | 23 | * @brief API to access peerinfo service |
24 | * @author Christian Grothoff | 24 | * @author Christian Grothoff |
25 | * | ||
26 | * TODO: | ||
27 | * - document NEW API implementation | ||
28 | * - add timeout for iteration | ||
29 | * - implement cancellation of iteration | ||
30 | * - prevent transmit during receive! | ||
31 | */ | 25 | */ |
32 | #include "platform.h" | 26 | #include "platform.h" |
33 | #include "gnunet_client_lib.h" | 27 | #include "gnunet_client_lib.h" |
@@ -74,7 +68,7 @@ struct TransmissionQueueEntry | |||
74 | void *cont_cls; | 68 | void *cont_cls; |
75 | 69 | ||
76 | /** | 70 | /** |
77 | * When this request times out. | 71 | * Timeout for the operation. |
78 | */ | 72 | */ |
79 | struct GNUNET_TIME_Absolute timeout; | 73 | struct GNUNET_TIME_Absolute timeout; |
80 | 74 | ||
@@ -121,6 +115,12 @@ struct GNUNET_PEERINFO_Handle | |||
121 | */ | 115 | */ |
122 | struct GNUNET_CLIENT_TransmitHandle *th; | 116 | struct GNUNET_CLIENT_TransmitHandle *th; |
123 | 117 | ||
118 | /** | ||
119 | * Set to GNUNET_YES if we are currently receiving replies from the | ||
120 | * service. | ||
121 | */ | ||
122 | int in_receive; | ||
123 | |||
124 | }; | 124 | }; |
125 | 125 | ||
126 | 126 | ||
@@ -269,6 +269,8 @@ trigger_transmit (struct GNUNET_PEERINFO_Handle *h) | |||
269 | return; | 269 | return; |
270 | if (h->th != NULL) | 270 | if (h->th != NULL) |
271 | return; | 271 | return; |
272 | if (h->in_receive == GNUNET_YES) | ||
273 | return; | ||
272 | h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, | 274 | h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, |
273 | tqe->size, | 275 | tqe->size, |
274 | GNUNET_TIME_absolute_get_remaining (tqe->timeout), | 276 | GNUNET_TIME_absolute_get_remaining (tqe->timeout), |
@@ -337,9 +339,24 @@ struct GNUNET_PEERINFO_NewIteratorContext | |||
337 | void *callback_cls; | 339 | void *callback_cls; |
338 | 340 | ||
339 | /** | 341 | /** |
342 | * Our entry in the transmission queue. | ||
343 | */ | ||
344 | struct TransmissionQueueEntry *tqe; | ||
345 | |||
346 | /** | ||
347 | * Task responsible for timeout. | ||
348 | */ | ||
349 | GNUNET_SCHEDULER_TaskIdentifier timeout_task; | ||
350 | |||
351 | /** | ||
340 | * Timeout for the operation. | 352 | * Timeout for the operation. |
341 | */ | 353 | */ |
342 | struct GNUNET_TIME_Absolute timeout; | 354 | struct GNUNET_TIME_Absolute timeout; |
355 | |||
356 | /** | ||
357 | * Are we now receiving? | ||
358 | */ | ||
359 | int in_receive; | ||
343 | }; | 360 | }; |
344 | 361 | ||
345 | 362 | ||
@@ -358,6 +375,7 @@ peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg) | |||
358 | const struct GNUNET_HELLO_Message *hello; | 375 | const struct GNUNET_HELLO_Message *hello; |
359 | uint16_t ms; | 376 | uint16_t ms; |
360 | 377 | ||
378 | ic->h->in_receive = GNUNET_NO; | ||
361 | if (msg == NULL) | 379 | if (msg == NULL) |
362 | { | 380 | { |
363 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 381 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
@@ -365,7 +383,11 @@ peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg) | |||
365 | "peerinfo"); | 383 | "peerinfo"); |
366 | reconnect (ic->h); | 384 | reconnect (ic->h); |
367 | trigger_transmit (ic->h); | 385 | trigger_transmit (ic->h); |
368 | ic->callback (ic->callback_cls, NULL, NULL, 1); | 386 | if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK) |
387 | GNUNET_SCHEDULER_cancel (ic->h->sched, | ||
388 | ic->timeout_task); | ||
389 | if (ic->callback != NULL) | ||
390 | ic->callback (ic->callback_cls, NULL, NULL, 1); | ||
369 | GNUNET_free (ic); | 391 | GNUNET_free (ic); |
370 | return; | 392 | return; |
371 | } | 393 | } |
@@ -376,7 +398,11 @@ peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg) | |||
376 | "Received end of list of peers from peerinfo database\n"); | 398 | "Received end of list of peers from peerinfo database\n"); |
377 | #endif | 399 | #endif |
378 | trigger_transmit (ic->h); | 400 | trigger_transmit (ic->h); |
379 | ic->callback (ic->callback_cls, NULL, NULL, 0); | 401 | if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK) |
402 | GNUNET_SCHEDULER_cancel (ic->h->sched, | ||
403 | ic->timeout_task); | ||
404 | if (ic->callback != NULL) | ||
405 | ic->callback (ic->callback_cls, NULL, NULL, 0); | ||
380 | GNUNET_free (ic); | 406 | GNUNET_free (ic); |
381 | return; | 407 | return; |
382 | } | 408 | } |
@@ -387,7 +413,11 @@ peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg) | |||
387 | GNUNET_break (0); | 413 | GNUNET_break (0); |
388 | reconnect (ic->h); | 414 | reconnect (ic->h); |
389 | trigger_transmit (ic->h); | 415 | trigger_transmit (ic->h); |
390 | ic->callback (ic->callback_cls, NULL, NULL, 2); | 416 | if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK) |
417 | GNUNET_SCHEDULER_cancel (ic->h->sched, | ||
418 | ic->timeout_task); | ||
419 | if (ic->callback != NULL) | ||
420 | ic->callback (ic->callback_cls, NULL, NULL, 2); | ||
391 | GNUNET_free (ic); | 421 | GNUNET_free (ic); |
392 | return; | 422 | return; |
393 | } | 423 | } |
@@ -401,7 +431,11 @@ peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg) | |||
401 | GNUNET_break (0); | 431 | GNUNET_break (0); |
402 | reconnect (ic->h); | 432 | reconnect (ic->h); |
403 | trigger_transmit (ic->h); | 433 | trigger_transmit (ic->h); |
404 | ic->callback (ic->callback_cls, NULL, NULL, 2); | 434 | if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK) |
435 | GNUNET_SCHEDULER_cancel (ic->h->sched, | ||
436 | ic->timeout_task); | ||
437 | if (ic->callback != NULL) | ||
438 | ic->callback (ic->callback_cls, NULL, NULL, 2); | ||
405 | GNUNET_free (ic); | 439 | GNUNET_free (ic); |
406 | return; | 440 | return; |
407 | } | 441 | } |
@@ -413,7 +447,9 @@ peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg) | |||
413 | "HELLO", | 447 | "HELLO", |
414 | GNUNET_i2s (&im->peer)); | 448 | GNUNET_i2s (&im->peer)); |
415 | #endif | 449 | #endif |
416 | ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust)); | 450 | ic->h->in_receive = GNUNET_YES; |
451 | if (ic->callback != NULL) | ||
452 | ic->callback (ic->callback_cls, &im->peer, hello, ntohl (im->trust)); | ||
417 | GNUNET_CLIENT_receive (ic->h->client, | 453 | GNUNET_CLIENT_receive (ic->h->client, |
418 | &peerinfo_handler, | 454 | &peerinfo_handler, |
419 | ic, | 455 | ic, |
@@ -442,6 +478,9 @@ iterator_start_receive (void *cls, | |||
442 | GNUNET_free (ic); | 478 | GNUNET_free (ic); |
443 | return; | 479 | return; |
444 | } | 480 | } |
481 | ic->h->in_receive = GNUNET_YES; | ||
482 | ic->in_receive = GNUNET_YES; | ||
483 | ic->tqe = NULL; | ||
445 | GNUNET_CLIENT_receive (ic->h->client, | 484 | GNUNET_CLIENT_receive (ic->h->client, |
446 | &peerinfo_handler, | 485 | &peerinfo_handler, |
447 | ic, | 486 | ic, |
@@ -450,6 +489,31 @@ iterator_start_receive (void *cls, | |||
450 | 489 | ||
451 | 490 | ||
452 | /** | 491 | /** |
492 | * Peerinfo iteration request has timed out. | ||
493 | * | ||
494 | * @param cls the 'struct GNUNET_PEERINFO_NewIteratorContext*' | ||
495 | * @param tc scheduler context | ||
496 | */ | ||
497 | static void | ||
498 | signal_timeout (void *cls, | ||
499 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
500 | { | ||
501 | struct GNUNET_PEERINFO_NewIteratorContext *ic = cls; | ||
502 | |||
503 | ic->timeout_task = GNUNET_SCHEDULER_NO_TASK; | ||
504 | ic->callback (ic->callback_cls, NULL, NULL, 1); | ||
505 | ic->callback = NULL; | ||
506 | if (ic->in_receive) | ||
507 | return; /* need to finish processing */ | ||
508 | GNUNET_CONTAINER_DLL_remove (ic->h->tq_head, | ||
509 | ic->h->tq_tail, | ||
510 | ic->tqe); | ||
511 | GNUNET_free (ic->tqe); | ||
512 | GNUNET_free (ic); | ||
513 | } | ||
514 | |||
515 | |||
516 | /** | ||
453 | * Call a method for each known matching host and change its trust | 517 | * Call a method for each known matching host and change its trust |
454 | * value. The callback method will be invoked once for each matching | 518 | * value. The callback method will be invoked once for each matching |
455 | * host and then finally once with a NULL pointer. After that final | 519 | * host and then finally once with a NULL pointer. After that final |
@@ -510,14 +574,19 @@ GNUNET_PEERINFO_iterate_new (struct GNUNET_PEERINFO_Handle *h, | |||
510 | memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity)); | 574 | memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity)); |
511 | } | 575 | } |
512 | ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NewIteratorContext)); | 576 | ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NewIteratorContext)); |
577 | ic->h = h; | ||
578 | ic->tqe = tqe; | ||
513 | ic->callback = callback; | 579 | ic->callback = callback; |
514 | ic->callback_cls = callback_cls; | 580 | ic->callback_cls = callback_cls; |
515 | ic->timeout = GNUNET_TIME_relative_to_absolute (timeout); | 581 | ic->timeout = GNUNET_TIME_relative_to_absolute (timeout); |
582 | ic->timeout_task = GNUNET_SCHEDULER_add_delayed (h->sched, | ||
583 | timeout, | ||
584 | &signal_timeout, | ||
585 | ic); | ||
516 | tqe->timeout = ic->timeout; | 586 | tqe->timeout = ic->timeout; |
517 | tqe->cont = &iterator_start_receive; | 587 | tqe->cont = &iterator_start_receive; |
518 | tqe->cont_cls = ic; | 588 | tqe->cont_cls = ic; |
519 | /* FIXME: sort DLL by timeout? */ | 589 | tqe->timeout = ic->timeout; |
520 | /* FIXME: add timeout task!? */ | ||
521 | GNUNET_CONTAINER_DLL_insert_after (h->tq_head, | 590 | GNUNET_CONTAINER_DLL_insert_after (h->tq_head, |
522 | h->tq_tail, | 591 | h->tq_tail, |
523 | h->tq_tail, | 592 | h->tq_tail, |
@@ -536,14 +605,24 @@ GNUNET_PEERINFO_iterate_new (struct GNUNET_PEERINFO_Handle *h, | |||
536 | void | 605 | void |
537 | GNUNET_PEERINFO_iterate_cancel_new (struct GNUNET_PEERINFO_NewIteratorContext *ic) | 606 | GNUNET_PEERINFO_iterate_cancel_new (struct GNUNET_PEERINFO_NewIteratorContext *ic) |
538 | { | 607 | { |
539 | GNUNET_assert (0); | 608 | if (ic->timeout_task != GNUNET_SCHEDULER_NO_TASK) |
540 | // FIXME: not implemented | 609 | { |
610 | GNUNET_SCHEDULER_cancel (ic->h->sched, | ||
611 | ic->timeout_task); | ||
612 | ic->timeout_task = GNUNET_SCHEDULER_NO_TASK; | ||
613 | } | ||
614 | ic->callback = NULL; | ||
615 | if (ic->in_receive) | ||
616 | return; /* need to finish processing */ | ||
617 | GNUNET_CONTAINER_DLL_remove (ic->h->tq_head, | ||
618 | ic->h->tq_tail, | ||
619 | ic->tqe); | ||
620 | GNUNET_free (ic->tqe); | ||
621 | GNUNET_free (ic); | ||
541 | } | 622 | } |
542 | 623 | ||
543 | 624 | ||
544 | 625 | ||
545 | |||
546 | |||
547 | /* ***************************** OLD API ****************************** */ | 626 | /* ***************************** OLD API ****************************** */ |
548 | 627 | ||
549 | 628 | ||