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/gnunet-service-rps_sampler.c
23 * @brief sampler implementation
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
31 #include "rps-sampler_common.h"
32 #include "gnunet-service-rps_sampler.h"
33 #include "gnunet-service-rps_sampler_elem.h"
38 #include "rps-test_util.h"
40 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
43 // multiple 'clients'?
45 // TODO check for overflows
47 // TODO align message structs
49 // hist_size_init, hist_size_max
51 /***********************************************************************
52 * WARNING: This section needs to be reviewed regarding the use of
53 * functions providing (pseudo)randomness!
54 ***********************************************************************/
56 // TODO care about invalid input of the caller (size 0 or less...)
59 * @brief Callback called each time a new peer was put into the sampler
61 * @param cls A possibly given closure
64 (*SamplerNotifyUpdateCB) (void *cls);
67 * @brief Context for a callback. Contains callback and closure.
69 * Meant to be an entry in an DLL.
71 struct SamplerNotifyUpdateCTX
74 * @brief The Callback to call on updates
76 SamplerNotifyUpdateCB notify_cb;
79 * @brief The according closure.
84 * @brief Next element in DLL.
86 struct SamplerNotifyUpdateCTX *next;
89 * @brief Previous element in DLL.
91 struct SamplerNotifyUpdateCTX *prev;
96 * Type of function used to differentiate between modified and not modified
100 (*RPS_get_peers_type) (void *cls);
103 * Get one random peer out of the sampled peers.
105 * We might want to reinitialise this sampler after giving the
106 * corrsponding peer to the client.
107 * Only used internally
110 sampler_get_rand_peer (void *cls);
114 * Closure to _get_n_rand_peers_ready_cb()
116 struct RPS_SamplerRequestHandle
121 struct RPS_SamplerRequestHandle *next;
122 struct RPS_SamplerRequestHandle *prev;
125 * Number of peers we are waiting for.
130 * Number of peers we currently have.
132 uint32_t cur_num_peers;
135 * Pointer to the array holding the ids.
137 struct GNUNET_PeerIdentity *ids;
140 * Head and tail for the DLL to store the tasks for single requests
142 struct GetPeerCls *gpc_head;
143 struct GetPeerCls *gpc_tail;
148 struct RPS_Sampler *sampler;
151 * Callback to be called when all ids are available.
153 RPS_sampler_n_rand_peers_ready_cb callback;
156 * Closure given to the callback
162 // * Global sampler variable.
164 //struct RPS_Sampler *sampler;
168 * The minimal size for the extended sampler elements.
170 static size_t min_size;
173 * The maximal size the extended sampler elements should grow to.
175 static size_t max_size;
178 * The size the extended sampler elements currently have.
180 //static size_t extra_size;
183 * Inedex to the sampler element that is the next to be returned
185 static uint32_t client_get_index;
189 * Initialise a tuple of sampler elements.
191 * @param init_size the size the sampler is initialised with
192 * @param max_round_interval maximum time a round takes
193 * @return a handle to a sampler that consists of sampler elements.
196 RPS_sampler_init (size_t init_size,
197 struct GNUNET_TIME_Relative max_round_interval)
199 struct RPS_Sampler *sampler;
201 /* Initialise context around extended sampler */
202 min_size = 10; // TODO make input to _samplers_init()
203 max_size = 1000; // TODO make input to _samplers_init()
205 sampler = GNUNET_new (struct RPS_Sampler);
207 sampler->max_round_interval = max_round_interval;
208 sampler->get_peers = sampler_get_rand_peer;
209 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
210 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
211 RPS_sampler_resize (sampler, init_size);
213 client_get_index = 0;
215 //GNUNET_assert (init_size == sampler->sampler_size);
220 * Get one random peer out of the sampled peers.
222 * We might want to reinitialise this sampler after giving the
223 * corrsponding peer to the client.
224 * Only used internally
227 sampler_get_rand_peer (void *cls)
229 struct GetPeerCls *gpc = cls;
231 struct RPS_Sampler *sampler;
233 gpc->get_peer_task = NULL;
234 gpc->notify_ctx = NULL;
235 sampler = gpc->req_handle->sampler;
238 * Choose the r_index of the peer we want to return
239 * at random from the interval of the gossip list
241 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
242 sampler->sampler_size);
244 if (EMPTY == sampler->sampler_elements[r_index]->is_empty)
246 //LOG (GNUNET_ERROR_TYPE_DEBUG,
247 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
250 sampler_notify_on_update (sampler,
251 &sampler_get_rand_peer,
256 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
257 gpc->req_handle->gpc_tail,
259 *gpc->id = sampler->sampler_elements[r_index]->peer_id;
260 gpc->cont (gpc->cont_cls, gpc->id, 0, sampler->sampler_elements[r_index]->num_peers);
266 /* end of gnunet-service-rps.c */