Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / rps / gnunet-service-rps_sampler.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
4
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.
9
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.
14     
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/>.
17 */
18
19 /**
20  * @file rps/gnunet-service-rps_sampler.h
21  * @brief sampler implementation
22  * @author Julius Bünger
23  */
24
25 #ifndef RPS_SAMPLER_H
26 #define RPS_SAMPLER_H
27 #include <inttypes.h>
28
29
30 /**
31  * A sampler sampling a stream of PeerIDs.
32  */
33 struct RPS_Sampler;
34
35 /**
36  * A handle to cancel a request.
37  */
38 struct RPS_SamplerRequestHandle;
39
40
41 /**
42  * Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
43  *
44  * @param cls the closure given alongside this function.
45  * @param ids the PeerIDs that were returned
46  *        to be freed
47  */
48   typedef void
49 (*RPS_sampler_n_rand_peers_ready_cb) (void *cls,
50     struct GNUNET_PeerIdentity *ids, uint32_t num_peers);
51
52
53 /**
54  * Get the size of the sampler.
55  *
56  * @param sampler the sampler to return the size of.
57  * @return the size of the sampler
58  */
59 unsigned int
60 RPS_sampler_get_size (struct RPS_Sampler *sampler);
61
62
63 /**
64  * Grow or shrink the size of the sampler.
65  *
66  * @param sampler the sampler to resize.
67  * @param new_size the new size of the sampler (not 0)
68  */
69 void
70 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
71
72
73 /**
74  * Initialise a tuple of samplers.
75  *
76  * @param init_size the size the sampler is initialised with
77  * @param max_round_interval maximum time a round takes
78  * @return a handle to a sampler that consists of sampler elements.
79  */
80 struct RPS_Sampler *
81 RPS_sampler_init (size_t init_size,
82     struct GNUNET_TIME_Relative max_round_interval);
83
84
85 /**
86  * Initialise a modified tuple of sampler elements.
87  *
88  * @param init_size the size the sampler is initialised with
89  * @param max_round_interval maximum time a round takes
90  * @return a handle to a sampler that consists of sampler elements.
91  */
92 struct RPS_Sampler *
93 RPS_sampler_mod_init (size_t init_size,
94                       struct GNUNET_TIME_Relative max_round_interval);
95
96
97 /**
98  * A fuction to update every sampler in the given list
99  *
100  * @param sampler the sampler to update.
101  * @param id the PeerID that is put in the sampler
102  */
103   void
104 RPS_sampler_update (struct RPS_Sampler *sampler,
105                     const struct GNUNET_PeerIdentity *id);
106
107
108 /**
109  * Reinitialise all previously initialised sampler elements with the given
110  * value.
111  *
112  * Used to get rid of a PeerID.
113  *
114  * @param sampler the sampler to reinitialise a sampler in.
115  * @param id the id of the samplers to update.
116  */
117   void
118 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
119                                    const struct GNUNET_PeerIdentity *id);
120
121
122 /**
123  * Get n random peers out of the sampled peers.
124  *
125  * We might want to reinitialise this sampler after giving the
126  * corrsponding peer to the client.
127  * Random with or without consumption?
128  *
129  * @param sampler the sampler to get peers from.
130  * @param cb callback that will be called once the ids are ready.
131  * @param cls closure given to @a cb
132  * @param for_client #GNUNET_YES if result is used for client,
133  *                   #GNUNET_NO if used internally
134  * @param num_peers the number of peers requested
135  */
136 struct RPS_SamplerRequestHandle *
137 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
138                               RPS_sampler_n_rand_peers_ready_cb cb,
139                               void *cls, uint32_t num_peers);
140
141 /**
142  * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
143  *
144  * @param req_handle the handle to the request
145  */
146 void
147 RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
148
149
150 /**
151  * Counts how many Samplers currently hold a given PeerID.
152  *
153  * @param sampler the sampler to cound ids in.
154  * @param id the PeerID to count.
155  *
156  * @return the number of occurrences of id.
157  */
158   uint32_t
159 RPS_sampler_count_id (struct RPS_Sampler *sampler,
160                       const struct GNUNET_PeerIdentity *id);
161
162
163 /**
164  * Cleans the samplers.
165  *
166  * @param sampler the sampler to destroy.
167  */
168   void
169 RPS_sampler_destroy (struct RPS_Sampler *sampler);
170
171 #endif
172 /* end of gnunet-service-rps.c */