diff options
Diffstat (limited to 'src/rps/rps-sampler_common.c')
-rw-r--r-- | src/rps/rps-sampler_common.c | 493 |
1 files changed, 245 insertions, 248 deletions
diff --git a/src/rps/rps-sampler_common.c b/src/rps/rps-sampler_common.c index adb69e1b5..48fa121b7 100644 --- a/src/rps/rps-sampler_common.c +++ b/src/rps/rps-sampler_common.c | |||
@@ -16,7 +16,7 @@ | |||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file rps/rps-sampler_common.c | 22 | * @file rps/rps-sampler_common.c |
@@ -35,15 +35,14 @@ | |||
35 | 35 | ||
36 | #include "rps-test_util.h" | 36 | #include "rps-test_util.h" |
37 | 37 | ||
38 | #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler_common",__VA_ARGS__) | 38 | #define LOG(kind, ...) GNUNET_log_from(kind, "rps-sampler_common", __VA_ARGS__) |
39 | 39 | ||
40 | /** | 40 | /** |
41 | * @brief Context for a callback. Contains callback and closure. | 41 | * @brief Context for a callback. Contains callback and closure. |
42 | * | 42 | * |
43 | * Meant to be an entry in an DLL. | 43 | * Meant to be an entry in an DLL. |
44 | */ | 44 | */ |
45 | struct SamplerNotifyUpdateCTX | 45 | struct SamplerNotifyUpdateCTX { |
46 | { | ||
47 | /** | 46 | /** |
48 | * @brief The Callback to call on updates | 47 | * @brief The Callback to call on updates |
49 | */ | 48 | */ |
@@ -69,8 +68,7 @@ struct SamplerNotifyUpdateCTX | |||
69 | /** | 68 | /** |
70 | * Closure to _get_n_rand_peers_ready_cb() | 69 | * Closure to _get_n_rand_peers_ready_cb() |
71 | */ | 70 | */ |
72 | struct RPS_SamplerRequestHandle | 71 | struct RPS_SamplerRequestHandle { |
73 | { | ||
74 | /** | 72 | /** |
75 | * DLL | 73 | * DLL |
76 | */ | 74 | */ |
@@ -118,8 +116,7 @@ struct RPS_SamplerRequestHandle | |||
118 | /** | 116 | /** |
119 | * Closure to _get_rand_peer_info() | 117 | * Closure to _get_rand_peer_info() |
120 | */ | 118 | */ |
121 | struct RPS_SamplerRequestHandleSingleInfo | 119 | struct RPS_SamplerRequestHandleSingleInfo { |
122 | { | ||
123 | /** | 120 | /** |
124 | * DLL | 121 | * DLL |
125 | */ | 122 | */ |
@@ -166,8 +163,8 @@ struct RPS_SamplerRequestHandleSingleInfo | |||
166 | * @param num_peers The estimated value | 163 | * @param num_peers The estimated value |
167 | */ | 164 | */ |
168 | void | 165 | void |
169 | RPS_sampler_update_with_nw_size (struct RPS_Sampler *sampler, | 166 | RPS_sampler_update_with_nw_size(struct RPS_Sampler *sampler, |
170 | uint32_t num_peers) | 167 | uint32_t num_peers) |
171 | { | 168 | { |
172 | sampler->num_peers_estim = num_peers; | 169 | sampler->num_peers_estim = num_peers; |
173 | } | 170 | } |
@@ -184,8 +181,8 @@ RPS_sampler_update_with_nw_size (struct RPS_Sampler *sampler, | |||
184 | * @param desired_probability | 181 | * @param desired_probability |
185 | */ | 182 | */ |
186 | void | 183 | void |
187 | RPS_sampler_set_desired_probability (struct RPS_Sampler *sampler, | 184 | RPS_sampler_set_desired_probability(struct RPS_Sampler *sampler, |
188 | double desired_probability) | 185 | double desired_probability) |
189 | { | 186 | { |
190 | sampler->desired_probability = desired_probability; | 187 | sampler->desired_probability = desired_probability; |
191 | } | 188 | } |
@@ -201,8 +198,8 @@ RPS_sampler_set_desired_probability (struct RPS_Sampler *sampler, | |||
201 | * @param desired_probability | 198 | * @param desired_probability |
202 | */ | 199 | */ |
203 | void | 200 | void |
204 | RPS_sampler_set_deficiency_factor (struct RPS_Sampler *sampler, | 201 | RPS_sampler_set_deficiency_factor(struct RPS_Sampler *sampler, |
205 | double deficiency_factor) | 202 | double deficiency_factor) |
206 | { | 203 | { |
207 | sampler->deficiency_factor = deficiency_factor; | 204 | sampler->deficiency_factor = deficiency_factor; |
208 | } | 205 | } |
@@ -219,20 +216,20 @@ RPS_sampler_set_deficiency_factor (struct RPS_Sampler *sampler, | |||
219 | * @return The context containing callback and closure | 216 | * @return The context containing callback and closure |
220 | */ | 217 | */ |
221 | struct SamplerNotifyUpdateCTX * | 218 | struct SamplerNotifyUpdateCTX * |
222 | sampler_notify_on_update (struct RPS_Sampler *sampler, | 219 | sampler_notify_on_update(struct RPS_Sampler *sampler, |
223 | SamplerNotifyUpdateCB notify_cb, | 220 | SamplerNotifyUpdateCB notify_cb, |
224 | void *cls) | 221 | void *cls) |
225 | { | 222 | { |
226 | struct SamplerNotifyUpdateCTX *notify_ctx; | 223 | struct SamplerNotifyUpdateCTX *notify_ctx; |
227 | 224 | ||
228 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 225 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
229 | "Inserting new context for notification\n"); | 226 | "Inserting new context for notification\n"); |
230 | notify_ctx = GNUNET_new (struct SamplerNotifyUpdateCTX); | 227 | notify_ctx = GNUNET_new(struct SamplerNotifyUpdateCTX); |
231 | notify_ctx->notify_cb = notify_cb; | 228 | notify_ctx->notify_cb = notify_cb; |
232 | notify_ctx->cls = cls; | 229 | notify_ctx->cls = cls; |
233 | GNUNET_CONTAINER_DLL_insert (sampler->notify_ctx_head, | 230 | GNUNET_CONTAINER_DLL_insert(sampler->notify_ctx_head, |
234 | sampler->notify_ctx_tail, | 231 | sampler->notify_ctx_tail, |
235 | notify_ctx); | 232 | notify_ctx); |
236 | return notify_ctx; | 233 | return notify_ctx; |
237 | } | 234 | } |
238 | 235 | ||
@@ -244,7 +241,7 @@ sampler_notify_on_update (struct RPS_Sampler *sampler, | |||
244 | * @return the size of the sampler | 241 | * @return the size of the sampler |
245 | */ | 242 | */ |
246 | unsigned int | 243 | unsigned int |
247 | RPS_sampler_get_size (struct RPS_Sampler *sampler) | 244 | RPS_sampler_get_size(struct RPS_Sampler *sampler) |
248 | { | 245 | { |
249 | return sampler->sampler_size; | 246 | return sampler->sampler_size; |
250 | } | 247 | } |
@@ -259,12 +256,12 @@ RPS_sampler_get_size (struct RPS_Sampler *sampler) | |||
259 | * @param sampler The sampler the updates are waiting for | 256 | * @param sampler The sampler the updates are waiting for |
260 | */ | 257 | */ |
261 | static void | 258 | static void |
262 | notify_update (struct RPS_Sampler *sampler) | 259 | notify_update(struct RPS_Sampler *sampler) |
263 | { | 260 | { |
264 | struct SamplerNotifyUpdateCTX *tmp_notify_head; | 261 | struct SamplerNotifyUpdateCTX *tmp_notify_head; |
265 | struct SamplerNotifyUpdateCTX *tmp_notify_tail; | 262 | struct SamplerNotifyUpdateCTX *tmp_notify_tail; |
266 | 263 | ||
267 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 264 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
268 | "Calling callbacks waiting for update notification.\n"); | 265 | "Calling callbacks waiting for update notification.\n"); |
269 | tmp_notify_head = sampler->notify_ctx_head; | 266 | tmp_notify_head = sampler->notify_ctx_head; |
270 | tmp_notify_tail = sampler->notify_ctx_tail; | 267 | tmp_notify_tail = sampler->notify_ctx_tail; |
@@ -273,14 +270,14 @@ notify_update (struct RPS_Sampler *sampler) | |||
273 | for (struct SamplerNotifyUpdateCTX *notify_iter = tmp_notify_head; | 270 | for (struct SamplerNotifyUpdateCTX *notify_iter = tmp_notify_head; |
274 | NULL != tmp_notify_head; | 271 | NULL != tmp_notify_head; |
275 | notify_iter = tmp_notify_head) | 272 | notify_iter = tmp_notify_head) |
276 | { | 273 | { |
277 | GNUNET_assert (NULL != notify_iter->notify_cb); | 274 | GNUNET_assert(NULL != notify_iter->notify_cb); |
278 | GNUNET_CONTAINER_DLL_remove (tmp_notify_head, | 275 | GNUNET_CONTAINER_DLL_remove(tmp_notify_head, |
279 | tmp_notify_tail, | 276 | tmp_notify_tail, |
280 | notify_iter); | 277 | notify_iter); |
281 | notify_iter->notify_cb (notify_iter->cls); | 278 | notify_iter->notify_cb(notify_iter->cls); |
282 | GNUNET_free (notify_iter); | 279 | GNUNET_free(notify_iter); |
283 | } | 280 | } |
284 | } | 281 | } |
285 | 282 | ||
286 | 283 | ||
@@ -290,16 +287,16 @@ notify_update (struct RPS_Sampler *sampler) | |||
290 | * @param sampler the sampler to update. | 287 | * @param sampler the sampler to update. |
291 | * @param id the PeerID that is put in the sampler | 288 | * @param id the PeerID that is put in the sampler |
292 | */ | 289 | */ |
293 | void | 290 | void |
294 | RPS_sampler_update (struct RPS_Sampler *sampler, | 291 | RPS_sampler_update(struct RPS_Sampler *sampler, |
295 | const struct GNUNET_PeerIdentity *id) | 292 | const struct GNUNET_PeerIdentity *id) |
296 | { | 293 | { |
297 | for (uint32_t i = 0; i < sampler->sampler_size; i++) | 294 | for (uint32_t i = 0; i < sampler->sampler_size; i++) |
298 | { | 295 | { |
299 | RPS_sampler_elem_next (sampler->sampler_elements[i], | 296 | RPS_sampler_elem_next(sampler->sampler_elements[i], |
300 | id); | 297 | id); |
301 | } | 298 | } |
302 | notify_update (sampler); | 299 | notify_update(sampler); |
303 | } | 300 | } |
304 | 301 | ||
305 | 302 | ||
@@ -317,21 +314,21 @@ RPS_sampler_update (struct RPS_Sampler *sampler, | |||
317 | * @param sampler the sampler to reinitialise a sampler element in. | 314 | * @param sampler the sampler to reinitialise a sampler element in. |
318 | * @param id the id of the sampler elements to update. | 315 | * @param id the id of the sampler elements to update. |
319 | */ | 316 | */ |
320 | void | 317 | void |
321 | RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler, | 318 | RPS_sampler_reinitialise_by_value(struct RPS_Sampler *sampler, |
322 | const struct GNUNET_PeerIdentity *id) | 319 | const struct GNUNET_PeerIdentity *id) |
323 | { | 320 | { |
324 | uint32_t i; | 321 | uint32_t i; |
325 | 322 | ||
326 | for (i = 0; i < sampler->sampler_size; i++) | 323 | for (i = 0; i < sampler->sampler_size; i++) |
327 | { | ||
328 | if (0 == GNUNET_memcmp(id, | ||
329 | &(sampler->sampler_elements[i]->peer_id)) ) | ||
330 | { | 324 | { |
331 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n"); | 325 | if (0 == GNUNET_memcmp(id, |
332 | RPS_sampler_elem_reinit (sampler->sampler_elements[i]); | 326 | &(sampler->sampler_elements[i]->peer_id))) |
327 | { | ||
328 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n"); | ||
329 | RPS_sampler_elem_reinit(sampler->sampler_elements[i]); | ||
330 | } | ||
333 | } | 331 | } |
334 | } | ||
335 | } | 332 | } |
336 | 333 | ||
337 | 334 | ||
@@ -343,20 +340,20 @@ RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler, | |||
343 | * | 340 | * |
344 | * @return the number of occurrences of id. | 341 | * @return the number of occurrences of id. |
345 | */ | 342 | */ |
346 | uint32_t | 343 | uint32_t |
347 | RPS_sampler_count_id (struct RPS_Sampler *sampler, | 344 | RPS_sampler_count_id(struct RPS_Sampler *sampler, |
348 | const struct GNUNET_PeerIdentity *id) | 345 | const struct GNUNET_PeerIdentity *id) |
349 | { | 346 | { |
350 | uint32_t count; | 347 | uint32_t count; |
351 | uint32_t i; | 348 | uint32_t i; |
352 | 349 | ||
353 | count = 0; | 350 | count = 0; |
354 | for ( i = 0 ; i < sampler->sampler_size ; i++ ) | 351 | for (i = 0; i < sampler->sampler_size; i++) |
355 | { | 352 | { |
356 | if ( 0 == GNUNET_memcmp (&sampler->sampler_elements[i]->peer_id, id) | 353 | if (0 == GNUNET_memcmp(&sampler->sampler_elements[i]->peer_id, id) |
357 | && EMPTY != sampler->sampler_elements[i]->is_empty) | 354 | && EMPTY != sampler->sampler_elements[i]->is_empty) |
358 | count++; | 355 | count++; |
359 | } | 356 | } |
360 | return count; | 357 | return count; |
361 | } | 358 | } |
362 | 359 | ||
@@ -368,7 +365,7 @@ RPS_sampler_count_id (struct RPS_Sampler *sampler, | |||
368 | * @param new_size the new size of the sampler | 365 | * @param new_size the new size of the sampler |
369 | */ | 366 | */ |
370 | static void | 367 | static void |
371 | sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size) | 368 | sampler_resize(struct RPS_Sampler *sampler, unsigned int new_size) |
372 | { | 369 | { |
373 | unsigned int old_size; | 370 | unsigned int old_size; |
374 | uint32_t i; | 371 | uint32_t i; |
@@ -378,49 +375,47 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size) | |||
378 | old_size = sampler->sampler_size; | 375 | old_size = sampler->sampler_size; |
379 | 376 | ||
380 | if (old_size > new_size) | 377 | if (old_size > new_size) |
381 | { /* Shrinking */ | 378 | { /* Shrinking */ |
382 | 379 | LOG(GNUNET_ERROR_TYPE_DEBUG, | |
383 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 380 | "Shrinking sampler %d -> %d\n", |
384 | "Shrinking sampler %d -> %d\n", | 381 | old_size, |
385 | old_size, | 382 | new_size); |
386 | new_size); | 383 | |
387 | 384 | for (i = new_size; i < old_size; i++) | |
388 | for (i = new_size ; i < old_size ; i++) | 385 | { |
389 | { | 386 | RPS_sampler_elem_destroy(sampler->sampler_elements[i]); |
390 | RPS_sampler_elem_destroy (sampler->sampler_elements[i]); | 387 | } |
388 | |||
389 | GNUNET_array_grow(sampler->sampler_elements, | ||
390 | sampler->sampler_size, | ||
391 | new_size); | ||
392 | LOG(GNUNET_ERROR_TYPE_DEBUG, | ||
393 | "sampler->sampler_elements now points to %p\n", | ||
394 | sampler->sampler_elements); | ||
391 | } | 395 | } |
392 | |||
393 | GNUNET_array_grow (sampler->sampler_elements, | ||
394 | sampler->sampler_size, | ||
395 | new_size); | ||
396 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
397 | "sampler->sampler_elements now points to %p\n", | ||
398 | sampler->sampler_elements); | ||
399 | |||
400 | } | ||
401 | else if (old_size < new_size) | 396 | else if (old_size < new_size) |
402 | { /* Growing */ | 397 | { /* Growing */ |
403 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 398 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
404 | "Growing sampler %d -> %d\n", | 399 | "Growing sampler %d -> %d\n", |
405 | old_size, | 400 | old_size, |
406 | new_size); | 401 | new_size); |
407 | 402 | ||
408 | GNUNET_array_grow (sampler->sampler_elements, | 403 | GNUNET_array_grow(sampler->sampler_elements, |
409 | sampler->sampler_size, | 404 | sampler->sampler_size, |
410 | new_size); | 405 | new_size); |
411 | 406 | ||
412 | for (i = old_size ; i < new_size ; i++) | 407 | for (i = old_size; i < new_size; i++) |
413 | { /* Add new sampler elements */ | 408 | { /* Add new sampler elements */ |
414 | sampler->sampler_elements[i] = RPS_sampler_elem_create (); | 409 | sampler->sampler_elements[i] = RPS_sampler_elem_create(); |
410 | } | ||
415 | } | 411 | } |
416 | } | ||
417 | else | 412 | else |
418 | { | 413 | { |
419 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n"); | 414 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Size remains the same -- nothing to do\n"); |
420 | return; | 415 | return; |
421 | } | 416 | } |
422 | 417 | ||
423 | GNUNET_assert (sampler->sampler_size == new_size); | 418 | GNUNET_assert(sampler->sampler_size == new_size); |
424 | } | 419 | } |
425 | 420 | ||
426 | 421 | ||
@@ -431,10 +426,10 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size) | |||
431 | * @param new_size the new size of the sampler | 426 | * @param new_size the new size of the sampler |
432 | */ | 427 | */ |
433 | void | 428 | void |
434 | RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size) | 429 | RPS_sampler_resize(struct RPS_Sampler *sampler, unsigned int new_size) |
435 | { | 430 | { |
436 | GNUNET_assert (0 < new_size); | 431 | GNUNET_assert(0 < new_size); |
437 | sampler_resize (sampler, new_size); | 432 | sampler_resize(sampler, new_size); |
438 | } | 433 | } |
439 | 434 | ||
440 | 435 | ||
@@ -445,9 +440,9 @@ RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size) | |||
445 | * @param new_size the new size of the sampler | 440 | * @param new_size the new size of the sampler |
446 | */ | 441 | */ |
447 | static void | 442 | static void |
448 | sampler_empty (struct RPS_Sampler *sampler) | 443 | sampler_empty(struct RPS_Sampler *sampler) |
449 | { | 444 | { |
450 | sampler_resize (sampler, 0); | 445 | sampler_resize(sampler, 0); |
451 | } | 446 | } |
452 | 447 | ||
453 | 448 | ||
@@ -464,47 +459,48 @@ sampler_empty (struct RPS_Sampler *sampler) | |||
464 | * @param num_observed How many ids this sampler has observed | 459 | * @param num_observed How many ids this sampler has observed |
465 | */ | 460 | */ |
466 | static void | 461 | static void |
467 | check_n_peers_ready (void *cls, | 462 | check_n_peers_ready(void *cls, |
468 | const struct GNUNET_PeerIdentity *id, | 463 | const struct GNUNET_PeerIdentity *id, |
469 | double probability, | 464 | double probability, |
470 | uint32_t num_observed) | 465 | uint32_t num_observed) |
471 | { | 466 | { |
472 | struct RPS_SamplerRequestHandle *req_handle = cls; | 467 | struct RPS_SamplerRequestHandle *req_handle = cls; |
473 | (void) id; | 468 | |
469 | (void)id; | ||
474 | RPS_sampler_n_rand_peers_ready_cb tmp_cb; | 470 | RPS_sampler_n_rand_peers_ready_cb tmp_cb; |
475 | struct GNUNET_PeerIdentity *peers; | 471 | struct GNUNET_PeerIdentity *peers; |
476 | uint32_t num_peers; | 472 | uint32_t num_peers; |
477 | void *cb_cls; | 473 | void *cb_cls; |
478 | (void) probability; | 474 | (void)probability; |
479 | (void) num_observed; | 475 | (void)num_observed; |
480 | 476 | ||
481 | req_handle->cur_num_peers++; | 477 | req_handle->cur_num_peers++; |
482 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 478 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
483 | "Got %" PRIX32 ". of %" PRIX32 " peers\n", | 479 | "Got %" PRIX32 ". of %" PRIX32 " peers\n", |
484 | req_handle->cur_num_peers, req_handle->num_peers); | 480 | req_handle->cur_num_peers, req_handle->num_peers); |
485 | 481 | ||
486 | if (req_handle->num_peers == req_handle->cur_num_peers) | 482 | if (req_handle->num_peers == req_handle->cur_num_peers) |
487 | { /* All peers are ready -- return those to the client */ | 483 | { /* All peers are ready -- return those to the client */ |
488 | GNUNET_assert (NULL != req_handle->callback); | 484 | GNUNET_assert(NULL != req_handle->callback); |
489 | 485 | ||
490 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 486 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
491 | "returning %" PRIX32 " peers to the client\n", | 487 | "returning %" PRIX32 " peers to the client\n", |
492 | req_handle->num_peers); | 488 | req_handle->num_peers); |
493 | 489 | ||
494 | /* Copy pointers and peers temporarily as they | 490 | /* Copy pointers and peers temporarily as they |
495 | * might be deleted from within the callback */ | 491 | * might be deleted from within the callback */ |
496 | tmp_cb = req_handle->callback; | 492 | tmp_cb = req_handle->callback; |
497 | num_peers = req_handle->num_peers; | 493 | num_peers = req_handle->num_peers; |
498 | peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity); | 494 | peers = GNUNET_new_array(num_peers, struct GNUNET_PeerIdentity); |
499 | GNUNET_memcpy (peers, | 495 | GNUNET_memcpy(peers, |
500 | req_handle->ids, | 496 | req_handle->ids, |
501 | num_peers * sizeof (struct GNUNET_PeerIdentity)); | 497 | num_peers * sizeof(struct GNUNET_PeerIdentity)); |
502 | cb_cls = req_handle->cls; | 498 | cb_cls = req_handle->cls; |
503 | RPS_sampler_request_cancel (req_handle); | 499 | RPS_sampler_request_cancel(req_handle); |
504 | req_handle = NULL; | 500 | req_handle = NULL; |
505 | tmp_cb (peers, num_peers, cb_cls); | 501 | tmp_cb(peers, num_peers, cb_cls); |
506 | GNUNET_free (peers); | 502 | GNUNET_free(peers); |
507 | } | 503 | } |
508 | } | 504 | } |
509 | 505 | ||
510 | 506 | ||
@@ -519,39 +515,40 @@ check_n_peers_ready (void *cls, | |||
519 | * @param num_observed How many ids this sampler has observed | 515 | * @param num_observed How many ids this sampler has observed |
520 | */ | 516 | */ |
521 | static void | 517 | static void |
522 | check_peer_info_ready (void *cls, | 518 | check_peer_info_ready(void *cls, |
523 | const struct GNUNET_PeerIdentity *id, | 519 | const struct GNUNET_PeerIdentity *id, |
524 | double probability, | 520 | double probability, |
525 | uint32_t num_observed) | 521 | uint32_t num_observed) |
526 | { | 522 | { |
527 | struct RPS_SamplerRequestHandleSingleInfo *req_handle = cls; | 523 | struct RPS_SamplerRequestHandleSingleInfo *req_handle = cls; |
528 | (void) id; | 524 | |
525 | (void)id; | ||
529 | RPS_sampler_sinlge_info_ready_cb tmp_cb; | 526 | RPS_sampler_sinlge_info_ready_cb tmp_cb; |
530 | struct GNUNET_PeerIdentity *peer; | 527 | struct GNUNET_PeerIdentity *peer; |
531 | void *cb_cls; | 528 | void *cb_cls; |
532 | (void) probability; | 529 | (void)probability; |
533 | (void) num_observed; | 530 | (void)num_observed; |
534 | 531 | ||
535 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 532 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
536 | "Got single peer with additional info\n"); | 533 | "Got single peer with additional info\n"); |
537 | 534 | ||
538 | GNUNET_assert (NULL != req_handle->callback); | 535 | GNUNET_assert(NULL != req_handle->callback); |
539 | 536 | ||
540 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 537 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
541 | "returning single peer with info to the client\n"); | 538 | "returning single peer with info to the client\n"); |
542 | 539 | ||
543 | /* Copy pointers and peers temporarily as they | 540 | /* Copy pointers and peers temporarily as they |
544 | * might be deleted from within the callback */ | 541 | * might be deleted from within the callback */ |
545 | tmp_cb = req_handle->callback; | 542 | tmp_cb = req_handle->callback; |
546 | peer = GNUNET_new (struct GNUNET_PeerIdentity); | 543 | peer = GNUNET_new(struct GNUNET_PeerIdentity); |
547 | GNUNET_memcpy (peer, | 544 | GNUNET_memcpy(peer, |
548 | req_handle->id, | 545 | req_handle->id, |
549 | sizeof (struct GNUNET_PeerIdentity)); | 546 | sizeof(struct GNUNET_PeerIdentity)); |
550 | cb_cls = req_handle->cls; | 547 | cb_cls = req_handle->cls; |
551 | RPS_sampler_request_single_info_cancel (req_handle); | 548 | RPS_sampler_request_single_info_cancel(req_handle); |
552 | req_handle = NULL; | 549 | req_handle = NULL; |
553 | tmp_cb (peer, cb_cls, probability, num_observed); | 550 | tmp_cb(peer, cb_cls, probability, num_observed); |
554 | GNUNET_free (peer); | 551 | GNUNET_free(peer); |
555 | } | 552 | } |
556 | 553 | ||
557 | 554 | ||
@@ -568,50 +565,50 @@ check_peer_info_ready (void *cls, | |||
568 | * @param num_peers the number of peers requested | 565 | * @param num_peers the number of peers requested |
569 | */ | 566 | */ |
570 | struct RPS_SamplerRequestHandle * | 567 | struct RPS_SamplerRequestHandle * |
571 | RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler, | 568 | RPS_sampler_get_n_rand_peers(struct RPS_Sampler *sampler, |
572 | uint32_t num_peers, | 569 | uint32_t num_peers, |
573 | RPS_sampler_n_rand_peers_ready_cb cb, | 570 | RPS_sampler_n_rand_peers_ready_cb cb, |
574 | void *cls) | 571 | void *cls) |
575 | { | 572 | { |
576 | uint32_t i; | 573 | uint32_t i; |
577 | struct RPS_SamplerRequestHandle *req_handle; | 574 | struct RPS_SamplerRequestHandle *req_handle; |
578 | struct GetPeerCls *gpc; | 575 | struct GetPeerCls *gpc; |
579 | 576 | ||
580 | GNUNET_assert (0 != sampler->sampler_size); | 577 | GNUNET_assert(0 != sampler->sampler_size); |
581 | if (0 == num_peers) | 578 | if (0 == num_peers) |
582 | return NULL; | 579 | return NULL; |
583 | 580 | ||
584 | // TODO check if we have too much (distinct) sampled peers | 581 | // TODO check if we have too much (distinct) sampled peers |
585 | req_handle = GNUNET_new (struct RPS_SamplerRequestHandle); | 582 | req_handle = GNUNET_new(struct RPS_SamplerRequestHandle); |
586 | req_handle->num_peers = num_peers; | 583 | req_handle->num_peers = num_peers; |
587 | req_handle->cur_num_peers = 0; | 584 | req_handle->cur_num_peers = 0; |
588 | req_handle->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity); | 585 | req_handle->ids = GNUNET_new_array(num_peers, struct GNUNET_PeerIdentity); |
589 | req_handle->sampler = sampler; | 586 | req_handle->sampler = sampler; |
590 | req_handle->callback = cb; | 587 | req_handle->callback = cb; |
591 | req_handle->cls = cls; | 588 | req_handle->cls = cls; |
592 | GNUNET_CONTAINER_DLL_insert (sampler->req_handle_head, | 589 | GNUNET_CONTAINER_DLL_insert(sampler->req_handle_head, |
593 | sampler->req_handle_tail, | 590 | sampler->req_handle_tail, |
594 | req_handle); | 591 | req_handle); |
595 | 592 | ||
596 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 593 | LOG(GNUNET_ERROR_TYPE_DEBUG, |
597 | "Scheduling requests for %" PRIu32 " peers\n", num_peers); | 594 | "Scheduling requests for %" PRIu32 " peers\n", num_peers); |
598 | 595 | ||
599 | for (i = 0; i < num_peers; i++) | 596 | for (i = 0; i < num_peers; i++) |
600 | { | 597 | { |
601 | gpc = GNUNET_new (struct GetPeerCls); | 598 | gpc = GNUNET_new(struct GetPeerCls); |
602 | gpc->req_handle = req_handle; | 599 | gpc->req_handle = req_handle; |
603 | gpc->req_single_info_handle = NULL; | 600 | gpc->req_single_info_handle = NULL; |
604 | gpc->cont = check_n_peers_ready; | 601 | gpc->cont = check_n_peers_ready; |
605 | gpc->cont_cls = req_handle; | 602 | gpc->cont_cls = req_handle; |
606 | gpc->id = &req_handle->ids[i]; | 603 | gpc->id = &req_handle->ids[i]; |
607 | 604 | ||
608 | GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head, | 605 | GNUNET_CONTAINER_DLL_insert(req_handle->gpc_head, |
609 | req_handle->gpc_tail, | 606 | req_handle->gpc_tail, |
610 | gpc); | 607 | gpc); |
611 | // maybe add a little delay | 608 | // maybe add a little delay |
612 | gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, | 609 | gpc->get_peer_task = GNUNET_SCHEDULER_add_now(sampler->get_peers, |
613 | gpc); | 610 | gpc); |
614 | } | 611 | } |
615 | return req_handle; | 612 | return req_handle; |
616 | } | 613 | } |
617 | 614 | ||
@@ -624,38 +621,38 @@ RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler, | |||
624 | * @param cls closure given to @a cb | 621 | * @param cls closure given to @a cb |
625 | */ | 622 | */ |
626 | struct RPS_SamplerRequestHandleSingleInfo * | 623 | struct RPS_SamplerRequestHandleSingleInfo * |
627 | RPS_sampler_get_rand_peer_info (struct RPS_Sampler *sampler, | 624 | RPS_sampler_get_rand_peer_info(struct RPS_Sampler *sampler, |
628 | RPS_sampler_sinlge_info_ready_cb cb, | 625 | RPS_sampler_sinlge_info_ready_cb cb, |
629 | void *cls) | 626 | void *cls) |
630 | { | 627 | { |
631 | struct RPS_SamplerRequestHandleSingleInfo *req_handle; | 628 | struct RPS_SamplerRequestHandleSingleInfo *req_handle; |
632 | struct GetPeerCls *gpc; | 629 | struct GetPeerCls *gpc; |
633 | 630 | ||
634 | GNUNET_assert (0 != sampler->sampler_size); | 631 | GNUNET_assert(0 != sampler->sampler_size); |
635 | 632 | ||
636 | // TODO check if we have too much (distinct) sampled peers | 633 | // TODO check if we have too much (distinct) sampled peers |
637 | req_handle = GNUNET_new (struct RPS_SamplerRequestHandleSingleInfo); | 634 | req_handle = GNUNET_new(struct RPS_SamplerRequestHandleSingleInfo); |
638 | req_handle->id = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity)); | 635 | req_handle->id = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity)); |
639 | req_handle->sampler = sampler; | 636 | req_handle->sampler = sampler; |
640 | req_handle->callback = cb; | 637 | req_handle->callback = cb; |
641 | req_handle->cls = cls; | 638 | req_handle->cls = cls; |
642 | GNUNET_CONTAINER_DLL_insert (sampler->req_handle_single_head, | 639 | GNUNET_CONTAINER_DLL_insert(sampler->req_handle_single_head, |
643 | sampler->req_handle_single_tail, | 640 | sampler->req_handle_single_tail, |
644 | req_handle); | 641 | req_handle); |
645 | 642 | ||
646 | gpc = GNUNET_new (struct GetPeerCls); | 643 | gpc = GNUNET_new(struct GetPeerCls); |
647 | gpc->req_handle = NULL; | 644 | gpc->req_handle = NULL; |
648 | gpc->req_single_info_handle = req_handle; | 645 | gpc->req_single_info_handle = req_handle; |
649 | gpc->cont = check_peer_info_ready; | 646 | gpc->cont = check_peer_info_ready; |
650 | gpc->cont_cls = req_handle; | 647 | gpc->cont_cls = req_handle; |
651 | gpc->id = req_handle->id; | 648 | gpc->id = req_handle->id; |
652 | 649 | ||
653 | GNUNET_CONTAINER_DLL_insert (req_handle->gpc_head, | 650 | GNUNET_CONTAINER_DLL_insert(req_handle->gpc_head, |
654 | req_handle->gpc_tail, | 651 | req_handle->gpc_tail, |
655 | gpc); | 652 | gpc); |
656 | // maybe add a little delay | 653 | // maybe add a little delay |
657 | gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, | 654 | gpc->get_peer_task = GNUNET_SCHEDULER_add_now(sampler->get_peers, |
658 | gpc); | 655 | gpc); |
659 | return req_handle; | 656 | return req_handle; |
660 | } | 657 | } |
661 | 658 | ||
@@ -666,35 +663,35 @@ RPS_sampler_get_rand_peer_info (struct RPS_Sampler *sampler, | |||
666 | * @param req_handle the handle to the request | 663 | * @param req_handle the handle to the request |
667 | */ | 664 | */ |
668 | void | 665 | void |
669 | RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle) | 666 | RPS_sampler_request_cancel(struct RPS_SamplerRequestHandle *req_handle) |
670 | { | 667 | { |
671 | struct GetPeerCls *i; | 668 | struct GetPeerCls *i; |
672 | 669 | ||
673 | while (NULL != (i = req_handle->gpc_head) ) | 670 | while (NULL != (i = req_handle->gpc_head)) |
674 | { | ||
675 | GNUNET_CONTAINER_DLL_remove (req_handle->gpc_head, | ||
676 | req_handle->gpc_tail, | ||
677 | i); | ||
678 | if (NULL != i->get_peer_task) | ||
679 | { | ||
680 | GNUNET_SCHEDULER_cancel (i->get_peer_task); | ||
681 | } | ||
682 | if (NULL != i->notify_ctx) | ||
683 | { | 671 | { |
684 | GNUNET_CONTAINER_DLL_remove (req_handle->sampler->notify_ctx_head, | 672 | GNUNET_CONTAINER_DLL_remove(req_handle->gpc_head, |
685 | req_handle->sampler->notify_ctx_tail, | 673 | req_handle->gpc_tail, |
686 | i->notify_ctx); | 674 | i); |
687 | GNUNET_free (i->notify_ctx); | 675 | if (NULL != i->get_peer_task) |
688 | i->notify_ctx = NULL; | 676 | { |
677 | GNUNET_SCHEDULER_cancel(i->get_peer_task); | ||
678 | } | ||
679 | if (NULL != i->notify_ctx) | ||
680 | { | ||
681 | GNUNET_CONTAINER_DLL_remove(req_handle->sampler->notify_ctx_head, | ||
682 | req_handle->sampler->notify_ctx_tail, | ||
683 | i->notify_ctx); | ||
684 | GNUNET_free(i->notify_ctx); | ||
685 | i->notify_ctx = NULL; | ||
686 | } | ||
687 | GNUNET_free(i); | ||
689 | } | 688 | } |
690 | GNUNET_free (i); | 689 | GNUNET_free(req_handle->ids); |
691 | } | ||
692 | GNUNET_free (req_handle->ids); | ||
693 | req_handle->ids = NULL; | 690 | req_handle->ids = NULL; |
694 | GNUNET_CONTAINER_DLL_remove (req_handle->sampler->req_handle_head, | 691 | GNUNET_CONTAINER_DLL_remove(req_handle->sampler->req_handle_head, |
695 | req_handle->sampler->req_handle_tail, | 692 | req_handle->sampler->req_handle_tail, |
696 | req_handle); | 693 | req_handle); |
697 | GNUNET_free (req_handle); | 694 | GNUNET_free(req_handle); |
698 | } | 695 | } |
699 | 696 | ||
700 | 697 | ||
@@ -704,56 +701,56 @@ RPS_sampler_request_cancel (struct RPS_SamplerRequestHandle *req_handle) | |||
704 | * @param req_handle the handle to the request | 701 | * @param req_handle the handle to the request |
705 | */ | 702 | */ |
706 | void | 703 | void |
707 | RPS_sampler_request_single_info_cancel ( | 704 | RPS_sampler_request_single_info_cancel( |
708 | struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle) | 705 | struct RPS_SamplerRequestHandleSingleInfo *req_single_info_handle) |
709 | { | 706 | { |
710 | struct GetPeerCls *i; | 707 | struct GetPeerCls *i; |
711 | 708 | ||
712 | while (NULL != (i = req_single_info_handle->gpc_head) ) | 709 | while (NULL != (i = req_single_info_handle->gpc_head)) |
713 | { | ||
714 | GNUNET_CONTAINER_DLL_remove (req_single_info_handle->gpc_head, | ||
715 | req_single_info_handle->gpc_tail, | ||
716 | i); | ||
717 | if (NULL != i->get_peer_task) | ||
718 | { | 710 | { |
719 | GNUNET_SCHEDULER_cancel (i->get_peer_task); | 711 | GNUNET_CONTAINER_DLL_remove(req_single_info_handle->gpc_head, |
712 | req_single_info_handle->gpc_tail, | ||
713 | i); | ||
714 | if (NULL != i->get_peer_task) | ||
715 | { | ||
716 | GNUNET_SCHEDULER_cancel(i->get_peer_task); | ||
717 | } | ||
718 | if (NULL != i->notify_ctx) | ||
719 | { | ||
720 | GNUNET_CONTAINER_DLL_remove(req_single_info_handle->sampler->notify_ctx_head, | ||
721 | req_single_info_handle->sampler->notify_ctx_tail, | ||
722 | i->notify_ctx); | ||
723 | GNUNET_free(i->notify_ctx); | ||
724 | i->notify_ctx = NULL; | ||
725 | } | ||
726 | GNUNET_free(i); | ||
720 | } | 727 | } |
721 | if (NULL != i->notify_ctx) | 728 | GNUNET_free(req_single_info_handle->id); |
722 | { | ||
723 | GNUNET_CONTAINER_DLL_remove (req_single_info_handle->sampler->notify_ctx_head, | ||
724 | req_single_info_handle->sampler->notify_ctx_tail, | ||
725 | i->notify_ctx); | ||
726 | GNUNET_free (i->notify_ctx); | ||
727 | i->notify_ctx = NULL; | ||
728 | } | ||
729 | GNUNET_free (i); | ||
730 | } | ||
731 | GNUNET_free (req_single_info_handle->id); | ||
732 | req_single_info_handle->id = NULL; | 729 | req_single_info_handle->id = NULL; |
733 | GNUNET_CONTAINER_DLL_remove (req_single_info_handle->sampler->req_handle_single_head, | 730 | GNUNET_CONTAINER_DLL_remove(req_single_info_handle->sampler->req_handle_single_head, |
734 | req_single_info_handle->sampler->req_handle_single_tail, | 731 | req_single_info_handle->sampler->req_handle_single_tail, |
735 | req_single_info_handle); | 732 | req_single_info_handle); |
736 | GNUNET_free (req_single_info_handle); | 733 | GNUNET_free(req_single_info_handle); |
737 | } | 734 | } |
738 | 735 | ||
739 | 736 | ||
740 | /** | 737 | /** |
741 | * Cleans the sampler. | 738 | * Cleans the sampler. |
742 | */ | 739 | */ |
743 | void | 740 | void |
744 | RPS_sampler_destroy (struct RPS_Sampler *sampler) | 741 | RPS_sampler_destroy(struct RPS_Sampler *sampler) |
745 | { | 742 | { |
746 | if (NULL != sampler->req_handle_head) | 743 | if (NULL != sampler->req_handle_head) |
747 | { | ||
748 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
749 | "There are still pending requests. Going to remove them.\n"); | ||
750 | while (NULL != sampler->req_handle_head) | ||
751 | { | 744 | { |
752 | RPS_sampler_request_cancel (sampler->req_handle_head); | 745 | LOG(GNUNET_ERROR_TYPE_WARNING, |
746 | "There are still pending requests. Going to remove them.\n"); | ||
747 | while (NULL != sampler->req_handle_head) | ||
748 | { | ||
749 | RPS_sampler_request_cancel(sampler->req_handle_head); | ||
750 | } | ||
753 | } | 751 | } |
754 | } | 752 | sampler_empty(sampler); |
755 | sampler_empty (sampler); | 753 | GNUNET_free(sampler); |
756 | GNUNET_free (sampler); | ||
757 | } | 754 | } |
758 | 755 | ||
759 | 756 | ||