aboutsummaryrefslogtreecommitdiff
path: root/src/rps/rps_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rps/rps_api.c')
-rw-r--r--src/rps/rps_api.c132
1 files changed, 122 insertions, 10 deletions
diff --git a/src/rps/rps_api.c b/src/rps/rps_api.c
index 7afe21d3a..a558c8a35 100644
--- a/src/rps/rps_api.c
+++ b/src/rps/rps_api.c
@@ -25,6 +25,7 @@
25#include "gnunet_util_lib.h" 25#include "gnunet_util_lib.h"
26#include "rps.h" 26#include "rps.h"
27#include "gnunet_rps_service.h" 27#include "gnunet_rps_service.h"
28#include "gnunet-service-rps_sampler.h"
28 29
29#include <inttypes.h> 30#include <inttypes.h>
30 31
@@ -524,6 +525,39 @@ GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
524 525
525 526
526/** 527/**
528 * @brief Create new request handle
529 *
530 * @param rps_handle Handle to the service
531 * @param num_requests Number of requests
532 * @param ready_cb Callback
533 * @param cls Closure
534 *
535 * @return The newly created request handle
536 */
537static struct GNUNET_RPS_Request_Handle *
538new_request_handle (struct GNUNET_RPS_Handle *rps_handle,
539 uint64_t num_requests,
540 struct RPS_Sampler *sampler,
541 GNUNET_RPS_NotifyReadyCB ready_cb,
542 void *cls)
543{
544 struct GNUNET_RPS_Request_Handle *rh;
545
546 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle);
547 rh->rps_handle = rps_handle;
548 rh->id = rps_handle->current_request_id++;
549 rh->num_requests = num_requests;
550 rh->sampler = sampler;
551 rh->ready_cb = ready_cb;
552 rh->ready_cb_cls = cls;
553 GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
554 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
555
556 return rh;
557}
558
559
560/**
527 * Request n random peers. 561 * Request n random peers.
528 * 562 *
529 * @param rps_handle handle to the rps service 563 * @param rps_handle handle to the rps service
@@ -533,34 +567,112 @@ GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
533 * @return a handle to cancel this request 567 * @return a handle to cancel this request
534 */ 568 */
535struct GNUNET_RPS_Request_Handle * 569struct GNUNET_RPS_Request_Handle *
536GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle, 570GNUNET_RPS_request_peers_2 (struct GNUNET_RPS_Handle *rps_handle,
537 uint32_t num_req_peers, 571 uint32_t num_req_peers,
538 GNUNET_RPS_NotifyReadyCB ready_cb, 572 GNUNET_RPS_NotifyReadyCB ready_cb,
539 void *cls) 573 void *cls)
540{ 574{
541 struct GNUNET_RPS_Request_Handle *rh; 575 struct GNUNET_RPS_Request_Handle *rh;
542 576
543 rh = GNUNET_new (struct GNUNET_RPS_Request_Handle); 577 rh = new_request_handle (rps_handle,
544 rh->rps_handle = rps_handle; 578 num_req_peers,
545 rh->id = rps_handle->current_request_id++; 579 NULL, /* no sampler needed */
546 rh->num_peers = num_req_peers; 580 ready_cb,
547 rh->ready_cb = ready_cb; 581 cls);
548 rh->ready_cb_cls = cls;
549 582
550 LOG (GNUNET_ERROR_TYPE_DEBUG, 583 LOG (GNUNET_ERROR_TYPE_DEBUG,
551 "Requesting %" PRIu32 " peers with id %" PRIu32 "\n", 584 "Requesting %" PRIu32 " peers with id %" PRIu32 "\n",
552 num_req_peers, 585 num_req_peers,
553 rh->id); 586 rh->id);
554 587
555 GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh,
556 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
557
558 send_request (rps_handle, rh->id, num_req_peers); 588 send_request (rps_handle, rh->id, num_req_peers);
559 return rh; 589 return rh;
560} 590}
561 591
562 592
563/** 593/**
594 * @brief Callback to collect the peers from the biased stream and put those
595 * into the sampler.
596 *
597 * @param cls The #GNUNET_RPS_Request_Handle
598 * @param num_peers The number of peer that have been returned
599 * @param peers The array of @a num_peers that have been returned
600 */
601void
602collect_peers_cb (void *cls,
603 uint64_t num_peers,
604 const struct GNUNET_PeerIdentity *peers)
605{
606 struct GNUNET_RPS_Request_Handle *rh = cls;
607
608 for (uint64_t i = 0; i < num_peers; i++)
609 {
610 RPS_sampler_update (rh->sampler, &peers[i]);
611 }
612}
613
614
615/**
616 * @brief Called once the sampler has collected all requested peers.
617 *
618 * Calls the callback provided by the client with the corresponding cls.
619 *
620 * @param peers The array of @a num_peers that has been returned.
621 * @param num_peers The number of peers that have been returned
622 * @param cls The #GNUNET_RPS_Request_Handle
623 */
624void
625peers_ready_cb (const struct GNUNET_PeerIdentity *peers,
626 uint32_t num_peers,
627 void *cls)
628{
629 struct GNUNET_RPS_Request_Handle *rh = cls;
630
631 rh->ready_cb (rh->ready_cb_cls,
632 num_peers,
633 peers);
634 // TODO cleanup, sampler, rh, cancel stuff
635 // TODO screw this function. We can give the cb,cls directly to the sampler.
636}
637
638/**
639 * Request n random peers.
640 *
641 * @param rps_handle handle to the rps service
642 * @param num_req_peers number of peers we want to receive
643 * @param ready_cb the callback called when the peers are available
644 * @param cls closure given to the callback
645 * @return a handle to cancel this request
646 */
647struct GNUNET_RPS_Request_Handle *
648GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle,
649 uint32_t num_req_peers,
650 GNUNET_RPS_NotifyReadyCB ready_cb,
651 void *cls)
652{
653 struct GNUNET_RPS_Request_Handle *rh;
654
655 rh = new_request_handle (rps_handle,
656 num_req_peers,
657 RPS_sampler_mod_init (num_req_peers,
658 GNUNET_TIME_UNIT_SECONDS), // TODO remove this time-stuff
659 ready_cb,
660 cls);
661 RPS_sampler_get_n_rand_peers (rh->sampler,
662 num_req_peers,
663 peers_ready_cb,
664 rh);
665
666 GNUNET_RPS_stream_request (rps_handle,
667 0, /* infinite updates */
668 collect_peers_cb,
669 rh); /* cls */
670
671 return rh;
672}
673
674
675/**
564 * Seed rps service with peerIDs. 676 * Seed rps service with peerIDs.
565 * 677 *
566 * @param h handle to the rps service 678 * @param h handle to the rps service