Be a little bit more consistent in style
[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) (const struct GNUNET_PeerIdentity *ids,
50                                       uint32_t num_peers,
51                                       void *cls);
52
53
54 /**
55  * Get the size of the sampler.
56  *
57  * @param sampler the sampler to return the size of.
58  * @return the size of the sampler
59  */
60 unsigned int
61 RPS_sampler_get_size (struct RPS_Sampler *sampler);
62
63
64 /**
65  * Grow or shrink the size of the sampler.
66  *
67  * @param sampler the sampler to resize.
68  * @param new_size the new size of the sampler (not 0)
69  */
70 void
71 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
72
73
74 /**
75  * Initialise a tuple of samplers.
76  *
77  * @param init_size the size the sampler is initialised with
78  * @param max_round_interval maximum time a round takes
79  * @return a handle to a sampler that consists of sampler elements.
80  */
81 struct RPS_Sampler *
82 RPS_sampler_init (size_t init_size,
83     struct GNUNET_TIME_Relative max_round_interval);
84
85
86 /**
87  * Initialise a modified tuple of sampler elements.
88  *
89  * @param init_size the size the sampler is initialised with
90  * @param max_round_interval maximum time a round takes
91  * @return a handle to a sampler that consists of sampler elements.
92  */
93 struct RPS_Sampler *
94 RPS_sampler_mod_init (size_t init_size,
95                       struct GNUNET_TIME_Relative max_round_interval);
96
97
98 /**
99  * A fuction to update every sampler in the given list
100  *
101  * @param sampler the sampler to update.
102  * @param id the PeerID that is put in the sampler
103  */
104   void
105 RPS_sampler_update (struct RPS_Sampler *sampler,
106                     const struct GNUNET_PeerIdentity *id);
107
108
109 /**
110  * Reinitialise all previously initialised sampler elements with the given
111  * value.
112  *
113  * Used to get rid of a PeerID.
114  *
115  * @param sampler the sampler to reinitialise a sampler in.
116  * @param id the id of the samplers to update.
117  */
118   void
119 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
120                                    const struct GNUNET_PeerIdentity *id);
121
122
123 /**
124  * Get n random peers out of the sampled peers.
125  *
126  * We might want to reinitialise this sampler after giving the
127  * corrsponding peer to the client.
128  * Random with or without consumption?
129  *
130  * @param sampler the sampler to get peers from.
131  * @param cb callback that will be called once the ids are ready.
132  * @param cls closure given to @a cb
133  * @param for_client #GNUNET_YES if result is used for client,
134  *                   #GNUNET_NO if used internally
135  * @param num_peers the number of peers requested
136  */
137 struct RPS_SamplerRequestHandle *
138 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
139                               uint32_t num_peers,
140                               RPS_sampler_n_rand_peers_ready_cb cb,
141                               void *cls);
142
143 /**
144  * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
145  *
146  * @param req_handle the handle to the request
147  */
148 void
149 RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
150
151
152 /**
153  * Counts how many Samplers currently hold a given PeerID.
154  *
155  * @param sampler the sampler to cound ids in.
156  * @param id the PeerID to count.
157  *
158  * @return the number of occurrences of id.
159  */
160   uint32_t
161 RPS_sampler_count_id (struct RPS_Sampler *sampler,
162                       const struct GNUNET_PeerIdentity *id);
163
164
165 /**
166  * Cleans the samplers.
167  *
168  * @param sampler the sampler to destroy.
169  */
170   void
171 RPS_sampler_destroy (struct RPS_Sampler *sampler);
172
173 #endif
174 /* end of gnunet-service-rps.c */