2 This file is part of GNUnet.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file rps/rps-sampler_common.h
23 * @brief Code common to client and service sampler
24 * @author Julius Bünger
27 #ifndef RPS_SAMPLER_COMMON_H
28 #define RPS_SAMPLER_COMMON_H
31 #include "gnunet_util_lib.h"
32 #include "gnunet_statistics_service.h"
34 #include "gnunet-service-rps_sampler_elem.h"
39 #include "rps-test_util.h"
43 * Callback that is called from _get_rand_peer() when the PeerID is ready.
45 * @param cls the closure given alongside this function.
46 * @param id the PeerID that was returned
47 * @param probability The probability with which this sampler has seen all ids
48 * @param num_observed How many ids this sampler has observed
51 (*RPS_sampler_rand_peer_ready_cont) (void *cls,
52 const struct GNUNET_PeerIdentity *id,
54 uint32_t num_observed);
58 * Type of function used to differentiate between modified and not modified
62 (*RPS_get_peers_type) (void *cls);
66 * Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
68 * @param cls the closure given alongside this function.
69 * @param ids the PeerIDs that were returned
73 (*RPS_sampler_n_rand_peers_ready_cb) (const struct GNUNET_PeerIdentity *ids,
79 * Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
81 * @param cls the closure given alongside this function.
82 * @param probability Probability with which all IDs have been observed
83 * @param num_observed Number of observed IDs
84 * @param ids the PeerIDs that were returned
88 (*RPS_sampler_sinlge_info_ready_cb) (const struct GNUNET_PeerIdentity *ids,
91 uint32_t num_observed);
95 * @brief Callback called each time a new peer was put into the sampler
97 * @param cls A possibly given closure
100 (*SamplerNotifyUpdateCB) (void *cls);
104 * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
111 struct GetPeerCls *next;
112 struct GetPeerCls *prev;
115 * The #RPS_SamplerRequestHandle this single request belongs to.
117 struct RPS_SamplerRequestHandle *req_handle;
120 * The #RPS_SamplerRequestHandleSingleInfo this single request belongs to.
122 struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle;
125 * The task for this function.
127 struct GNUNET_SCHEDULER_Task *get_peer_task;
130 * @brief Context to the given callback.
132 struct SamplerNotifyUpdateCTX *notify_ctx;
137 RPS_sampler_rand_peer_ready_cont cont;
140 * The closure to the callback @e cont
145 * The address of the id to be stored at
147 struct GNUNET_PeerIdentity *id;
152 * Sampler with its own array of SamplerElements
157 * Number of sampler elements we hold.
159 unsigned int sampler_size;
163 * All sampler elements in one array.
165 struct RPS_SamplerElement **sampler_elements;
168 * Maximum time a round takes
170 * Used in the context of RPS
172 struct GNUNET_TIME_Relative max_round_interval;
175 * @brief The estimated total number of peers in the network
177 uint32_t num_peers_estim;
180 * @brief The desired probability with which we want to have observed all
183 double desired_probability;
186 * @brief A factor that catches the 'bias' of a random stream of peer ids.
188 * As introduced by Brahms: Factor between the number of unique ids in a
189 * truly random stream and number of unique ids in the gossip stream.
191 double deficiency_factor;
194 * Stores the function to return peers. Which one it is depends on whether
195 * the Sampler is the modified one or not.
197 RPS_get_peers_type get_peers;
200 * Head and tail for the DLL to store the #RPS_SamplerRequestHandle
202 struct RPS_SamplerRequestHandle *req_handle_head;
203 struct RPS_SamplerRequestHandle *req_handle_tail;
206 * Head and tail for the DLL to store the #RPS_SamplerRequestHandleSingleInfo
208 struct RPS_SamplerRequestHandleSingleInfo *req_handle_single_head;
209 struct RPS_SamplerRequestHandleSingleInfo *req_handle_single_tail;
211 struct SamplerNotifyUpdateCTX *notify_ctx_head;
212 struct SamplerNotifyUpdateCTX *notify_ctx_tail;
217 * @brief Update the current estimate of the network size stored at the sampler
219 * Used for computing the condition when to return elements to the client
221 * @param sampler The sampler to update
222 * @param num_peers The estimated value
225 RPS_sampler_update_with_nw_size (struct RPS_Sampler *sampler,
230 * @brief Set the probability that is needed at least with what a sampler
231 * element has to have observed all elements from the network.
233 * Only used/useful with the client sampler
234 * (Maybe move to rps-sampler_client.{h|c} ?)
237 * @param desired_probability
240 RPS_sampler_set_desired_probability (struct RPS_Sampler *sampler,
241 double desired_probability);
245 * @brief Set the deficiency factor.
247 * Only used/useful with the client sampler
248 * (Maybe move to rps-sampler_client.{h|c} ?)
251 * @param desired_probability
254 RPS_sampler_set_deficiency_factor (struct RPS_Sampler *sampler,
255 double deficiency_factor);
259 * @brief Add a callback that will be called when the next peer is inserted
262 * @param sampler The sampler on which update it will be called
263 * @param notify_cb The callback
264 * @param cls Closure given to the callback
266 * @return The context containing callback and closure
268 struct SamplerNotifyUpdateCTX *
269 sampler_notify_on_update (struct RPS_Sampler *sampler,
270 SamplerNotifyUpdateCB notify_cb,
275 * Update every sampler element of this sampler with given peer
277 * @param sampler the sampler to update.
278 * @param id the PeerID that is put in the sampler
281 RPS_sampler_update (struct RPS_Sampler *sampler,
282 const struct GNUNET_PeerIdentity *id);
286 * Reinitialise all previously initialised sampler elements with the given value.
288 * Used to get rid of a PeerID.
290 * @param sampler the sampler to reinitialise a sampler element in.
291 * @param id the id of the sampler elements to update.
294 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
295 const struct GNUNET_PeerIdentity *id);
299 * Get the size of the sampler.
301 * @param sampler the sampler to return the size of.
302 * @return the size of the sampler
305 RPS_sampler_get_size (struct RPS_Sampler *sampler);
309 * Grow or shrink the size of the sampler.
311 * @param sampler the sampler to resize.
312 * @param new_size the new size of the sampler
315 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
319 * Get n random peers out of the sampled peers.
321 * We might want to reinitialise this sampler after giving the
322 * corrsponding peer to the client.
323 * Random with or without consumption?
325 * @param sampler the sampler to get peers from.
326 * @param cb callback that will be called once the ids are ready.
327 * @param cls closure given to @a cb
328 * @param for_client #GNUNET_YES if result is used for client,
329 * #GNUNET_NO if used internally
330 * @param num_peers the number of peers requested
332 struct RPS_SamplerRequestHandle *
333 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
335 RPS_sampler_n_rand_peers_ready_cb cb,
340 * Get one random peer with additional information.
342 * @param sampler the sampler to get peers from.
343 * @param cb callback that will be called once the ids are ready.
344 * @param cls closure given to @a cb
346 struct RPS_SamplerRequestHandleSingleInfo *
347 RPS_sampler_get_rand_peer_info (struct RPS_Sampler *sampler,
348 RPS_sampler_sinlge_info_ready_cb cb,
353 * Counts how many Samplers currently hold a given PeerID.
355 * @param sampler the sampler to count ids in.
356 * @param id the PeerID to count.
358 * @return the number of occurrences of id.
361 RPS_sampler_count_id (struct RPS_Sampler *sampler,
362 const struct GNUNET_PeerIdentity *id);
366 * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
368 * @param req_handle the handle to the request
371 RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
375 * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
377 * @param req_handle the handle to the request
380 RPS_sampler_request_single_info_cancel (
381 struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle);
385 * Cleans the sampler.
388 RPS_sampler_destroy (struct RPS_Sampler *sampler);
390 #endif /* RPS_SAMPLER_COMMON_H */
391 /* end of rps-sampler_common.h */