diff options
author | Julius Bünger <buenger@mytum.de> | 2015-01-16 00:57:50 +0000 |
---|---|---|
committer | Julius Bünger <buenger@mytum.de> | 2015-01-16 00:57:50 +0000 |
commit | d9a59c38bc4928dceacb2cacbc6aa361e0381996 (patch) | |
tree | 111d8f93a91ecf0c8aed742d13b53ad6fe7232bf /src/rps/gnunet-service-rps_sampler.c | |
parent | 13faf361bba55fda139ce33d7e57af02ec4c9ce0 (diff) | |
download | gnunet-d9a59c38bc4928dceacb2cacbc6aa361e0381996.tar.gz gnunet-d9a59c38bc4928dceacb2cacbc6aa361e0381996.zip |
restructured the sampler
Diffstat (limited to 'src/rps/gnunet-service-rps_sampler.c')
-rw-r--r-- | src/rps/gnunet-service-rps_sampler.c | 112 |
1 files changed, 92 insertions, 20 deletions
diff --git a/src/rps/gnunet-service-rps_sampler.c b/src/rps/gnunet-service-rps_sampler.c index 93b6b1263..1ab00aaf5 100644 --- a/src/rps/gnunet-service-rps_sampler.c +++ b/src/rps/gnunet-service-rps_sampler.c | |||
@@ -133,6 +133,47 @@ struct RPS_Sampler | |||
133 | }; | 133 | }; |
134 | 134 | ||
135 | /** | 135 | /** |
136 | * Closure to _get_n_rand_peers_ready_cb() | ||
137 | */ | ||
138 | struct RPS_GetNRandPeersReadyCls | ||
139 | { | ||
140 | /** | ||
141 | * Number of peers we are waiting for. | ||
142 | */ | ||
143 | uint64_t num_peers; | ||
144 | |||
145 | /** | ||
146 | * Number of peers we currently have. | ||
147 | */ | ||
148 | uint64_t cur_num_peers; | ||
149 | |||
150 | /** | ||
151 | * Pointer to the array holding the ids. | ||
152 | */ | ||
153 | struct GNUNET_PeerIdentity *ids; | ||
154 | |||
155 | /** | ||
156 | * Callback to be called when all ids are available. | ||
157 | */ | ||
158 | RPS_sampler_n_rand_peers_ready_cb callback; | ||
159 | |||
160 | /** | ||
161 | * Closure given to the callback | ||
162 | */ | ||
163 | void *cls; | ||
164 | }; | ||
165 | |||
166 | /** | ||
167 | * Callback that is called from _get_rand_peer() when the PeerID is ready. | ||
168 | * | ||
169 | * @param cls the closure given alongside this function. | ||
170 | * @param id the PeerID that was returned | ||
171 | */ | ||
172 | typedef void | ||
173 | (*RPS_sampler_rand_peer_ready_cb) (void *cls, | ||
174 | const struct GNUNET_PeerIdentity *id); | ||
175 | |||
176 | /** | ||
136 | * Global sampler variable. | 177 | * Global sampler variable. |
137 | */ | 178 | */ |
138 | struct RPS_Sampler *sampler; | 179 | struct RPS_Sampler *sampler; |
@@ -160,6 +201,31 @@ static uint64_t client_get_index; | |||
160 | 201 | ||
161 | 202 | ||
162 | /** | 203 | /** |
204 | * Callback to _get_rand_peer() used by _get_n_rand_peers(). | ||
205 | * | ||
206 | * Checks whether all n peers are available. If they are, | ||
207 | * give those back. | ||
208 | */ | ||
209 | void | ||
210 | RPS_sampler_get_n_rand_peers_ready_cb (void *cls, | ||
211 | const struct GNUNET_PeerIdentity *id) | ||
212 | { | ||
213 | struct RPS_GetNRandPeersReadyCls *n_peers_cls; | ||
214 | |||
215 | n_peers_cls = (struct RPS_GetNRandPeersReadyCls *) cls; | ||
216 | |||
217 | if (n_peers_cls->num_peers == n_peers_cls->cur_num_peers) | ||
218 | { | ||
219 | GNUNET_assert (NULL != n_peers_cls->callback); | ||
220 | |||
221 | n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers); | ||
222 | |||
223 | GNUNET_free (n_peers_cls); | ||
224 | } | ||
225 | } | ||
226 | |||
227 | |||
228 | /** | ||
163 | * Reinitialise a previously initialised sampler element. | 229 | * Reinitialise a previously initialised sampler element. |
164 | * | 230 | * |
165 | * @param sampler pointer to the memory that keeps the value. | 231 | * @param sampler pointer to the memory that keeps the value. |
@@ -507,20 +573,22 @@ RPS_sampler_get_n_rand_peers_ (uint64_t n) | |||
507 | * | 573 | * |
508 | * @return a random PeerID of the PeerIDs previously put into the sampler. | 574 | * @return a random PeerID of the PeerIDs previously put into the sampler. |
509 | */ | 575 | */ |
510 | const struct GNUNET_PeerIdentity * | 576 | void |
511 | RPS_sampler_get_rand_peer () | 577 | RPS_sampler_get_rand_peer (RPS_sampler_rand_peer_ready_cb cb, |
578 | void *cls, struct GNUNET_PeerIdentity *id) | ||
512 | { | 579 | { |
513 | struct GNUNET_PeerIdentity *peer; | 580 | do |
581 | { | ||
582 | *id = sampler->sampler_elements[client_get_index]->peer_id; | ||
514 | 583 | ||
515 | // use _get_rand_peer_ ? | ||
516 | peer = GNUNET_new (struct GNUNET_PeerIdentity); | ||
517 | *peer = sampler->sampler_elements[client_get_index]->peer_id; | ||
518 | RPS_sampler_elem_reinit (sampler->sampler_elements[client_get_index]); | 584 | RPS_sampler_elem_reinit (sampler->sampler_elements[client_get_index]); |
519 | if ( client_get_index == sampler->sampler_size ) | 585 | if ( client_get_index == sampler->sampler_size ) |
520 | client_get_index = 0; | 586 | client_get_index = 0; |
521 | else | 587 | else |
522 | client_get_index++; | 588 | client_get_index++; |
523 | return peer; | 589 | } while (NOT_EMPTY == sampler->sampler_elements[client_get_index]->is_empty); |
590 | |||
591 | cb (cls, id); | ||
524 | } | 592 | } |
525 | 593 | ||
526 | 594 | ||
@@ -531,35 +599,39 @@ RPS_sampler_get_rand_peer () | |||
531 | * corrsponding peer to the client. | 599 | * corrsponding peer to the client. |
532 | * Random with or without consumption? | 600 | * Random with or without consumption? |
533 | * | 601 | * |
534 | * @return n random PeerIDs of the PeerIDs previously put into the sampler. | 602 | * @param cb callback that will be called once the ids are ready. |
603 | * @param cls closure given to @a cb | ||
604 | * @param num_peers the number of peers requested | ||
535 | */ | 605 | */ |
536 | const struct GNUNET_PeerIdentity * | 606 | void |
537 | RPS_sampler_get_n_rand_peers (uint64_t n) | 607 | RPS_sampler_get_n_rand_peers (RPS_sampler_n_rand_peers_ready_cb cb, |
608 | void *cls, uint64_t num_peers) | ||
538 | { | 609 | { |
539 | // use _get_rand_peers_ ? | 610 | // use _get_rand_peers_ ? |
540 | if ( 0 == sampler->sampler_size ) | 611 | if ( 0 == sampler->sampler_size ) |
541 | { | 612 | { |
542 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 613 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
543 | "Sgrp: List empty - Returning NULL\n"); | 614 | "Sgrp: List empty - Returning NULL\n"); |
544 | return NULL; | ||
545 | } | 615 | } |
546 | else | 616 | else |
547 | { | 617 | { |
548 | // TODO check if we have too much (distinct) sampled peers | 618 | // TODO check if we have too much (distinct) sampled peers |
549 | // If we are not ready yet maybe schedule for later | 619 | // If we are not ready yet maybe schedule for later |
550 | struct GNUNET_PeerIdentity *peers; | 620 | struct GNUNET_PeerIdentity *peers; |
551 | const struct GNUNET_PeerIdentity *peer; | ||
552 | uint64_t i; | 621 | uint64_t i; |
622 | struct RPS_GetNRandPeersReadyCls *cb_cls; | ||
553 | 623 | ||
554 | peers = GNUNET_malloc (n * sizeof (struct GNUNET_PeerIdentity)); | 624 | peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity); |
555 | 625 | ||
556 | for ( i = 0 ; i < n ; i++ ) { | 626 | cb_cls = GNUNET_new (struct RPS_GetNRandPeersReadyCls); |
557 | //peers[i] = RPS_sampler_get_rand_peer_(sampler->sampler_elements); | 627 | cb_cls->num_peers = num_peers; |
558 | peer = RPS_sampler_get_rand_peer (); | 628 | cb_cls->cur_num_peers = 0; |
559 | memcpy (&peers[i], peer, sizeof (struct GNUNET_PeerIdentity)); | 629 | cb_cls->callback = NULL; |
560 | //GNUNET_free (peer); | 630 | cb_cls->cls = NULL; |
561 | } | 631 | |
562 | return peers; | 632 | for ( i = 0 ; i < num_peers ; i++ ) |
633 | RPS_sampler_get_rand_peer (RPS_sampler_get_n_rand_peers_ready_cb, | ||
634 | cb_cls, &peers[i]); | ||
563 | } | 635 | } |
564 | } | 636 | } |
565 | 637 | ||