diff options
Diffstat (limited to 'src/rps/gnunet-service-rps_sampler.c')
-rw-r--r-- | src/rps/gnunet-service-rps_sampler.c | 91 |
1 files changed, 15 insertions, 76 deletions
diff --git a/src/rps/gnunet-service-rps_sampler.c b/src/rps/gnunet-service-rps_sampler.c index 89dbb4dac..222a600bd 100644 --- a/src/rps/gnunet-service-rps_sampler.c +++ b/src/rps/gnunet-service-rps_sampler.c | |||
@@ -104,6 +104,7 @@ struct RPS_SamplerElement | |||
104 | uint32_t num_change; | 104 | uint32_t num_change; |
105 | }; | 105 | }; |
106 | 106 | ||
107 | |||
107 | /** | 108 | /** |
108 | * Sampler with its own array of SamplerElements | 109 | * Sampler with its own array of SamplerElements |
109 | */ | 110 | */ |
@@ -126,26 +127,6 @@ struct RPS_Sampler | |||
126 | * Used in the context of RPS | 127 | * Used in the context of RPS |
127 | */ | 128 | */ |
128 | struct GNUNET_TIME_Relative max_round_interval; | 129 | struct GNUNET_TIME_Relative max_round_interval; |
129 | |||
130 | /** | ||
131 | * Callback to be called when a peer gets inserted into a sampler. | ||
132 | */ | ||
133 | RPS_sampler_insert_cb insert_cb; | ||
134 | |||
135 | /** | ||
136 | * Closure to the insert_cb. | ||
137 | */ | ||
138 | void *insert_cls; | ||
139 | |||
140 | /** | ||
141 | * Callback to be called when a peer gets inserted into a sampler. | ||
142 | */ | ||
143 | RPS_sampler_remove_cb remove_cb; | ||
144 | |||
145 | /** | ||
146 | * Closure to the remove_cb. | ||
147 | */ | ||
148 | void *remove_cls; | ||
149 | }; | 130 | }; |
150 | 131 | ||
151 | /** | 132 | /** |
@@ -344,15 +325,13 @@ RPS_sampler_elem_create (void) | |||
344 | static void | 325 | static void |
345 | RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem, | 326 | RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem, |
346 | struct RPS_Sampler *sampler, | 327 | struct RPS_Sampler *sampler, |
347 | const struct GNUNET_PeerIdentity *other, | 328 | const struct GNUNET_PeerIdentity *other) |
348 | RPS_sampler_insert_cb insert_cb, void *insert_cls, | ||
349 | RPS_sampler_remove_cb remove_cb, void *remove_cls) | ||
350 | { | 329 | { |
351 | struct GNUNET_HashCode other_hash; | 330 | struct GNUNET_HashCode other_hash; |
352 | 331 | ||
353 | s_elem->num_peers++; | 332 | s_elem->num_peers++; |
354 | 333 | ||
355 | if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id)) ) | 334 | if (0 == GNUNET_CRYPTO_cmp_peer_identity (other, &(s_elem->peer_id))) |
356 | { | 335 | { |
357 | LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n", | 336 | LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n", |
358 | GNUNET_i2s (other)); | 337 | GNUNET_i2s (other)); |
@@ -366,50 +345,31 @@ RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem, | |||
366 | sizeof(struct GNUNET_PeerIdentity), | 345 | sizeof(struct GNUNET_PeerIdentity), |
367 | &other_hash); | 346 | &other_hash); |
368 | 347 | ||
369 | if ( EMPTY == s_elem->is_empty ) | 348 | if (EMPTY == s_elem->is_empty) |
370 | { | 349 | { |
371 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Got PeerID %s; Simply accepting (was empty previously).\n", | 350 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
372 | GNUNET_i2s(other)); | 351 | "Got PeerID %s; Simply accepting (was empty previously).\n", |
352 | GNUNET_i2s(other)); | ||
373 | s_elem->peer_id = *other; | 353 | s_elem->peer_id = *other; |
374 | s_elem->peer_id_hash = other_hash; | 354 | s_elem->peer_id_hash = other_hash; |
375 | 355 | ||
376 | if (NULL != insert_cb) | ||
377 | insert_cb (insert_cls, sampler, &(s_elem->peer_id)); | ||
378 | |||
379 | s_elem->num_change++; | 356 | s_elem->num_change++; |
380 | } | 357 | } |
381 | else if ( 0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash) ) | 358 | else if (0 > GNUNET_CRYPTO_hash_cmp (&other_hash, &s_elem->peer_id_hash)) |
382 | { | 359 | { |
383 | LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n", | 360 | LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n", |
384 | GNUNET_i2s (other)); | 361 | GNUNET_i2s (other)); |
385 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n", | 362 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Discarding old PeerID %s\n", |
386 | GNUNET_i2s (&s_elem->peer_id)); | 363 | GNUNET_i2s (&s_elem->peer_id)); |
387 | 364 | ||
388 | if ( NULL != remove_cb ) | ||
389 | { | ||
390 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing old PeerID %s with the remove callback.\n", | ||
391 | GNUNET_i2s (&s_elem->peer_id)); | ||
392 | remove_cb (remove_cls, sampler, &s_elem->peer_id); | ||
393 | } | ||
394 | |||
395 | s_elem->peer_id = *other; | ||
396 | s_elem->peer_id_hash = other_hash; | ||
397 | |||
398 | if ( NULL != insert_cb ) | ||
399 | { | ||
400 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Inserting new PeerID %s with the insert callback.\n", | ||
401 | GNUNET_i2s (&s_elem->peer_id)); | ||
402 | insert_cb (insert_cls, sampler, &s_elem->peer_id); | ||
403 | } | ||
404 | |||
405 | s_elem->num_change++; | 365 | s_elem->num_change++; |
406 | } | 366 | } |
407 | else | 367 | else |
408 | { | 368 | { |
409 | LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n", | 369 | LOG (GNUNET_ERROR_TYPE_DEBUG, " Got PeerID %s\n", |
410 | GNUNET_i2s(other)); | 370 | GNUNET_i2s (other)); |
411 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n", | 371 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Keeping old PeerID %s\n", |
412 | GNUNET_i2s(&s_elem->peer_id)); | 372 | GNUNET_i2s (&s_elem->peer_id)); |
413 | } | 373 | } |
414 | } | 374 | } |
415 | s_elem->is_empty = NOT_EMPTY; | 375 | s_elem->is_empty = NOT_EMPTY; |
@@ -440,7 +400,6 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size) | |||
440 | { | 400 | { |
441 | unsigned int old_size; | 401 | unsigned int old_size; |
442 | uint32_t i; | 402 | uint32_t i; |
443 | struct RPS_SamplerElement **rem_list; | ||
444 | 403 | ||
445 | // TODO check min and max size | 404 | // TODO check min and max size |
446 | 405 | ||
@@ -449,10 +408,6 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size) | |||
449 | if (old_size > new_size) | 408 | if (old_size > new_size) |
450 | { /* Shrinking */ | 409 | { /* Shrinking */ |
451 | /* Temporary store those to properly call the removeCB on those later */ | 410 | /* Temporary store those to properly call the removeCB on those later */ |
452 | rem_list = GNUNET_malloc ((old_size - new_size) * sizeof (struct RPS_SamplerElement *)); | ||
453 | memcpy (rem_list, | ||
454 | &sampler->sampler_elements[new_size], | ||
455 | (old_size - new_size) * sizeof (struct RPS_SamplerElement *)); | ||
456 | 411 | ||
457 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Shrinking sampler %d -> %d\n", old_size, new_size); | 412 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Shrinking sampler %d -> %d\n", old_size, new_size); |
458 | GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, new_size); | 413 | GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, new_size); |
@@ -460,14 +415,6 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size) | |||
460 | "sampler->sampler_elements now points to %p\n", | 415 | "sampler->sampler_elements now points to %p\n", |
461 | sampler->sampler_elements); | 416 | sampler->sampler_elements); |
462 | 417 | ||
463 | for (i = 0 ; i < old_size - new_size ; i++) | ||
464 | {/* Remove unneeded rest */ | ||
465 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Removing %" PRIX32 ". sampler\n", i); | ||
466 | if (NULL != sampler->remove_cb) | ||
467 | sampler->remove_cb (sampler->remove_cls, sampler, &rem_list[i]->peer_id); | ||
468 | GNUNET_free (rem_list[i]); | ||
469 | } | ||
470 | GNUNET_free (rem_list); | ||
471 | } | 418 | } |
472 | else if (old_size < new_size) | 419 | else if (old_size < new_size) |
473 | { /* Growing */ | 420 | { /* Growing */ |
@@ -480,8 +427,6 @@ sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size) | |||
480 | for ( i = old_size ; i < new_size ; i++ ) | 427 | for ( i = old_size ; i < new_size ; i++ ) |
481 | { /* Add new sampler elements */ | 428 | { /* Add new sampler elements */ |
482 | sampler->sampler_elements[i] = RPS_sampler_elem_create (); | 429 | sampler->sampler_elements[i] = RPS_sampler_elem_create (); |
483 | if (NULL != sampler->insert_cb) | ||
484 | sampler->insert_cb (sampler->insert_cls, sampler, &sampler->sampler_elements[i]->peer_id); | ||
485 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 430 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
486 | "Added %" PRIX32 ". sampler, now pointing to %p, contains %s\n", | 431 | "Added %" PRIX32 ". sampler, now pointing to %p, contains %s\n", |
487 | i, &sampler->sampler_elements[i], GNUNET_i2s (&sampler->sampler_elements[i]->peer_id)); | 432 | i, &sampler->sampler_elements[i], GNUNET_i2s (&sampler->sampler_elements[i]->peer_id)); |
@@ -539,9 +484,7 @@ sampler_empty (struct RPS_Sampler *sampler) | |||
539 | */ | 484 | */ |
540 | struct RPS_Sampler * | 485 | struct RPS_Sampler * |
541 | RPS_sampler_init (size_t init_size, | 486 | RPS_sampler_init (size_t init_size, |
542 | struct GNUNET_TIME_Relative max_round_interval, | 487 | struct GNUNET_TIME_Relative max_round_interval) |
543 | RPS_sampler_insert_cb ins_cb, void *ins_cls, | ||
544 | RPS_sampler_remove_cb rem_cb, void *rem_cls) | ||
545 | { | 488 | { |
546 | struct RPS_Sampler *sampler; | 489 | struct RPS_Sampler *sampler; |
547 | //uint32_t i; | 490 | //uint32_t i; |
@@ -554,10 +497,6 @@ RPS_sampler_init (size_t init_size, | |||
554 | sampler->sampler_size = 0; | 497 | sampler->sampler_size = 0; |
555 | sampler->sampler_elements = NULL; | 498 | sampler->sampler_elements = NULL; |
556 | sampler->max_round_interval = max_round_interval; | 499 | sampler->max_round_interval = max_round_interval; |
557 | sampler->insert_cb = ins_cb; | ||
558 | sampler->insert_cls = ins_cls; | ||
559 | sampler->remove_cb = rem_cb; | ||
560 | sampler->remove_cls = rem_cls; | ||
561 | //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity); | 500 | //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity); |
562 | //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size); | 501 | //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size); |
563 | RPS_sampler_resize (sampler, init_size); | 502 | RPS_sampler_resize (sampler, init_size); |
@@ -583,9 +522,8 @@ RPS_sampler_update (struct RPS_Sampler *sampler, | |||
583 | 522 | ||
584 | for ( i = 0 ; i < sampler->sampler_size ; i++ ) | 523 | for ( i = 0 ; i < sampler->sampler_size ; i++ ) |
585 | RPS_sampler_elem_next (sampler->sampler_elements[i], | 524 | RPS_sampler_elem_next (sampler->sampler_elements[i], |
586 | sampler, id, | 525 | sampler, |
587 | sampler->insert_cb, sampler->insert_cls, | 526 | id); |
588 | sampler->remove_cb, sampler->remove_cls); | ||
589 | } | 527 | } |
590 | 528 | ||
591 | 529 | ||
@@ -705,7 +643,8 @@ sampler_get_rand_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
705 | tmp_id = gpc->sampler->sampler_elements[client_get_index]->peer_id; | 643 | tmp_id = gpc->sampler->sampler_elements[client_get_index]->peer_id; |
706 | RPS_sampler_elem_reinit (gpc->sampler->sampler_elements[client_get_index]); | 644 | RPS_sampler_elem_reinit (gpc->sampler->sampler_elements[client_get_index]); |
707 | RPS_sampler_elem_next (gpc->sampler->sampler_elements[client_get_index], | 645 | RPS_sampler_elem_next (gpc->sampler->sampler_elements[client_get_index], |
708 | gpc->sampler, &tmp_id, NULL, NULL, NULL, NULL); | 646 | gpc->sampler, |
647 | &tmp_id); | ||
709 | 648 | ||
710 | /* Cycle the #client_get_index one step further */ | 649 | /* Cycle the #client_get_index one step further */ |
711 | if ( client_get_index == gpc->sampler->sampler_size - 1 ) | 650 | if ( client_get_index == gpc->sampler->sampler_size - 1 ) |