diff options
Diffstat (limited to 'src/rps/rps_api.c')
-rw-r--r-- | src/rps/rps_api.c | 59 |
1 files changed, 37 insertions, 22 deletions
diff --git a/src/rps/rps_api.c b/src/rps/rps_api.c index 1993ca8b8..6971fb716 100644 --- a/src/rps/rps_api.c +++ b/src/rps/rps_api.c | |||
@@ -51,6 +51,16 @@ struct GNUNET_RPS_Handle | |||
51 | * The message queue to the client. | 51 | * The message queue to the client. |
52 | */ | 52 | */ |
53 | struct GNUNET_MQ_Handle *mq; | 53 | struct GNUNET_MQ_Handle *mq; |
54 | |||
55 | /** | ||
56 | * Array of Request_Handles. | ||
57 | */ | ||
58 | struct GNUNET_CONTAINER_MultiHashMap32 *req_handlers; | ||
59 | |||
60 | /** | ||
61 | * The id of the last request. | ||
62 | */ | ||
63 | uint32_t current_request_id; | ||
54 | }; | 64 | }; |
55 | 65 | ||
56 | 66 | ||
@@ -65,6 +75,11 @@ struct GNUNET_RPS_Request_Handle | |||
65 | struct GNUNET_RPS_Handle *rps_handle; | 75 | struct GNUNET_RPS_Handle *rps_handle; |
66 | 76 | ||
67 | /** | 77 | /** |
78 | * The id of the request. | ||
79 | */ | ||
80 | uint32_t id; | ||
81 | |||
82 | /** | ||
68 | * The callback to be called when we receive an answer. | 83 | * The callback to be called when we receive an answer. |
69 | */ | 84 | */ |
70 | GNUNET_RPS_NotifyReadyCB ready_cb; | 85 | GNUNET_RPS_NotifyReadyCB ready_cb; |
@@ -73,26 +88,10 @@ struct GNUNET_RPS_Request_Handle | |||
73 | * The closure for the callback. | 88 | * The closure for the callback. |
74 | */ | 89 | */ |
75 | void *ready_cb_cls; | 90 | void *ready_cb_cls; |
76 | |||
77 | /** | ||
78 | * The id of the request. | ||
79 | */ | ||
80 | uint32_t id; | ||
81 | }; | 91 | }; |
82 | 92 | ||
83 | 93 | ||
84 | /** | 94 | /** |
85 | * Array of Request_Handles. | ||
86 | */ | ||
87 | struct GNUNET_RPS_Request_Handle *req_handlers = NULL; | ||
88 | |||
89 | /** | ||
90 | * Current length of req_handlers. | ||
91 | */ | ||
92 | unsigned int req_handlers_size = 0; | ||
93 | |||
94 | |||
95 | /** | ||
96 | * Struct used to pack the callback, its closure (provided by the caller) | 95 | * Struct used to pack the callback, its closure (provided by the caller) |
97 | * and the connection handler to the service to pass it to a callback function. | 96 | * and the connection handler to the service to pass it to a callback function. |
98 | */ | 97 | */ |
@@ -127,20 +126,27 @@ struct cb_cls_pack | |||
127 | handle_reply (void *cls, | 126 | handle_reply (void *cls, |
128 | const struct GNUNET_MessageHeader *message) | 127 | const struct GNUNET_MessageHeader *message) |
129 | { | 128 | { |
129 | struct GNUNET_RPS_Handle *h = (struct GNUNET_RPS_Handle *) cls; | ||
130 | struct GNUNET_RPS_CS_ReplyMessage *msg; | 130 | struct GNUNET_RPS_CS_ReplyMessage *msg; |
131 | struct GNUNET_PeerIdentity *peers; | 131 | struct GNUNET_PeerIdentity *peers; |
132 | struct GNUNET_RPS_Request_Handle *rh; | 132 | struct GNUNET_RPS_Request_Handle *rh; |
133 | uint32_t id; | ||
133 | 134 | ||
134 | /* Give the peers back */ | 135 | /* Give the peers back */ |
135 | msg = (struct GNUNET_RPS_CS_ReplyMessage *) message; | 136 | msg = (struct GNUNET_RPS_CS_ReplyMessage *) message; |
137 | id = ntohl (msg->id); | ||
136 | 138 | ||
137 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 139 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
138 | "Service replied with %" PRIu32 " peers for id %" PRIu32 "\n", | 140 | "Service replied with %" PRIu32 " peers for id %" PRIu32 "\n", |
139 | ntohl (msg->num_peers), | 141 | ntohl (msg->num_peers), |
140 | ntohl (msg->id)); | 142 | id); |
141 | 143 | ||
142 | peers = (struct GNUNET_PeerIdentity *) &msg[1]; | 144 | peers = (struct GNUNET_PeerIdentity *) &msg[1]; |
143 | rh = &req_handlers[ntohl (msg->id)]; | 145 | GNUNET_assert (GNUNET_YES == |
146 | GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers, id)); | ||
147 | rh = GNUNET_CONTAINER_multihashmap32_get (h->req_handlers, id); | ||
148 | GNUNET_assert (NULL != rh); | ||
149 | GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, id); | ||
144 | rh->ready_cb((rh)->ready_cb_cls, ntohl (msg->num_peers), peers); | 150 | rh->ready_cb((rh)->ready_cb_cls, ntohl (msg->num_peers), peers); |
145 | } | 151 | } |
146 | 152 | ||
@@ -211,6 +217,7 @@ GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) | |||
211 | h = GNUNET_new(struct GNUNET_RPS_Handle); | 217 | h = GNUNET_new(struct GNUNET_RPS_Handle); |
212 | h->cfg = GNUNET_CONFIGURATION_dup (cfg); | 218 | h->cfg = GNUNET_CONFIGURATION_dup (cfg); |
213 | reconnect (h); | 219 | reconnect (h); |
220 | h->req_handlers = GNUNET_CONTAINER_multihashmap32_create (4); | ||
214 | return h; | 221 | return h; |
215 | } | 222 | } |
216 | 223 | ||
@@ -237,8 +244,7 @@ GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle, | |||
237 | // assert func != NULL | 244 | // assert func != NULL |
238 | rh = GNUNET_new (struct GNUNET_RPS_Request_Handle); | 245 | rh = GNUNET_new (struct GNUNET_RPS_Request_Handle); |
239 | rh->rps_handle = rps_handle; | 246 | rh->rps_handle = rps_handle; |
240 | GNUNET_assert (req_handlers_size < UINT32_MAX); | 247 | rh->id = rps_handle->current_request_id++; |
241 | rh->id = (uint32_t) req_handlers_size; | ||
242 | rh->ready_cb = ready_cb; | 248 | rh->ready_cb = ready_cb; |
243 | rh->ready_cb_cls = cls; | 249 | rh->ready_cb_cls = cls; |
244 | 250 | ||
@@ -247,8 +253,8 @@ GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle, | |||
247 | num_req_peers, | 253 | num_req_peers, |
248 | rh->id); | 254 | rh->id); |
249 | 255 | ||
250 | GNUNET_array_append (req_handlers, req_handlers_size, *rh); | 256 | GNUNET_CONTAINER_multihashmap32_put (rps_handle->req_handlers, rh->id, rh, |
251 | //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_Handle)); | 257 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); |
252 | 258 | ||
253 | ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST); | 259 | ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST); |
254 | msg->num_peers = htonl (num_req_peers); | 260 | msg->num_peers = htonl (num_req_peers); |
@@ -420,6 +426,7 @@ GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h, | |||
420 | void | 426 | void |
421 | GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh) | 427 | GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh) |
422 | { | 428 | { |
429 | struct GNUNET_RPS_Handle *h; | ||
423 | struct GNUNET_MQ_Envelope *ev; | 430 | struct GNUNET_MQ_Envelope *ev; |
424 | struct GNUNET_RPS_CS_RequestCancelMessage*msg; | 431 | struct GNUNET_RPS_CS_RequestCancelMessage*msg; |
425 | 432 | ||
@@ -427,6 +434,10 @@ GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh) | |||
427 | "Cancelling request with id %" PRIu32 "\n", | 434 | "Cancelling request with id %" PRIu32 "\n", |
428 | rh->id); | 435 | rh->id); |
429 | 436 | ||
437 | h = rh->rps_handle; | ||
438 | GNUNET_assert (GNUNET_CONTAINER_multihashmap32_contains (h->req_handlers, | ||
439 | rh->id)); | ||
440 | GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, rh->id); | ||
430 | ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL); | 441 | ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST_CANCEL); |
431 | msg->id = htonl (rh->id); | 442 | msg->id = htonl (rh->id); |
432 | GNUNET_MQ_send (rh->rps_handle->mq, ev); | 443 | GNUNET_MQ_send (rh->rps_handle->mq, ev); |
@@ -445,6 +456,10 @@ GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h) | |||
445 | GNUNET_CLIENT_disconnect (h->conn); | 456 | GNUNET_CLIENT_disconnect (h->conn); |
446 | GNUNET_CONFIGURATION_destroy (h->cfg); | 457 | GNUNET_CONFIGURATION_destroy (h->cfg); |
447 | GNUNET_MQ_destroy (h->mq); | 458 | GNUNET_MQ_destroy (h->mq); |
459 | if (0 < GNUNET_CONTAINER_multihashmap32_size (h->req_handlers)) | ||
460 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
461 | "Still waiting for requests\n"); | ||
462 | GNUNET_CONTAINER_multihashmap32_destroy (h->req_handlers); | ||
448 | GNUNET_free (h); | 463 | GNUNET_free (h); |
449 | } | 464 | } |
450 | 465 | ||