diff options
Diffstat (limited to 'src/rps/rps_api.c')
-rw-r--r-- | src/rps/rps_api.c | 193 |
1 files changed, 186 insertions, 7 deletions
diff --git a/src/rps/rps_api.c b/src/rps/rps_api.c index 7a3adfa94..83dff27e8 100644 --- a/src/rps/rps_api.c +++ b/src/rps/rps_api.c | |||
@@ -128,6 +128,16 @@ struct GNUNET_RPS_Handle | |||
128 | struct GNUNET_RPS_Request_Handle *rh_tail; | 128 | struct GNUNET_RPS_Request_Handle *rh_tail; |
129 | 129 | ||
130 | /** | 130 | /** |
131 | * @brief Pointer to the head element in DLL of single request handles | ||
132 | */ | ||
133 | struct GNUNET_RPS_Request_Handle_Single_Info *rhs_head; | ||
134 | |||
135 | /** | ||
136 | * @brief Pointer to the tail element in DLL of single request handles | ||
137 | */ | ||
138 | struct GNUNET_RPS_Request_Handle_Single_Info *rhs_tail; | ||
139 | |||
140 | /** | ||
131 | * @brief The desired probability with which we want to have observed all | 141 | * @brief The desired probability with which we want to have observed all |
132 | * peers. | 142 | * peers. |
133 | */ | 143 | */ |
@@ -197,6 +207,54 @@ struct GNUNET_RPS_Request_Handle | |||
197 | 207 | ||
198 | 208 | ||
199 | /** | 209 | /** |
210 | * Handler for a single request from a client. | ||
211 | */ | ||
212 | struct GNUNET_RPS_Request_Handle_Single_Info | ||
213 | { | ||
214 | /** | ||
215 | * The client issuing the request. | ||
216 | */ | ||
217 | struct GNUNET_RPS_Handle *rps_handle; | ||
218 | |||
219 | /** | ||
220 | * @brief The Sampler for the client request | ||
221 | */ | ||
222 | struct RPS_Sampler *sampler; | ||
223 | |||
224 | /** | ||
225 | * @brief Request handle of the request to the sampler - needed to cancel the request | ||
226 | */ | ||
227 | struct RPS_SamplerRequestHandleSingleInfo *sampler_rh; | ||
228 | |||
229 | /** | ||
230 | * @brief Request handle of the request of the biased stream of peers - | ||
231 | * needed to cancel the request | ||
232 | */ | ||
233 | struct GNUNET_RPS_StreamRequestHandle *srh; | ||
234 | |||
235 | /** | ||
236 | * The callback to be called when we receive an answer. | ||
237 | */ | ||
238 | GNUNET_RPS_NotifyReadySingleInfoCB ready_cb; | ||
239 | |||
240 | /** | ||
241 | * The closure for the callback. | ||
242 | */ | ||
243 | void *ready_cb_cls; | ||
244 | |||
245 | /** | ||
246 | * @brief Pointer to next element in DLL | ||
247 | */ | ||
248 | struct GNUNET_RPS_Request_Handle_Single_Info *next; | ||
249 | |||
250 | /** | ||
251 | * @brief Pointer to previous element in DLL | ||
252 | */ | ||
253 | struct GNUNET_RPS_Request_Handle_Single_Info *prev; | ||
254 | }; | ||
255 | |||
256 | |||
257 | /** | ||
200 | * Struct used to pack the callback, its closure (provided by the caller) | 258 | * Struct used to pack the callback, its closure (provided by the caller) |
201 | * and the connection handler to the service to pass it to a callback function. | 259 | * and the connection handler to the service to pass it to a callback function. |
202 | */ | 260 | */ |
@@ -309,6 +367,36 @@ peers_ready_cb (const struct GNUNET_PeerIdentity *peers, | |||
309 | 367 | ||
310 | 368 | ||
311 | /** | 369 | /** |
370 | * @brief Called once the sampler has collected the requested peer. | ||
371 | * | ||
372 | * Calls the callback provided by the client with the corresponding cls. | ||
373 | * | ||
374 | * @param peers The array of @a num_peers that has been returned. | ||
375 | * @param num_peers The number of peers that have been returned | ||
376 | * @param cls The #GNUNET_RPS_Request_Handle | ||
377 | * @param probability Probability with which all IDs have been observed | ||
378 | * @param num_observed Number of observed IDs | ||
379 | */ | ||
380 | static void | ||
381 | peer_info_ready_cb (const struct GNUNET_PeerIdentity *peers, | ||
382 | void *cls, | ||
383 | double probability, | ||
384 | uint32_t num_observed) | ||
385 | { | ||
386 | struct GNUNET_RPS_Request_Handle *rh = cls; | ||
387 | (void) probability; | ||
388 | (void) num_observed; | ||
389 | uint32_t num_peers = 1; | ||
390 | |||
391 | rh->sampler_rh = NULL; | ||
392 | rh->ready_cb (rh->ready_cb_cls, | ||
393 | num_peers, | ||
394 | peers); | ||
395 | GNUNET_RPS_request_cancel (rh); | ||
396 | } | ||
397 | |||
398 | |||
399 | /** | ||
312 | * @brief Callback to collect the peers from the biased stream and put those | 400 | * @brief Callback to collect the peers from the biased stream and put those |
313 | * into the sampler. | 401 | * into the sampler. |
314 | * | 402 | * |
@@ -632,15 +720,15 @@ mq_error_handler (void *cls, | |||
632 | */ | 720 | */ |
633 | static void | 721 | static void |
634 | hash_from_share_val (const char *share_val, | 722 | hash_from_share_val (const char *share_val, |
635 | struct GNUNET_HashCode *hash) | 723 | struct GNUNET_HashCode *hash) |
636 | { | 724 | { |
637 | GNUNET_CRYPTO_kdf (hash, | 725 | GNUNET_CRYPTO_kdf (hash, |
638 | sizeof (struct GNUNET_HashCode), | 726 | sizeof (struct GNUNET_HashCode), |
639 | "rps", | 727 | "rps", |
640 | strlen ("rps"), | 728 | strlen ("rps"), |
641 | share_val, | 729 | share_val, |
642 | strlen (share_val), | 730 | strlen (share_val), |
643 | NULL, 0); | 731 | NULL, 0); |
644 | } | 732 | } |
645 | 733 | ||
646 | 734 | ||
@@ -672,6 +760,13 @@ nse_cb (void *cls, | |||
672 | RPS_sampler_update_with_nw_size (rh_iter->sampler, | 760 | RPS_sampler_update_with_nw_size (rh_iter->sampler, |
673 | GNUNET_NSE_log_estimate_to_n (logestimate)); | 761 | GNUNET_NSE_log_estimate_to_n (logestimate)); |
674 | } | 762 | } |
763 | for (struct GNUNET_RPS_Request_Handle_Single_Info *rhs_iter = h->rhs_head; | ||
764 | NULL != rhs_iter && NULL != rhs_iter->next; | ||
765 | rhs_iter = rhs_iter->next) | ||
766 | { | ||
767 | RPS_sampler_update_with_nw_size (rhs_iter->sampler, | ||
768 | GNUNET_NSE_log_estimate_to_n (logestimate)); | ||
769 | } | ||
675 | } | 770 | } |
676 | 771 | ||
677 | 772 | ||
@@ -857,6 +952,48 @@ GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle, | |||
857 | 952 | ||
858 | 953 | ||
859 | /** | 954 | /** |
955 | * Request one random peer, getting additional information. | ||
956 | * | ||
957 | * @param rps_handle handle to the rps service | ||
958 | * @param ready_cb the callback called when the peers are available | ||
959 | * @param cls closure given to the callback | ||
960 | * @return a handle to cancel this request | ||
961 | */ | ||
962 | struct GNUNET_RPS_Request_Handle_Single_Info * | ||
963 | GNUNET_RPS_request_peer_info (struct GNUNET_RPS_Handle *rps_handle, | ||
964 | GNUNET_RPS_NotifyReadySingleInfoCB ready_cb, | ||
965 | void *cls) | ||
966 | { | ||
967 | struct GNUNET_RPS_Request_Handle_Single_Info *rhs; | ||
968 | uint32_t num_req_peers = 1; | ||
969 | |||
970 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
971 | "Client requested peer with additional info\n"); | ||
972 | rhs = GNUNET_new (struct GNUNET_RPS_Request_Handle_Single_Info); | ||
973 | rhs->rps_handle = rps_handle; | ||
974 | rhs->sampler = RPS_sampler_mod_init (num_req_peers, | ||
975 | GNUNET_TIME_UNIT_SECONDS); // TODO remove this time-stuff | ||
976 | RPS_sampler_set_desired_probability (rhs->sampler, | ||
977 | rps_handle->desired_probability); | ||
978 | RPS_sampler_set_deficiency_factor (rhs->sampler, | ||
979 | rps_handle->deficiency_factor); | ||
980 | rhs->sampler_rh = RPS_sampler_get_rand_peer_info (rhs->sampler, | ||
981 | peer_info_ready_cb, | ||
982 | rhs); | ||
983 | rhs->srh = GNUNET_RPS_stream_request (rps_handle, | ||
984 | collect_peers_cb, | ||
985 | rhs); /* cls */ | ||
986 | rhs->ready_cb = ready_cb; | ||
987 | rhs->ready_cb_cls = cls; | ||
988 | GNUNET_CONTAINER_DLL_insert (rps_handle->rhs_head, | ||
989 | rps_handle->rhs_tail, | ||
990 | rhs); | ||
991 | |||
992 | return rhs; | ||
993 | } | ||
994 | |||
995 | |||
996 | /** | ||
860 | * Seed rps service with peerIDs. | 997 | * Seed rps service with peerIDs. |
861 | * | 998 | * |
862 | * @param h handle to the rps service | 999 | * @param h handle to the rps service |
@@ -1047,6 +1184,37 @@ GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh) | |||
1047 | 1184 | ||
1048 | 1185 | ||
1049 | /** | 1186 | /** |
1187 | * Cancle an issued single info request. | ||
1188 | * | ||
1189 | * @param rhs request handle of request to cancle | ||
1190 | */ | ||
1191 | void | ||
1192 | GNUNET_RPS_request_single_info_cancel ( | ||
1193 | struct GNUNET_RPS_Request_Handle_Single_Info *rhs) | ||
1194 | { | ||
1195 | struct GNUNET_RPS_Handle *h; | ||
1196 | |||
1197 | h = rhs->rps_handle; | ||
1198 | GNUNET_assert (NULL != rhs); | ||
1199 | GNUNET_assert (NULL != rhs->srh); | ||
1200 | GNUNET_assert (h == rhs->srh->rps_handle); | ||
1201 | GNUNET_RPS_stream_cancel (rhs->srh); | ||
1202 | rhs->srh = NULL; | ||
1203 | if (NULL == h->stream_requests_head) cancel_stream(h); | ||
1204 | if (NULL != rhs->sampler_rh) | ||
1205 | { | ||
1206 | RPS_sampler_request_single_info_cancel (rhs->sampler_rh); | ||
1207 | } | ||
1208 | RPS_sampler_destroy (rhs->sampler); | ||
1209 | rhs->sampler = NULL; | ||
1210 | GNUNET_CONTAINER_DLL_remove (h->rhs_head, | ||
1211 | h->rhs_tail, | ||
1212 | rhs); | ||
1213 | GNUNET_free (rhs); | ||
1214 | } | ||
1215 | |||
1216 | |||
1217 | /** | ||
1050 | * Disconnect from the rps service | 1218 | * Disconnect from the rps service |
1051 | * | 1219 | * |
1052 | * @param h the handle to the rps service | 1220 | * @param h the handle to the rps service |
@@ -1079,6 +1247,17 @@ GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h) | |||
1079 | GNUNET_RPS_request_cancel (rh_iter); | 1247 | GNUNET_RPS_request_cancel (rh_iter); |
1080 | } | 1248 | } |
1081 | } | 1249 | } |
1250 | if (NULL != h->rhs_head) | ||
1251 | { | ||
1252 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1253 | "Not all requests were cancelled!\n"); | ||
1254 | for (struct GNUNET_RPS_Request_Handle_Single_Info *rhs_iter = h->rhs_head; | ||
1255 | h->rhs_head != NULL; | ||
1256 | rhs_iter = h->rhs_head) | ||
1257 | { | ||
1258 | GNUNET_RPS_request_single_info_cancel (rhs_iter); | ||
1259 | } | ||
1260 | } | ||
1082 | if (NULL != srh_callback_peers) | 1261 | if (NULL != srh_callback_peers) |
1083 | { | 1262 | { |
1084 | GNUNET_free (srh_callback_peers); | 1263 | GNUNET_free (srh_callback_peers); |