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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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"
35 #include "rps-test_util.h"
37 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
40 // multiple 'clients'?
42 // TODO check for overflows
44 // TODO align message structs
46 // hist_size_init, hist_size_max
48 /***********************************************************************
49 * WARNING: This section needs to be reviewed regarding the use of
50 * functions providing (pseudo)randomness!
51 ***********************************************************************/
53 // TODO care about invalid input of the caller (size 0 or less...)
62 * A sampler element sampling one PeerID at a time.
64 struct RPS_SamplerElement
67 * Min-wise linear permutation used by this sampler.
69 * This is an key later used by a hmac.
71 struct GNUNET_CRYPTO_AuthKey auth_key;
74 * The PeerID this sampler currently samples.
76 struct GNUNET_PeerIdentity peer_id;
79 * The according hash value of this PeerID.
81 struct GNUNET_HashCode peer_id_hash;
85 * Time of last request.
87 struct GNUNET_TIME_Absolute last_client_request;
90 * Flag that indicates that we are not holding a valid PeerID right now.
92 enum RPS_SamplerEmpty is_empty;
97 struct GNUNET_TIME_Absolute birth;
100 * How many times a PeerID was put in this sampler.
105 * How many times this sampler changed the peer_id.
110 * The file name this sampler element should log to
118 * Callback that is called from _get_rand_peer() when the PeerID is ready.
120 * @param cls the closure given alongside this function.
121 * @param id the PeerID that was returned
124 (*RPS_sampler_rand_peer_ready_cont) (void *cls,
125 const struct GNUNET_PeerIdentity *id);
129 * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
136 struct GetPeerCls *next;
141 struct GetPeerCls *prev;
144 * The sampler this function operates on.
146 struct RPS_Sampler *sampler;
149 * The task for this function.
151 struct GNUNET_SCHEDULER_Task *get_peer_task;
156 RPS_sampler_rand_peer_ready_cont cont;
159 * The closure to the callback @e cont
164 * The address of the id to be stored at
166 struct GNUNET_PeerIdentity *id;
171 * Type of function used to differentiate between modified and not modified
175 (*RPS_get_peers_type) (void *cls,
176 const struct GNUNET_SCHEDULER_TaskContext *tc);
179 * Get one random peer out of the sampled peers.
181 * We might want to reinitialise this sampler after giving the
182 * corrsponding peer to the client.
183 * Only used internally
186 sampler_get_rand_peer (void *cls,
187 const struct GNUNET_SCHEDULER_TaskContext *tc);
190 * Get one random peer out of the sampled peers.
192 * We might want to reinitialise this sampler after giving the
193 * corrsponding peer to the client.
196 sampler_mod_get_rand_peer (void *cls,
197 const struct GNUNET_SCHEDULER_TaskContext *tc);
201 * Sampler with its own array of SamplerElements
206 * Number of sampler elements we hold.
208 unsigned int sampler_size;
212 * All Samplers in one array.
214 struct RPS_SamplerElement **sampler_elements;
217 * Maximum time a round takes
219 * Used in the context of RPS
221 struct GNUNET_TIME_Relative max_round_interval;
224 * Stores the function to return peers. Which one it is depends on whether
225 * the Sampler is the modified one or not.
227 RPS_get_peers_type get_peers;
230 * Head for the DLL to store the closures to pending requests.
232 struct GetPeerCls *gpc_head;
235 * Tail for the DLL to store the closures to pending requests.
237 struct GetPeerCls *gpc_tail;
241 * File name to log to
248 * Closure to _get_n_rand_peers_ready_cb()
250 struct NRandPeersReadyCls
253 * Number of peers we are waiting for.
258 * Number of peers we currently have.
260 uint32_t cur_num_peers;
263 * Pointer to the array holding the ids.
265 struct GNUNET_PeerIdentity *ids;
268 * Callback to be called when all ids are available.
270 RPS_sampler_n_rand_peers_ready_cb callback;
273 * Closure given to the callback
279 // * Global sampler variable.
281 //struct RPS_Sampler *sampler;
285 * The minimal size for the extended sampler elements.
287 static size_t min_size;
290 * The maximal size the extended sampler elements should grow to.
292 static size_t max_size;
295 * The size the extended sampler elements currently have.
297 //static size_t extra_size;
300 * Inedex to the sampler element that is the next to be returned
302 static uint32_t client_get_index;
306 * Callback to _get_rand_peer() used by _get_n_rand_peers().
308 * Checks whether all n peers are available. If they are,
312 check_n_peers_ready (void *cls,
313 const struct GNUNET_PeerIdentity *id)
315 struct NRandPeersReadyCls *n_peers_cls = cls;
317 n_peers_cls->cur_num_peers++;
318 LOG (GNUNET_ERROR_TYPE_DEBUG,
319 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
320 n_peers_cls->cur_num_peers, n_peers_cls->num_peers);
322 if (n_peers_cls->num_peers == n_peers_cls->cur_num_peers)
323 { /* All peers are ready -- return those to the client */
324 GNUNET_assert (NULL != n_peers_cls->callback);
326 LOG (GNUNET_ERROR_TYPE_DEBUG,
327 "returning %" PRIX32 " peers to the client\n",
328 n_peers_cls->num_peers);
329 n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers);
331 GNUNET_free (n_peers_cls);
337 * Reinitialise a previously initialised sampler element.
339 * @param sampler pointer to the memory that keeps the value.
342 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
344 sampler_el->is_empty = EMPTY;
346 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
347 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
348 &(sampler_el->auth_key.key),
349 GNUNET_CRYPTO_HASH_LENGTH);
352 /* Create a file(-name) to store internals to */
354 name_buf = auth_key_to_string (sampler_el->auth_key);
356 sampler_el->file_name = create_file (name_buf);
357 GNUNET_free (name_buf);
360 sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
362 sampler_el->birth = GNUNET_TIME_absolute_get ();
363 sampler_el->num_peers = 0;
364 sampler_el->num_change = 0;
369 * (Re)Initialise given Sampler with random min-wise independent function.
371 * In this implementation this means choosing an auth_key for later use in
374 * @return a newly created RPS_SamplerElement which currently holds no id.
376 struct RPS_SamplerElement *
377 RPS_sampler_elem_create (void)
379 struct RPS_SamplerElement *s;
381 s = GNUNET_new (struct RPS_SamplerElement);
383 RPS_sampler_elem_reinit (s);
390 * Input an PeerID into the given sampler element.
392 * @param sampler the sampler the @a s_elem belongs to.
396 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
397 struct RPS_Sampler *sampler,
398 const struct GNUNET_PeerIdentity *other)
400 struct GNUNET_HashCode other_hash;
404 to_file (s_elem->file_name,
406 GNUNET_i2s_full (other));
408 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
410 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
412 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
413 GNUNET_i2s (&(s_elem->peer_id)));
417 GNUNET_CRYPTO_hmac(&s_elem->auth_key,
419 sizeof(struct GNUNET_PeerIdentity),
422 if (EMPTY == s_elem->is_empty)
424 LOG (GNUNET_ERROR_TYPE_DEBUG,
425 "Got PeerID %s; Simply accepting (was empty previously).\n",
427 s_elem->peer_id = *other;
428 s_elem->peer_id_hash = other_hash;
430 s_elem->num_change++;
432 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
434 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
436 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
437 GNUNET_i2s (&s_elem->peer_id));
438 s_elem->peer_id = *other;
439 s_elem->peer_id_hash = other_hash;
441 s_elem->num_change++;
445 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
447 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
448 GNUNET_i2s (&s_elem->peer_id));
451 s_elem->is_empty = NOT_EMPTY;
453 to_file (s_elem->file_name,
455 GNUNET_i2s_full (&s_elem->peer_id));
460 * Get the size of the sampler.
462 * @param sampler the sampler to return the size of.
463 * @return the size of the sampler
466 RPS_sampler_get_size (struct RPS_Sampler *sampler)
468 return sampler->sampler_size;
473 * Grow or shrink the size of the sampler.
475 * @param sampler the sampler to resize.
476 * @param new_size the new size of the sampler
479 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
481 unsigned int old_size;
484 // TODO check min and max size
486 old_size = sampler->sampler_size;
488 if (old_size > new_size)
490 /* Temporary store those to properly call the removeCB on those later */
492 LOG (GNUNET_ERROR_TYPE_DEBUG,
493 "Shrinking sampler %d -> %d\n",
497 to_file (sampler->file_name,
498 "Shrinking sampler %d -> %d",
502 for (i = new_size ; i < old_size ; i++)
504 to_file (sampler->file_name,
507 sampler->sampler_elements[i]->file_name);
510 GNUNET_array_grow (sampler->sampler_elements,
511 sampler->sampler_size,
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
514 "sampler->sampler_elements now points to %p\n",
515 sampler->sampler_elements);
518 else if (old_size < new_size)
520 LOG (GNUNET_ERROR_TYPE_DEBUG,
521 "Growing sampler %d -> %d\n",
525 to_file (sampler->file_name,
526 "Growing sampler %d -> %d",
530 GNUNET_array_grow (sampler->sampler_elements,
531 sampler->sampler_size,
534 for (i = old_size ; i < new_size ; i++)
535 { /* Add new sampler elements */
536 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
538 to_file (sampler->file_name,
541 sampler->sampler_elements[i]->file_name);
546 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
550 GNUNET_assert (sampler->sampler_size == new_size);
555 * Grow or shrink the size of the sampler.
557 * @param sampler the sampler to resize.
558 * @param new_size the new size of the sampler
561 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
563 GNUNET_assert (0 < new_size);
564 sampler_resize (sampler, new_size);
571 * @param sampler the sampler to empty.
572 * @param new_size the new size of the sampler
575 sampler_empty (struct RPS_Sampler *sampler)
577 sampler_resize (sampler, 0);
582 * Initialise a tuple of sampler elements.
584 * @param init_size the size the sampler is initialised with
585 * @param max_round_interval maximum time a round takes
586 * @return a handle to a sampler that consists of sampler elements.
589 RPS_sampler_init (size_t init_size,
590 struct GNUNET_TIME_Relative max_round_interval)
592 struct RPS_Sampler *sampler;
594 /* Initialise context around extended sampler */
595 min_size = 10; // TODO make input to _samplers_init()
596 max_size = 1000; // TODO make input to _samplers_init()
598 sampler = GNUNET_new (struct RPS_Sampler);
601 sampler->file_name = create_file ("sampler-");
603 LOG (GNUNET_ERROR_TYPE_DEBUG,
604 "Initialised sampler %s\n",
608 sampler->sampler_size = 0;
609 sampler->sampler_elements = NULL;
610 sampler->max_round_interval = max_round_interval;
611 sampler->get_peers = sampler_get_rand_peer;
612 sampler->gpc_head = NULL;
613 sampler->gpc_tail = NULL;
614 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
615 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
616 RPS_sampler_resize (sampler, init_size);
618 client_get_index = 0;
620 //GNUNET_assert (init_size == sampler->sampler_size);
625 * Initialise a modified tuple of sampler elements.
627 * @param init_size the size the sampler is initialised with
628 * @param max_round_interval maximum time a round takes
629 * @return a handle to a sampler that consists of sampler elements.
632 RPS_sampler_mod_init (size_t init_size,
633 struct GNUNET_TIME_Relative max_round_interval)
635 struct RPS_Sampler *sampler;
637 sampler = RPS_sampler_init (init_size, max_round_interval);
638 sampler->get_peers = sampler_mod_get_rand_peer;
640 LOG (GNUNET_ERROR_TYPE_DEBUG,
641 "Initialised modified sampler %s\n",
643 to_file (sampler->file_name,
644 "This is a modified sampler");
651 * A fuction to update every sampler in the given list
653 * @param sampler the sampler to update.
654 * @param id the PeerID that is put in the sampler
657 RPS_sampler_update (struct RPS_Sampler *sampler,
658 const struct GNUNET_PeerIdentity *id)
662 to_file (sampler->file_name,
664 GNUNET_i2s_full (id));
666 for (i = 0 ; i < sampler->sampler_size ; i++)
668 RPS_sampler_elem_next (sampler->sampler_elements[i],
676 * Reinitialise all previously initialised sampler elements with the given value.
678 * Used to get rid of a PeerID.
680 * @param sampler the sampler to reinitialise a sampler element in.
681 * @param id the id of the sampler elements to update.
684 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
685 const struct GNUNET_PeerIdentity *id)
689 for ( i = 0 ; i < sampler->sampler_size ; i++ )
691 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) )
693 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
694 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
701 * Get one random peer out of the sampled peers.
703 * We might want to reinitialise this sampler after giving the
704 * corrsponding peer to the client.
705 * Only used internally
708 sampler_get_rand_peer (void *cls,
709 const struct GNUNET_SCHEDULER_TaskContext *tc)
711 struct GetPeerCls *gpc = cls;
714 gpc->get_peer_task = NULL;
715 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
719 * Choose the r_index of the peer we want to return
720 * at random from the interval of the gossip list
722 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
723 gpc->sampler->sampler_size);
725 if (EMPTY == gpc->sampler->sampler_elements[r_index]->is_empty)
727 //LOG (GNUNET_ERROR_TYPE_DEBUG,
728 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
730 /* FIXME no active wait - get notified, when new id arrives?
731 * Might also be a freshly emptied one. Others might still contain ids.
735 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
736 GNUNET_TIME_UNIT_SECONDS, 0.1),
737 &sampler_get_rand_peer,
742 *gpc->id = gpc->sampler->sampler_elements[r_index]->peer_id;
744 gpc->cont (gpc->cont_cls, gpc->id);
746 GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head,
747 gpc->sampler->gpc_tail,
755 * Get one random peer out of the sampled peers.
757 * We might want to reinitialise this sampler after giving the
758 * corrsponding peer to the client.
761 sampler_mod_get_rand_peer (void *cls,
762 const struct GNUNET_SCHEDULER_TaskContext *tc)
764 struct GetPeerCls *gpc = cls;
765 struct GNUNET_PeerIdentity tmp_id;
766 unsigned int empty_flag;
767 struct RPS_SamplerElement *s_elem;
768 struct GNUNET_TIME_Relative last_request_diff;
769 uint32_t tmp_client_get_index;
771 gpc->get_peer_task = NULL;
772 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
775 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
778 /* Store the next #client_get_index to check whether we cycled over the whole list */
779 if (0 < client_get_index)
780 tmp_client_get_index = client_get_index - 1;
782 tmp_client_get_index = gpc->sampler->sampler_size - 1;
784 LOG (GNUNET_ERROR_TYPE_DEBUG,
785 "sched for later if index reaches %" PRIX32 " (sampler size: %" PRIX32 ").\n",
786 tmp_client_get_index, gpc->sampler->sampler_size);
789 { /* Get first non empty sampler */
790 if (tmp_client_get_index == client_get_index)
791 { /* We once cycled over the whole list */
792 LOG (GNUNET_ERROR_TYPE_DEBUG, "reached tmp_index %" PRIX32 ".\n",
794 GNUNET_assert (NULL == gpc->get_peer_task);
796 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
797 &sampler_mod_get_rand_peer,
802 tmp_id = gpc->sampler->sampler_elements[client_get_index]->peer_id;
803 empty_flag = gpc->sampler->sampler_elements[client_get_index]->is_empty;
804 RPS_sampler_elem_reinit (gpc->sampler->sampler_elements[client_get_index]);
805 if (EMPTY != empty_flag)
806 RPS_sampler_elem_next (gpc->sampler->sampler_elements[client_get_index],
810 /* Cycle the #client_get_index one step further */
811 if ( client_get_index == gpc->sampler->sampler_size - 1 )
812 client_get_index = 0;
816 LOG (GNUNET_ERROR_TYPE_DEBUG, "incremented index to %" PRIX32 ".\n",
818 } while (EMPTY == gpc->sampler->sampler_elements[client_get_index]->is_empty);
820 s_elem = gpc->sampler->sampler_elements[client_get_index];
821 *gpc->id = s_elem->peer_id;
823 /* Check whether we may use this sampler to give it back to the client */
824 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
827 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
828 GNUNET_TIME_absolute_get ());
829 /* We're not going to give it back now if it was
830 * already requested by a client this round */
831 if (last_request_diff.rel_value_us < gpc->sampler->max_round_interval.rel_value_us)
833 LOG (GNUNET_ERROR_TYPE_DEBUG,
834 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
835 ///* How many time remains untile the next round has started? */
836 //inv_last_request_diff =
837 // GNUNET_TIME_absolute_get_difference (last_request_diff,
838 // sampler->max_round_interval);
839 // add a little delay
840 /* Schedule it one round later */
841 GNUNET_assert (NULL == gpc->get_peer_task);
843 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
844 &sampler_mod_get_rand_peer,
848 // TODO add other reasons to wait here
851 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
853 GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head,
854 gpc->sampler->gpc_tail,
856 gpc->cont (gpc->cont_cls, gpc->id);
862 * Get n random peers out of the sampled peers.
864 * We might want to reinitialise this sampler after giving the
865 * corrsponding peer to the client.
866 * Random with or without consumption?
868 * @param sampler the sampler to get peers from.
869 * @param cb callback that will be called once the ids are ready.
870 * @param cls closure given to @a cb
871 * @param for_client #GNUNET_YES if result is used for client,
872 * #GNUNET_NO if used internally
873 * @param num_peers the number of peers requested
876 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
877 RPS_sampler_n_rand_peers_ready_cb cb,
878 void *cls, uint32_t num_peers)
880 GNUNET_assert (0 != sampler->sampler_size);
882 // TODO check if we have too much (distinct) sampled peers
884 struct NRandPeersReadyCls *cb_cls;
885 struct GetPeerCls *gpc;
887 cb_cls = GNUNET_new (struct NRandPeersReadyCls);
888 cb_cls->num_peers = num_peers;
889 cb_cls->cur_num_peers = 0;
890 cb_cls->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
891 cb_cls->callback = cb;
894 LOG (GNUNET_ERROR_TYPE_DEBUG,
895 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
897 for (i = 0 ; i < num_peers ; i++)
899 gpc = GNUNET_new (struct GetPeerCls);
900 gpc->sampler = sampler;
901 gpc->cont = check_n_peers_ready;
902 gpc->cont_cls = cb_cls;
903 gpc->id = &cb_cls->ids[i];
905 // maybe add a little delay
906 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, gpc);
908 GNUNET_CONTAINER_DLL_insert (sampler->gpc_head,
916 * Counts how many Samplers currently hold a given PeerID.
918 * @param sampler the sampler to count ids in.
919 * @param id the PeerID to count.
921 * @return the number of occurrences of id.
924 RPS_sampler_count_id (struct RPS_Sampler *sampler,
925 const struct GNUNET_PeerIdentity *id)
931 for ( i = 0 ; i < sampler->sampler_size ; i++ )
933 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
934 && EMPTY != sampler->sampler_elements[i]->is_empty)
942 * Cleans the sampler.
945 RPS_sampler_destroy (struct RPS_Sampler *sampler)
947 struct GetPeerCls *i;
949 for (i = sampler->gpc_head; NULL != i; i = sampler->gpc_head)
951 GNUNET_CONTAINER_DLL_remove (sampler->gpc_head,
954 GNUNET_SCHEDULER_cancel (i->get_peer_task);
958 sampler_empty (sampler);
959 GNUNET_free (sampler);
962 /* end of gnunet-service-rps.c */