diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-01-15 10:07:33 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-01-15 10:07:33 +0000 |
commit | 2e482e23a8d51f58e6ff80782e360cf9f47e174c (patch) | |
tree | 856066bdd39afffcbb42ec315fed8cfc64541028 /src/peerinfo/peerinfo_api.c | |
parent | 697aa41b1e22503e2eb2d8802bdaf627b3931997 (diff) | |
download | gnunet-2e482e23a8d51f58e6ff80782e360cf9f47e174c.tar.gz gnunet-2e482e23a8d51f58e6ff80782e360cf9f47e174c.zip |
allow cancellation of peerinfo iterations
Diffstat (limited to 'src/peerinfo/peerinfo_api.c')
-rw-r--r-- | src/peerinfo/peerinfo_api.c | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/src/peerinfo/peerinfo_api.c b/src/peerinfo/peerinfo_api.c index 906c01d77..3ed2172db 100644 --- a/src/peerinfo/peerinfo_api.c +++ b/src/peerinfo/peerinfo_api.c | |||
@@ -120,7 +120,7 @@ GNUNET_PEERINFO_add_peer (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
120 | /** | 120 | /** |
121 | * Context for the info handler. | 121 | * Context for the info handler. |
122 | */ | 122 | */ |
123 | struct InfoContext | 123 | struct GNUNET_PEERINFO_IteratorContext |
124 | { | 124 | { |
125 | 125 | ||
126 | /** | 126 | /** |
@@ -156,7 +156,7 @@ struct InfoContext | |||
156 | static void | 156 | static void |
157 | info_handler (void *cls, const struct GNUNET_MessageHeader *msg) | 157 | info_handler (void *cls, const struct GNUNET_MessageHeader *msg) |
158 | { | 158 | { |
159 | struct InfoContext *ic = cls; | 159 | struct GNUNET_PEERINFO_IteratorContext *ic = cls; |
160 | const struct InfoMessage *im; | 160 | const struct InfoMessage *im; |
161 | const struct GNUNET_HELLO_Message *hello; | 161 | const struct GNUNET_HELLO_Message *hello; |
162 | uint16_t ms; | 162 | uint16_t ms; |
@@ -234,9 +234,10 @@ info_handler (void *cls, const struct GNUNET_MessageHeader *msg) | |||
234 | * @param timeout how long to wait until timing out | 234 | * @param timeout how long to wait until timing out |
235 | * @param callback the method to call for each peer | 235 | * @param callback the method to call for each peer |
236 | * @param callback_cls closure for callback | 236 | * @param callback_cls closure for callback |
237 | * @return NULL on error, otherwise an iterator context | ||
237 | */ | 238 | */ |
238 | void | 239 | struct GNUNET_PEERINFO_IteratorContext * |
239 | GNUNET_PEERINFO_for_all (const struct GNUNET_CONFIGURATION_Handle *cfg, | 240 | GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg, |
240 | struct GNUNET_SCHEDULER_Handle *sched, | 241 | struct GNUNET_SCHEDULER_Handle *sched, |
241 | const struct GNUNET_PeerIdentity *peer, | 242 | const struct GNUNET_PeerIdentity *peer, |
242 | int trust_delta, | 243 | int trust_delta, |
@@ -247,28 +248,23 @@ GNUNET_PEERINFO_for_all (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
247 | struct GNUNET_CLIENT_Connection *client; | 248 | struct GNUNET_CLIENT_Connection *client; |
248 | struct ListAllPeersMessage *lapm; | 249 | struct ListAllPeersMessage *lapm; |
249 | struct ListPeerMessage *lpm; | 250 | struct ListPeerMessage *lpm; |
250 | struct InfoContext *ihc; | 251 | struct GNUNET_PEERINFO_IteratorContext *ihc; |
251 | 252 | ||
252 | client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg); | 253 | client = GNUNET_CLIENT_connect (sched, "peerinfo", cfg); |
253 | if (client == NULL) | 254 | if (client == NULL) |
254 | { | 255 | { |
255 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 256 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
256 | _("Could not connect to `%s' service.\n"), "peerinfo"); | 257 | _("Could not connect to `%s' service.\n"), "peerinfo"); |
257 | callback (callback_cls, NULL, NULL, 2); | 258 | return NULL; |
258 | return; | ||
259 | } | 259 | } |
260 | #if DEBUG_PEERINFO | 260 | #if DEBUG_PEERINFO |
261 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 261 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
262 | "Requesting list of peers from peerinfo database\n"); | 262 | "Requesting list of peers from peerinfo database\n"); |
263 | #endif | 263 | #endif |
264 | ihc = GNUNET_malloc (sizeof (struct InfoContext) + | ||
265 | sizeof (struct ListPeerMessage)); | ||
266 | ihc->client = client; | ||
267 | ihc->callback = callback; | ||
268 | ihc->callback_cls = callback_cls; | ||
269 | ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout); | ||
270 | if (peer == NULL) | 264 | if (peer == NULL) |
271 | { | 265 | { |
266 | ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) + | ||
267 | sizeof (struct ListAllPeersMessage)); | ||
272 | lapm = (struct ListAllPeersMessage *) &ihc[1]; | 268 | lapm = (struct ListAllPeersMessage *) &ihc[1]; |
273 | lapm->header.size = htons (sizeof (struct ListAllPeersMessage)); | 269 | lapm->header.size = htons (sizeof (struct ListAllPeersMessage)); |
274 | lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL); | 270 | lapm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL); |
@@ -276,12 +272,18 @@ GNUNET_PEERINFO_for_all (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
276 | } | 272 | } |
277 | else | 273 | else |
278 | { | 274 | { |
275 | ihc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext) + | ||
276 | sizeof (struct ListPeerMessage)); | ||
279 | lpm = (struct ListPeerMessage *) &ihc[1]; | 277 | lpm = (struct ListPeerMessage *) &ihc[1]; |
280 | lpm->header.size = htons (sizeof (struct ListPeerMessage)); | 278 | lpm->header.size = htons (sizeof (struct ListPeerMessage)); |
281 | lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET); | 279 | lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET); |
282 | lpm->trust_change = htonl (trust_delta); | 280 | lpm->trust_change = htonl (trust_delta); |
283 | memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity)); | 281 | memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity)); |
284 | } | 282 | } |
283 | ihc->client = client; | ||
284 | ihc->callback = callback; | ||
285 | ihc->callback_cls = callback_cls; | ||
286 | ihc->timeout = GNUNET_TIME_relative_to_absolute (timeout); | ||
285 | if (GNUNET_OK != | 287 | if (GNUNET_OK != |
286 | GNUNET_CLIENT_transmit_and_get_response (client, | 288 | GNUNET_CLIENT_transmit_and_get_response (client, |
287 | (const struct GNUNET_MessageHeader*) &ihc[1], | 289 | (const struct GNUNET_MessageHeader*) &ihc[1], |
@@ -291,14 +293,26 @@ GNUNET_PEERINFO_for_all (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
291 | ihc)) | 293 | ihc)) |
292 | { | 294 | { |
293 | GNUNET_break (0); | 295 | GNUNET_break (0); |
294 | ihc->callback (ihc->callback_cls, NULL, NULL, 1); | ||
295 | GNUNET_CLIENT_disconnect (ihc->client); | 296 | GNUNET_CLIENT_disconnect (ihc->client); |
296 | GNUNET_free (ihc); | 297 | GNUNET_free (ihc); |
297 | return; | 298 | return NULL; |
298 | } | 299 | } |
300 | return ihc; | ||
299 | } | 301 | } |
300 | 302 | ||
301 | 303 | ||
304 | /** | ||
305 | * Cancel an iteration over peer information. | ||
306 | * | ||
307 | * @param ic context of the iterator to cancel | ||
308 | */ | ||
309 | void | ||
310 | GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic) | ||
311 | { | ||
312 | GNUNET_CLIENT_disconnect (ic->client); | ||
313 | GNUNET_free (ic); | ||
314 | } | ||
315 | |||
302 | 316 | ||
303 | /** | 317 | /** |
304 | * Context for the info handler. | 318 | * Context for the info handler. |