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