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 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
37 // multiple 'clients'?
39 // TODO check for overflows
41 // TODO align message structs
43 // hist_size_init, hist_size_max
45 /***********************************************************************
46 * WARNING: This section needs to be reviewed regarding the use of
47 * functions providing (pseudo)randomness!
48 ***********************************************************************/
50 // TODO care about invalid input of the caller (size 0 or less...)
59 * A sampler element sampling one PeerID at a time.
61 struct RPS_SamplerElement
64 * Min-wise linear permutation used by this sampler.
66 * This is an key later used by a hmac.
68 struct GNUNET_CRYPTO_AuthKey auth_key;
71 * The PeerID this sampler currently samples.
73 struct GNUNET_PeerIdentity peer_id;
76 * The according hash value of this PeerID.
78 struct GNUNET_HashCode peer_id_hash;
82 * Time of last request.
84 struct GNUNET_TIME_Absolute last_client_request;
87 * Flag that indicates that we are not holding a valid PeerID right now.
89 enum RPS_SamplerEmpty is_empty;
94 struct GNUNET_TIME_Absolute birth;
97 * How many times a PeerID was put in this sampler.
102 * How many times this sampler changed the peer_id.
107 * The file name this sampler element should log to
115 * Callback that is called from _get_rand_peer() when the PeerID is ready.
117 * @param cls the closure given alongside this function.
118 * @param id the PeerID that was returned
121 (*RPS_sampler_rand_peer_ready_cont) (void *cls,
122 const struct GNUNET_PeerIdentity *id);
126 * Closure for #sampler_get_rand_peer()
133 struct GetPeerCls *next;
138 struct GetPeerCls *prev;
141 * The sampler this function operates on.
143 struct RPS_Sampler *sampler;
146 * The task for this function.
148 struct GNUNET_SCHEDULER_Task *get_peer_task;
153 RPS_sampler_rand_peer_ready_cont cont;
156 * The closure to the callback @e cont
161 * The address of the id to be stored at
163 struct GNUNET_PeerIdentity *id;
168 * Type of function used to differentiate between modified and not modified
172 (*RPS_get_peers_type) (void *cls,
173 const struct GNUNET_SCHEDULER_TaskContext *tc);
176 * Get one random peer out of the sampled peers.
178 * We might want to reinitialise this sampler after giving the
179 * corrsponding peer to the client.
180 * Only used internally
183 sampler_get_rand_peer2 (void *cls,
184 const struct GNUNET_SCHEDULER_TaskContext *tc);
187 * Get one random peer out of the sampled peers.
189 * We might want to reinitialise this sampler after giving the
190 * corrsponding peer to the client.
193 sampler_get_rand_peer (void *cls,
194 const struct GNUNET_SCHEDULER_TaskContext *tc);
198 * Sampler with its own array of SamplerElements
203 * Number of sampler elements we hold.
205 unsigned int sampler_size;
209 * All Samplers in one array.
211 struct RPS_SamplerElement **sampler_elements;
214 * Maximum time a round takes
216 * Used in the context of RPS
218 struct GNUNET_TIME_Relative max_round_interval;
221 * Stores the function to return peers. Which one it is depends on whether
222 * the Sampler is the modified one or not.
224 RPS_get_peers_type get_peers;
227 * Head for the DLL to store the closures to pending requests.
229 struct GetPeerCls *gpc_head;
232 * Tail for the DLL to store the closures to pending requests.
234 struct GetPeerCls *gpc_tail;
238 * File name to log to
245 * Closure to _get_n_rand_peers_ready_cb()
247 struct NRandPeersReadyCls
250 * Number of peers we are waiting for.
255 * Number of peers we currently have.
257 uint32_t cur_num_peers;
260 * Pointer to the array holding the ids.
262 struct GNUNET_PeerIdentity *ids;
265 * Callback to be called when all ids are available.
267 RPS_sampler_n_rand_peers_ready_cb callback;
270 * Closure given to the callback
276 // * Global sampler variable.
278 //struct RPS_Sampler *sampler;
282 * The minimal size for the extended sampler elements.
284 static size_t min_size;
287 * The maximal size the extended sampler elements should grow to.
289 static size_t max_size;
292 * The size the extended sampler elements currently have.
294 //static size_t extra_size;
297 * Inedex to the sampler element that is the next to be returned
299 static uint32_t client_get_index;
304 * This function is used to facilitate writing important information to disk
306 #define to_file(file_name, ...) do {char tmp_buf[512];\
308 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
310 LOG (GNUNET_ERROR_TYPE_WARNING,\
311 "Failed to create tmp_buf\n");\
313 to_file_(file_name,tmp_buf);\
317 to_file_ (char *file_name, char *line)
319 struct GNUNET_DISK_FileHandle *f;
320 char output_buffer[512];
326 if (NULL == (f = GNUNET_DISK_file_open (file_name,
327 GNUNET_DISK_OPEN_APPEND |
328 GNUNET_DISK_OPEN_WRITE |
329 GNUNET_DISK_OPEN_CREATE,
330 GNUNET_DISK_PERM_USER_WRITE)))
332 LOG (GNUNET_ERROR_TYPE_WARNING,
333 "Not able to open file %s\n",
337 size = GNUNET_snprintf (output_buffer,
338 sizeof (output_buffer),
340 GNUNET_TIME_absolute_get ().abs_value_us,
344 LOG (GNUNET_ERROR_TYPE_WARNING,
345 "Failed to write string to buffer (size: %i)\n",
350 size2 = GNUNET_DISK_file_write (f, output_buffer, size);
353 LOG (GNUNET_ERROR_TYPE_WARNING,
354 "Unable to write to file! (Size: %u, size2: %u)\n",
360 if (GNUNET_YES != GNUNET_DISK_file_close (f))
361 LOG (GNUNET_ERROR_TYPE_WARNING,
362 "Unable to close file\n");
367 /** FIXME document */
368 static struct GetPeerCls *gpc_head;
369 static struct GetPeerCls *gpc_tail;
373 * Callback to _get_rand_peer() used by _get_n_rand_peers().
375 * Checks whether all n peers are available. If they are,
379 check_n_peers_ready (void *cls,
380 const struct GNUNET_PeerIdentity *id)
382 struct NRandPeersReadyCls *n_peers_cls = cls;
384 n_peers_cls->cur_num_peers++;
385 LOG (GNUNET_ERROR_TYPE_DEBUG,
386 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
387 n_peers_cls->cur_num_peers, n_peers_cls->num_peers);
389 if (n_peers_cls->num_peers == n_peers_cls->cur_num_peers)
390 { /* All peers are ready -- return those to the client */
391 GNUNET_assert (NULL != n_peers_cls->callback);
393 LOG (GNUNET_ERROR_TYPE_DEBUG,
394 "returning %" PRIX32 " peers to the client\n",
395 n_peers_cls->num_peers);
396 n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers);
398 GNUNET_free (n_peers_cls);
404 * Reinitialise a previously initialised sampler element.
406 * @param sampler pointer to the memory that keeps the value.
409 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
411 sampler_el->is_empty = EMPTY;
413 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
414 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
415 &(sampler_el->auth_key.key),
416 GNUNET_CRYPTO_HASH_LENGTH);
419 /* Create a file(-name) to store internals to */
424 size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;
427 keylen += 5 - keylen % 5;
429 buf = GNUNET_malloc (keylen + 1);
431 end = GNUNET_STRINGS_data_to_string (&(sampler_el->auth_key.key),
432 sizeof (struct GNUNET_CRYPTO_AuthKey),
446 size = GNUNET_snprintf (name_buf, sizeof (name_buf), "sampler_el-%s-", buf);
448 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
450 if (NULL == (sampler_el->file_name = GNUNET_DISK_mktemp (name_buf)))
451 LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");
454 sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
456 sampler_el->birth = GNUNET_TIME_absolute_get ();
457 sampler_el->num_peers = 0;
458 sampler_el->num_change = 0;
463 * (Re)Initialise given Sampler with random min-wise independent function.
465 * In this implementation this means choosing an auth_key for later use in
468 * @return a newly created RPS_SamplerElement which currently holds no id.
470 struct RPS_SamplerElement *
471 RPS_sampler_elem_create (void)
473 struct RPS_SamplerElement *s;
475 s = GNUNET_new (struct RPS_SamplerElement);
477 RPS_sampler_elem_reinit (s);
484 * Input an PeerID into the given sampler element.
486 * @param sampler the sampler the @a s_elem belongs to.
490 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
491 struct RPS_Sampler *sampler,
492 const struct GNUNET_PeerIdentity *other)
494 struct GNUNET_HashCode other_hash;
499 to_file (s_elem->file_name,
501 GNUNET_i2s_full (other));
504 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
506 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
508 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
509 GNUNET_i2s (&(s_elem->peer_id)));
513 GNUNET_CRYPTO_hmac(&s_elem->auth_key,
515 sizeof(struct GNUNET_PeerIdentity),
518 if (EMPTY == s_elem->is_empty)
520 LOG (GNUNET_ERROR_TYPE_DEBUG,
521 "Got PeerID %s; Simply accepting (was empty previously).\n",
523 s_elem->peer_id = *other;
524 s_elem->peer_id_hash = other_hash;
526 s_elem->num_change++;
528 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
530 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
532 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
533 GNUNET_i2s (&s_elem->peer_id));
534 s_elem->peer_id = *other;
535 s_elem->peer_id_hash = other_hash;
537 s_elem->num_change++;
541 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
543 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
544 GNUNET_i2s (&s_elem->peer_id));
547 s_elem->is_empty = NOT_EMPTY;
549 to_file (s_elem->file_name,
551 GNUNET_i2s_full (&s_elem->peer_id));
557 * Get the size of the sampler.
559 * @param sampler the sampler to return the size of.
560 * @return the size of the sampler
563 RPS_sampler_get_size (struct RPS_Sampler *sampler)
565 return sampler->sampler_size;
570 * Grow or shrink the size of the sampler.
572 * @param sampler the sampler to resize.
573 * @param new_size the new size of the sampler
576 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
578 unsigned int old_size;
581 // TODO check min and max size
583 old_size = sampler->sampler_size;
585 if (old_size > new_size)
587 /* Temporary store those to properly call the removeCB on those later */
589 LOG (GNUNET_ERROR_TYPE_DEBUG,
590 "Shrinking sampler %d -> %d\n",
594 to_file (sampler->file_name,
595 "Shrinking sampler %d -> %d",
599 for (i = new_size ; i < old_size ; i++)
601 to_file (sampler->file_name,
604 sampler->sampler_elements[i]->file_name);
607 GNUNET_array_grow (sampler->sampler_elements,
608 sampler->sampler_size,
610 LOG (GNUNET_ERROR_TYPE_DEBUG,
611 "sampler->sampler_elements now points to %p\n",
612 sampler->sampler_elements);
615 else if (old_size < new_size)
617 LOG (GNUNET_ERROR_TYPE_DEBUG,
618 "Growing sampler %d -> %d\n",
622 to_file (sampler->file_name,
623 "Growing sampler %d -> %d",
627 GNUNET_array_grow (sampler->sampler_elements,
628 sampler->sampler_size,
631 for (i = old_size ; i < new_size ; i++)
632 { /* Add new sampler elements */
633 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
635 to_file (sampler->file_name,
638 sampler->sampler_elements[i]->file_name);
644 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
648 GNUNET_assert (sampler->sampler_size == new_size);
653 * Grow or shrink the size of the sampler.
655 * @param sampler the sampler to resize.
656 * @param new_size the new size of the sampler
659 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
661 GNUNET_assert (0 < new_size);
662 sampler_resize (sampler, new_size);
669 * @param sampler the sampler to empty.
670 * @param new_size the new size of the sampler
673 sampler_empty (struct RPS_Sampler *sampler)
675 sampler_resize (sampler, 0);
680 * Initialise a tuple of sampler elements.
682 * @param init_size the size the sampler is initialised with
683 * @param max_round_interval maximum time a round takes
684 * @return a handle to a sampler that consists of sampler elements.
687 RPS_sampler_init (size_t init_size,
688 struct GNUNET_TIME_Relative max_round_interval)
690 struct RPS_Sampler *sampler;
692 /* Initialise context around extended sampler */
693 min_size = 10; // TODO make input to _samplers_init()
694 max_size = 1000; // TODO make input to _samplers_init()
696 sampler = GNUNET_new (struct RPS_Sampler);
699 if (NULL == (sampler->file_name = GNUNET_DISK_mktemp ("sampler-")))
700 LOG (GNUNET_ERROR_TYPE_WARNING,
701 "Could not create file\n");
702 LOG (GNUNET_ERROR_TYPE_DEBUG,
703 "Initialised sampler %s\n",
707 sampler->sampler_size = 0;
708 sampler->sampler_elements = NULL;
709 sampler->max_round_interval = max_round_interval;
710 sampler->get_peers = sampler_get_rand_peer;
711 sampler->gpc_head = NULL;
712 sampler->gpc_tail = NULL;
713 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
714 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
715 RPS_sampler_resize (sampler, init_size);
717 client_get_index = 0;
719 //GNUNET_assert (init_size == sampler->sampler_size);
724 * Initialise a modified tuple of sampler elements.
726 * @param init_size the size the sampler is initialised with
727 * @param max_round_interval maximum time a round takes
728 * @return a handle to a sampler that consists of sampler elements.
731 RPS_sampler_mod_init (size_t init_size,
732 struct GNUNET_TIME_Relative max_round_interval)
734 struct RPS_Sampler *sampler;
736 sampler = RPS_sampler_init (init_size, max_round_interval);
737 sampler->get_peers = sampler_get_rand_peer2;
740 LOG (GNUNET_ERROR_TYPE_DEBUG,
741 "Initialised modified sampler %s\n",
743 to_file (sampler->file_name,
744 "This is a modified sampler");
752 * A fuction to update every sampler in the given list
754 * @param sampler the sampler to update.
755 * @param id the PeerID that is put in the sampler
758 RPS_sampler_update (struct RPS_Sampler *sampler,
759 const struct GNUNET_PeerIdentity *id)
764 to_file (sampler->file_name,
766 GNUNET_i2s_full (id));
769 for (i = 0 ; i < sampler->sampler_size ; i++)
771 RPS_sampler_elem_next (sampler->sampler_elements[i],
779 * Reinitialise all previously initialised sampler elements with the given value.
781 * Used to get rid of a PeerID.
783 * @param sampler the sampler to reinitialise a sampler element in.
784 * @param id the id of the sampler elements to update.
787 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
788 const struct GNUNET_PeerIdentity *id)
792 for ( i = 0 ; i < sampler->sampler_size ; i++ )
794 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) )
796 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
797 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
804 * Get one random peer out of the sampled peers.
806 * We might want to reinitialise this sampler after giving the
807 * corrsponding peer to the client.
808 * Only used internally
811 sampler_get_rand_peer2 (void *cls,
812 const struct GNUNET_SCHEDULER_TaskContext *tc)
814 struct GetPeerCls *gpc = cls;
817 gpc->get_peer_task = NULL;
818 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
822 * Choose the r_index of the peer we want to return
823 * at random from the interval of the gossip list
825 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
826 gpc->sampler->sampler_size);
828 if (EMPTY == gpc->sampler->sampler_elements[r_index]->is_empty)
830 //LOG (GNUNET_ERROR_TYPE_DEBUG,
831 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
834 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
835 GNUNET_TIME_UNIT_SECONDS, 0.1),
836 &sampler_get_rand_peer2,
841 *gpc->id = gpc->sampler->sampler_elements[r_index]->peer_id;
843 gpc->cont (gpc->cont_cls, gpc->id);
845 GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head,
846 gpc->sampler->gpc_tail,
854 * Get one random peer out of the sampled peers.
856 * We might want to reinitialise this sampler after giving the
857 * corrsponding peer to the client.
860 sampler_get_rand_peer (void *cls,
861 const struct GNUNET_SCHEDULER_TaskContext *tc)
863 struct GetPeerCls *gpc = cls;
864 struct GNUNET_PeerIdentity tmp_id;
865 unsigned int empty_flag;
866 struct RPS_SamplerElement *s_elem;
867 struct GNUNET_TIME_Relative last_request_diff;
868 uint32_t tmp_client_get_index;
870 gpc->get_peer_task = NULL;
871 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
874 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
877 /* Store the next #client_get_index to check whether we cycled over the whole list */
878 if (0 < client_get_index)
879 tmp_client_get_index = client_get_index - 1;
881 tmp_client_get_index = gpc->sampler->sampler_size - 1;
883 LOG (GNUNET_ERROR_TYPE_DEBUG,
884 "sched for later if index reaches %" PRIX32 " (sampler size: %" PRIX32 ").\n",
885 tmp_client_get_index, gpc->sampler->sampler_size);
888 { /* Get first non empty sampler */
889 if (tmp_client_get_index == client_get_index)
890 { /* We once cycled over the whole list */
891 LOG (GNUNET_ERROR_TYPE_DEBUG, "reached tmp_index %" PRIX32 ".\n",
893 GNUNET_assert (NULL == gpc->get_peer_task);
895 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
896 &sampler_get_rand_peer,
901 tmp_id = gpc->sampler->sampler_elements[client_get_index]->peer_id;
902 empty_flag = gpc->sampler->sampler_elements[client_get_index]->is_empty;
903 RPS_sampler_elem_reinit (gpc->sampler->sampler_elements[client_get_index]);
904 if (EMPTY != empty_flag)
905 RPS_sampler_elem_next (gpc->sampler->sampler_elements[client_get_index],
909 /* Cycle the #client_get_index one step further */
910 if ( client_get_index == gpc->sampler->sampler_size - 1 )
911 client_get_index = 0;
915 LOG (GNUNET_ERROR_TYPE_DEBUG, "incremented index to %" PRIX32 ".\n",
917 } while (EMPTY == gpc->sampler->sampler_elements[client_get_index]->is_empty);
919 s_elem = gpc->sampler->sampler_elements[client_get_index];
920 *gpc->id = s_elem->peer_id;
922 /* Check whether we may use this sampler to give it back to the client */
923 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
926 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
927 GNUNET_TIME_absolute_get ());
928 /* We're not going to give it back now if it was
929 * already requested by a client this round */
930 if (last_request_diff.rel_value_us < gpc->sampler->max_round_interval.rel_value_us)
932 LOG (GNUNET_ERROR_TYPE_DEBUG,
933 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
934 ///* How many time remains untile the next round has started? */
935 //inv_last_request_diff =
936 // GNUNET_TIME_absolute_get_difference (last_request_diff,
937 // sampler->max_round_interval);
938 // add a little delay
939 /* Schedule it one round later */
940 GNUNET_assert (NULL == gpc->get_peer_task);
942 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
943 &sampler_get_rand_peer,
947 // TODO add other reasons to wait here
950 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
952 GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head,
953 gpc->sampler->gpc_tail,
955 gpc->cont (gpc->cont_cls, gpc->id);
961 * Get n random peers out of the sampled peers.
963 * We might want to reinitialise this sampler after giving the
964 * corrsponding peer to the client.
965 * Random with or without consumption?
967 * @param sampler the sampler to get peers from.
968 * @param cb callback that will be called once the ids are ready.
969 * @param cls closure given to @a cb
970 * @param for_client #GNUNET_YES if result is used for client,
971 * #GNUNET_NO if used internally
972 * @param num_peers the number of peers requested
975 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
976 RPS_sampler_n_rand_peers_ready_cb cb,
977 void *cls, uint32_t num_peers)
979 GNUNET_assert (0 != sampler->sampler_size);
981 // TODO check if we have too much (distinct) sampled peers
983 struct NRandPeersReadyCls *cb_cls;
984 struct GetPeerCls *gpc;
986 cb_cls = GNUNET_new (struct NRandPeersReadyCls);
987 cb_cls->num_peers = num_peers;
988 cb_cls->cur_num_peers = 0;
989 cb_cls->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
990 cb_cls->callback = cb;
993 LOG (GNUNET_ERROR_TYPE_DEBUG,
994 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
996 for (i = 0 ; i < num_peers ; i++)
998 gpc = GNUNET_new (struct GetPeerCls);
999 gpc->sampler = sampler;
1000 gpc->cont = check_n_peers_ready;
1001 gpc->cont_cls = cb_cls;
1002 gpc->id = &cb_cls->ids[i];
1004 // maybe add a little delay
1005 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, gpc);
1007 GNUNET_CONTAINER_DLL_insert (sampler->gpc_head,
1015 * Counts how many Samplers currently hold a given PeerID.
1017 * @param sampler the sampler to count ids in.
1018 * @param id the PeerID to count.
1020 * @return the number of occurrences of id.
1023 RPS_sampler_count_id (struct RPS_Sampler *sampler,
1024 const struct GNUNET_PeerIdentity *id)
1030 for ( i = 0 ; i < sampler->sampler_size ; i++ )
1032 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
1033 && EMPTY != sampler->sampler_elements[i]->is_empty)
1041 * Cleans the sampler.
1044 RPS_sampler_destroy (struct RPS_Sampler *sampler)
1046 struct GetPeerCls *i;
1048 for (i = sampler->gpc_head; NULL != i; i = sampler->gpc_head)
1050 GNUNET_CONTAINER_DLL_remove (sampler->gpc_head,
1053 GNUNET_SCHEDULER_cancel (i->get_peer_task);
1057 sampler_empty (sampler);
1058 GNUNET_free (sampler);
1061 /* end of gnunet-service-rps.c */