2 This file is part of GNUnet.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file rps/gnunet-service-rps_sampler.c
23 * @brief sampler implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
30 #include "gnunet-service-rps_sampler.h"
31 #include "gnunet-service-rps_sampler_elem.h"
36 #include "rps-test_util.h"
38 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
41 // multiple 'clients'?
43 // TODO check for overflows
45 // TODO align message structs
47 // hist_size_init, hist_size_max
49 /***********************************************************************
50 * WARNING: This section needs to be reviewed regarding the use of
51 * functions providing (pseudo)randomness!
52 ***********************************************************************/
54 // TODO care about invalid input of the caller (size 0 or less...)
57 * Callback that is called from _get_rand_peer() when the PeerID is ready.
59 * @param cls the closure given alongside this function.
60 * @param id the PeerID that was returned
63 (*RPS_sampler_rand_peer_ready_cont) (void *cls,
64 const struct GNUNET_PeerIdentity *id);
68 * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
75 struct GetPeerCls *next;
80 struct GetPeerCls *prev;
83 * The sampler this function operates on.
85 struct RPS_Sampler *sampler;
88 * The task for this function.
90 struct GNUNET_SCHEDULER_Task *get_peer_task;
95 RPS_sampler_rand_peer_ready_cont cont;
98 * The closure to the callback @e cont
103 * The address of the id to be stored at
105 struct GNUNET_PeerIdentity *id;
110 * Type of function used to differentiate between modified and not modified
114 (*RPS_get_peers_type) (void *cls,
115 const struct GNUNET_SCHEDULER_TaskContext *tc);
118 * Get one random peer out of the sampled peers.
120 * We might want to reinitialise this sampler after giving the
121 * corrsponding peer to the client.
122 * Only used internally
125 sampler_get_rand_peer (void *cls,
126 const struct GNUNET_SCHEDULER_TaskContext *tc);
129 * Get one random peer out of the sampled peers.
131 * We might want to reinitialise this sampler after giving the
132 * corrsponding peer to the client.
135 sampler_mod_get_rand_peer (void *cls,
136 const struct GNUNET_SCHEDULER_TaskContext *tc);
140 * Sampler with its own array of SamplerElements
145 * Number of sampler elements we hold.
147 unsigned int sampler_size;
151 * All sampler elements in one array.
153 struct RPS_SamplerElement **sampler_elements;
156 * Maximum time a round takes
158 * Used in the context of RPS
160 struct GNUNET_TIME_Relative max_round_interval;
163 * Stores the function to return peers. Which one it is depends on whether
164 * the Sampler is the modified one or not.
166 RPS_get_peers_type get_peers;
169 * Head for the DLL to store the closures to pending requests.
171 struct GetPeerCls *gpc_head;
174 * Tail for the DLL to store the closures to pending requests.
176 struct GetPeerCls *gpc_tail;
180 * File name to log to
187 * Closure to _get_n_rand_peers_ready_cb()
189 struct NRandPeersReadyCls
192 * Number of peers we are waiting for.
197 * Number of peers we currently have.
199 uint32_t cur_num_peers;
202 * Pointer to the array holding the ids.
204 struct GNUNET_PeerIdentity *ids;
207 * Callback to be called when all ids are available.
209 RPS_sampler_n_rand_peers_ready_cb callback;
212 * Closure given to the callback
218 // * Global sampler variable.
220 //struct RPS_Sampler *sampler;
224 * The minimal size for the extended sampler elements.
226 static size_t min_size;
229 * The maximal size the extended sampler elements should grow to.
231 static size_t max_size;
234 * The size the extended sampler elements currently have.
236 //static size_t extra_size;
239 * Inedex to the sampler element that is the next to be returned
241 static uint32_t client_get_index;
245 * Callback to _get_rand_peer() used by _get_n_rand_peers().
247 * Checks whether all n peers are available. If they are,
251 check_n_peers_ready (void *cls,
252 const struct GNUNET_PeerIdentity *id)
254 struct NRandPeersReadyCls *n_peers_cls = cls;
256 n_peers_cls->cur_num_peers++;
257 LOG (GNUNET_ERROR_TYPE_DEBUG,
258 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
259 n_peers_cls->cur_num_peers, n_peers_cls->num_peers);
261 if (n_peers_cls->num_peers == n_peers_cls->cur_num_peers)
262 { /* All peers are ready -- return those to the client */
263 GNUNET_assert (NULL != n_peers_cls->callback);
265 LOG (GNUNET_ERROR_TYPE_DEBUG,
266 "returning %" PRIX32 " peers to the client\n",
267 n_peers_cls->num_peers);
268 n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers);
270 GNUNET_free (n_peers_cls);
276 * Get the size of the sampler.
278 * @param sampler the sampler to return the size of.
279 * @return the size of the sampler
282 RPS_sampler_get_size (struct RPS_Sampler *sampler)
284 return sampler->sampler_size;
289 * Grow or shrink the size of the sampler.
291 * @param sampler the sampler to resize.
292 * @param new_size the new size of the sampler
295 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
297 unsigned int old_size;
300 // TODO check min and max size
302 old_size = sampler->sampler_size;
304 if (old_size > new_size)
307 LOG (GNUNET_ERROR_TYPE_DEBUG,
308 "Shrinking sampler %d -> %d\n",
312 to_file (sampler->file_name,
313 "Shrinking sampler %d -> %d",
317 for (i = new_size ; i < old_size ; i++)
319 to_file (sampler->file_name,
322 sampler->sampler_elements[i]->file_name);
325 GNUNET_array_grow (sampler->sampler_elements,
326 sampler->sampler_size,
328 LOG (GNUNET_ERROR_TYPE_DEBUG,
329 "sampler->sampler_elements now points to %p\n",
330 sampler->sampler_elements);
333 else if (old_size < new_size)
335 LOG (GNUNET_ERROR_TYPE_DEBUG,
336 "Growing sampler %d -> %d\n",
340 to_file (sampler->file_name,
341 "Growing sampler %d -> %d",
345 GNUNET_array_grow (sampler->sampler_elements,
346 sampler->sampler_size,
349 for (i = old_size ; i < new_size ; i++)
350 { /* Add new sampler elements */
351 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
353 to_file (sampler->file_name,
356 sampler->sampler_elements[i]->file_name);
361 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
365 GNUNET_assert (sampler->sampler_size == new_size);
370 * Grow or shrink the size of the sampler.
372 * @param sampler the sampler to resize.
373 * @param new_size the new size of the sampler
376 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
378 GNUNET_assert (0 < new_size);
379 sampler_resize (sampler, new_size);
386 * @param sampler the sampler to empty.
387 * @param new_size the new size of the sampler
390 sampler_empty (struct RPS_Sampler *sampler)
392 sampler_resize (sampler, 0);
397 * Initialise a tuple of sampler elements.
399 * @param init_size the size the sampler is initialised with
400 * @param max_round_interval maximum time a round takes
401 * @return a handle to a sampler that consists of sampler elements.
404 RPS_sampler_init (size_t init_size,
405 struct GNUNET_TIME_Relative max_round_interval)
407 struct RPS_Sampler *sampler;
409 /* Initialise context around extended sampler */
410 min_size = 10; // TODO make input to _samplers_init()
411 max_size = 1000; // TODO make input to _samplers_init()
413 sampler = GNUNET_new (struct RPS_Sampler);
416 sampler->file_name = create_file ("sampler-");
418 LOG (GNUNET_ERROR_TYPE_DEBUG,
419 "Initialised sampler %s\n",
423 sampler->sampler_size = 0;
424 sampler->sampler_elements = NULL;
425 sampler->max_round_interval = max_round_interval;
426 sampler->get_peers = sampler_get_rand_peer;
427 sampler->gpc_head = NULL;
428 sampler->gpc_tail = NULL;
429 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
430 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
431 RPS_sampler_resize (sampler, init_size);
433 client_get_index = 0;
435 //GNUNET_assert (init_size == sampler->sampler_size);
440 * Initialise a modified tuple of sampler elements.
442 * @param init_size the size the sampler is initialised with
443 * @param max_round_interval maximum time a round takes
444 * @return a handle to a sampler that consists of sampler elements.
447 RPS_sampler_mod_init (size_t init_size,
448 struct GNUNET_TIME_Relative max_round_interval)
450 struct RPS_Sampler *sampler;
452 sampler = RPS_sampler_init (init_size, max_round_interval);
453 sampler->get_peers = sampler_mod_get_rand_peer;
455 LOG (GNUNET_ERROR_TYPE_DEBUG,
456 "Initialised modified sampler %s\n",
458 to_file (sampler->file_name,
459 "This is a modified sampler");
466 * A fuction to update every sampler in the given list
468 * @param sampler the sampler to update.
469 * @param id the PeerID that is put in the sampler
472 RPS_sampler_update (struct RPS_Sampler *sampler,
473 const struct GNUNET_PeerIdentity *id)
477 to_file (sampler->file_name,
479 GNUNET_i2s_full (id));
481 for (i = 0 ; i < sampler->sampler_size ; i++)
483 RPS_sampler_elem_next (sampler->sampler_elements[i],
491 * Reinitialise all previously initialised sampler elements with the given value.
493 * Used to get rid of a PeerID.
495 * @param sampler the sampler to reinitialise a sampler element in.
496 * @param id the id of the sampler elements to update.
499 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
500 const struct GNUNET_PeerIdentity *id)
503 struct RPS_SamplerElement *trash_entry;
505 for ( i = 0 ; i < sampler->sampler_size ; i++ )
507 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) )
509 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
510 trash_entry = GNUNET_new (struct RPS_SamplerElement);
511 *trash_entry = *(sampler->sampler_elements[i]);
512 to_file (trash_entry->file_name,
514 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
521 * Get one random peer out of the sampled peers.
523 * We might want to reinitialise this sampler after giving the
524 * corrsponding peer to the client.
525 * Only used internally
528 sampler_get_rand_peer (void *cls,
529 const struct GNUNET_SCHEDULER_TaskContext *tc)
531 struct GetPeerCls *gpc = cls;
534 gpc->get_peer_task = NULL;
535 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
539 * Choose the r_index of the peer we want to return
540 * at random from the interval of the gossip list
542 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
543 gpc->sampler->sampler_size);
545 if (EMPTY == gpc->sampler->sampler_elements[r_index]->is_empty)
547 //LOG (GNUNET_ERROR_TYPE_DEBUG,
548 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
550 /* FIXME no active wait - get notified, when new id arrives?
551 * Might also be a freshly emptied one. Others might still contain ids.
555 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
556 GNUNET_TIME_UNIT_SECONDS, 0.1),
557 &sampler_get_rand_peer,
562 *gpc->id = gpc->sampler->sampler_elements[r_index]->peer_id;
564 gpc->cont (gpc->cont_cls, gpc->id);
566 GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head,
567 gpc->sampler->gpc_tail,
575 * Get one random peer out of the sampled peers.
577 * We might want to reinitialise this sampler after giving the
578 * corrsponding peer to the client.
581 sampler_mod_get_rand_peer (void *cls,
582 const struct GNUNET_SCHEDULER_TaskContext *tc)
584 struct GetPeerCls *gpc = cls;
585 struct RPS_SamplerElement *s_elem;
586 struct GNUNET_TIME_Relative last_request_diff;
588 gpc->get_peer_task = NULL;
589 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
592 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
594 /* Cycle the #client_get_index one step further */
595 client_get_index = (client_get_index + 1) % gpc->sampler->sampler_size;
597 s_elem = gpc->sampler->sampler_elements[client_get_index];
598 *gpc->id = s_elem->peer_id;
599 GNUNET_assert (NULL != s_elem);
601 if (EMPTY == s_elem->is_empty)
603 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sampler_mod element empty, rescheduling.\n");
604 GNUNET_assert (NULL == gpc->get_peer_task);
606 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
607 &sampler_mod_get_rand_peer,
612 /* Check whether we may use this sampler to give it back to the client */
613 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
616 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
617 GNUNET_TIME_absolute_get ());
618 /* We're not going to give it back now if it was
619 * already requested by a client this round */
620 if (last_request_diff.rel_value_us < gpc->sampler->max_round_interval.rel_value_us)
622 LOG (GNUNET_ERROR_TYPE_DEBUG,
623 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
624 ///* How many time remains untile the next round has started? */
625 //inv_last_request_diff =
626 // GNUNET_TIME_absolute_get_difference (last_request_diff,
627 // sampler->max_round_interval);
628 // add a little delay
629 /* Schedule it one round later */
630 GNUNET_assert (NULL == gpc->get_peer_task);
632 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
633 &sampler_mod_get_rand_peer,
637 // TODO add other reasons to wait here
640 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
642 GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head,
643 gpc->sampler->gpc_tail,
645 gpc->cont (gpc->cont_cls, gpc->id);
651 * Get n random peers out of the sampled peers.
653 * We might want to reinitialise this sampler after giving the
654 * corrsponding peer to the client.
655 * Random with or without consumption?
657 * @param sampler the sampler to get peers from.
658 * @param cb callback that will be called once the ids are ready.
659 * @param cls closure given to @a cb
660 * @param for_client #GNUNET_YES if result is used for client,
661 * #GNUNET_NO if used internally
662 * @param num_peers the number of peers requested
665 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
666 RPS_sampler_n_rand_peers_ready_cb cb,
667 void *cls, uint32_t num_peers)
669 GNUNET_assert (0 != sampler->sampler_size);
673 // TODO check if we have too much (distinct) sampled peers
675 struct NRandPeersReadyCls *cb_cls;
676 struct GetPeerCls *gpc;
678 cb_cls = GNUNET_new (struct NRandPeersReadyCls);
679 cb_cls->num_peers = num_peers;
680 cb_cls->cur_num_peers = 0;
681 cb_cls->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
682 cb_cls->callback = cb;
685 LOG (GNUNET_ERROR_TYPE_DEBUG,
686 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
688 for (i = 0 ; i < num_peers ; i++)
690 gpc = GNUNET_new (struct GetPeerCls);
691 gpc->sampler = sampler;
692 gpc->cont = check_n_peers_ready;
693 gpc->cont_cls = cb_cls;
694 gpc->id = &cb_cls->ids[i];
696 // maybe add a little delay
697 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, gpc);
699 GNUNET_CONTAINER_DLL_insert (sampler->gpc_head,
707 * Counts how many Samplers currently hold a given PeerID.
709 * @param sampler the sampler to count ids in.
710 * @param id the PeerID to count.
712 * @return the number of occurrences of id.
715 RPS_sampler_count_id (struct RPS_Sampler *sampler,
716 const struct GNUNET_PeerIdentity *id)
722 for ( i = 0 ; i < sampler->sampler_size ; i++ )
724 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
725 && EMPTY != sampler->sampler_elements[i]->is_empty)
733 * Cleans the sampler.
736 RPS_sampler_destroy (struct RPS_Sampler *sampler)
738 struct GetPeerCls *i;
740 for (i = sampler->gpc_head; NULL != i; i = sampler->gpc_head)
742 GNUNET_CONTAINER_DLL_remove (sampler->gpc_head,
745 GNUNET_SCHEDULER_cancel (i->get_peer_task);
749 sampler_empty (sampler);
750 GNUNET_free (sampler);
753 /* end of gnunet-service-rps.c */