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