diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/rps/gnunet-service-rps_sampler.c | 152 |
1 files changed, 89 insertions, 63 deletions
diff --git a/src/rps/gnunet-service-rps_sampler.c b/src/rps/gnunet-service-rps_sampler.c index a0624c942..570f4f66e 100644 --- a/src/rps/gnunet-service-rps_sampler.c +++ b/src/rps/gnunet-service-rps_sampler.c | |||
@@ -112,6 +112,59 @@ struct RPS_SamplerElement | |||
112 | }; | 112 | }; |
113 | 113 | ||
114 | /** | 114 | /** |
115 | * Callback that is called from _get_rand_peer() when the PeerID is ready. | ||
116 | * | ||
117 | * @param cls the closure given alongside this function. | ||
118 | * @param id the PeerID that was returned | ||
119 | */ | ||
120 | typedef void | ||
121 | (*RPS_sampler_rand_peer_ready_cont) (void *cls, | ||
122 | const struct GNUNET_PeerIdentity *id); | ||
123 | |||
124 | |||
125 | /** | ||
126 | * Closure for #sampler_get_rand_peer() | ||
127 | */ | ||
128 | struct GetPeerCls | ||
129 | { | ||
130 | /** | ||
131 | * DLL | ||
132 | */ | ||
133 | struct GetPeerCls *next; | ||
134 | |||
135 | /** | ||
136 | * DLL | ||
137 | */ | ||
138 | struct GetPeerCls *prev; | ||
139 | |||
140 | /** | ||
141 | * The sampler this function operates on. | ||
142 | */ | ||
143 | struct RPS_Sampler *sampler; | ||
144 | |||
145 | /** | ||
146 | * The task for this function. | ||
147 | */ | ||
148 | struct GNUNET_SCHEDULER_Task *get_peer_task; | ||
149 | |||
150 | /** | ||
151 | * The callback | ||
152 | */ | ||
153 | RPS_sampler_rand_peer_ready_cont cont; | ||
154 | |||
155 | /** | ||
156 | * The closure to the callback @e cont | ||
157 | */ | ||
158 | void *cont_cls; | ||
159 | |||
160 | /** | ||
161 | * The address of the id to be stored at | ||
162 | */ | ||
163 | struct GNUNET_PeerIdentity *id; | ||
164 | }; | ||
165 | |||
166 | |||
167 | /** | ||
115 | * Type of function used to differentiate between modified and not modified | 168 | * Type of function used to differentiate between modified and not modified |
116 | * Sampler. | 169 | * Sampler. |
117 | */ | 170 | */ |
@@ -170,6 +223,16 @@ struct RPS_Sampler | |||
170 | */ | 223 | */ |
171 | RPS_get_peers_type get_peers; | 224 | RPS_get_peers_type get_peers; |
172 | 225 | ||
226 | /** | ||
227 | * Head for the DLL to store the closures to pending requests. | ||
228 | */ | ||
229 | struct GetPeerCls *gpc_head; | ||
230 | |||
231 | /** | ||
232 | * Tail for the DLL to store the closures to pending requests. | ||
233 | */ | ||
234 | struct GetPeerCls *gpc_tail; | ||
235 | |||
173 | #ifdef TO_FILE | 236 | #ifdef TO_FILE |
174 | /** | 237 | /** |
175 | * File name to log to | 238 | * File name to log to |
@@ -209,58 +272,6 @@ struct NRandPeersReadyCls | |||
209 | void *cls; | 272 | void *cls; |
210 | }; | 273 | }; |
211 | 274 | ||
212 | /** | ||
213 | * Callback that is called from _get_rand_peer() when the PeerID is ready. | ||
214 | * | ||
215 | * @param cls the closure given alongside this function. | ||
216 | * @param id the PeerID that was returned | ||
217 | */ | ||
218 | typedef void | ||
219 | (*RPS_sampler_rand_peer_ready_cont) (void *cls, | ||
220 | const struct GNUNET_PeerIdentity *id); | ||
221 | |||
222 | /** | ||
223 | * Closure for #sampler_get_rand_peer() | ||
224 | */ | ||
225 | struct GetPeerCls | ||
226 | { | ||
227 | /** | ||
228 | * DLL | ||
229 | */ | ||
230 | struct GetPeerCls *next; | ||
231 | |||
232 | /** | ||
233 | * DLL | ||
234 | */ | ||
235 | struct GetPeerCls *prev; | ||
236 | |||
237 | /** | ||
238 | * The sampler this function operates on. | ||
239 | */ | ||
240 | struct RPS_Sampler *sampler; | ||
241 | |||
242 | /** | ||
243 | * The task for this function. | ||
244 | */ | ||
245 | struct GNUNET_SCHEDULER_Task *get_peer_task; | ||
246 | |||
247 | /** | ||
248 | * The callback | ||
249 | */ | ||
250 | RPS_sampler_rand_peer_ready_cont cont; | ||
251 | |||
252 | /** | ||
253 | * The closure to the callback @e cont | ||
254 | */ | ||
255 | void *cont_cls; | ||
256 | |||
257 | /** | ||
258 | * The address of the id to be stored at | ||
259 | */ | ||
260 | struct GNUNET_PeerIdentity *id; | ||
261 | }; | ||
262 | |||
263 | |||
264 | ///** | 275 | ///** |
265 | // * Global sampler variable. | 276 | // * Global sampler variable. |
266 | // */ | 277 | // */ |
@@ -697,6 +708,8 @@ RPS_sampler_init (size_t init_size, | |||
697 | sampler->sampler_elements = NULL; | 708 | sampler->sampler_elements = NULL; |
698 | sampler->max_round_interval = max_round_interval; | 709 | sampler->max_round_interval = max_round_interval; |
699 | sampler->get_peers = sampler_get_rand_peer; | 710 | sampler->get_peers = sampler_get_rand_peer; |
711 | sampler->gpc_head = NULL; | ||
712 | sampler->gpc_tail = NULL; | ||
700 | //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity); | 713 | //sampler->sampler_elements = GNUNET_new_array(init_size, struct GNUNET_PeerIdentity); |
701 | //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size); | 714 | //GNUNET_array_grow (sampler->sampler_elements, sampler->sampler_size, min_size); |
702 | RPS_sampler_resize (sampler, init_size); | 715 | RPS_sampler_resize (sampler, init_size); |
@@ -802,7 +815,6 @@ sampler_get_rand_peer2 (void *cls, | |||
802 | uint32_t r_index; | 815 | uint32_t r_index; |
803 | 816 | ||
804 | gpc->get_peer_task = NULL; | 817 | gpc->get_peer_task = NULL; |
805 | GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, gpc); | ||
806 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | 818 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) |
807 | return; | 819 | return; |
808 | 820 | ||
@@ -813,19 +825,27 @@ sampler_get_rand_peer2 (void *cls, | |||
813 | r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG, | 825 | r_index = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_STRONG, |
814 | gpc->sampler->sampler_size); | 826 | gpc->sampler->sampler_size); |
815 | 827 | ||
816 | if ( EMPTY == gpc->sampler->sampler_elements[r_index]->is_empty ) | 828 | if (EMPTY == gpc->sampler->sampler_elements[r_index]->is_empty) |
817 | { | 829 | { |
818 | gpc->get_peer_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply( | 830 | //LOG (GNUNET_ERROR_TYPE_DEBUG, |
819 | GNUNET_TIME_UNIT_SECONDS, | 831 | // "Not returning randomly selected, empty PeerID. - Rescheduling.\n"); |
820 | .1), | 832 | |
821 | &sampler_get_rand_peer2, | 833 | gpc->get_peer_task = |
822 | cls); | 834 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply ( |
835 | GNUNET_TIME_UNIT_SECONDS, 0.1), | ||
836 | &sampler_get_rand_peer2, | ||
837 | cls); | ||
823 | return; | 838 | return; |
824 | } | 839 | } |
825 | 840 | ||
826 | *gpc->id = gpc->sampler->sampler_elements[r_index]->peer_id; | 841 | *gpc->id = gpc->sampler->sampler_elements[r_index]->peer_id; |
827 | 842 | ||
828 | gpc->cont (gpc->cont_cls, gpc->id); | 843 | gpc->cont (gpc->cont_cls, gpc->id); |
844 | |||
845 | GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head, | ||
846 | gpc->sampler->gpc_tail, | ||
847 | gpc); | ||
848 | |||
829 | GNUNET_free (gpc); | 849 | GNUNET_free (gpc); |
830 | } | 850 | } |
831 | 851 | ||
@@ -929,7 +949,9 @@ sampler_get_rand_peer (void *cls, | |||
929 | 949 | ||
930 | s_elem->last_client_request = GNUNET_TIME_absolute_get (); | 950 | s_elem->last_client_request = GNUNET_TIME_absolute_get (); |
931 | 951 | ||
932 | GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, gpc); | 952 | GNUNET_CONTAINER_DLL_remove (gpc->sampler->gpc_head, |
953 | gpc->sampler->gpc_tail, | ||
954 | gpc); | ||
933 | gpc->cont (gpc->cont_cls, gpc->id); | 955 | gpc->cont (gpc->cont_cls, gpc->id); |
934 | GNUNET_free (gpc); | 956 | GNUNET_free (gpc); |
935 | } | 957 | } |
@@ -982,7 +1004,9 @@ RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler, | |||
982 | // maybe add a little delay | 1004 | // maybe add a little delay |
983 | gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, gpc); | 1005 | gpc->get_peer_task = GNUNET_SCHEDULER_add_now (sampler->get_peers, gpc); |
984 | 1006 | ||
985 | GNUNET_CONTAINER_DLL_insert (gpc_head, gpc_tail, gpc); | 1007 | GNUNET_CONTAINER_DLL_insert (sampler->gpc_head, |
1008 | sampler->gpc_tail, | ||
1009 | gpc); | ||
986 | } | 1010 | } |
987 | } | 1011 | } |
988 | 1012 | ||
@@ -1021,9 +1045,11 @@ RPS_sampler_destroy (struct RPS_Sampler *sampler) | |||
1021 | { | 1045 | { |
1022 | struct GetPeerCls *i; | 1046 | struct GetPeerCls *i; |
1023 | 1047 | ||
1024 | for (i = gpc_head; NULL != i; i = gpc_head) | 1048 | for (i = sampler->gpc_head; NULL != i; i = sampler->gpc_head) |
1025 | { | 1049 | { |
1026 | GNUNET_CONTAINER_DLL_remove (gpc_head, gpc_tail, i); | 1050 | GNUNET_CONTAINER_DLL_remove (sampler->gpc_head, |
1051 | sampler->gpc_tail, | ||
1052 | i); | ||
1027 | GNUNET_SCHEDULER_cancel (i->get_peer_task); | 1053 | GNUNET_SCHEDULER_cancel (i->get_peer_task); |
1028 | GNUNET_free (i); | 1054 | GNUNET_free (i); |
1029 | } | 1055 | } |