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/>.
20 * @file rps/gnunet-service-rps_sampler.c
21 * @brief sampler implementation
22 * @author Julius Bünger
25 #include "gnunet_util_lib.h"
26 #include "gnunet_statistics_service.h"
29 #include "rps-sampler_common.h"
30 #include "gnunet-service-rps_sampler.h"
31 #include "gnunet-service-rps_sampler_elem.h"
36 #include "rps-test_util.h"
38 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
41 // multiple 'clients'?
43 // TODO check for overflows
45 // TODO align message structs
47 // hist_size_init, hist_size_max
49 /***********************************************************************
50 * WARNING: This section needs to be reviewed regarding the use of
51 * functions providing (pseudo)randomness!
52 ***********************************************************************/
54 // TODO care about invalid input of the caller (size 0 or less...)
57 * @brief Callback called each time a new peer was put into the sampler
59 * @param cls A possibly given closure
62 (*SamplerNotifyUpdateCB) (void *cls);
65 * @brief Context for a callback. Contains callback and closure.
67 * Meant to be an entry in an DLL.
69 struct SamplerNotifyUpdateCTX
72 * @brief The Callback to call on updates
74 SamplerNotifyUpdateCB notify_cb;
77 * @brief The according closure.
82 * @brief Next element in DLL.
84 struct SamplerNotifyUpdateCTX *next;
87 * @brief Previous element in DLL.
89 struct SamplerNotifyUpdateCTX *prev;
94 * Type of function used to differentiate between modified and not modified
98 (*RPS_get_peers_type) (void *cls);
101 * Get one random peer out of the sampled peers.
103 * We might want to reinitialise this sampler after giving the
104 * corrsponding peer to the client.
105 * Only used internally
108 sampler_get_rand_peer (void *cls);
112 * Closure to _get_n_rand_peers_ready_cb()
114 struct RPS_SamplerRequestHandle
119 struct RPS_SamplerRequestHandle *next;
120 struct RPS_SamplerRequestHandle *prev;
123 * Number of peers we are waiting for.
128 * Number of peers we currently have.
130 uint32_t cur_num_peers;
133 * Pointer to the array holding the ids.
135 struct GNUNET_PeerIdentity *ids;
138 * Head and tail for the DLL to store the tasks for single requests
140 struct GetPeerCls *gpc_head;
141 struct GetPeerCls *gpc_tail;
146 struct RPS_Sampler *sampler;
149 * Callback to be called when all ids are available.
151 RPS_sampler_n_rand_peers_ready_cb callback;
154 * Closure given to the callback
160 // * Global sampler variable.
162 //struct RPS_Sampler *sampler;
166 * The minimal size for the extended sampler elements.
168 static size_t min_size;
171 * The maximal size the extended sampler elements should grow to.
173 static size_t max_size;
176 * The size the extended sampler elements currently have.
178 //static size_t extra_size;
181 * Inedex to the sampler element that is the next to be returned
183 static uint32_t client_get_index;
187 * Initialise a tuple of sampler elements.
189 * @param init_size the size the sampler is initialised with
190 * @param max_round_interval maximum time a round takes
191 * @return a handle to a sampler that consists of sampler elements.
194 RPS_sampler_init (size_t init_size,
195 struct GNUNET_TIME_Relative max_round_interval)
197 struct RPS_Sampler *sampler;
199 /* Initialise context around extended sampler */
200 min_size = 10; // TODO make input to _samplers_init()
201 max_size = 1000; // TODO make input to _samplers_init()
203 sampler = GNUNET_new (struct RPS_Sampler);
206 sampler->file_name = create_file ("sampler-");
208 LOG (GNUNET_ERROR_TYPE_DEBUG,
209 "Initialised sampler %s\n",
213 sampler->max_round_interval = max_round_interval;
214 sampler->get_peers = sampler_get_rand_peer;
215 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
216 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
217 RPS_sampler_resize (sampler, init_size);
219 client_get_index = 0;
221 //GNUNET_assert (init_size == sampler->sampler_size);
226 * Get one random peer out of the sampled peers.
228 * We might want to reinitialise this sampler after giving the
229 * corrsponding peer to the client.
230 * Only used internally
233 sampler_get_rand_peer (void *cls)
235 struct GetPeerCls *gpc = cls;
237 struct RPS_Sampler *sampler;
239 gpc->get_peer_task = NULL;
240 gpc->notify_ctx = NULL;
241 sampler = gpc->req_handle->sampler;
244 * Choose the r_index of the peer we want to return
245 * at random from the interval of the gossip list
247 r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG,
248 sampler->sampler_size);
250 if (EMPTY == sampler->sampler_elements[r_index]->is_empty)
252 //LOG (GNUNET_ERROR_TYPE_DEBUG,
253 // "Not returning randomly selected, empty PeerID. - Rescheduling.\n");
256 sampler_notify_on_update (sampler,
257 &sampler_get_rand_peer,
262 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
263 gpc->req_handle->gpc_tail,
265 *gpc->id = sampler->sampler_elements[r_index]->peer_id;
266 gpc->cont (gpc->cont_cls, gpc->id);
272 /* end of gnunet-service-rps.c */