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;
262 req_handle->cur_num_peers++;
263 LOG (GNUNET_ERROR_TYPE_DEBUG,
264 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
265 req_handle->cur_num_peers, req_handle->num_peers);
267 if (req_handle->num_peers == req_handle->cur_num_peers)
268 { /* All peers are ready -- return those to the client */
269 GNUNET_assert (NULL != req_handle->callback);
271 LOG (GNUNET_ERROR_TYPE_DEBUG,
272 "returning %" PRIX32 " peers to the client\n",
273 req_handle->num_peers);
274 req_handle->callback (req_handle->cls, req_handle->ids, req_handle->num_peers);
276 RPS_sampler_request_cancel (req_handle);
282 * Get the size of the sampler.
284 * @param sampler the sampler to return the size of.
285 * @return the size of the sampler
288 RPS_sampler_get_size (struct RPS_Sampler *sampler)
290 return sampler->sampler_size;
295 * Grow or shrink the size of the sampler.
297 * @param sampler the sampler to resize.
298 * @param new_size the new size of the sampler
301 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
303 unsigned int old_size;
306 // TODO check min and max size
308 old_size = sampler->sampler_size;
310 if (old_size > new_size)
313 LOG (GNUNET_ERROR_TYPE_DEBUG,
314 "Shrinking sampler %d -> %d\n",
318 to_file (sampler->file_name,
319 "Shrinking sampler %d -> %d",
323 for (i = new_size ; i < old_size ; i++)
325 to_file (sampler->file_name,
328 sampler->sampler_elements[i]->file_name);
329 RPS_sampler_elem_destroy (sampler->sampler_elements[i]);
332 GNUNET_array_grow (sampler->sampler_elements,
333 sampler->sampler_size,
335 LOG (GNUNET_ERROR_TYPE_DEBUG,
336 "sampler->sampler_elements now points to %p\n",
337 sampler->sampler_elements);
340 else if (old_size < new_size)
342 LOG (GNUNET_ERROR_TYPE_DEBUG,
343 "Growing sampler %d -> %d\n",
347 to_file (sampler->file_name,
348 "Growing sampler %d -> %d",
352 GNUNET_array_grow (sampler->sampler_elements,
353 sampler->sampler_size,
356 for (i = old_size ; i < new_size ; i++)
357 { /* Add new sampler elements */
358 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
360 to_file (sampler->file_name,
363 sampler->sampler_elements[i]->file_name);
368 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
372 GNUNET_assert (sampler->sampler_size == new_size);
377 * Grow or shrink the size of the sampler.
379 * @param sampler the sampler to resize.
380 * @param new_size the new size of the sampler
383 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
385 GNUNET_assert (0 < new_size);
386 sampler_resize (sampler, new_size);
393 * @param sampler the sampler to empty.
394 * @param new_size the new size of the sampler
397 sampler_empty (struct RPS_Sampler *sampler)
399 sampler_resize (sampler, 0);
404 * Initialise a tuple of sampler elements.
406 * @param init_size the size the sampler is initialised with
407 * @param max_round_interval maximum time a round takes
408 * @return a handle to a sampler that consists of sampler elements.
411 RPS_sampler_init (size_t init_size,
412 struct GNUNET_TIME_Relative max_round_interval)
414 struct RPS_Sampler *sampler;
416 /* Initialise context around extended sampler */
417 min_size = 10; // TODO make input to _samplers_init()
418 max_size = 1000; // TODO make input to _samplers_init()
420 sampler = GNUNET_new (struct RPS_Sampler);
423 sampler->file_name = create_file ("sampler-");
425 LOG (GNUNET_ERROR_TYPE_DEBUG,
426 "Initialised sampler %s\n",
430 sampler->max_round_interval = max_round_interval;
431 sampler->get_peers = sampler_get_rand_peer;
432 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
433 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
434 RPS_sampler_resize (sampler, init_size);
436 client_get_index = 0;
438 //GNUNET_assert (init_size == sampler->sampler_size);
443 * Initialise a modified tuple of sampler elements.
445 * @param init_size the size the sampler is initialised with
446 * @param max_round_interval maximum time a round takes
447 * @return a handle to a sampler that consists of sampler elements.
450 RPS_sampler_mod_init (size_t init_size,
451 struct GNUNET_TIME_Relative max_round_interval)
453 struct RPS_Sampler *sampler;
455 sampler = RPS_sampler_init (init_size, max_round_interval);
456 sampler->get_peers = sampler_mod_get_rand_peer;
459 LOG (GNUNET_ERROR_TYPE_DEBUG,
460 "Initialised modified sampler %s\n",
462 to_file (sampler->file_name,
463 "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)
509 for (i = 0; i < sampler->sampler_size; i++)
511 if (0 == GNUNET_CRYPTO_cmp_peer_identity(id,
512 &(sampler->sampler_elements[i]->peer_id)) )
514 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
515 to_file (sampler->sampler_elements[i]->file_name,
517 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
524 * Get one random peer out of the sampled peers.
526 * We might want to reinitialise this sampler after giving the
527 * corrsponding peer to the client.
528 * Only used internally
531 sampler_get_rand_peer (void *cls)
533 struct GetPeerCls *gpc = cls;
535 struct RPS_Sampler *sampler;
537 gpc->get_peer_task = NULL;
538 sampler = gpc->req_handle->sampler;
541 * Choose the r_index of the peer we want to return
542 * at random from the interval of the gossip list
544 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
545 sampler->sampler_size);
547 if (EMPTY == sampler->sampler_elements[r_index]->is_empty)
549 //LOG (GNUNET_ERROR_TYPE_DEBUG,
550 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
552 /* FIXME no active wait - get notified, when new id arrives?
553 * Might also be a freshly emptied one. Others might still contain ids.
557 GNUNET_SCHEDULER_add_delayed (
558 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
559 &sampler_get_rand_peer,
564 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
565 gpc->req_handle->gpc_tail,
567 *gpc->id = sampler->sampler_elements[r_index]->peer_id;
568 gpc->cont (gpc->cont_cls, gpc->id);
575 * Get one random peer out of the sampled peers.
577 * This reinitialises the queried sampler element.
580 sampler_mod_get_rand_peer (void *cls)
582 struct GetPeerCls *gpc = cls;
583 struct RPS_SamplerElement *s_elem;
584 struct GNUNET_TIME_Relative last_request_diff;
585 struct RPS_Sampler *sampler;
587 gpc->get_peer_task = NULL;
588 sampler = gpc->req_handle->sampler;
590 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
592 /* Cycle the #client_get_index one step further */
593 client_get_index = (client_get_index + 1) % sampler->sampler_size;
595 s_elem = sampler->sampler_elements[client_get_index];
596 *gpc->id = s_elem->peer_id;
597 GNUNET_assert (NULL != s_elem);
599 if (EMPTY == s_elem->is_empty)
601 LOG (GNUNET_ERROR_TYPE_DEBUG,
602 "Sampler_mod element empty, rescheduling.\n");
603 GNUNET_assert (NULL == gpc->get_peer_task);
605 GNUNET_SCHEDULER_add_delayed (sampler->max_round_interval,
606 &sampler_mod_get_rand_peer,
611 /* Check whether we may use this sampler to give it back to the client */
612 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
615 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
616 GNUNET_TIME_absolute_get ());
617 /* We're not going to give it back now if it was
618 * already requested by a client this round */
619 if (last_request_diff.rel_value_us < sampler->max_round_interval.rel_value_us)
621 LOG (GNUNET_ERROR_TYPE_DEBUG,
622 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
623 ///* How many time remains untile the next round has started? */
624 //inv_last_request_diff =
625 // GNUNET_TIME_absolute_get_difference (last_request_diff,
626 // sampler->max_round_interval);
627 // add a little delay
628 /* Schedule it one round later */
629 GNUNET_assert (NULL == gpc->get_peer_task);
631 GNUNET_SCHEDULER_add_delayed (sampler->max_round_interval,
632 &sampler_mod_get_rand_peer,
636 // TODO add other reasons to wait here
639 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
640 RPS_sampler_elem_reinit (s_elem);
642 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
643 gpc->req_handle->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
664 struct RPS_SamplerRequestHandle *
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 RPS_SamplerRequestHandle *req_handle;
676 struct GetPeerCls *gpc;
678 req_handle = GNUNET_new (struct RPS_SamplerRequestHandle);
679 req_handle->num_peers = num_peers;
680 req_handle->cur_num_peers = 0;
681 req_handle->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
682 req_handle->sampler = sampler;
683 req_handle->callback = cb;
684 req_handle->cls = cls;
685 GNUNET_CONTAINER_DLL_insert (sampler->req_handle_head,
686 sampler->req_handle_tail,
689 LOG (GNUNET_ERROR_TYPE_DEBUG,
690 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
692 for (i = 0 ; i < num_peers ; i++)
694 gpc = GNUNET_new (struct GetPeerCls);
695 gpc->req_handle = req_handle;
696 gpc->cont = check_n_peers_ready;
697 gpc->cont_cls = req_handle;
698 gpc->id = &req_handle->ids[i];
700 GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head,
701 req_handle->gpc_tail,
703 // maybe add a little delay
704 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers,
711 * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
713 * @param req_handle the handle to the request
716 RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle)
718 struct GetPeerCls *i;
720 while (NULL != (i = req_handle->gpc_head) )
722 GNUNET_CONTAINER_DLL_remove (req_handle->gpc_head,
723 req_handle->gpc_tail,
725 if (NULL != i->get_peer_task)
727 GNUNET_SCHEDULER_cancel (i->get_peer_task);
731 GNUNET_free (req_handle->ids);
732 GNUNET_CONTAINER_DLL_remove (req_handle->sampler->req_handle_head,
733 req_handle->sampler->req_handle_tail,
735 GNUNET_free (req_handle);
740 * Counts how many Samplers currently hold a given PeerID.
742 * @param sampler the sampler to count ids in.
743 * @param id the PeerID to count.
745 * @return the number of occurrences of id.
748 RPS_sampler_count_id (struct RPS_Sampler *sampler,
749 const struct GNUNET_PeerIdentity *id)
755 for ( i = 0 ; i < sampler->sampler_size ; i++ )
757 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
758 && EMPTY != sampler->sampler_elements[i]->is_empty)
766 * Cleans the sampler.
769 RPS_sampler_destroy (struct RPS_Sampler *sampler)
771 if (NULL != sampler->req_handle_head)
773 LOG (GNUNET_ERROR_TYPE_WARNING,
774 "There are still pending requests. Going to remove them.\n");
775 while (NULL != sampler->req_handle_head)
777 RPS_sampler_request_cancel (sampler->req_handle_head);
780 sampler_empty (sampler);
781 GNUNET_free (sampler);
784 /* end of gnunet-service-rps.c */