RPS test util: Improve logging
[oweals/gnunet.git] / src / rps / rps-sampler_common.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/rps-sampler_common.h
23  * @brief Code common to client and service sampler
24  * @author Julius Bünger
25  */
26
27 #ifndef RPS_SAMPLER_COMMON_H
28 #define RPS_SAMPLER_COMMON_H
29
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_statistics_service.h"
33
34 #include "gnunet-service-rps_sampler_elem.h"
35
36 #include <math.h>
37 #include <inttypes.h>
38
39 #include "rps-test_util.h"
40
41
42 /**
43  * Callback that is called from _get_rand_peer() when the PeerID is ready.
44  *
45  * @param cls the closure given alongside this function.
46  * @param id the PeerID that was returned
47  */
48 typedef void
49 (*RPS_sampler_rand_peer_ready_cont) (void *cls,
50                                      const struct GNUNET_PeerIdentity *id);
51
52
53 /**
54  * Type of function used to differentiate between modified and not modified
55  * Sampler.
56  */
57 typedef void
58 (*RPS_get_peers_type) (void *cls);
59
60
61 /**
62  * Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
63  *
64  * @param cls the closure given alongside this function.
65  * @param ids the PeerIDs that were returned
66  *        to be freed
67  */
68   typedef void
69 (*RPS_sampler_n_rand_peers_ready_cb) (const struct GNUNET_PeerIdentity *ids,
70                                       uint32_t num_peers,
71                                       void *cls);
72
73
74 /**
75  * @brief Callback called each time a new peer was put into the sampler
76  *
77  * @param cls A possibly given closure
78  */
79 typedef void
80 (*SamplerNotifyUpdateCB) (void *cls);
81
82
83 /**
84  * Closure for #sampler_mod_get_rand_peer() and #sampler_get_rand_peer
85  */
86 struct GetPeerCls
87 {
88   /**
89    * DLL
90    */
91   struct GetPeerCls *next;
92   struct GetPeerCls *prev;
93
94   /**
95    * The #RPS_SamplerRequestHandle this single request belongs to.
96    */
97   struct RPS_SamplerRequestHandle *req_handle;
98
99   /**
100    * The task for this function.
101    */
102   struct GNUNET_SCHEDULER_Task *get_peer_task;
103
104   /**
105    * @brief Context to the given callback.
106    */
107   struct SamplerNotifyUpdateCTX *notify_ctx;
108
109   /**
110    * The callback
111    */
112   RPS_sampler_rand_peer_ready_cont cont;
113
114   /**
115    * The closure to the callback @e cont
116    */
117   void *cont_cls;
118
119   /**
120    * The address of the id to be stored at
121    */
122   struct GNUNET_PeerIdentity *id;
123 };
124
125
126 /**
127  * Sampler with its own array of SamplerElements
128  */
129 struct RPS_Sampler
130 {
131   /**
132    * Number of sampler elements we hold.
133    */
134   unsigned int sampler_size;
135   //size_t size;
136
137   /**
138    * All sampler elements in one array.
139    */
140   struct RPS_SamplerElement **sampler_elements;
141
142   /**
143    * Maximum time a round takes
144    *
145    * Used in the context of RPS
146    */
147   struct GNUNET_TIME_Relative max_round_interval;
148
149   /**
150    * Stores the function to return peers. Which one it is depends on whether
151    * the Sampler is the modified one or not.
152    */
153   RPS_get_peers_type get_peers;
154
155   /**
156    * Head and tail for the DLL to store the #RPS_SamplerRequestHandle
157    */
158   struct RPS_SamplerRequestHandle *req_handle_head;
159   struct RPS_SamplerRequestHandle *req_handle_tail;
160
161   struct SamplerNotifyUpdateCTX *notify_ctx_head;
162   struct SamplerNotifyUpdateCTX *notify_ctx_tail;
163 };
164
165
166 /**
167  * @brief Add a callback that will be called when the next peer is inserted
168  * into the sampler
169  *
170  * @param sampler The sampler on which update it will be called
171  * @param notify_cb The callback
172  * @param cls Closure given to the callback
173  *
174  * @return The context containing callback and closure
175  */
176 struct SamplerNotifyUpdateCTX *
177 sampler_notify_on_update (struct RPS_Sampler *sampler,
178                           SamplerNotifyUpdateCB notify_cb,
179                           void *cls);
180
181
182 /**
183  * Update every sampler element of this sampler with given peer
184  *
185  * @param sampler the sampler to update.
186  * @param id the PeerID that is put in the sampler
187  */
188   void
189 RPS_sampler_update (struct RPS_Sampler *sampler,
190                     const struct GNUNET_PeerIdentity *id);
191
192
193 /**
194  * Reinitialise all previously initialised sampler elements with the given value.
195  *
196  * Used to get rid of a PeerID.
197  *
198  * @param sampler the sampler to reinitialise a sampler element in.
199  * @param id the id of the sampler elements to update.
200  */
201   void
202 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
203                                    const struct GNUNET_PeerIdentity *id);
204
205
206 /**
207  * Get the size of the sampler.
208  *
209  * @param sampler the sampler to return the size of.
210  * @return the size of the sampler
211  */
212 unsigned int
213 RPS_sampler_get_size (struct RPS_Sampler *sampler);
214
215
216 /**
217  * Grow or shrink the size of the sampler.
218  *
219  * @param sampler the sampler to resize.
220  * @param new_size the new size of the sampler
221  */
222 void
223 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
224
225
226 /**
227  * Get n random peers out of the sampled peers.
228  *
229  * We might want to reinitialise this sampler after giving the
230  * corrsponding peer to the client.
231  * Random with or without consumption?
232  *
233  * @param sampler the sampler to get peers from.
234  * @param cb callback that will be called once the ids are ready.
235  * @param cls closure given to @a cb
236  * @param for_client #GNUNET_YES if result is used for client,
237  *                   #GNUNET_NO if used internally
238  * @param num_peers the number of peers requested
239  */
240 struct RPS_SamplerRequestHandle *
241 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
242                               uint32_t num_peers,
243                               RPS_sampler_n_rand_peers_ready_cb cb,
244                               void *cls);
245
246
247 /**
248  * Counts how many Samplers currently hold a given PeerID.
249  *
250  * @param sampler the sampler to count ids in.
251  * @param id the PeerID to count.
252  *
253  * @return the number of occurrences of id.
254  */
255   uint32_t
256 RPS_sampler_count_id (struct RPS_Sampler *sampler,
257                       const struct GNUNET_PeerIdentity *id);
258
259
260 /**
261  * Cancle a request issued through #RPS_sampler_n_rand_peers_ready_cb.
262  *
263  * @param req_handle the handle to the request
264  */
265 void
266 RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle);
267
268
269 /**
270  * Cleans the sampler.
271  */
272   void
273 RPS_sampler_destroy (struct RPS_Sampler *sampler);
274
275 #endif /* RPS_SAMPLER_COMMON_H */
276 /* end of rps-sampler_common.h */