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;
76 struct GetPeerCls *prev;
79 * The #RPS_SamplerRequestHandle this single request belongs to.
81 struct RPS_SamplerRequestHandle *req_handle;
84 * The task for this function.
86 struct GNUNET_SCHEDULER_Task *get_peer_task;
91 RPS_sampler_rand_peer_ready_cont cont;
94 * The closure to the callback @e cont
99 * The address of the id to be stored at
101 struct GNUNET_PeerIdentity *id;
106 * Type of function used to differentiate between modified and not modified
110 (*RPS_get_peers_type) (void *cls,
111 const struct GNUNET_SCHEDULER_TaskContext *tc);
114 * Get one random peer out of the sampled peers.
116 * We might want to reinitialise this sampler after giving the
117 * corrsponding peer to the client.
118 * Only used internally
121 sampler_get_rand_peer (void *cls,
122 const struct GNUNET_SCHEDULER_TaskContext *tc);
125 * Get one random peer out of the sampled peers.
127 * We might want to reinitialise this sampler after giving the
128 * corrsponding peer to the client.
131 sampler_mod_get_rand_peer (void *cls,
132 const struct GNUNET_SCHEDULER_TaskContext *tc);
136 * Sampler with its own array of SamplerElements
141 * Number of sampler elements we hold.
143 unsigned int sampler_size;
147 * All sampler elements in one array.
149 struct RPS_SamplerElement **sampler_elements;
152 * Maximum time a round takes
154 * Used in the context of RPS
156 struct GNUNET_TIME_Relative max_round_interval;
159 * Stores the function to return peers. Which one it is depends on whether
160 * the Sampler is the modified one or not.
162 RPS_get_peers_type get_peers;
165 * Head and tail for the DLL to store the #RPS_SamplerRequestHandle
167 struct RPS_SamplerRequestHandle *req_handle_head;
168 struct RPS_SamplerRequestHandle *req_handle_tail;
172 * File name to log to
179 * Closure to _get_n_rand_peers_ready_cb()
181 struct RPS_SamplerRequestHandle
186 struct RPS_SamplerRequestHandle *next;
187 struct RPS_SamplerRequestHandle *prev;
190 * Number of peers we are waiting for.
195 * Number of peers we currently have.
197 uint32_t cur_num_peers;
200 * Pointer to the array holding the ids.
202 struct GNUNET_PeerIdentity *ids;
205 * Head and tail for the DLL to store the tasks for single requests
207 struct GetPeerCls *gpc_head;
208 struct GetPeerCls *gpc_tail;
213 struct RPS_Sampler *sampler;
216 * Callback to be called when all ids are available.
218 RPS_sampler_n_rand_peers_ready_cb callback;
221 * Closure given to the callback
227 // * Global sampler variable.
229 //struct RPS_Sampler *sampler;
233 * The minimal size for the extended sampler elements.
235 static size_t min_size;
238 * The maximal size the extended sampler elements should grow to.
240 static size_t max_size;
243 * The size the extended sampler elements currently have.
245 //static size_t extra_size;
248 * Inedex to the sampler element that is the next to be returned
250 static uint32_t client_get_index;
254 * Callback to _get_rand_peer() used by _get_n_rand_peers().
256 * Checks whether all n peers are available. If they are,
260 check_n_peers_ready (void *cls,
261 const struct GNUNET_PeerIdentity *id)
263 struct RPS_SamplerRequestHandle *req_handle = cls;
265 req_handle->cur_num_peers++;
266 LOG (GNUNET_ERROR_TYPE_DEBUG,
267 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
268 req_handle->cur_num_peers, req_handle->num_peers);
270 if (req_handle->num_peers == req_handle->cur_num_peers)
271 { /* All peers are ready -- return those to the client */
272 GNUNET_assert (NULL != req_handle->callback);
274 LOG (GNUNET_ERROR_TYPE_DEBUG,
275 "returning %" PRIX32 " peers to the client\n",
276 req_handle->num_peers);
277 req_handle->callback (req_handle->cls, req_handle->ids, req_handle->num_peers);
279 RPS_sampler_request_cancel (req_handle);
285 * Get the size of the sampler.
287 * @param sampler the sampler to return the size of.
288 * @return the size of the sampler
291 RPS_sampler_get_size (struct RPS_Sampler *sampler)
293 return sampler->sampler_size;
298 * Grow or shrink the size of the sampler.
300 * @param sampler the sampler to resize.
301 * @param new_size the new size of the sampler
304 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
306 unsigned int old_size;
309 // TODO check min and max size
311 old_size = sampler->sampler_size;
313 if (old_size > new_size)
316 LOG (GNUNET_ERROR_TYPE_DEBUG,
317 "Shrinking sampler %d -> %d\n",
321 to_file (sampler->file_name,
322 "Shrinking sampler %d -> %d",
326 for (i = new_size ; i < old_size ; i++)
328 to_file (sampler->file_name,
331 sampler->sampler_elements[i]->file_name);
334 GNUNET_array_grow (sampler->sampler_elements,
335 sampler->sampler_size,
337 LOG (GNUNET_ERROR_TYPE_DEBUG,
338 "sampler->sampler_elements now points to %p\n",
339 sampler->sampler_elements);
342 else if (old_size < new_size)
344 LOG (GNUNET_ERROR_TYPE_DEBUG,
345 "Growing sampler %d -> %d\n",
349 to_file (sampler->file_name,
350 "Growing sampler %d -> %d",
354 GNUNET_array_grow (sampler->sampler_elements,
355 sampler->sampler_size,
358 for (i = old_size ; i < new_size ; i++)
359 { /* Add new sampler elements */
360 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
362 to_file (sampler->file_name,
365 sampler->sampler_elements[i]->file_name);
370 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
374 GNUNET_assert (sampler->sampler_size == new_size);
379 * Grow or shrink the size of the sampler.
381 * @param sampler the sampler to resize.
382 * @param new_size the new size of the sampler
385 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
387 GNUNET_assert (0 < new_size);
388 sampler_resize (sampler, new_size);
395 * @param sampler the sampler to empty.
396 * @param new_size the new size of the sampler
399 sampler_empty (struct RPS_Sampler *sampler)
401 sampler_resize (sampler, 0);
406 * Initialise a tuple of sampler elements.
408 * @param init_size the size the sampler is initialised with
409 * @param max_round_interval maximum time a round takes
410 * @return a handle to a sampler that consists of sampler elements.
413 RPS_sampler_init (size_t init_size,
414 struct GNUNET_TIME_Relative max_round_interval)
416 struct RPS_Sampler *sampler;
418 /* Initialise context around extended sampler */
419 min_size = 10; // TODO make input to _samplers_init()
420 max_size = 1000; // TODO make input to _samplers_init()
422 sampler = GNUNET_new (struct RPS_Sampler);
425 sampler->file_name = create_file ("sampler-");
427 LOG (GNUNET_ERROR_TYPE_DEBUG,
428 "Initialised sampler %s\n",
432 sampler->max_round_interval = max_round_interval;
433 sampler->get_peers = sampler_get_rand_peer;
434 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
435 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
436 RPS_sampler_resize (sampler, init_size);
438 client_get_index = 0;
440 //GNUNET_assert (init_size == sampler->sampler_size);
445 * Initialise a modified tuple of sampler elements.
447 * @param init_size the size the sampler is initialised with
448 * @param max_round_interval maximum time a round takes
449 * @return a handle to a sampler that consists of sampler elements.
452 RPS_sampler_mod_init (size_t init_size,
453 struct GNUNET_TIME_Relative max_round_interval)
455 struct RPS_Sampler *sampler;
457 sampler = RPS_sampler_init (init_size, max_round_interval);
458 sampler->get_peers = sampler_mod_get_rand_peer;
460 LOG (GNUNET_ERROR_TYPE_DEBUG,
461 "Initialised modified sampler %s\n",
463 to_file (sampler->file_name,
464 "This is a modified sampler");
471 * A fuction to update every sampler in the given list
473 * @param sampler the sampler to update.
474 * @param id the PeerID that is put in the sampler
477 RPS_sampler_update (struct RPS_Sampler *sampler,
478 const struct GNUNET_PeerIdentity *id)
482 to_file (sampler->file_name,
484 GNUNET_i2s_full (id));
486 for (i = 0 ; i < sampler->sampler_size ; i++)
488 RPS_sampler_elem_next (sampler->sampler_elements[i],
496 * Reinitialise all previously initialised sampler elements with the given value.
498 * Used to get rid of a PeerID.
500 * @param sampler the sampler to reinitialise a sampler element in.
501 * @param id the id of the sampler elements to update.
504 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
505 const struct GNUNET_PeerIdentity *id)
508 struct RPS_SamplerElement *trash_entry;
510 for ( i = 0 ; i < sampler->sampler_size ; i++ )
512 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) )
514 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
515 trash_entry = GNUNET_new (struct RPS_SamplerElement);
516 *trash_entry = *(sampler->sampler_elements[i]);
517 to_file (trash_entry->file_name,
519 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
526 * Get one random peer out of the sampled peers.
528 * We might want to reinitialise this sampler after giving the
529 * corrsponding peer to the client.
530 * Only used internally
533 sampler_get_rand_peer (void *cls,
534 const struct GNUNET_SCHEDULER_TaskContext *tc)
536 struct GetPeerCls *gpc = cls;
538 struct RPS_Sampler *sampler;
540 gpc->get_peer_task = NULL;
541 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
543 sampler = gpc->req_handle->sampler;
546 * Choose the r_index of the peer we want to return
547 * at random from the interval of the gossip list
549 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
550 sampler->sampler_size);
552 if (EMPTY == sampler->sampler_elements[r_index]->is_empty)
554 //LOG (GNUNET_ERROR_TYPE_DEBUG,
555 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
557 /* FIXME no active wait - get notified, when new id arrives?
558 * Might also be a freshly emptied one. Others might still contain ids.
562 GNUNET_SCHEDULER_add_delayed (
563 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
564 &sampler_get_rand_peer,
569 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
570 gpc->req_handle->gpc_tail,
572 *gpc->id = sampler->sampler_elements[r_index]->peer_id;
573 gpc->cont (gpc->cont_cls, gpc->id);
580 * Get one random peer out of the sampled peers.
582 * We might want to reinitialise this sampler after giving the
583 * corrsponding peer to the client.
586 sampler_mod_get_rand_peer (void *cls,
587 const struct GNUNET_SCHEDULER_TaskContext *tc)
589 struct GetPeerCls *gpc = cls;
590 struct RPS_SamplerElement *s_elem;
591 struct GNUNET_TIME_Relative last_request_diff;
592 struct RPS_Sampler *sampler;
594 gpc->get_peer_task = NULL;
595 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
597 sampler = gpc->req_handle->sampler;
599 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
601 /* Cycle the #client_get_index one step further */
602 client_get_index = (client_get_index + 1) % sampler->sampler_size;
604 s_elem = sampler->sampler_elements[client_get_index];
605 *gpc->id = s_elem->peer_id;
606 GNUNET_assert (NULL != s_elem);
608 if (EMPTY == s_elem->is_empty)
610 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sampler_mod element empty, rescheduling.\n");
611 GNUNET_assert (NULL == gpc->get_peer_task);
613 GNUNET_SCHEDULER_add_delayed (sampler->max_round_interval,
614 &sampler_mod_get_rand_peer,
619 /* Check whether we may use this sampler to give it back to the client */
620 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
623 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
624 GNUNET_TIME_absolute_get ());
625 /* We're not going to give it back now if it was
626 * already requested by a client this round */
627 if (last_request_diff.rel_value_us < sampler->max_round_interval.rel_value_us)
629 LOG (GNUNET_ERROR_TYPE_DEBUG,
630 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
631 ///* How many time remains untile the next round has started? */
632 //inv_last_request_diff =
633 // GNUNET_TIME_absolute_get_difference (last_request_diff,
634 // sampler->max_round_interval);
635 // add a little delay
636 /* Schedule it one round later */
637 GNUNET_assert (NULL == gpc->get_peer_task);
639 GNUNET_SCHEDULER_add_delayed (sampler->max_round_interval,
640 &sampler_mod_get_rand_peer,
644 // TODO add other reasons to wait here
647 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
649 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
650 gpc->req_handle->gpc_tail,
652 gpc->cont (gpc->cont_cls, gpc->id);
658 * Get n random peers out of the sampled peers.
660 * We might want to reinitialise this sampler after giving the
661 * corrsponding peer to the client.
662 * Random with or without consumption?
664 * @param sampler the sampler to get peers from.
665 * @param cb callback that will be called once the ids are ready.
666 * @param cls closure given to @a cb
667 * @param for_client #GNUNET_YES if result is used for client,
668 * #GNUNET_NO if used internally
669 * @param num_peers the number of peers requested
671 struct RPS_SamplerRequestHandle *
672 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
673 RPS_sampler_n_rand_peers_ready_cb cb,
674 void *cls, uint32_t num_peers)
676 GNUNET_assert (0 != sampler->sampler_size);
680 // TODO check if we have too much (distinct) sampled peers
682 struct RPS_SamplerRequestHandle *req_handle;
683 struct GetPeerCls *gpc;
685 req_handle = GNUNET_new (struct RPS_SamplerRequestHandle);
686 req_handle->num_peers = num_peers;
687 req_handle->cur_num_peers = 0;
688 req_handle->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
689 req_handle->sampler = sampler;
690 req_handle->callback = cb;
691 req_handle->cls = cls;
692 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_head,
693 sampler->req_handle_tail,
696 LOG (GNUNET_ERROR_TYPE_DEBUG,
697 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
699 for (i = 0 ; i < num_peers ; i++)
701 gpc = GNUNET_new (struct GetPeerCls);
702 gpc->req_handle = req_handle;
703 gpc->cont = check_n_peers_ready;
704 gpc->cont_cls = req_handle;
705 gpc->id = &req_handle->ids[i];
707 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
708 req_handle->gpc_tail,
710 // maybe add a little delay
711 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, gpc);
717 * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
719 * @param req_handle the handle to the request
722 RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle)
724 struct GetPeerCls *i;
726 while (NULL != (i = req_handle->gpc_head) )
728 GNUNET_CONTAINER_DLL_remove (req_handle->gpc_head,
729 req_handle->gpc_tail,
731 if (NULL != i->get_peer_task)
732 GNUNET_SCHEDULER_cancel (i->get_peer_task);
735 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->req_handle_head,
736 req_handle->sampler->req_handle_tail,
738 GNUNET_free (req_handle);
743 * Counts how many Samplers currently hold a given PeerID.
745 * @param sampler the sampler to count ids in.
746 * @param id the PeerID to count.
748 * @return the number of occurrences of id.
751 RPS_sampler_count_id (struct RPS_Sampler *sampler,
752 const struct GNUNET_PeerIdentity *id)
758 for ( i = 0 ; i < sampler->sampler_size ; i++ )
760 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
761 && EMPTY != sampler->sampler_elements[i]->is_empty)
769 * Cleans the sampler.
772 RPS_sampler_destroy (struct RPS_Sampler *sampler)
774 if (NULL != sampler->req_handle_head)
776 LOG (GNUNET_ERROR_TYPE_WARNING,
777 "There are still pending requests. Going to remove them.\n");
778 while (NULL != sampler->req_handle_head)
779 RPS_sampler_request_cancel (sampler->req_handle_head);
781 sampler_empty (sampler);
782 GNUNET_free (sampler);
785 /* end of gnunet-service-rps.c */