aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-01-15 10:07:33 +0000
committerChristian Grothoff <christian@grothoff.org>2010-01-15 10:07:33 +0000
commit2e482e23a8d51f58e6ff80782e360cf9f47e174c (patch)
tree856066bdd39afffcbb42ec315fed8cfc64541028
parent697aa41b1e22503e2eb2d8802bdaf627b3931997 (diff)
downloadgnunet-2e482e23a8d51f58e6ff80782e360cf9f47e174c.tar.gz
gnunet-2e482e23a8d51f58e6ff80782e360cf9f47e174c.zip
allow cancellation of peerinfo iterations
-rw-r--r--src/include/gnunet_peerinfo_service.h33
-rw-r--r--src/peerinfo/gnunet-peerinfo.c14
-rw-r--r--src/peerinfo/peerinfo_api.c44
3 files changed, 62 insertions, 29 deletions
diff --git a/src/include/gnunet_peerinfo_service.h b/src/include/gnunet_peerinfo_service.h
index 463131e6a..6deed04d9 100644
--- a/src/include/gnunet_peerinfo_service.h
+++ b/src/include/gnunet_peerinfo_service.h
@@ -73,13 +73,21 @@ typedef void
73 73
74 74
75/** 75/**
76 * Handle for cancellation of iteration over peers.
77 */
78struct GNUNET_PEERINFO_IteratorContext;
79
80
81/**
76 * Call a method for each known matching host and change 82 * Call a method for each known matching host and change
77 * its trust value. The method will be invoked once for 83 * its trust value. The method will be invoked once for
78 * each host and then finally once with a NULL pointer. 84 * each host and then finally once with a NULL pointer. After
79 * Note that the last call can be triggered by timeout or 85 * that final invocation, the iterator context must no longer
80 * by simply being done; however, the trust argument will 86 * be used.
81 * be set to zero if we are done, 1 if we timed out and 87 *
82 * 2 for fatal error. 88 * Note that the last call can be triggered by timeout or by simply
89 * being done; however, the trust argument will be set to zero if we
90 * are done, 1 if we timed out and 2 for fatal error.
83 * 91 *
84 * @param cfg configuration to use 92 * @param cfg configuration to use
85 * @param sched scheduler to use 93 * @param sched scheduler to use
@@ -88,9 +96,11 @@ typedef void
88 * @param timeout how long to wait until timing out 96 * @param timeout how long to wait until timing out
89 * @param callback the method to call for each peer 97 * @param callback the method to call for each peer
90 * @param callback_cls closure for callback 98 * @param callback_cls closure for callback
99 * @return NULL on error (in this case, 'callback' is never called!),
100 * otherwise an iterator context
91 */ 101 */
92void 102struct GNUNET_PEERINFO_IteratorContext *
93GNUNET_PEERINFO_for_all (const struct GNUNET_CONFIGURATION_Handle *cfg, 103GNUNET_PEERINFO_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
94 struct GNUNET_SCHEDULER_Handle *sched, 104 struct GNUNET_SCHEDULER_Handle *sched,
95 const struct GNUNET_PeerIdentity *peer, 105 const struct GNUNET_PeerIdentity *peer,
96 int trust_delta, 106 int trust_delta,
@@ -100,6 +110,15 @@ GNUNET_PEERINFO_for_all (const struct GNUNET_CONFIGURATION_Handle *cfg,
100 110
101 111
102/** 112/**
113 * Cancel an iteration over peer information.
114 *
115 * @param ic context of the iterator to cancel
116 */
117void
118GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic);
119
120
121/**
103 * Handle for notifications about changes to the set of known peers. 122 * Handle for notifications about changes to the set of known peers.
104 */ 123 */
105struct GNUNET_PEERINFO_NotifyContext; 124struct GNUNET_PEERINFO_NotifyContext;
diff --git a/src/peerinfo/gnunet-peerinfo.c b/src/peerinfo/gnunet-peerinfo.c
index 931673ed9..8c469dad9 100644
--- a/src/peerinfo/gnunet-peerinfo.c
+++ b/src/peerinfo/gnunet-peerinfo.c
@@ -85,13 +85,13 @@ run (void *cls,
85 85
86 if (get_self != GNUNET_YES) 86 if (get_self != GNUNET_YES)
87 { 87 {
88 GNUNET_PEERINFO_for_all (cfg, 88 (void) GNUNET_PEERINFO_iterate (cfg,
89 sched, 89 sched,
90 NULL, 90 NULL,
91 0, 91 0,
92 GNUNET_TIME_relative_multiply 92 GNUNET_TIME_relative_multiply
93 (GNUNET_TIME_UNIT_SECONDS, 30), 93 (GNUNET_TIME_UNIT_SECONDS, 30),
94 &print_peer_info, NULL); 94 &print_peer_info, NULL);
95 } 95 }
96 else 96 else
97 { 97 {
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 */
123struct InfoContext 123struct GNUNET_PEERINFO_IteratorContext
124{ 124{
125 125
126 /** 126 /**
@@ -156,7 +156,7 @@ struct InfoContext
156static void 156static void
157info_handler (void *cls, const struct GNUNET_MessageHeader *msg) 157info_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 */
238void 239struct GNUNET_PEERINFO_IteratorContext *
239GNUNET_PEERINFO_for_all (const struct GNUNET_CONFIGURATION_Handle *cfg, 240GNUNET_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 */
309void
310GNUNET_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.