tolerate additional IPv4 address now available for gnunet.org
[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      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file rps/gnunet-service-rps_sampler.h
23  * @brief sampler implementation
24  * @author Julius Bünger
25  */
26
27 #ifndef RPS_SAMPLER_H
28 #define RPS_SAMPLER_H
29 #include <inttypes.h>
30 #include "rps-sampler_common.h"
31
32
33 /**
34  * A sampler sampling a stream of PeerIDs.
35  */
36 struct RPS_Sampler;
37
38 /**
39  * A handle to cancel a request.
40  */
41 struct RPS_SamplerRequestHandle;
42
43
44 /**
45  * Get the size of the sampler.
46  *
47  * @param sampler the sampler to return the size of.
48  * @return the size of the sampler
49  */
50 unsigned int
51 RPS_sampler_get_size (struct RPS_Sampler *sampler);
52
53
54 /**
55  * Grow or shrink the size of the sampler.
56  *
57  * @param sampler the sampler to resize.
58  * @param new_size the new size of the sampler (not 0)
59  */
60 void
61 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
62
63
64 /**
65  * Initialise a tuple of samplers.
66  *
67  * @param init_size the size the sampler is initialised with
68  * @param max_round_interval maximum time a round takes
69  * @return a handle to a sampler that consists of sampler elements.
70  */
71 struct RPS_Sampler *
72 RPS_sampler_init (size_t init_size,
73                   struct GNUNET_TIME_Relative max_round_interval);
74
75
76 /**
77  * Update every sampler element of this sampler with given peer
78  *
79  * @param sampler the sampler to update.
80  * @param id the PeerID that is put in the sampler
81  */
82   void
83 RPS_sampler_update (struct RPS_Sampler *sampler,
84                     const struct GNUNET_PeerIdentity *id);
85
86
87 /**
88  * Reinitialise all previously initialised sampler elements with the given
89  * value.
90  *
91  * Used to get rid of a PeerID.
92  *
93  * FIXME: This should also consider currently pending requests
94  *        (Pending requests already collect peerids. As long as not all
95  *        requested IDs have been collected, they are kept.
96  *        Ideally, the @p id should be removed from all pending requests. This
97  *        seems quite complicated.)
98  *
99  * @param sampler the sampler to reinitialise a sampler in.
100  * @param id the id of the samplers to update.
101  */
102   void
103 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
104                                    const struct GNUNET_PeerIdentity *id);
105
106
107 /**
108  * Get n random peers out of the sampled peers.
109  *
110  * We might want to reinitialise this sampler after giving the
111  * corrsponding peer to the client.
112  * Random with or without consumption?
113  *
114  * @param sampler the sampler to get peers from.
115  * @param cb callback that will be called once the ids are ready.
116  * @param cls closure given to @a cb
117  * @param for_client #GNUNET_YES if result is used for client,
118  *                   #GNUNET_NO if used internally
119  * @param num_peers the number of peers requested
120  */
121 struct RPS_SamplerRequestHandle *
122 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
123                               uint32_t num_peers,
124                               RPS_sampler_n_rand_peers_ready_cb cb,
125                               void *cls);
126
127 /**
128  * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
129  *
130  * @param req_handle the handle to the request
131  */
132 void
133 RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
134
135
136 /**
137  * Counts how many Samplers currently hold a given PeerID.
138  *
139  * @param sampler the sampler to cound ids in.
140  * @param id the PeerID to count.
141  *
142  * @return the number of occurrences of id.
143  */
144   uint32_t
145 RPS_sampler_count_id (struct RPS_Sampler *sampler,
146                       const struct GNUNET_PeerIdentity *id);
147
148
149 /**
150  * Cleans the samplers.
151  *
152  * @param sampler the sampler to destroy.
153  */
154   void
155 RPS_sampler_destroy (struct RPS_Sampler *sampler);
156
157 #endif
158 /* end of gnunet-service-rps.c */