diff options
author | Julius Bünger <buenger@mytum.de> | 2015-01-18 05:06:13 +0000 |
---|---|---|
committer | Julius Bünger <buenger@mytum.de> | 2015-01-18 05:06:13 +0000 |
commit | d9e72f536edc429a375957bcbcc9cc9603ce965b (patch) | |
tree | 00f6af64d7fd94f57c24105e5f210bd81be22ed5 /src/rps/gnunet-service-rps_sampler.c | |
parent | e993426d1447a693c96920beb5bb469f78f1fdae (diff) | |
download | gnunet-d9e72f536edc429a375957bcbcc9cc9603ce965b.tar.gz gnunet-d9e72f536edc429a375957bcbcc9cc9603ce965b.zip |
fixed integer sizes and _get_rand_peer()
Diffstat (limited to 'src/rps/gnunet-service-rps_sampler.c')
-rw-r--r-- | src/rps/gnunet-service-rps_sampler.c | 83 |
1 files changed, 46 insertions, 37 deletions
diff --git a/src/rps/gnunet-service-rps_sampler.c b/src/rps/gnunet-service-rps_sampler.c index 0d131e198..ac7e8203a 100644 --- a/src/rps/gnunet-service-rps_sampler.c +++ b/src/rps/gnunet-service-rps_sampler.c | |||
@@ -121,13 +121,6 @@ struct RPS_Sampler | |||
121 | struct RPS_SamplerElement **sampler_elements; | 121 | struct RPS_SamplerElement **sampler_elements; |
122 | 122 | ||
123 | /** | 123 | /** |
124 | * Index to a sampler element. | ||
125 | * | ||
126 | * Gets cycled on every hist_request. | ||
127 | */ | ||
128 | uint64_t sampler_elem_index; | ||
129 | |||
130 | /** | ||
131 | * Max time a round takes | 124 | * Max time a round takes |
132 | * | 125 | * |
133 | * Used in the context of RPS | 126 | * Used in the context of RPS |
@@ -163,12 +156,12 @@ struct RPS_GetNRandPeersReadyCls | |||
163 | /** | 156 | /** |
164 | * Number of peers we are waiting for. | 157 | * Number of peers we are waiting for. |
165 | */ | 158 | */ |
166 | uint64_t num_peers; | 159 | uint32_t num_peers; |
167 | 160 | ||
168 | /** | 161 | /** |
169 | * Number of peers we currently have. | 162 | * Number of peers we currently have. |
170 | */ | 163 | */ |
171 | uint64_t cur_num_peers; | 164 | uint32_t cur_num_peers; |
172 | 165 | ||
173 | /** | 166 | /** |
174 | * Pointer to the array holding the ids. | 167 | * Pointer to the array holding the ids. |
@@ -252,7 +245,7 @@ static size_t max_size; | |||
252 | /** | 245 | /** |
253 | * Inedex to the sampler element that is the next to be returned | 246 | * Inedex to the sampler element that is the next to be returned |
254 | */ | 247 | */ |
255 | static uint64_t client_get_index; | 248 | static uint32_t client_get_index; |
256 | 249 | ||
257 | 250 | ||
258 | /** | 251 | /** |
@@ -270,15 +263,15 @@ RPS_sampler_get_n_rand_peers_ready_cb (void *cls, | |||
270 | n_peers_cls = (struct RPS_GetNRandPeersReadyCls *) cls; | 263 | n_peers_cls = (struct RPS_GetNRandPeersReadyCls *) cls; |
271 | 264 | ||
272 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 265 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
273 | "SAMPLER: Got %" PRIX64 "th of %" PRIX64 " peers\n", | 266 | "SAMPLER: Got %" PRIX32 "th of %" PRIX32 " peers\n", |
274 | n_peers_cls->cur_num_peers, n_peers_cls->num_peers); | 267 | n_peers_cls->cur_num_peers, n_peers_cls->num_peers); |
275 | 268 | ||
276 | if (n_peers_cls->num_peers == n_peers_cls->cur_num_peers) | 269 | if (n_peers_cls->num_peers - 1 == n_peers_cls->cur_num_peers) |
277 | { /* All peers are ready -- return those to the client */ | 270 | { /* All peers are ready -- return those to the client */ |
278 | GNUNET_assert (NULL != n_peers_cls->callback); | 271 | GNUNET_assert (NULL != n_peers_cls->callback); |
279 | 272 | ||
280 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 273 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
281 | "SAMPLER: returning %" PRIX64 " peers to the client\n", | 274 | "SAMPLER: returning %" PRIX32 " peers to the client\n", |
282 | n_peers_cls->num_peers); | 275 | n_peers_cls->num_peers); |
283 | n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers); | 276 | n_peers_cls->callback (n_peers_cls->cls, n_peers_cls->ids, n_peers_cls->num_peers); |
284 | 277 | ||
@@ -423,7 +416,7 @@ RPS_sampler_elem_next (struct RPS_SamplerElement *s_elem, const struct GNUNET_Pe | |||
423 | RPS_sampler_resize (unsigned int new_size) | 416 | RPS_sampler_resize (unsigned int new_size) |
424 | { | 417 | { |
425 | unsigned int old_size; | 418 | unsigned int old_size; |
426 | uint64_t i; | 419 | uint32_t i; |
427 | struct RPS_SamplerElement **rem_list; | 420 | struct RPS_SamplerElement **rem_list; |
428 | 421 | ||
429 | // TODO check min and max size | 422 | // TODO check min and max size |
@@ -446,7 +439,7 @@ RPS_sampler_resize (unsigned int new_size) | |||
446 | 439 | ||
447 | for (i = 0 ; i < old_size - new_size ; i++) | 440 | for (i = 0 ; i < old_size - new_size ; i++) |
448 | {/* Remove unneeded rest */ | 441 | {/* Remove unneeded rest */ |
449 | LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing %" PRIX64 ". sampler\n", i); | 442 | LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Removing %" PRIX32 ". sampler\n", i); |
450 | if (NULL != sampler->remove_cb) | 443 | if (NULL != sampler->remove_cb) |
451 | sampler->remove_cb (sampler->remove_cls, &rem_list[i]->peer_id); | 444 | sampler->remove_cb (sampler->remove_cls, &rem_list[i]->peer_id); |
452 | GNUNET_free (rem_list[i]); | 445 | GNUNET_free (rem_list[i]); |
@@ -467,7 +460,7 @@ RPS_sampler_resize (unsigned int new_size) | |||
467 | if (NULL != sampler->insert_cb) | 460 | if (NULL != sampler->insert_cb) |
468 | sampler->insert_cb (sampler->insert_cls, &sampler->sampler_elements[i]->peer_id); | 461 | sampler->insert_cb (sampler->insert_cls, &sampler->sampler_elements[i]->peer_id); |
469 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 462 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
470 | "SAMPLER: Added %" PRIX64 ". sampler, now pointing to %p, contains %s\n", | 463 | "SAMPLER: Added %" PRIX32 ". sampler, now pointing to %p, contains %s\n", |
471 | i, &sampler->sampler_elements[i], GNUNET_i2s (&sampler->sampler_elements[i]->peer_id)); | 464 | i, &sampler->sampler_elements[i], GNUNET_i2s (&sampler->sampler_elements[i]->peer_id)); |
472 | } | 465 | } |
473 | } | 466 | } |
@@ -502,7 +495,7 @@ RPS_sampler_init (size_t init_size, | |||
502 | RPS_sampler_remove_cb rem_cb, void *rem_cls) | 495 | RPS_sampler_remove_cb rem_cb, void *rem_cls) |
503 | { | 496 | { |
504 | //struct RPS_Sampler *sampler; | 497 | //struct RPS_Sampler *sampler; |
505 | //uint64_t i; | 498 | //uint32_t i; |
506 | 499 | ||
507 | /* Initialise context around extended sampler */ | 500 | /* Initialise context around extended sampler */ |
508 | min_size = 10; // TODO make input to _samplers_init() | 501 | min_size = 10; // TODO make input to _samplers_init() |
@@ -536,7 +529,7 @@ RPS_sampler_init (size_t init_size, | |||
536 | void | 529 | void |
537 | RPS_sampler_update_list (const struct GNUNET_PeerIdentity *id) | 530 | RPS_sampler_update_list (const struct GNUNET_PeerIdentity *id) |
538 | { | 531 | { |
539 | uint64_t i; | 532 | uint32_t i; |
540 | 533 | ||
541 | for ( i = 0 ; i < sampler->sampler_size ; i++ ) | 534 | for ( i = 0 ; i < sampler->sampler_size ; i++ ) |
542 | RPS_sampler_elem_next (sampler->sampler_elements[i], id, | 535 | RPS_sampler_elem_next (sampler->sampler_elements[i], id, |
@@ -555,7 +548,7 @@ RPS_sampler_update_list (const struct GNUNET_PeerIdentity *id) | |||
555 | void | 548 | void |
556 | RPS_sampler_reinitialise_by_value (const struct GNUNET_PeerIdentity *id) | 549 | RPS_sampler_reinitialise_by_value (const struct GNUNET_PeerIdentity *id) |
557 | { | 550 | { |
558 | uint64_t i; | 551 | uint32_t i; |
559 | 552 | ||
560 | for ( i = 0 ; i < sampler->sampler_size ; i++ ) | 553 | for ( i = 0 ; i < sampler->sampler_size ; i++ ) |
561 | { | 554 | { |
@@ -578,7 +571,7 @@ RPS_sampler_reinitialise_by_value (const struct GNUNET_PeerIdentity *id) | |||
578 | const struct GNUNET_PeerIdentity * | 571 | const struct GNUNET_PeerIdentity * |
579 | RPS_sampler_get_rand_peer_ () | 572 | RPS_sampler_get_rand_peer_ () |
580 | { | 573 | { |
581 | uint64_t r_index; | 574 | uint32_t r_index; |
582 | const struct GNUNET_PeerIdentity *peer; // do we have to malloc that? | 575 | const struct GNUNET_PeerIdentity *peer; // do we have to malloc that? |
583 | 576 | ||
584 | // TODO implement extra logic | 577 | // TODO implement extra logic |
@@ -611,7 +604,7 @@ RPS_sampler_get_rand_peer_ () | |||
611 | * Only used internally | 604 | * Only used internally |
612 | */ | 605 | */ |
613 | const struct GNUNET_PeerIdentity * | 606 | const struct GNUNET_PeerIdentity * |
614 | RPS_sampler_get_n_rand_peers_ (uint64_t n) | 607 | RPS_sampler_get_n_rand_peers_ (uint32_t n) |
615 | { | 608 | { |
616 | if ( 0 == sampler->sampler_size ) | 609 | if ( 0 == sampler->sampler_size ) |
617 | { | 610 | { |
@@ -624,7 +617,7 @@ RPS_sampler_get_n_rand_peers_ (uint64_t n) | |||
624 | // TODO check if we have too much (distinct) sampled peers | 617 | // TODO check if we have too much (distinct) sampled peers |
625 | // If we are not ready yet maybe schedule for later | 618 | // If we are not ready yet maybe schedule for later |
626 | struct GNUNET_PeerIdentity *peers; | 619 | struct GNUNET_PeerIdentity *peers; |
627 | uint64_t i; | 620 | uint32_t i; |
628 | 621 | ||
629 | peers = GNUNET_malloc (n * sizeof(struct GNUNET_PeerIdentity)); | 622 | peers = GNUNET_malloc (n * sizeof(struct GNUNET_PeerIdentity)); |
630 | 623 | ||
@@ -654,23 +647,41 @@ RPS_sampler_get_rand_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext | |||
654 | struct RPS_SamplerElement *s_elem; | 647 | struct RPS_SamplerElement *s_elem; |
655 | struct GNUNET_TIME_Relative last_request_diff; | 648 | struct GNUNET_TIME_Relative last_request_diff; |
656 | struct GNUNET_HashCode *hash; | 649 | struct GNUNET_HashCode *hash; |
650 | uint32_t tmp_client_get_index; | ||
657 | //struct GNUNET_TIME_Relative inv_last_request_diff; | 651 | //struct GNUNET_TIME_Relative inv_last_request_diff; |
658 | 652 | ||
659 | LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Single peer was requested\n"); | 653 | LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: Single peer was requested\n"); |
660 | 654 | ||
661 | gpc = (struct GetPeerCls *) cls; | 655 | gpc = (struct GetPeerCls *) cls; |
662 | hash = GNUNET_new (struct GNUNET_HashCode); | 656 | hash = GNUNET_new (struct GNUNET_HashCode); |
657 | if (0 < client_get_index) | ||
658 | tmp_client_get_index = client_get_index - 1; | ||
659 | else | ||
660 | tmp_client_get_index = sampler->sampler_size - 1; | ||
661 | |||
662 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
663 | "SAMPLER: scheduling for later if index reaches %" PRIX32 " (sampler size: %" PRIX32 ".\n", | ||
664 | tmp_client_get_index, sampler->sampler_size); | ||
663 | 665 | ||
664 | do | 666 | do |
665 | { /* Get first non empty sampler */ | 667 | { /* Get first non empty sampler */ |
666 | // TODO schedule for later if all samplers are empty | 668 | if (tmp_client_get_index == client_get_index) |
669 | { | ||
670 | LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: reached tmp_index %" PRIX32 ".\n", client_get_index); | ||
671 | gpc->get_peer_task = GNUNET_SCHEDULER_add_delayed (sampler->max_round_interval, | ||
672 | &RPS_sampler_get_rand_peer, | ||
673 | cls); | ||
674 | return; | ||
675 | } | ||
676 | |||
667 | *gpc->id = sampler->sampler_elements[client_get_index]->peer_id; | 677 | *gpc->id = sampler->sampler_elements[client_get_index]->peer_id; |
668 | 678 | ||
669 | RPS_sampler_elem_reinit (sampler->sampler_elements[client_get_index]); | 679 | RPS_sampler_elem_reinit (sampler->sampler_elements[client_get_index]); |
670 | if ( client_get_index == sampler->sampler_size ) | 680 | if ( client_get_index == sampler->sampler_size - 1 ) |
671 | client_get_index = 0; | 681 | client_get_index = 0; |
672 | else | 682 | else |
673 | client_get_index++; | 683 | client_get_index++; |
684 | LOG (GNUNET_ERROR_TYPE_DEBUG, "SAMPLER: incremented index to %" PRIX32 ".\n", client_get_index); | ||
674 | } while (EMPTY == sampler->sampler_elements[client_get_index]->is_empty); | 685 | } while (EMPTY == sampler->sampler_elements[client_get_index]->is_empty); |
675 | 686 | ||
676 | s_elem = sampler->sampler_elements[client_get_index]; | 687 | s_elem = sampler->sampler_elements[client_get_index]; |
@@ -698,7 +709,7 @@ RPS_sampler_get_rand_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext | |||
698 | // TODO add other reasons to wait here | 709 | // TODO add other reasons to wait here |
699 | } | 710 | } |
700 | 711 | ||
701 | GNUNET_CRYPTO_hash (gpc->get_peer_task, sizeof (struct GNUNET_SCHEDULER_Task *), hash); | 712 | GNUNET_CRYPTO_hash (&gpc->get_peer_task, sizeof (struct GNUNET_SCHEDULER_Task *), hash); |
702 | if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (get_peer_tasks, hash, gpc->get_peer_task)) | 713 | if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (get_peer_tasks, hash, gpc->get_peer_task)) |
703 | LOG (GNUNET_ERROR_TYPE_WARNING, "SAMPLER: Key to remove is not in the hashmap\n"); | 714 | LOG (GNUNET_ERROR_TYPE_WARNING, "SAMPLER: Key to remove is not in the hashmap\n"); |
704 | GNUNET_free (gpc->get_peer_task); | 715 | GNUNET_free (gpc->get_peer_task); |
@@ -722,7 +733,7 @@ RPS_sampler_get_rand_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext | |||
722 | */ | 733 | */ |
723 | void | 734 | void |
724 | RPS_sampler_get_n_rand_peers (RPS_sampler_n_rand_peers_ready_cb cb, | 735 | RPS_sampler_get_n_rand_peers (RPS_sampler_n_rand_peers_ready_cb cb, |
725 | void *cls, uint64_t num_peers) | 736 | void *cls, uint32_t num_peers) |
726 | { | 737 | { |
727 | if ( 0 == sampler->sampler_size ) | 738 | if ( 0 == sampler->sampler_size ) |
728 | { | 739 | { |
@@ -734,35 +745,33 @@ RPS_sampler_get_n_rand_peers (RPS_sampler_n_rand_peers_ready_cb cb, | |||
734 | { | 745 | { |
735 | // TODO check if we have too much (distinct) sampled peers | 746 | // TODO check if we have too much (distinct) sampled peers |
736 | // If we are not ready yet maybe schedule for later | 747 | // If we are not ready yet maybe schedule for later |
737 | struct GNUNET_PeerIdentity *peers; | 748 | uint32_t i; |
738 | uint64_t i; | ||
739 | struct RPS_GetNRandPeersReadyCls *cb_cls; | 749 | struct RPS_GetNRandPeersReadyCls *cb_cls; |
740 | struct GetPeerCls *gpc; | 750 | struct GetPeerCls *gpc; |
741 | struct GNUNET_HashCode *hash; | 751 | struct GNUNET_HashCode *hash; |
742 | 752 | ||
743 | peers = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity); | ||
744 | hash = GNUNET_new (struct GNUNET_HashCode); | 753 | hash = GNUNET_new (struct GNUNET_HashCode); |
745 | 754 | ||
746 | cb_cls = GNUNET_new (struct RPS_GetNRandPeersReadyCls); | 755 | cb_cls = GNUNET_new (struct RPS_GetNRandPeersReadyCls); |
747 | cb_cls->num_peers = num_peers; | 756 | cb_cls->num_peers = num_peers; |
748 | cb_cls->cur_num_peers = 0; | 757 | cb_cls->cur_num_peers = 0; |
749 | cb_cls->ids = peers; | 758 | cb_cls->ids = GNUNET_new_array (num_peers, struct GNUNET_PeerIdentity); |
750 | cb_cls->callback = cb; | 759 | cb_cls->callback = cb; |
751 | cb_cls->cls = cls; | 760 | cb_cls->cls = cls; |
752 | 761 | ||
753 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 762 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
754 | "SAMPLER: Scheduling requests for %" PRIX64 " peers\n", num_peers); | 763 | "SAMPLER: Scheduling requests for %" PRIX32 " peers\n", num_peers); |
755 | 764 | ||
756 | for ( i = 0 ; i < num_peers ; i++ ) | 765 | for ( i = 0 ; i < num_peers ; i++ ) |
757 | { | 766 | { |
758 | gpc = GNUNET_new (struct GetPeerCls); | 767 | gpc = GNUNET_new (struct GetPeerCls); |
759 | gpc->cb = RPS_sampler_get_n_rand_peers_ready_cb; | 768 | gpc->cb = RPS_sampler_get_n_rand_peers_ready_cb; |
760 | gpc->cb_cls = cb_cls; | 769 | gpc->cb_cls = cb_cls; |
761 | gpc->id = &peers[i]; | 770 | gpc->id = &cb_cls->ids[i]; |
762 | 771 | ||
763 | // maybe add a little delay | 772 | // maybe add a little delay |
764 | gpc->get_peer_task = GNUNET_SCHEDULER_add_now (&RPS_sampler_get_rand_peer, gpc); | 773 | gpc->get_peer_task = GNUNET_SCHEDULER_add_now (&RPS_sampler_get_rand_peer, gpc); |
765 | GNUNET_CRYPTO_hash (gpc->get_peer_task, sizeof (struct GNUNET_SCHEDULER_Task *), hash); | 774 | GNUNET_CRYPTO_hash (&gpc->get_peer_task, sizeof (struct GNUNET_SCHEDULER_Task *), hash); |
766 | (void) GNUNET_CONTAINER_multihashmap_put (get_peer_tasks, hash, gpc->get_peer_task, | 775 | (void) GNUNET_CONTAINER_multihashmap_put (get_peer_tasks, hash, gpc->get_peer_task, |
767 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | 776 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); |
768 | //RPS_sampler_get_rand_peer (RPS_sampler_get_n_rand_peers_ready_cb, | 777 | //RPS_sampler_get_rand_peer (RPS_sampler_get_n_rand_peers_ready_cb, |
@@ -779,11 +788,11 @@ RPS_sampler_get_n_rand_peers (RPS_sampler_n_rand_peers_ready_cb cb, | |||
779 | * | 788 | * |
780 | * @return the number of occurrences of id. | 789 | * @return the number of occurrences of id. |
781 | */ | 790 | */ |
782 | uint64_t | 791 | uint32_t |
783 | RPS_sampler_count_id (const struct GNUNET_PeerIdentity *id) | 792 | RPS_sampler_count_id (const struct GNUNET_PeerIdentity *id) |
784 | { | 793 | { |
785 | uint64_t count; | 794 | uint32_t count; |
786 | uint64_t i; | 795 | uint32_t i; |
787 | 796 | ||
788 | count = 0; | 797 | count = 0; |
789 | for ( i = 0 ; i < sampler->sampler_size ; i++ ) | 798 | for ( i = 0 ; i < sampler->sampler_size ; i++ ) |