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 * Type of function used to differentiate between modified and not modified
119 (*RPS_get_peers_type) (void *cls,
120 const struct GNUNET_SCHEDULER_TaskContext *tc);
123 * Get one random peer out of the sampled peers.
125 * We might want to reinitialise this sampler after giving the
126 * corrsponding peer to the client.
127 * Only used internally
130 sampler_get_rand_peer2 (void *cls,
131 const struct GNUNET_SCHEDULER_TaskContext *tc);
134 * Get one random peer out of the sampled peers.
136 * We might want to reinitialise this sampler after giving the
137 * corrsponding peer to the client.
140 sampler_get_rand_peer (void *cls,
141 const struct GNUNET_SCHEDULER_TaskContext *tc);
145 * Sampler with its own array of SamplerElements
150 * Number of sampler elements we hold.
152 unsigned int sampler_size;
156 * All Samplers in one array.
158 struct RPS_SamplerElement **sampler_elements;
161 * Maximum time a round takes
163 * Used in the context of RPS
165 struct GNUNET_TIME_Relative max_round_interval;
168 * Stores the function to return peers. Which one it is depends on whether
169 * the Sampler is the modified one or not.
171 RPS_get_peers_type get_peers;
175 * File name to log to
182 * Closure to _get_n_rand_peers_ready_cb()
184 struct NRandPeersReadyCls
187 * Number of peers we are waiting for.
192 * Number of peers we currently have.
194 uint32_t cur_num_peers;
197 * Pointer to the array holding the ids.
199 struct GNUNET_PeerIdentity *ids;
202 * Callback to be called when all ids are available.
204 RPS_sampler_n_rand_peers_ready_cb callback;
207 * Closure given to the callback
213 * Callback that is called from _get_rand_peer() when the PeerID is ready.
215 * @param cls the closure given alongside this function.
216 * @param id the PeerID that was returned
219 (*RPS_sampler_rand_peer_ready_cont) (void *cls,
220 const struct GNUNET_PeerIdentity *id);
223 * Closure for #sampler_get_rand_peer()
230 struct GetPeerCls *next;
235 struct GetPeerCls *prev;
238 * The sampler this function operates on.
240 struct RPS_Sampler *sampler;
243 * The task for this function.
245 struct GNUNET_SCHEDULER_Task *get_peer_task;
250 RPS_sampler_rand_peer_ready_cont cont;
253 * The closure to the callback @e cont
258 * The address of the id to be stored at
260 struct GNUNET_PeerIdentity *id;
265 // * Global sampler variable.
267 //struct RPS_Sampler *sampler;
271 * The minimal size for the extended sampler elements.
273 static size_t min_size;
276 * The maximal size the extended sampler elements should grow to.
278 static size_t max_size;
281 * The size the extended sampler elements currently have.
283 //static size_t extra_size;
286 * Inedex to the sampler element that is the next to be returned
288 static uint32_t client_get_index;
293 * This function is used to facilitate writing important information to disk
295 #define to_file(file_name, ...) do {char tmp_buf[512];\
297 size = GNUNET_snprintf(tmp_buf,sizeof(tmp_buf),__VA_ARGS__);\
299 LOG (GNUNET_ERROR_TYPE_WARNING,\
300 "Failed to create tmp_buf\n");\
302 to_file_(file_name,tmp_buf);\
306 to_file_ (char *file_name, char *line)
308 struct GNUNET_DISK_FileHandle *f;
309 char output_buffer[512];
315 if (NULL == (f = GNUNET_DISK_file_open (file_name,
316 GNUNET_DISK_OPEN_APPEND |
317 GNUNET_DISK_OPEN_WRITE |
318 GNUNET_DISK_OPEN_CREATE,
319 GNUNET_DISK_PERM_USER_WRITE)))
321 LOG (GNUNET_ERROR_TYPE_WARNING,
322 "Not able to open file %s\n",
326 size = GNUNET_snprintf (output_buffer,
327 sizeof (output_buffer),
329 GNUNET_TIME_absolute_get ().abs_value_us,
333 LOG (GNUNET_ERROR_TYPE_WARNING,
334 "Failed to write string to buffer (size: %i)\n",
339 size2 = GNUNET_DISK_file_write (f, output_buffer, size);
342 LOG (GNUNET_ERROR_TYPE_WARNING,
343 "Unable to write to file! (Size: %u, size2: %u)\n",
349 if (GNUNET_YES != GNUNET_DISK_file_close (f))
350 LOG (GNUNET_ERROR_TYPE_WARNING,
351 "Unable to close file\n");
356 /** FIXME document */
357 static struct GetPeerCls *gpc_head;
358 static struct GetPeerCls *gpc_tail;
362 * Callback to _get_rand_peer() used by _get_n_rand_peers().
364 * Checks whether all n peers are available. If they are,
368 check_n_peers_ready (void *cls,
369 const struct GNUNET_PeerIdentity *id)
371 struct NRandPeersReadyCls *n_peers_cls = cls;
373 n_peers_cls->cur_num_peers++;
374 LOG (GNUNET_ERROR_TYPE_DEBUG,
375 "Got %" PRIX32 ". of %" PRIX32 " peers\n",
376 n_peers_cls->cur_num_peers, n_peers_cls->num_peers);
378 if (n_peers_cls->num_peers == n_peers_cls->cur_num_peers)
379 { /* All peers are ready -- return those to the client */
380 GNUNET_assert (NULL != n_peers_cls->callback);
382 LOG (GNUNET_ERROR_TYPE_DEBUG,
383 "returning %" PRIX32 " peers to the client\n",
384 n_peers_cls->num_peers);
385 n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers);
387 GNUNET_free (n_peers_cls);
393 * Reinitialise a previously initialised sampler element.
395 * @param sampler pointer to the memory that keeps the value.
398 RPS_sampler_elem_reinit (struct RPS_SamplerElement *sampler_el)
400 sampler_el->is_empty = EMPTY;
402 // I guess I don't need to call GNUNET_CRYPTO_hmac_derive_key()...
403 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_STRONG,
404 &(sampler_el->auth_key.key),
405 GNUNET_CRYPTO_HASH_LENGTH);
408 /* Create a file(-name) to store internals to */
413 size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;
416 keylen += 5 - keylen % 5;
418 buf = GNUNET_malloc (keylen + 1);
420 end = GNUNET_STRINGS_data_to_string (&(sampler_el->auth_key.key),
421 sizeof (struct GNUNET_CRYPTO_AuthKey),
435 size = GNUNET_snprintf (name_buf, sizeof (name_buf), "sampler_el-%s-", buf);
437 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
439 if (NULL == (sampler_el->file_name = GNUNET_DISK_mktemp (name_buf)))
440 LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");
443 sampler_el->last_client_request = GNUNET_TIME_UNIT_FOREVER_ABS;
445 sampler_el->birth = GNUNET_TIME_absolute_get ();
446 sampler_el->num_peers = 0;
447 sampler_el->num_change = 0;
452 * (Re)Initialise given Sampler with random min-wise independent function.
454 * In this implementation this means choosing an auth_key for later use in
457 * @return a newly created RPS_SamplerElement which currently holds no id.
459 struct RPS_SamplerElement *
460 RPS_sampler_elem_create (void)
462 struct RPS_SamplerElement *s;
464 s = GNUNET_new (struct RPS_SamplerElement);
466 RPS_sampler_elem_reinit (s);
473 * Input an PeerID into the given sampler element.
475 * @param sampler the sampler the @a s_elem belongs to.
479 RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem,
480 struct RPS_Sampler *sampler,
481 const struct GNUNET_PeerIdentity *other)
483 struct GNUNET_HashCode other_hash;
488 to_file (s_elem->file_name,
490 GNUNET_i2s_full (other));
493 if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)))
495 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
497 LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
498 GNUNET_i2s (&(s_elem->peer_id)));
502 GNUNET_CRYPTO_hmac(&s_elem->auth_key,
504 sizeof(struct GNUNET_PeerIdentity),
507 if (EMPTY == s_elem->is_empty)
509 LOG (GNUNET_ERROR_TYPE_DEBUG,
510 "Got PeerID %s; Simply accepting (was empty previously).\n",
512 s_elem->peer_id = *other;
513 s_elem->peer_id_hash = other_hash;
515 s_elem->num_change++;
517 else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash))
519 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
521 LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n",
522 GNUNET_i2s (&s_elem->peer_id));
523 s_elem->peer_id = *other;
524 s_elem->peer_id_hash = other_hash;
526 s_elem->num_change++;
530 LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n",
532 LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n",
533 GNUNET_i2s (&s_elem->peer_id));
536 s_elem->is_empty = NOT_EMPTY;
538 to_file (s_elem->file_name,
540 GNUNET_i2s_full (&s_elem->peer_id));
546 * Get the size of the sampler.
548 * @param sampler the sampler to return the size of.
549 * @return the size of the sampler
552 RPS_sampler_get_size (struct RPS_Sampler *sampler)
554 return sampler->sampler_size;
559 * Grow or shrink the size of the sampler.
561 * @param sampler the sampler to resize.
562 * @param new_size the new size of the sampler
565 sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
567 unsigned int old_size;
570 // TODO check min and max size
572 old_size = sampler->sampler_size;
574 if (old_size > new_size)
576 /* Temporary store those to properly call the removeCB on those later */
578 LOG (GNUNET_ERROR_TYPE_DEBUG,
579 "Shrinking sampler %d -> %d\n",
583 to_file (sampler->file_name,
584 "Shrinking sampler %d -> %d",
588 for (i = new_size ; i < old_size ; i++)
590 to_file (sampler->file_name,
593 sampler->sampler_elements[i]->file_name);
596 GNUNET_array_grow (sampler->sampler_elements,
597 sampler->sampler_size,
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "sampler->sampler_elements now points to %p\n",
601 sampler->sampler_elements);
604 else if (old_size < new_size)
606 LOG (GNUNET_ERROR_TYPE_DEBUG,
607 "Growing sampler %d -> %d\n",
611 to_file (sampler->file_name,
612 "Growing sampler %d -> %d",
616 GNUNET_array_grow (sampler->sampler_elements,
617 sampler->sampler_size,
620 for (i = old_size ; i < new_size ; i++)
621 { /* Add new sampler elements */
622 sampler->sampler_elements[i] = RPS_sampler_elem_create ();
624 to_file (sampler->file_name,
627 sampler->sampler_elements[i]->file_name);
633 LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n");
637 GNUNET_assert (sampler->sampler_size == new_size);
642 * Grow or shrink the size of the sampler.
644 * @param sampler the sampler to resize.
645 * @param new_size the new size of the sampler
648 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size)
650 GNUNET_assert (0 < new_size);
651 sampler_resize (sampler, new_size);
658 * @param sampler the sampler to empty.
659 * @param new_size the new size of the sampler
662 sampler_empty (struct RPS_Sampler *sampler)
664 sampler_resize (sampler, 0);
669 * Initialise a tuple of sampler elements.
671 * @param init_size the size the sampler is initialised with
672 * @param max_round_interval maximum time a round takes
673 * @return a handle to a sampler that consists of sampler elements.
676 RPS_sampler_init (size_t init_size,
677 struct GNUNET_TIME_Relative max_round_interval)
679 struct RPS_Sampler *sampler;
681 /* Initialise context around extended sampler */
682 min_size = 10; // TODO make input to _samplers_init()
683 max_size = 1000; // TODO make input to _samplers_init()
685 sampler = GNUNET_new (struct RPS_Sampler);
688 if (NULL == (sampler->file_name = GNUNET_DISK_mktemp ("sampler-")))
689 LOG (GNUNET_ERROR_TYPE_WARNING,
690 "Could not create file\n");
691 LOG (GNUNET_ERROR_TYPE_DEBUG,
692 "Initialised sampler %s\n",
696 sampler->sampler_size = 0;
697 sampler->sampler_elements = NULL;
698 sampler->max_round_interval = max_round_interval;
699 sampler->get_peers = sampler_get_rand_peer;
700 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
701 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
702 RPS_sampler_resize (sampler, init_size);
704 client_get_index = 0;
706 //GNUNET_assert (init_size == sampler->sampler_size);
711 * Initialise a modified tuple of sampler elements.
713 * @param init_size the size the sampler is initialised with
714 * @param max_round_interval maximum time a round takes
715 * @return a handle to a sampler that consists of sampler elements.
718 RPS_sampler_mod_init (size_t init_size,
719 struct GNUNET_TIME_Relative max_round_interval)
721 struct RPS_Sampler *sampler;
723 sampler = RPS_sampler_init (init_size, max_round_interval);
724 sampler->get_peers = sampler_get_rand_peer2;
727 LOG (GNUNET_ERROR_TYPE_DEBUG,
728 "Initialised modified sampler %s\n",
730 to_file (sampler->file_name,
731 "This is a modified sampler");
739 * A fuction to update every sampler in the given list
741 * @param sampler the sampler to update.
742 * @param id the PeerID that is put in the sampler
745 RPS_sampler_update (struct RPS_Sampler *sampler,
746 const struct GNUNET_PeerIdentity *id)
751 to_file (sampler->file_name,
753 GNUNET_i2s_full (id));
756 for (i = 0 ; i < sampler->sampler_size ; i++)
758 RPS_sampler_elem_next (sampler->sampler_elements[i],
766 * Reinitialise all previously initialised sampler elements with the given value.
768 * Used to get rid of a PeerID.
770 * @param sampler the sampler to reinitialise a sampler element in.
771 * @param id the id of the sampler elements to update.
774 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
775 const struct GNUNET_PeerIdentity *id)
779 for ( i = 0 ; i < sampler->sampler_size ; i++ )
781 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity(id, &(sampler->sampler_elements[i]->peer_id)) )
783 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
784 RPS_sampler_elem_reinit (sampler->sampler_elements[i]);
791 * Get one random peer out of the sampled peers.
793 * We might want to reinitialise this sampler after giving the
794 * corrsponding peer to the client.
795 * Only used internally
798 sampler_get_rand_peer2 (void *cls,
799 const struct GNUNET_SCHEDULER_TaskContext *tc)
801 struct GetPeerCls *gpc = cls;
804 gpc->get_peer_task = NULL;
805 GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, gpc);
806 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
810 * Choose the r_index of the peer we want to return
811 * at random from the interval of the gossip list
813 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
814 gpc->sampler->sampler_size);
816 if ( EMPTY == gpc->sampler->sampler_elements[r_index]->is_empty )
818 gpc->get_peer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
819 GNUNET_TIME_UNIT_SECONDS,
821 &sampler_get_rand_peer2,
826 *gpc->id = gpc->sampler->sampler_elements[r_index]->peer_id;
828 gpc->cont (gpc->cont_cls, gpc->id);
834 * Get one random peer out of the sampled peers.
836 * We might want to reinitialise this sampler after giving the
837 * corrsponding peer to the client.
840 sampler_get_rand_peer (void *cls,
841 const struct GNUNET_SCHEDULER_TaskContext *tc)
843 struct GetPeerCls *gpc = cls;
844 struct GNUNET_PeerIdentity tmp_id;
845 unsigned int empty_flag;
846 struct RPS_SamplerElement *s_elem;
847 struct GNUNET_TIME_Relative last_request_diff;
848 uint32_t tmp_client_get_index;
850 gpc->get_peer_task = NULL;
851 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
854 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
857 /* Store the next #client_get_index to check whether we cycled over the whole list */
858 if (0 < client_get_index)
859 tmp_client_get_index = client_get_index - 1;
861 tmp_client_get_index = gpc->sampler->sampler_size - 1;
863 LOG (GNUNET_ERROR_TYPE_DEBUG,
864 "sched for later if index reaches %" PRIX32 " (sampler size: %" PRIX32 ").\n",
865 tmp_client_get_index, gpc->sampler->sampler_size);
868 { /* Get first non empty sampler */
869 if (tmp_client_get_index == client_get_index)
870 { /* We once cycled over the whole list */
871 LOG (GNUNET_ERROR_TYPE_DEBUG, "reached tmp_index %" PRIX32 ".\n",
873 GNUNET_assert (NULL == gpc->get_peer_task);
875 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
876 &sampler_get_rand_peer,
881 tmp_id = gpc->sampler->sampler_elements[client_get_index]->peer_id;
882 empty_flag = gpc->sampler->sampler_elements[client_get_index]->is_empty;
883 RPS_sampler_elem_reinit (gpc->sampler->sampler_elements[client_get_index]);
884 if (EMPTY != empty_flag)
885 RPS_sampler_elem_next (gpc->sampler->sampler_elements[client_get_index],
889 /* Cycle the #client_get_index one step further */
890 if ( client_get_index == gpc->sampler->sampler_size - 1 )
891 client_get_index = 0;
895 LOG (GNUNET_ERROR_TYPE_DEBUG, "incremented index to %" PRIX32 ".\n",
897 } while (EMPTY == gpc->sampler->sampler_elements[client_get_index]->is_empty);
899 s_elem = gpc->sampler->sampler_elements[client_get_index];
900 *gpc->id = s_elem->peer_id;
902 /* Check whether we may use this sampler to give it back to the client */
903 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
906 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
907 GNUNET_TIME_absolute_get ());
908 /* We're not going to give it back now if it was
909 * already requested by a client this round */
910 if (last_request_diff.rel_value_us < gpc->sampler->max_round_interval.rel_value_us)
912 LOG (GNUNET_ERROR_TYPE_DEBUG,
913 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
914 ///* How many time remains untile the next round has started? */
915 //inv_last_request_diff =
916 // GNUNET_TIME_absolute_get_difference (last_request_diff,
917 // sampler->max_round_interval);
918 // add a little delay
919 /* Schedule it one round later */
920 GNUNET_assert (NULL == gpc->get_peer_task);
922 GNUNET_SCHEDULER_add_delayed (gpc->sampler->max_round_interval,
923 &sampler_get_rand_peer,
927 // TODO add other reasons to wait here
930 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
932 GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, gpc);
933 gpc->cont (gpc->cont_cls, gpc->id);
939 * Get n random peers out of the sampled peers.
941 * We might want to reinitialise this sampler after giving the
942 * corrsponding peer to the client.
943 * Random with or without consumption?
945 * @param sampler the sampler to get peers from.
946 * @param cb callback that will be called once the ids are ready.
947 * @param cls closure given to @a cb
948 * @param for_client #GNUNET_YES if result is used for client,
949 * #GNUNET_NO if used internally
950 * @param num_peers the number of peers requested
953 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
954 RPS_sampler_n_rand_peers_ready_cb cb,
955 void *cls, uint32_t num_peers)
957 GNUNET_assert (0 != sampler->sampler_size);
959 // TODO check if we have too much (distinct) sampled peers
961 struct NRandPeersReadyCls *cb_cls;
962 struct GetPeerCls *gpc;
964 cb_cls = GNUNET_new (struct NRandPeersReadyCls);
965 cb_cls->num_peers = num_peers;
966 cb_cls->cur_num_peers = 0;
967 cb_cls->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity);
968 cb_cls->callback = cb;
971 LOG (GNUNET_ERROR_TYPE_DEBUG,
972 "Scheduling requests for %" PRIu32 " peers\n", num_peers);
974 for (i = 0 ; i < num_peers ; i++)
976 gpc = GNUNET_new (struct GetPeerCls);
977 gpc->sampler = sampler;
978 gpc->cont = check_n_peers_ready;
979 gpc->cont_cls = cb_cls;
980 gpc->id = &cb_cls->ids[i];
982 // maybe add a little delay
983 gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, gpc);
985 GNUNET_CONTAINER_DLL_insert (gpc_head, gpc_tail, gpc);
991 * Counts how many Samplers currently hold a given PeerID.
993 * @param sampler the sampler to count ids in.
994 * @param id the PeerID to count.
996 * @return the number of occurrences of id.
999 RPS_sampler_count_id (struct RPS_Sampler *sampler,
1000 const struct GNUNET_PeerIdentity *id)
1006 for ( i = 0 ; i < sampler->sampler_size ; i++ )
1008 if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
1009 && EMPTY != sampler->sampler_elements[i]->is_empty)
1017 * Cleans the sampler.
1020 RPS_sampler_destroy (struct RPS_Sampler *sampler)
1022 struct GetPeerCls *i;
1024 for (i = gpc_head; NULL != i; i = gpc_head)
1026 GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, i);
1027 GNUNET_SCHEDULER_cancel (i->get_peer_task);
1031 sampler_empty (sampler);
1032 GNUNET_free (sampler);
1035 /* end of gnunet-service-rps.c */