aboutsummaryrefslogtreecommitdiff
path: root/src/rps/rps-sampler_client.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rps/rps-sampler_client.c')
-rw-r--r--src/rps/rps-sampler_client.c328
1 files changed, 328 insertions, 0 deletions
diff --git a/src/rps/rps-sampler_client.c b/src/rps/rps-sampler_client.c
new file mode 100644
index 000000000..da832a323
--- /dev/null
+++ b/src/rps/rps-sampler_client.c
@@ -0,0 +1,328 @@
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
19/**
20 * @file rps/gnunet-service-rps_sampler.c
21 * @brief sampler implementation
22 * @author Julius Bünger
23 */
24#include "platform.h"
25#include "gnunet_util_lib.h"
26#include "gnunet_statistics_service.h"
27#include "rps.h"
28
29#include "rps-sampler_common.h"
30#include "gnunet-service-rps_sampler.h"
31#include "gnunet-service-rps_sampler_elem.h"
32
33#include <math.h>
34#include <inttypes.h>
35
36#include "rps-test_util.h"
37
38#define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
39
40
41// multiple 'clients'?
42
43// TODO check for overflows
44
45// TODO align message structs
46
47// hist_size_init, hist_size_max
48
49/***********************************************************************
50 * WARNING: This section needs to be reviewed regarding the use of
51 * functions providing (pseudo)randomness!
52***********************************************************************/
53
54// TODO care about invalid input of the caller (size 0 or less...)
55
56/**
57 * @brief Callback called each time a new peer was put into the sampler
58 *
59 * @param cls A possibly given closure
60 */
61typedef void
62(*SamplerNotifyUpdateCB) (void *cls);
63
64/**
65 * @brief Context for a callback. Contains callback and closure.
66 *
67 * Meant to be an entry in an DLL.
68 */
69struct SamplerNotifyUpdateCTX
70{
71 /**
72 * @brief The Callback to call on updates
73 */
74 SamplerNotifyUpdateCB notify_cb;
75
76 /**
77 * @brief The according closure.
78 */
79 void *cls;
80
81 /**
82 * @brief Next element in DLL.
83 */
84 struct SamplerNotifyUpdateCTX *next;
85
86 /**
87 * @brief Previous element in DLL.
88 */
89 struct SamplerNotifyUpdateCTX *prev;
90};
91
92
93/**
94 * Type of function used to differentiate between modified and not modified
95 * Sampler.
96 */
97typedef void
98(*RPS_get_peers_type) (void *cls);
99
100
101/**
102 * Get one random peer out of the sampled peers.
103 *
104 * We might want to reinitialise this sampler after giving the
105 * corrsponding peer to the client.
106 */
107static void
108sampler_mod_get_rand_peer (void *cls);
109
110
111/**
112 * Closure to _get_n_rand_peers_ready_cb()
113 */
114struct RPS_SamplerRequestHandle
115{
116 /**
117 * DLL
118 */
119 struct RPS_SamplerRequestHandle *next;
120 struct RPS_SamplerRequestHandle *prev;
121
122 /**
123 * Number of peers we are waiting for.
124 */
125 uint32_t num_peers;
126
127 /**
128 * Number of peers we currently have.
129 */
130 uint32_t cur_num_peers;
131
132 /**
133 * Pointer to the array holding the ids.
134 */
135 struct GNUNET_PeerIdentity *ids;
136
137 /**
138 * Head and tail for the DLL to store the tasks for single requests
139 */
140 struct GetPeerCls *gpc_head;
141 struct GetPeerCls *gpc_tail;
142
143 /**
144 * Sampler.
145 */
146 struct RPS_Sampler *sampler;
147
148 /**
149 * Callback to be called when all ids are available.
150 */
151 RPS_sampler_n_rand_peers_ready_cb callback;
152
153 /**
154 * Closure given to the callback
155 */
156 void *cls;
157};
158
159///**
160// * Global sampler variable.
161// */
162//struct RPS_Sampler *sampler;
163
164
165/**
166 * The minimal size for the extended sampler elements.
167 */
168static size_t min_size;
169
170/**
171 * The maximal size the extended sampler elements should grow to.
172 */
173static size_t max_size;
174
175/**
176 * The size the extended sampler elements currently have.
177 */
178//static size_t extra_size;
179
180/**
181 * Inedex to the sampler element that is the next to be returned
182 */
183static uint32_t client_get_index;
184
185
186/**
187 * Initialise a modified tuple of sampler elements.
188 *
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.
192 */
193struct RPS_Sampler *
194RPS_sampler_mod_init (size_t init_size,
195 struct GNUNET_TIME_Relative max_round_interval)
196{
197 struct RPS_Sampler *sampler;
198
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()
202
203 sampler = GNUNET_new (struct RPS_Sampler);
204
205 sampler->max_round_interval = max_round_interval;
206 sampler->get_peers = sampler_mod_get_rand_peer;
207 //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity);
208 //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size);
209 RPS_sampler_resize (sampler, init_size);
210
211 client_get_index = 0;
212
213 //GNUNET_assert (init_size == sampler->sampler_size);
214
215#ifdef TO_FILE
216 sampler->file_name = create_file ("sampler-");
217
218 LOG (GNUNET_ERROR_TYPE_DEBUG,
219 "Initialised modified sampler %s\n",
220 sampler->file_name);
221 to_file (sampler->file_name,
222 "This is a modified sampler");
223#endif /* TO_FILE */
224
225 return sampler;
226}
227
228
229/**
230 * Get one random peer out of the sampled peers.
231 *
232 * This reinitialises the queried sampler element.
233 */
234static void
235sampler_mod_get_rand_peer (void *cls)
236{
237 struct GetPeerCls *gpc = cls;
238 struct RPS_SamplerElement *s_elem;
239 struct GNUNET_TIME_Relative last_request_diff;
240 struct RPS_Sampler *sampler;
241
242 gpc->get_peer_task = NULL;
243 gpc->notify_ctx = NULL;
244 sampler = gpc->req_handle->sampler;
245
246 LOG (GNUNET_ERROR_TYPE_DEBUG, "Single peer was requested\n");
247
248 /* Cycle the #client_get_index one step further */
249 client_get_index = (client_get_index + 1) % sampler->sampler_size;
250
251 s_elem = sampler->sampler_elements[client_get_index];
252 *gpc->id = s_elem->peer_id;
253 GNUNET_assert (NULL != s_elem);
254
255 if (EMPTY == s_elem->is_empty)
256 {
257 LOG (GNUNET_ERROR_TYPE_DEBUG,
258 "Sampler_mod element empty, rescheduling.\n");
259 GNUNET_assert (NULL == gpc->notify_ctx);
260 gpc->notify_ctx =
261 sampler_notify_on_update (sampler,
262 &sampler_mod_get_rand_peer,
263 gpc);
264 return;
265 }
266
267 /* Check whether we may use this sampler to give it back to the client */
268 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != s_elem->last_client_request.abs_value_us)
269 {
270 // TODO remove this condition at least for the client sampler
271 last_request_diff =
272 GNUNET_TIME_absolute_get_difference (s_elem->last_client_request,
273 GNUNET_TIME_absolute_get ());
274 /* We're not going to give it back now if it was
275 * already requested by a client this round */
276 if (last_request_diff.rel_value_us < sampler->max_round_interval.rel_value_us)
277 {
278 LOG (GNUNET_ERROR_TYPE_DEBUG,
279 "Last client request on this sampler was less than max round interval ago -- scheduling for later\n");
280 ///* How many time remains untile the next round has started? */
281 //inv_last_request_diff =
282 // GNUNET_TIME_absolute_get_difference (last_request_diff,
283 // sampler->max_round_interval);
284 // add a little delay
285 /* Schedule it one round later */
286 GNUNET_assert (NULL == gpc->notify_ctx);
287 gpc->notify_ctx =
288 sampler_notify_on_update (sampler,
289 &sampler_mod_get_rand_peer,
290 gpc);
291 return;
292 }
293 }
294 if (2 > s_elem->num_peers)
295 {
296 LOG (GNUNET_ERROR_TYPE_DEBUG,
297 "This s_elem saw less than two peers -- scheduling for later\n");
298 GNUNET_assert (NULL == gpc->notify_ctx);
299 gpc->notify_ctx =
300 sampler_notify_on_update (sampler,
301 &sampler_mod_get_rand_peer,
302 gpc);
303 return;
304 }
305 /* More reasons to wait could be added here */
306
307// GNUNET_STATISTICS_set (stats,
308// "# client sampler element input",
309// s_elem->num_peers,
310// GNUNET_NO);
311// GNUNET_STATISTICS_set (stats,
312// "# client sampler element change",
313// s_elem->num_change,
314// GNUNET_NO);
315
316 RPS_sampler_elem_reinit (s_elem);
317 s_elem->last_client_request = GNUNET_TIME_absolute_get ();
318
319 GNUNET_CONTAINER_DLL_remove (gpc->req_handle->gpc_head,
320 gpc->req_handle->gpc_tail,
321 gpc);
322 gpc->cont (gpc->cont_cls, gpc->id);
323 GNUNET_free (gpc);
324}
325
326
327/* end of gnunet-service-rps.c */
328