aboutsummaryrefslogtreecommitdiff
path: root/src/contrib/service/rps/rps-sampler_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/service/rps/rps-sampler_common.c')
-rw-r--r--src/contrib/service/rps/rps-sampler_common.c742
1 files changed, 742 insertions, 0 deletions
diff --git a/src/contrib/service/rps/rps-sampler_common.c b/src/contrib/service/rps/rps-sampler_common.c
new file mode 100644
index 000000000..e3fb79501
--- /dev/null
+++ b/src/contrib/service/rps/rps-sampler_common.c
@@ -0,0 +1,742 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C)
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file rps/rps-sampler_common.c
23 * @brief Code common to client and service sampler
24 * @author Julius Bünger
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_statistics_service.h"
29
30#include "rps-sampler_common.h"
31#include "gnunet-service-rps_sampler_elem.h"
32
33#include <math.h>
34#include <inttypes.h>
35
36#include "rps-test_util.h"
37
38#define LOG(kind, ...) GNUNET_log_from (kind, "rps-sampler_common", __VA_ARGS__)
39
40/**
41 * @brief Context for a callback. Contains callback and closure.
42 *
43 * Meant to be an entry in an DLL.
44 */
45struct SamplerNotifyUpdateCTX
46{
47 /**
48 * @brief The Callback to call on updates
49 */
50 SamplerNotifyUpdateCB notify_cb;
51
52 /**
53 * @brief The according closure.
54 */
55 void *cls;
56
57 /**
58 * @brief Next element in DLL.
59 */
60 struct SamplerNotifyUpdateCTX *next;
61
62 /**
63 * @brief Previous element in DLL.
64 */
65 struct SamplerNotifyUpdateCTX *prev;
66};
67
68
69/**
70 * Closure to _get_n_rand_peers_ready_cb()
71 */
72struct RPS_SamplerRequestHandle
73{
74 /**
75 * DLL
76 */
77 struct RPS_SamplerRequestHandle *next;
78 struct RPS_SamplerRequestHandle *prev;
79
80 /**
81 * Number of peers we are waiting for.
82 */
83 uint32_t num_peers;
84
85 /**
86 * Number of peers we currently have.
87 */
88 uint32_t cur_num_peers;
89
90 /**
91 * Pointer to the array holding the ids.
92 */
93 struct GNUNET_PeerIdentity *ids;
94
95 /**
96 * Head and tail for the DLL to store the tasks for single requests
97 */
98 struct GetPeerCls *gpc_head;
99 struct GetPeerCls *gpc_tail;
100
101 /**
102 * Sampler.
103 */
104 struct RPS_Sampler *sampler;
105
106 /**
107 * Callback to be called when all ids are available.
108 */
109 RPS_sampler_n_rand_peers_ready_cb callback;
110
111 /**
112 * Closure given to the callback
113 */
114 void *cls;
115};
116
117
118/**
119 * Closure to _get_rand_peer_info()
120 */
121struct RPS_SamplerRequestHandleSingleInfo
122{
123 /**
124 * DLL
125 */
126 struct RPS_SamplerRequestHandleSingleInfo *next;
127 struct RPS_SamplerRequestHandleSingleInfo *prev;
128
129 /**
130 * Pointer to the id
131 */
132 struct GNUNET_PeerIdentity *id;
133
134 /**
135 * Head and tail for the DLL to store the tasks for single requests
136 */
137 struct GetPeerCls *gpc_head;
138 struct GetPeerCls *gpc_tail;
139
140 /**
141 * Sampler.
142 */
143 struct RPS_Sampler *sampler;
144
145 /**
146 * Callback to be called when all ids are available.
147 */
148 RPS_sampler_sinlge_info_ready_cb callback;
149
150 /**
151 * Closure given to the callback
152 */
153 void *cls;
154};
155
156
157/**
158 * @brief Update the current estimate of the network size stored at the sampler
159 *
160 * Used for computing the condition when to return elements to the client
161 *
162 * Only used/useful with the client sampler
163 * (Maybe move to rps-sampler_client.{h|c} ?)
164 *
165 * @param sampler The sampler to update
166 * @param num_peers The estimated value
167 */
168void
169RPS_sampler_update_with_nw_size (struct RPS_Sampler *sampler,
170 uint32_t num_peers)
171{
172 sampler->num_peers_estim = num_peers;
173}
174
175
176/**
177 * @brief Set the probability that is needed at least with what a sampler
178 * element has to have observed all elements from the network.
179 *
180 * Only used/useful with the client sampler
181 * (Maybe move to rps-sampler_client.{h|c} ?)
182 *
183 * @param sampler
184 * @param desired_probability
185 */
186void
187RPS_sampler_set_desired_probability (struct RPS_Sampler *sampler,
188 double desired_probability)
189{
190 sampler->desired_probability = desired_probability;
191}
192
193
194void
195RPS_sampler_set_deficiency_factor (struct RPS_Sampler *sampler,
196 double deficiency_factor)
197{
198 sampler->deficiency_factor = deficiency_factor;
199}
200
201
202/**
203 * @brief Add a callback that will be called when the next peer is inserted
204 * into the sampler
205 *
206 * @param sampler The sampler on which update it will be called
207 * @param notify_cb The callback
208 * @param cls Closure given to the callback
209 *
210 * @return The context containing callback and closure
211 */
212struct SamplerNotifyUpdateCTX *
213sampler_notify_on_update (struct RPS_Sampler *sampler,
214 SamplerNotifyUpdateCB notify_cb,
215 void *cls)
216{
217 struct SamplerNotifyUpdateCTX *notify_ctx;
218
219 LOG (GNUNET_ERROR_TYPE_DEBUG,
220 "Inserting new context for notification\n");
221 notify_ctx = GNUNET_new (struct SamplerNotifyUpdateCTX);
222 notify_ctx->notify_cb = notify_cb;
223 notify_ctx->cls = cls;
224 GNUNET_CONTAINER_DLL_insert (sampler->notify_ctx_head,
225 sampler->notify_ctx_tail,
226 notify_ctx);
227 return notify_ctx;
228}
229
230
231/**
232 * Get the size of the sampler.
233 *
234 * @param sampler the sampler to return the size of.
235 * @return the size of the sampler
236 */
237unsigned int
238RPS_sampler_get_size (struct RPS_Sampler *sampler)
239{
240 return sampler->sampler_size;
241}
242
243
244/**
245 * @brief Notify about update of the sampler.
246 *
247 * Call the callbacks that are waiting for notification on updates to the
248 * sampler.
249 *
250 * @param sampler The sampler the updates are waiting for
251 */
252static void
253notify_update (struct RPS_Sampler *sampler)
254{
255 struct SamplerNotifyUpdateCTX *tmp_notify_head;
256 struct SamplerNotifyUpdateCTX *tmp_notify_tail;
257
258 LOG (GNUNET_ERROR_TYPE_DEBUG,
259 "Calling callbacks waiting for update notification.\n");
260 tmp_notify_head = sampler->notify_ctx_head;
261 tmp_notify_tail = sampler->notify_ctx_tail;
262 sampler->notify_ctx_head = NULL;
263 sampler->notify_ctx_tail = NULL;
264 for (struct SamplerNotifyUpdateCTX *notify_iter = tmp_notify_head;
265 NULL != tmp_notify_head;
266 notify_iter = tmp_notify_head)
267 {
268 GNUNET_assert (NULL != notify_iter->notify_cb);
269 GNUNET_CONTAINER_DLL_remove (tmp_notify_head,
270 tmp_notify_tail,
271 notify_iter);
272 notify_iter->notify_cb (notify_iter->cls);
273 GNUNET_free (notify_iter);
274 }
275}
276
277
278/**
279 * Update every sampler element of this sampler with given peer
280 *
281 * @param sampler the sampler to update.
282 * @param id the PeerID that is put in the sampler
283 */
284void
285RPS_sampler_update (struct RPS_Sampler *sampler,
286 const struct GNUNET_PeerIdentity *id)
287{
288 for (uint32_t i = 0; i < sampler->sampler_size; i++)
289 {
290 RPS_sampler_elem_next (sampler->sampler_elements[i],
291 id);
292 }
293 notify_update (sampler);
294}
295
296
297/**
298 * Reinitialise all previously initialised sampler elements with the given value.
299 *
300 * Used to get rid of a PeerID.
301 *
302 * FIXME: This should also consider currently pending requests
303 * (Pending requests already collect peerids. As long as not all
304 * requested IDs have been collected, they are kept.
305 * Ideally, the @p id should be removed from all pending requests. This
306 * seems quite complicated.)
307 *
308 * @param sampler the sampler to reinitialise a sampler element in.
309 * @param id the id of the sampler elements to update.
310 */
311void
312RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
313 const struct GNUNET_PeerIdentity *id)
314{
315 uint32_t i;
316
317 for (i = 0; i < sampler->sampler_size; i++)
318 {
319 if (0 == GNUNET_memcmp (id,
320 &(sampler->sampler_elements[i]->peer_id)))
321 {
322 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
323 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
324 }
325 }
326}
327
328
329/**
330 * Counts how many Samplers currently hold a given PeerID.
331 *
332 * @param sampler the sampler to count ids in.
333 * @param id the PeerID to count.
334 *
335 * @return the number of occurrences of id.
336 */
337uint32_t
338RPS_sampler_count_id (struct RPS_Sampler *sampler,
339 const struct GNUNET_PeerIdentity *id)
340{
341 uint32_t count;
342 uint32_t i;
343
344 count = 0;
345 for (i = 0; i < sampler->sampler_size; i++)
346 {
347 if ((0 == GNUNET_memcmp (&sampler->sampler_elements[i]->peer_id, id))
348 && (EMPTY != sampler->sampler_elements[i]->is_empty) )
349 count++;
350 }
351 return count;
352}
353
354
355/**
356 * Grow or shrink the size of the sampler.
357 *
358 * @param sampler the sampler to resize.
359 * @param new_size the new size of the sampler
360 */
361static void
362sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
363{
364 unsigned int old_size;
365 uint32_t i;
366
367 // TODO check min and max size
368
369 old_size = sampler->sampler_size;
370
371 if (old_size > new_size)
372 { /* Shrinking */
373 LOG (GNUNET_ERROR_TYPE_DEBUG,
374 "Shrinking sampler %d -> %d\n",
375 old_size,
376 new_size);
377
378 for (i = new_size; i < old_size; i++)
379 {
380 RPS_sampler_elem_destroy (sampler->sampler_elements[i]);
381 }
382
383 GNUNET_array_grow (sampler->sampler_elements,
384 sampler->sampler_size,
385 new_size);
386 LOG (GNUNET_ERROR_TYPE_DEBUG,
387 "sampler->sampler_elements now points to %p\n",
388 sampler->sampler_elements);
389 }
390 else if (old_size < new_size)
391 { /* Growing */
392 LOG (GNUNET_ERROR_TYPE_DEBUG,
393 "Growing sampler %d -> %d\n",
394 old_size,
395 new_size);
396
397 GNUNET_array_grow (sampler->sampler_elements,
398 sampler->sampler_size,
399 new_size);
400
401 for (i = old_size; i < new_size; i++)
402 { /* Add new sampler elements */
403 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
404 }
405 }
406 else
407 {
408 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
409 return;
410 }
411
412 GNUNET_assert (sampler->sampler_size == new_size);
413}
414
415
416/**
417 * Grow or shrink the size of the sampler.
418 *
419 * @param sampler the sampler to resize.
420 * @param new_size the new size of the sampler
421 */
422void
423RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
424{
425 GNUNET_assert (0 < new_size);
426 sampler_resize (sampler, new_size);
427}
428
429
430/**
431 * Empty the sampler.
432 *
433 * @param sampler the sampler to empty.
434 */
435static void
436sampler_empty (struct RPS_Sampler *sampler)
437{
438 sampler_resize (sampler, 0);
439}
440
441
442/**
443 * Callback to _get_rand_peer() used by _get_n_rand_peers().
444 *
445 * Implements #RPS_sampler_rand_peer_ready_cont
446 *
447 * Checks whether all n peers are available. If they are,
448 * give those back.
449 * @param cls Closure
450 * @param id Peer ID
451 * @param probability The probability with which this sampler has seen all ids
452 * @param num_observed How many ids this sampler has observed
453 */
454static void
455check_n_peers_ready (void *cls,
456 const struct GNUNET_PeerIdentity *id,
457 double probability,
458 uint32_t num_observed)
459{
460 struct RPS_SamplerRequestHandle *req_handle = cls;
461
462 (void) id;
463 RPS_sampler_n_rand_peers_ready_cb tmp_cb;
464 struct GNUNET_PeerIdentity *peers;
465 uint32_t num_peers;
466 void *cb_cls;
467 (void) probability;
468 (void) num_observed;
469
470 req_handle->cur_num_peers++;
471 LOG (GNUNET_ERROR_TYPE_DEBUG,
472 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
473 req_handle->cur_num_peers, req_handle->num_peers);
474
475 if (req_handle->num_peers == req_handle->cur_num_peers)
476 { /* All peers are ready -- return those to the client */
477 GNUNET_assert (NULL != req_handle->callback);
478
479 LOG (GNUNET_ERROR_TYPE_DEBUG,
480 "returning %" PRIX32 " peers to the client\n",
481 req_handle->num_peers);
482
483 /* Copy pointers and peers temporarily as they
484 * might be deleted from within the callback */
485 tmp_cb = req_handle->callback;
486 num_peers = req_handle->num_peers;
487 peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
488 GNUNET_memcpy (peers,
489 req_handle->ids,
490 num_peers * sizeof(struct GNUNET_PeerIdentity));
491 cb_cls = req_handle->cls;
492 RPS_sampler_request_cancel (req_handle);
493 req_handle = NULL;
494 tmp_cb (peers, num_peers, cb_cls);
495 GNUNET_free (peers);
496 }
497}
498
499
500/**
501 * Callback to _get_rand_peer() used by _get_rand_peer_info().
502 *
503 * Implements #RPS_sampler_rand_peer_ready_cont
504 *
505 * @param cls Closure
506 * @param id Peer ID
507 * @param probability The probability with which this sampler has seen all ids
508 * @param num_observed How many ids this sampler has observed
509 */
510static void
511check_peer_info_ready (void *cls,
512 const struct GNUNET_PeerIdentity *id,
513 double probability,
514 uint32_t num_observed)
515{
516 struct RPS_SamplerRequestHandleSingleInfo *req_handle = cls;
517
518 (void) id;
519 RPS_sampler_sinlge_info_ready_cb tmp_cb;
520 struct GNUNET_PeerIdentity *peer;
521 void *cb_cls;
522 (void) probability;
523 (void) num_observed;
524
525 LOG (GNUNET_ERROR_TYPE_DEBUG,
526 "Got single peer with additional info\n");
527
528 GNUNET_assert (NULL != req_handle->callback);
529
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "returning single peer with info to the client\n");
532
533 /* Copy pointers and peers temporarily as they
534 * might be deleted from within the callback */
535 tmp_cb = req_handle->callback;
536 peer = GNUNET_new (struct GNUNET_PeerIdentity);
537 GNUNET_memcpy (peer,
538 req_handle->id,
539 sizeof(struct GNUNET_PeerIdentity));
540 cb_cls = req_handle->cls;
541 RPS_sampler_request_single_info_cancel (req_handle);
542 req_handle = NULL;
543 tmp_cb (peer, cb_cls, probability, num_observed);
544 GNUNET_free (peer);
545}
546
547
548struct RPS_SamplerRequestHandle *
549RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
550 uint32_t num_peers,
551 RPS_sampler_n_rand_peers_ready_cb cb,
552 void *cls)
553{
554 uint32_t i;
555 struct RPS_SamplerRequestHandle *req_handle;
556 struct GetPeerCls *gpc;
557
558 GNUNET_assert (0 != sampler->sampler_size);
559 if (0 == num_peers)
560 return NULL;
561
562 // TODO check if we have too much (distinct) sampled peers
563 req_handle = GNUNET_new (struct RPS_SamplerRequestHandle);
564 req_handle->num_peers = num_peers;
565 req_handle->cur_num_peers = 0;
566 req_handle->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
567 req_handle->sampler = sampler;
568 req_handle->callback = cb;
569 req_handle->cls = cls;
570 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_head,
571 sampler->req_handle_tail,
572 req_handle);
573
574 LOG (GNUNET_ERROR_TYPE_DEBUG,
575 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
576
577 for (i = 0; i < num_peers; i++)
578 {
579 gpc = GNUNET_new (struct GetPeerCls);
580 gpc->req_handle = req_handle;
581 gpc->req_single_info_handle = NULL;
582 gpc->cont = check_n_peers_ready;
583 gpc->cont_cls = req_handle;
584 gpc->id = &req_handle->ids[i];
585
586 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
587 req_handle->gpc_tail,
588 gpc);
589 // maybe add a little delay
590 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers,
591 gpc);
592 }
593 return req_handle;
594}
595
596
597/**
598 * Get one random peer with additional information.
599 *
600 * @param sampler the sampler to get peers from.
601 * @param cb callback that will be called once the ids are ready.
602 * @param cls closure given to @a cb
603 */
604struct RPS_SamplerRequestHandleSingleInfo *
605RPS_sampler_get_rand_peer_info (struct RPS_Sampler *sampler,
606 RPS_sampler_sinlge_info_ready_cb cb,
607 void *cls)
608{
609 struct RPS_SamplerRequestHandleSingleInfo *req_handle;
610 struct GetPeerCls *gpc;
611
612 GNUNET_assert (0 != sampler->sampler_size);
613
614 // TODO check if we have too much (distinct) sampled peers
615 req_handle = GNUNET_new (struct RPS_SamplerRequestHandleSingleInfo);
616 req_handle->id = GNUNET_malloc (sizeof(struct GNUNET_PeerIdentity));
617 req_handle->sampler = sampler;
618 req_handle->callback = cb;
619 req_handle->cls = cls;
620 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_single_head,
621 sampler->req_handle_single_tail,
622 req_handle);
623
624 gpc = GNUNET_new (struct GetPeerCls);
625 gpc->req_handle = NULL;
626 gpc->req_single_info_handle = req_handle;
627 gpc->cont = check_peer_info_ready;
628 gpc->cont_cls = req_handle;
629 gpc->id = req_handle->id;
630
631 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
632 req_handle->gpc_tail,
633 gpc);
634 // maybe add a little delay
635 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers,
636 gpc);
637 return req_handle;
638}
639
640
641/**
642 * Cancel a request issued through #RPS_sampler_n_rand_peers_ready_cb.
643 *
644 * @param req_handle the handle to the request
645 */
646void
647RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle)
648{
649 struct GetPeerCls *i;
650
651 while (NULL != (i = req_handle->gpc_head))
652 {
653 GNUNET_CONTAINER_DLL_remove (req_handle->gpc_head,
654 req_handle->gpc_tail,
655 i);
656 if (NULL != i->get_peer_task)
657 {
658 GNUNET_SCHEDULER_cancel (i->get_peer_task);
659 }
660 if (NULL != i->notify_ctx)
661 {
662 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->notify_ctx_head,
663 req_handle->sampler->notify_ctx_tail,
664 i->notify_ctx);
665 GNUNET_free (i->notify_ctx);
666 i->notify_ctx = NULL;
667 }
668 GNUNET_free (i);
669 }
670 GNUNET_free (req_handle->ids);
671 req_handle->ids = NULL;
672 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->req_handle_head,
673 req_handle->sampler->req_handle_tail,
674 req_handle);
675 GNUNET_free (req_handle);
676}
677
678
679/**
680 * Cancel a request issued through #RPS_sampler_sinlge_info_ready_cb.
681 *
682 * @param req_handle the handle to the request
683 */
684void
685RPS_sampler_request_single_info_cancel (
686 struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle)
687{
688 struct GetPeerCls *i;
689
690 while (NULL != (i = req_single_info_handle->gpc_head))
691 {
692 GNUNET_CONTAINER_DLL_remove (req_single_info_handle->gpc_head,
693 req_single_info_handle->gpc_tail,
694 i);
695 if (NULL != i->get_peer_task)
696 {
697 GNUNET_SCHEDULER_cancel (i->get_peer_task);
698 }
699 if (NULL != i->notify_ctx)
700 {
701 GNUNET_CONTAINER_DLL_remove (
702 req_single_info_handle->sampler->notify_ctx_head,
703 req_single_info_handle->sampler->
704 notify_ctx_tail,
705 i->notify_ctx);
706 GNUNET_free (i->notify_ctx);
707 i->notify_ctx = NULL;
708 }
709 GNUNET_free (i);
710 }
711 GNUNET_free (req_single_info_handle->id);
712 req_single_info_handle->id = NULL;
713 GNUNET_CONTAINER_DLL_remove (
714 req_single_info_handle->sampler->req_handle_single_head,
715 req_single_info_handle->sampler->
716 req_handle_single_tail,
717 req_single_info_handle);
718 GNUNET_free (req_single_info_handle);
719}
720
721
722/**
723 * Cleans the sampler.
724 */
725void
726RPS_sampler_destroy (struct RPS_Sampler *sampler)
727{
728 if (NULL != sampler->req_handle_head)
729 {
730 LOG (GNUNET_ERROR_TYPE_WARNING,
731 "There are still pending requests. Going to remove them.\n");
732 while (NULL != sampler->req_handle_head)
733 {
734 RPS_sampler_request_cancel (sampler->req_handle_head);
735 }
736 }
737 sampler_empty (sampler);
738 GNUNET_free (sampler);
739}
740
741
742/* end of rps-sampler_common.c */