2 This file is part of GNUnet.
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.
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.
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/>.
20 * @file rps/gnunet-service-rps_sampler.c
21 * @brief sampler implementation
22 * @author Julius Bünger
25 #include "gnunet_util_lib.h"
28 #include "gnunet-service-rps_sampler.h"
29 #include "gnunet-service-rps_sampler_elem.h"
34 #include "rps-test_util.h"
36 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
39 // multiple 'clients'?
41 // TODO check for overflows
43 // TODO align message structs
45 // hist_size_init, hist_size_max
47 /***********************************************************************
48 * WARNING: This section needs to be reviewed regarding the use of
49 * functions providing (pseudo)randomness!
50 ***********************************************************************/
52 // TODO care about invalid input of the caller (size 0 or less...)
55 * Callback that is called from _get_rand_peer() when the PeerID is ready.
57 * @param cls the closure given alongside this function.
58 * @param id the PeerID that was returned
61 (*RPS_sampler_rand_peer_ready_cont) (void *cls,
62 const struct GNUNET_PeerIdentity *id);
66 * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
73 struct GetPeerCls *next;
74 struct GetPeerCls *prev;
77 * The #RPS_SamplerRequestHandle this single request belongs to.
79 struct RPS_SamplerRequestHandle *req_handle;
82 * The task for this function.
84 struct GNUNET_SCHEDULER_Task *get_peer_task;
89 RPS_sampler_rand_peer_ready_cont cont;
92 * The closure to the callback @e cont
97 * The address of the id to be stored at
99 struct GNUNET_PeerIdentity *id;
104 * Type of function used to differentiate between modified and not modified
108 (*RPS_get_peers_type) (void *cls);
111 * Get one random peer out of the sampled peers.
113 * We might want to reinitialise this sampler after giving the
114 * corrsponding peer to the client.
115 * Only used internally
118 sampler_get_rand_peer (void *cls);
122 * Get one random peer out of the sampled peers.
124 * We might want to reinitialise this sampler after giving the
125 * corrsponding peer to the client.
128 sampler_mod_get_rand_peer (void *cls);
132 * Sampler with its own array of SamplerElements
137 * Number of sampler elements we hold.
139 unsigned int sampler_size;
143 * All sampler elements in one array.
145 struct RPS_SamplerElement **sampler_elements;
148 * Maximum time a round takes
150 * Used in the context of RPS
152 struct GNUNET_TIME_Relative max_round_interval;
155 * Stores the function to return peers. Which one it is depends on whether
156 * the Sampler is the modified one or not.
158 RPS_get_peers_type get_peers;
161 * Head and tail for the DLL to store the #RPS_SamplerRequestHandle
163 struct RPS_SamplerRequestHandle *req_handle_head;
164 struct RPS_SamplerRequestHandle *req_handle_tail;
168 * File name to log to
175 * Closure to _get_n_rand_peers_ready_cb()
177 struct RPS_SamplerRequestHandle
182 struct RPS_SamplerRequestHandle *next;
183 struct RPS_SamplerRequestHandle *prev;
186 * Number of peers we are waiting for.
191 * Number of peers we currently have.
193 uint32_t cur_num_peers;
196 * Pointer to the array holding the ids.
198 struct GNUNET_PeerIdentity *ids;
201 * Head and tail for the DLL to store the tasks for single requests
203 struct GetPeerCls *gpc_head;
204 struct GetPeerCls *gpc_tail;
209 struct RPS_Sampler *sampler;
212 * Callback to be called when all ids are available.
214 RPS_sampler_n_rand_peers_ready_cb callback;
217 * Closure given to the callback
223 // * Global sampler variable.
225 //struct RPS_Sampler *sampler;
229 * The minimal size for the extended sampler elements.
231 static size_t min_size;
234 * The maximal size the extended sampler elements should grow to.
236 static size_t max_size;
239 * The size the extended sampler elements currently have.
241 //static size_t extra_size;
244 * Inedex to the sampler element that is the next to be returned
246 static uint32_t client_get_index;
250 * Callback to _get_rand_peer() used by _get_n_rand_peers().
252 * Checks whether all n peers are available. If they are,
256 check_n_peers_ready (void *cls,
257 const struct GNUNET_PeerIdentity *id)
259 struct RPS_SamplerRequestHandle *req_handle = cls;
261 req_handle->cur_num_peers++;
262 LOG (GNUNET_ERROR_TYPE_DEBUG,
263 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
264 req_handle->cur_num_peers, req_handle->num_peers);
266 if (req_handle->num_peers == req_handle->cur_num_peers)
267 { /* All peers are ready -- return those to the client */
268 GNUNET_assert (NULL != req_handle->callback);
270 LOG (GNUNET_ERROR_TYPE_DEBUG,
271 "returning %" PRIX32 " peers to the client\n",
272 req_handle->num_peers);
273 req_handle->callback (req_handle->cls, req_handle->ids, req_handle->num_peers);
275 RPS_sampler_request_cancel (req_handle);
281 * Get the size of the sampler.
283 * @param sampler the sampler to return the size of.
284 * @return the size of the sampler
287 RPS_sampler_get_size (struct RPS_Sampler *sampler)
289 return sampler->sampler_size;
294 * Grow or shrink the size of the sampler.
296 * @param sampler the sampler to resize.
297 * @param new_size the new size of the sampler
300 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
302 unsigned int old_size;
305 // TODO check min and max size
307 old_size = sampler->sampler_size;
309 if (old_size > new_size)
312 LOG (GNUNET_ERROR_TYPE_DEBUG,
313 "Shrinking sampler %d -> %d\n",
317 to_file (sampler->file_name,
318 "Shrinking sampler %d -> %d",
322 for (i = new_size ; i < old_size ; i++)
324 to_file (sampler->file_name,
327 sampler->sampler_elements[i]->file_name);
328 RPS_sampler_elem_destroy (sampler->sampler_elements[i]);
331 GNUNET_array_grow (sampler->sampler_elements,
332 sampler->sampler_size,
334 LOG (GNUNET_ERROR_TYPE_DEBUG,
335 "sampler->sampler_elements now points to %p\n",
336 sampler->sampler_elements);
339 else if (old_size < new_size)
341 LOG (GNUNET_ERROR_TYPE_DEBUG,
342 "Growing sampler %d -> %d\n",
346 to_file (sampler->file_name,
347 "Growing sampler %d -> %d",
351 GNUNET_array_grow (sampler->sampler_elements,
352 sampler->sampler_size,
355 for (i = old_size ; i < new_size ; i++)
356 { /* Add new sampler elements */
357 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
359 to_file (sampler->file_name,
362 sampler->sampler_elements[i]->file_name);
367 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
371 GNUNET_assert (sampler->sampler_size == new_size);
376 * Grow or shrink the size of the sampler.
378 * @param sampler the sampler to resize.
379 * @param new_size the new size of the sampler
382 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
384 GNUNET_assert (0 < new_size);
385 sampler_resize (sampler, new_size);
392 * @param sampler the sampler to empty.
393 * @param new_size the new size of the sampler
396 sampler_empty (struct RPS_Sampler *sampler)
398 sampler_resize (sampler, 0);
403 * Initialise a tuple of sampler elements.
405 * @param init_size the size the sampler is initialised with
406 * @param max_round_interval maximum time a round takes
407 * @return a handle to a sampler that consists of sampler elements.
410 RPS_sampler_init (size_t init_size,
411 struct GNUNET_TIME_Relative max_round_interval)
413 struct RPS_Sampler *sampler;
415 /* Initialise context around extended sampler */
416 min_size = 10; // TODO make input to _samplers_init()
417 max_size = 1000; // TODO make input to _samplers_init()
419 sampler = GNUNET_new (struct RPS_Sampler);
422 sampler->file_name = create_file ("sampler-");
424 LOG (GNUNET_ERROR_TYPE_DEBUG,
425 "Initialised sampler %s\n",
429 sampler->max_round_interval = max_round_interval;
430 sampler->get_peers = sampler_get_rand_peer;
431 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
432 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
433 RPS_sampler_resize (sampler, init_size);
435 client_get_index = 0;
437 //GNUNET_assert (init_size == sampler->sampler_size);
442 * Initialise a modified tuple of sampler elements.
444 * @param init_size the size the sampler is initialised with
445 * @param max_round_interval maximum time a round takes
446 * @return a handle to a sampler that consists of sampler elements.
449 RPS_sampler_mod_init (size_t init_size,
450 struct GNUNET_TIME_Relative max_round_interval)
452 struct RPS_Sampler *sampler;
454 sampler = RPS_sampler_init (init_size, max_round_interval);
455 sampler->get_peers = sampler_mod_get_rand_peer;
457 LOG (GNUNET_ERROR_TYPE_DEBUG,
458 "Initialised modified sampler %s\n",
460 to_file (sampler->file_name,
461 "This is a modified sampler");
468 * A fuction to update every sampler in the given list
470 * @param sampler the sampler to update.
471 * @param id the PeerID that is put in the sampler
474 RPS_sampler_update (struct RPS_Sampler *sampler,
475 const struct GNUNET_PeerIdentity *id)
479 to_file (sampler->file_name,
481 GNUNET_i2s_full (id));
483 for (i = 0 ; i < sampler->sampler_size ; i++)
485 RPS_sampler_elem_next (sampler->sampler_elements[i],
493 * Reinitialise all previously initialised sampler elements with the given value.
495 * Used to get rid of a PeerID.
497 * @param sampler the sampler to reinitialise a sampler element in.
498 * @param id the id of the sampler elements to update.
501 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
502 const struct GNUNET_PeerIdentity *id)
506 for (i = 0; i < sampler->sampler_size; i++)
508 if (0 == GNUNET_CRYPTO_cmp_peer_identity(id,
509 &(sampler->sampler_elements[i]->peer_id)) )
511 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
512 to_file (sampler->sampler_elements[i]->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)
530 struct GetPeerCls *gpc = cls;
532 struct RPS_Sampler *sampler;
534 gpc->get_peer_task = NULL;
535 sampler = gpc->req_handle->sampler;
538 * Choose the r_index of the peer we want to return
539 * at random from the interval of the gossip list
541 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
542 sampler->sampler_size);
544 if (EMPTY == sampler->sampler_elements[r_index]->is_empty)
546 //LOG (GNUNET_ERROR_TYPE_DEBUG,
547 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
549 /* FIXME no active wait - get notified, when new id arrives?
550 * Might also be a freshly emptied one. Others might still contain ids.
554 GNUNET_SCHEDULER_add_delayed (
555 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
556 &sampler_get_rand_peer,
561 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
562 gpc->req_handle->gpc_tail,
564 *gpc->id = sampler->sampler_elements[r_index]->peer_id;
565 gpc->cont (gpc->cont_cls, gpc->id);
572 * Get one random peer out of the sampled peers.
574 * We might want to reinitialise this sampler after giving the
575 * corrsponding peer to the client.
578 sampler_mod_get_rand_peer (void *cls)
580 struct GetPeerCls *gpc = cls;
581 struct RPS_SamplerElement *s_elem;
582 struct GNUNET_TIME_Relative last_request_diff;
583 struct RPS_Sampler *sampler;
585 gpc->get_peer_task = NULL;
586 sampler = gpc->req_handle->sampler;
588 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
590 /* Cycle the #client_get_index one step further */
591 client_get_index = (client_get_index + 1) % sampler->sampler_size;
593 s_elem = sampler->sampler_elements[client_get_index];
594 *gpc->id = s_elem->peer_id;
595 GNUNET_assert (NULL != s_elem);
597 if (EMPTY == s_elem->is_empty)
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "Sampler_mod element empty, rescheduling.\n");
601 GNUNET_assert (NULL == gpc->get_peer_task);
603 GNUNET_SCHEDULER_add_delayed (sampler->max_round_interval,
604 &sampler_mod_get_rand_peer,
609 /* Check whether we may use this sampler to give it back to the client */
610 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
613 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
614 GNUNET_TIME_absolute_get ());
615 /* We're not going to give it back now if it was
616 * already requested by a client this round */
617 if (last_request_diff.rel_value_us < sampler->max_round_interval.rel_value_us)
619 LOG (GNUNET_ERROR_TYPE_DEBUG,
620 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
621 ///* How many time remains untile the next round has started? */
622 //inv_last_request_diff =
623 // GNUNET_TIME_absolute_get_difference (last_request_diff,
624 // sampler->max_round_interval);
625 // add a little delay
626 /* Schedule it one round later */
627 GNUNET_assert (NULL == gpc->get_peer_task);
629 GNUNET_SCHEDULER_add_delayed (sampler->max_round_interval,
630 &sampler_mod_get_rand_peer,
634 // TODO add other reasons to wait here
637 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
639 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
640 gpc->req_handle->gpc_tail,
642 gpc->cont (gpc->cont_cls, gpc->id);
648 * Get n random peers out of the sampled peers.
650 * We might want to reinitialise this sampler after giving the
651 * corrsponding peer to the client.
652 * Random with or without consumption?
654 * @param sampler the sampler to get peers from.
655 * @param cb callback that will be called once the ids are ready.
656 * @param cls closure given to @a cb
657 * @param for_client #GNUNET_YES if result is used for client,
658 * #GNUNET_NO if used internally
659 * @param num_peers the number of peers requested
661 struct RPS_SamplerRequestHandle *
662 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
663 RPS_sampler_n_rand_peers_ready_cb cb,
664 void *cls, uint32_t num_peers)
666 GNUNET_assert (0 != sampler->sampler_size);
670 // TODO check if we have too much (distinct) sampled peers
672 struct RPS_SamplerRequestHandle *req_handle;
673 struct GetPeerCls *gpc;
675 req_handle = GNUNET_new (struct RPS_SamplerRequestHandle);
676 req_handle->num_peers = num_peers;
677 req_handle->cur_num_peers = 0;
678 req_handle->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
679 req_handle->sampler = sampler;
680 req_handle->callback = cb;
681 req_handle->cls = cls;
682 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_head,
683 sampler->req_handle_tail,
686 LOG (GNUNET_ERROR_TYPE_DEBUG,
687 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
689 for (i = 0 ; i < num_peers ; i++)
691 gpc = GNUNET_new (struct GetPeerCls);
692 gpc->req_handle = req_handle;
693 gpc->cont = check_n_peers_ready;
694 gpc->cont_cls = req_handle;
695 gpc->id = &req_handle->ids[i];
697 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
698 req_handle->gpc_tail,
700 // maybe add a little delay
701 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers,
708 * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
710 * @param req_handle the handle to the request
713 RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle)
715 struct GetPeerCls *i;
717 while (NULL != (i = req_handle->gpc_head) )
719 GNUNET_CONTAINER_DLL_remove (req_handle->gpc_head,
720 req_handle->gpc_tail,
722 if (NULL != i->get_peer_task)
724 GNUNET_SCHEDULER_cancel (i->get_peer_task);
728 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->req_handle_head,
729 req_handle->sampler->req_handle_tail,
731 GNUNET_free (req_handle);
736 * Counts how many Samplers currently hold a given PeerID.
738 * @param sampler the sampler to count ids in.
739 * @param id the PeerID to count.
741 * @return the number of occurrences of id.
744 RPS_sampler_count_id (struct RPS_Sampler *sampler,
745 const struct GNUNET_PeerIdentity *id)
751 for ( i = 0 ; i < sampler->sampler_size ; i++ )
753 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
754 && EMPTY != sampler->sampler_elements[i]->is_empty)
762 * Cleans the sampler.
765 RPS_sampler_destroy (struct RPS_Sampler *sampler)
767 if (NULL != sampler->req_handle_head)
769 LOG (GNUNET_ERROR_TYPE_WARNING,
770 "There are still pending requests. Going to remove them.\n");
771 while (NULL != sampler->req_handle_head)
773 RPS_sampler_request_cancel (sampler->req_handle_head);
776 sampler_empty (sampler);
777 GNUNET_free (sampler);
780 /* end of gnunet-service-rps.c */