new timeout tests for WLAN and bluetooth
[oweals/gnunet.git] / src / set / set_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file set/set_api.c
23  * @brief api for the set service
24  * @author Florian Dold
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_set_service.h"
31 #include "set.h"
32
33
34 #define LOG(kind,...) GNUNET_log_from (kind, "set-api",__VA_ARGS__)
35
36 /**
37  * Opaque handle to a set.
38  */
39 struct GNUNET_SET_Handle
40 {
41   /**
42    * Client connected to the set service.
43    */
44   struct GNUNET_CLIENT_Connection *client;
45
46   /**
47    * Message queue for 'client'.
48    */
49   struct GNUNET_MQ_Handle *mq;
50
51   /**
52    * Linked list of operations on the set.
53    */
54   struct GNUNET_SET_OperationHandle *ops_head;
55
56   /**
57    * Linked list of operations on the set.
58    */
59   struct GNUNET_SET_OperationHandle *ops_tail;
60
61   /**
62    * Should the set be destroyed once all operations are gone?
63    */
64   int destroy_requested;
65
66   /**
67    * Has the set become invalid (e.g. service died)?
68    */
69   int invalid;
70
71   /**
72    * Callback for the current iteration over the set,
73    * NULL if no iterator is active.
74    */
75   GNUNET_SET_ElementIterator iterator;
76
77   /**
78    * Closure for 'iterator'
79    */
80   void *iterator_cls;
81 };
82
83
84 /**
85  * Opaque handle to a set operation request from another peer.
86  */
87 struct GNUNET_SET_Request
88 {
89   /**
90    * Id of the request, used to identify the request when
91    * accepting/rejecting it.
92    */
93   uint32_t accept_id;
94
95   /**
96    * Has the request been accepted already?
97    * GNUNET_YES/GNUNET_NO
98    */
99   int accepted;
100 };
101
102
103 /**
104  * Handle to an operation.
105  * Only known to the service after commiting
106  * the handle with a set.
107  */
108 struct GNUNET_SET_OperationHandle
109 {
110   /**
111    * Function to be called when we have a result,
112    * or an error.
113    */
114   GNUNET_SET_ResultIterator result_cb;
115
116   /**
117    * Closure for result_cb.
118    */
119   void *result_cls;
120
121   /**
122    * Local set used for the operation,
123    * NULL if no set has been provided by conclude yet.
124    */
125   struct GNUNET_SET_Handle *set;
126
127   /**
128    * Request ID to identify the operation within the set.
129    */
130   uint32_t request_id;
131
132   /**
133    * Message sent to the server on calling conclude,
134    * NULL if conclude has been called.
135    */
136   struct GNUNET_MQ_Envelope *conclude_mqm;
137
138   /**
139    * Address of the request if in the conclude message,
140    * used to patch the request id into the message when the set is known.
141    */
142   uint32_t *request_id_addr;
143
144   /**
145    * Handles are kept in a linked list.
146    */
147   struct GNUNET_SET_OperationHandle *prev;
148
149   /**
150    * Handles are kept in a linked list.
151    */
152   struct GNUNET_SET_OperationHandle *next;
153 };
154
155
156 /**
157  * Opaque handle to a listen operation.
158  */
159 struct GNUNET_SET_ListenHandle
160 {
161   /**
162    * Connection to the service.
163    */
164   struct GNUNET_CLIENT_Connection *client;
165
166   /**
167    * Message queue for the client.
168    */
169   struct GNUNET_MQ_Handle* mq;
170
171   /**
172    * Configuration handle for the listener, stored
173    * here to be able to reconnect transparently on
174    * connection failure.
175    */
176   const struct GNUNET_CONFIGURATION_Handle *cfg;
177
178   /**
179    * Function to call on a new incoming request,
180    * or on error.
181    */
182   GNUNET_SET_ListenCallback listen_cb;
183
184   /**
185    * Closure for listen_cb.
186    */
187   void *listen_cls;
188
189   /**
190    * Operation we listen for.
191    */
192   enum GNUNET_SET_OperationType operation;
193
194   /**
195    * Application ID we listen for.
196    */
197   struct GNUNET_HashCode app_id;
198
199   /**
200    * Time to wait until we try to reconnect on failure.
201    */
202   struct GNUNET_TIME_Relative reconnect_backoff;
203
204   /**
205    * Task for reconnecting when the listener fails.
206    */
207   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
208 };
209
210
211 /* forward declaration */
212 static void
213 listen_connect (void *cls,
214                 const struct GNUNET_SCHEDULER_TaskContext *tc);
215
216
217 /**
218  * Handle element for iteration over the set.
219  *
220  * @param cls the set
221  * @param mh the message
222  */
223 static void
224 handle_iter_element (void *cls, const struct GNUNET_MessageHeader *mh)
225 {
226   struct GNUNET_SET_Handle *set = cls;
227   struct GNUNET_SET_Element element;
228   const struct GNUNET_SET_IterResponseMessage *msg =
229     (const struct GNUNET_SET_IterResponseMessage *) mh;
230   struct GNUNET_SET_IterAckMessage *ack_msg;
231   struct GNUNET_MQ_Envelope *ev;
232
233   if (NULL == set->iterator)
234     return;
235
236   element.size = ntohs (mh->size) - sizeof (struct GNUNET_SET_IterResponseMessage);
237   element.type = htons (msg->element_type);
238   element.data = &msg[1];
239   set->iterator (set->iterator_cls, &element);
240   ev = GNUNET_MQ_msg (ack_msg, GNUNET_MESSAGE_TYPE_SET_ITER_ACK);
241   ack_msg->send_more = htonl (1);
242   GNUNET_MQ_send (set->mq, ev);
243 }
244
245
246 /**
247  * Handle element for iteration over the set.
248  *
249  * @param cls the set
250  * @param mh the message
251  */
252 static void
253 handle_iter_done (void *cls, const struct GNUNET_MessageHeader *mh)
254 {
255   struct GNUNET_SET_Handle *set = cls;
256
257   if (NULL == set->iterator)
258     return;
259
260   set->iterator (set->iterator_cls, NULL);
261 }
262
263
264 /**
265  * Handle result message for a set operation.
266  *
267  * @param cls the set
268  * @param mh the message
269  */
270 static void
271 handle_result (void *cls, const struct GNUNET_MessageHeader *mh)
272 {
273   const struct GNUNET_SET_ResultMessage *msg;
274   struct GNUNET_SET_Handle *set = cls;
275   struct GNUNET_SET_OperationHandle *oh;
276   struct GNUNET_SET_Element e;
277   enum GNUNET_SET_Status result_status;
278
279   msg = (const struct GNUNET_SET_ResultMessage *) mh;
280   GNUNET_assert (NULL != set);
281   GNUNET_assert (NULL != set->mq);
282
283   result_status = ntohs (msg->result_status);
284
285   oh = GNUNET_MQ_assoc_get (set->mq, ntohl (msg->request_id));
286   GNUNET_assert (NULL != oh);
287   /* status is not STATUS_OK => there's no attached element,
288    * and this is the last result message we get */
289   if (GNUNET_SET_STATUS_OK != result_status)
290   {
291     GNUNET_MQ_assoc_remove (set->mq, ntohl (msg->request_id));
292     GNUNET_CONTAINER_DLL_remove (oh->set->ops_head, oh->set->ops_tail, oh);
293     if (GNUNET_YES == oh->set->destroy_requested)
294       GNUNET_SET_destroy (oh->set);
295     if (NULL != oh->result_cb)
296       oh->result_cb (oh->result_cls, NULL, result_status);
297     GNUNET_free (oh);
298     return;
299   }
300
301   e.data = &msg[1];
302   e.size = ntohs (mh->size) - sizeof (struct GNUNET_SET_ResultMessage);
303   e.type = msg->element_type;
304   if (NULL != oh->result_cb)
305     oh->result_cb (oh->result_cls, &e, result_status);
306 }
307
308
309 /**
310  * Handle request message for a listen operation
311  *
312  * @param cls the listen handle
313  * @param mh the message
314  */
315 static void
316 handle_request (void *cls, const struct GNUNET_MessageHeader *mh)
317 {
318   const struct GNUNET_SET_RequestMessage *msg = (const struct GNUNET_SET_RequestMessage *) mh;
319   struct GNUNET_SET_ListenHandle *lh = cls;
320   struct GNUNET_SET_Request *req;
321   struct GNUNET_MessageHeader *context_msg;
322
323   LOG (GNUNET_ERROR_TYPE_DEBUG, "processing operation request\n");
324   req = GNUNET_new (struct GNUNET_SET_Request);
325   req->accept_id = ntohl (msg->accept_id);
326   context_msg = GNUNET_MQ_extract_nested_mh (msg);
327   /* calling GNUNET_SET_accept in the listen cb will set req->accepted */
328   lh->listen_cb (lh->listen_cls, &msg->peer_id, context_msg, req);
329
330   /* we got another request => reset the backoff */
331   lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
332
333   if (GNUNET_NO == req->accepted)
334   {
335     struct GNUNET_MQ_Envelope *mqm;
336     struct GNUNET_SET_AcceptRejectMessage *amsg;
337
338     mqm = GNUNET_MQ_msg (amsg, GNUNET_MESSAGE_TYPE_SET_REJECT);
339     /* no request id, as we refused */
340     amsg->request_id = htonl (0);
341     amsg->accept_reject_id = msg->accept_id;
342     GNUNET_MQ_send (lh->mq, mqm);
343     LOG (GNUNET_ERROR_TYPE_DEBUG, "rejecting request\n");
344   }
345   GNUNET_free (req);
346
347   LOG (GNUNET_ERROR_TYPE_DEBUG, "processed op request from service\n");
348
349   /* the accept-case is handled in GNUNET_SET_accept,
350    * as we have the accept message available there */
351 }
352
353
354 static void
355 handle_client_listener_error (void *cls, enum GNUNET_MQ_Error error)
356 {
357   struct GNUNET_SET_ListenHandle *lh = cls;
358
359   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "listener broke down, re-connecting\n");
360   GNUNET_CLIENT_disconnect (lh->client);
361   lh->client = NULL;
362   GNUNET_MQ_destroy (lh->mq);
363   lh->mq = NULL;
364
365   lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff, listen_connect, lh);
366   lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff);
367 }
368
369
370 /**
371  * Destroy the set handle if no operations are left, mark the set
372  * for destruction otherwise.
373  *
374  * @param set set handle to destroy
375  */
376 static int
377 set_destroy (struct GNUNET_SET_Handle *set)
378 {
379   if (NULL != set->ops_head)
380   {
381     set->destroy_requested = GNUNET_YES;
382     return GNUNET_NO;
383   }
384   GNUNET_CLIENT_disconnect (set->client);
385   set->client = NULL;
386   GNUNET_MQ_destroy (set->mq);
387   set->mq = NULL;
388   GNUNET_free (set);
389   return GNUNET_YES;
390 }
391
392
393
394
395 /**
396  * Cancel the given set operation.  We need to send an explicit cancel message,
397  * as all operations one one set communicate using one handle.
398  *
399  * In contrast to GNUNET_SET_operation_cancel, this function indicates whether
400  * the set of the operation has been destroyed because all operations are done and
401  * the set's destruction was requested before.
402  *
403  * @param oh set operation to cancel
404  * @return GNUNET_YES if the set of the operation was destroyed
405  */
406 static int
407 set_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
408 {
409   int ret = GNUNET_NO;
410
411   if (NULL != oh->conclude_mqm)
412     GNUNET_MQ_discard (oh->conclude_mqm);
413
414   /* is the operation already commited? */
415   if (NULL != oh->set)
416   {
417     struct GNUNET_SET_OperationHandle *h_assoc;
418     struct GNUNET_MQ_Envelope *mqm;
419
420     GNUNET_CONTAINER_DLL_remove (oh->set->ops_head, oh->set->ops_tail, oh);
421     h_assoc = GNUNET_MQ_assoc_remove (oh->set->mq, oh->request_id);
422     GNUNET_assert ((h_assoc == NULL) || (h_assoc == oh));
423     mqm = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_CANCEL);
424     GNUNET_MQ_send (oh->set->mq, mqm);
425
426     if (GNUNET_YES == oh->set->destroy_requested)
427       ret = set_destroy (oh->set);
428   }
429
430   GNUNET_free (oh);
431   
432   return ret;
433 }
434
435
436 /**
437  * Cancel the given set operation.  We need to send an explicit cancel message,
438  * as all operations one one set communicate using one handle.
439  *
440  * @param oh set operation to cancel
441  */
442 void
443 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
444 {
445   (void) set_operation_cancel (oh);
446 }
447
448
449 static void
450 handle_client_set_error (void *cls, enum GNUNET_MQ_Error error)
451 {
452   struct GNUNET_SET_Handle *set = cls;
453
454   while (NULL != set->ops_head)
455   {
456     if (NULL != set->ops_head->result_cb)
457       set->ops_head->result_cb (set->ops_head->result_cls, NULL,
458                                 GNUNET_SET_STATUS_FAILURE);
459     if (GNUNET_YES == set_operation_cancel (set->ops_head))
460       return; /* stop if the set is destroyed */
461   }
462   set->invalid = GNUNET_YES;
463 }
464
465
466 /**
467  * Create an empty set, supporting the specified operation.
468  *
469  * @param cfg configuration to use for connecting to the
470  *        set service
471  * @param op operation supported by the set
472  *        Note that the operation has to be specified
473  *        beforehand, as certain set operations need to maintain
474  *        data structures spefific to the operation
475  * @return a handle to the set
476  */
477 struct GNUNET_SET_Handle *
478 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
479                    enum GNUNET_SET_OperationType op)
480 {
481   struct GNUNET_SET_Handle *set;
482   struct GNUNET_MQ_Envelope *mqm;
483   struct GNUNET_SET_CreateMessage *msg;
484   static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
485     {handle_result, GNUNET_MESSAGE_TYPE_SET_RESULT, 0},
486     {handle_iter_element, GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT, 0},
487     {handle_iter_done, GNUNET_MESSAGE_TYPE_SET_ITER_DONE, 0},
488     GNUNET_MQ_HANDLERS_END
489   };
490
491   set = GNUNET_new (struct GNUNET_SET_Handle);
492   set->client = GNUNET_CLIENT_connect ("set", cfg);
493   LOG (GNUNET_ERROR_TYPE_DEBUG, "set client created\n");
494   GNUNET_assert (NULL != set->client);
495   set->mq = GNUNET_MQ_queue_for_connection_client (set->client, mq_handlers,
496                                                    handle_client_set_error, set);
497   GNUNET_assert (NULL != set->mq);
498   mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_CREATE);
499   msg->operation = htons (op);
500   GNUNET_MQ_send (set->mq, mqm);
501   return set;
502 }
503
504
505 /**
506  * Add an element to the given set.
507  * After the element has been added (in the sense of being
508  * transmitted to the set service), cont will be called.
509  * Calls to add_element can be queued
510  *
511  * @param set set to add element to
512  * @param element element to add to the set
513  * @param cont continuation called after the element has been added
514  * @param cont_cls closure for @a cont
515  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
516  *         set is invalid (e.g. the set service crashed)
517  */
518 int
519 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
520                         const struct GNUNET_SET_Element *element,
521                         GNUNET_SET_Continuation cont,
522                         void *cont_cls)
523 {
524   struct GNUNET_MQ_Envelope *mqm;
525   struct GNUNET_SET_ElementMessage *msg;
526
527   if (GNUNET_YES == set->invalid)
528   {
529     if (NULL != cont)
530       cont (cont_cls);
531     return GNUNET_SYSERR;
532   }
533
534   mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_ADD);
535   msg->element_type = element->type;
536   memcpy (&msg[1], element->data, element->size);
537   GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
538   GNUNET_MQ_send (set->mq, mqm);
539   return GNUNET_OK;
540 }
541
542
543 /**
544  * Remove an element to the given set.
545  * After the element has been removed (in the sense of the
546  * request being transmitted to the set service), cont will be called.
547  * Calls to remove_element can be queued
548  *
549  * @param set set to remove element from
550  * @param element element to remove from the set
551  * @param cont continuation called after the element has been removed
552  * @param cont_cls closure for cont
553  * @return GNUNET_OK on success, GNUNET_SYSERR if the
554  *         set is invalid (e.g. the set service crashed)
555  */
556 int
557 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
558                            const struct GNUNET_SET_Element *element,
559                            GNUNET_SET_Continuation cont,
560                            void *cont_cls)
561 {
562   struct GNUNET_MQ_Envelope *mqm;
563   struct GNUNET_SET_ElementMessage *msg;
564
565   if (GNUNET_YES == set->invalid)
566   {
567     if (NULL != cont)
568       cont (cont_cls);
569     return GNUNET_SYSERR;
570   }
571
572   mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_REMOVE);
573   msg->element_type = element->type;
574   memcpy (&msg[1], element->data, element->size);
575   GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
576   GNUNET_MQ_send (set->mq, mqm);
577   return GNUNET_OK;
578 }
579
580
581 /**
582  * Destroy the set handle, and free all associated resources.
583  *
584  * @param set set handle to destroy
585  */
586 void
587 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set)
588 {
589   (void) set_destroy (set);
590 }
591
592
593 /**
594  * Prepare a set operation to be evaluated with another peer.
595  * The evaluation will not start until the client provides
596  * a local set with GNUNET_SET_commit.
597  *
598  * @param other_peer peer with the other set
599  * @param app_id hash for the application using the set
600  * @param context_msg additional information for the request
601  * @param salt salt used for the set operation; sometimes set operations
602  *        fail due to hash collisions, using a different salt for each operation
603  *        makes it harder for an attacker to exploit this
604  * @param result_mode specified how results will be returned,
605  *        see 'GNUNET_SET_ResultMode'.
606  * @param result_cb called on error or success
607  * @param result_cls closure for result_cb
608  * @return a handle to cancel the operation
609  */
610 struct GNUNET_SET_OperationHandle *
611 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
612                     const struct GNUNET_HashCode *app_id,
613                     const struct GNUNET_MessageHeader *context_msg,
614                     uint16_t salt,
615                     enum GNUNET_SET_ResultMode result_mode,
616                     GNUNET_SET_ResultIterator result_cb,
617                     void *result_cls)
618 {
619   struct GNUNET_MQ_Envelope *mqm;
620   struct GNUNET_SET_OperationHandle *oh;
621   struct GNUNET_SET_EvaluateMessage *msg;
622
623   oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
624   oh->result_cb = result_cb;
625   oh->result_cls = result_cls;
626
627   mqm = GNUNET_MQ_msg_nested_mh (msg, GNUNET_MESSAGE_TYPE_SET_EVALUATE, context_msg);
628
629   msg->app_id = *app_id;
630   msg->result_mode = htons (result_mode);
631   msg->target_peer = *other_peer;
632   msg->salt = salt;
633   oh->conclude_mqm = mqm;
634   oh->request_id_addr = &msg->request_id;
635
636   return oh;
637 }
638
639
640 /**
641  * Connect to the set service in order to listen
642  * for request.
643  *
644  * @param cls the listen handle to connect
645  * @param tc task context if invoked as a task, NULL otherwise
646  */
647 static void
648 listen_connect (void *cls,
649                 const struct GNUNET_SCHEDULER_TaskContext *tc)
650 {
651   struct GNUNET_MQ_Envelope *mqm;
652   struct GNUNET_SET_ListenMessage *msg;
653   struct GNUNET_SET_ListenHandle *lh = cls;
654   static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
655     {handle_request, GNUNET_MESSAGE_TYPE_SET_REQUEST},
656     GNUNET_MQ_HANDLERS_END
657   };
658
659   if ((tc != NULL) &&(tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
660   {
661     LOG (GNUNET_ERROR_TYPE_DEBUG, "listener not reconnecting due to shutdown\n");
662     return;
663   }
664
665   lh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
666
667   GNUNET_assert (NULL == lh->client);
668   lh->client = GNUNET_CLIENT_connect ("set", lh->cfg);
669   if (NULL == lh->client)
670   {
671     LOG (GNUNET_ERROR_TYPE_ERROR,
672          "could not connect to set (wrong configuration?), giving up listening\n");
673     return;
674   }
675   GNUNET_assert (NULL == lh->mq);
676   lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client, mq_handlers,
677                                                   handle_client_listener_error, lh);
678   mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
679   msg->operation = htonl (lh->operation);
680   msg->app_id = lh->app_id;
681   GNUNET_MQ_send (lh->mq, mqm);
682 }
683
684
685 /**
686  * Wait for set operation requests for the given application id
687  *
688  * @param cfg configuration to use for connecting to
689  *            the set service, needs to be valid for the lifetime of the listen handle
690  * @param operation operation we want to listen for
691  * @param app_id id of the application that handles set operation requests
692  * @param listen_cb called for each incoming request matching the operation
693  *                  and application id
694  * @param listen_cls handle for listen_cb
695  * @return a handle that can be used to cancel the listen operation
696  */
697 struct GNUNET_SET_ListenHandle *
698 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
699                    enum GNUNET_SET_OperationType operation,
700                    const struct GNUNET_HashCode *app_id,
701                    GNUNET_SET_ListenCallback listen_cb,
702                    void *listen_cls)
703 {
704   struct GNUNET_SET_ListenHandle *lh;
705
706   lh = GNUNET_new (struct GNUNET_SET_ListenHandle);
707   lh->listen_cb = listen_cb;
708   lh->listen_cls = listen_cls;
709   lh->cfg = cfg;
710   lh->operation = operation;
711   lh->app_id = *app_id;
712   lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
713   listen_connect (lh, NULL);
714   return lh;
715 }
716
717
718 /**
719  * Cancel the given listen operation.
720  *
721  * @param lh handle for the listen operation
722  */
723 void
724 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh)
725 {
726   LOG (GNUNET_ERROR_TYPE_DEBUG, "canceling listener\n");
727   /* listener's connection may have failed, thus mq/client could be NULL */
728   if (NULL != lh->mq)
729   {
730     GNUNET_MQ_destroy (lh->mq);
731     lh->mq = NULL;
732   }
733   if (NULL != lh->client)
734   {
735     GNUNET_CLIENT_disconnect (lh->client);
736     lh->client = NULL;
737   }
738   if (GNUNET_SCHEDULER_NO_TASK != lh->reconnect_task)
739   {
740     GNUNET_SCHEDULER_cancel (lh->reconnect_task);
741     lh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
742   }
743   GNUNET_free (lh);
744 }
745
746
747 /**
748  * Accept a request we got via #GNUNET_SET_listen.  Must be called during
749  * #GNUNET_SET_listen, as the 'struct GNUNET_SET_Request' becomes invalid
750  * afterwards.
751  * Call #GNUNET_SET_commit to provide the local set to use for the operation,
752  * and to begin the exchange with the remote peer.
753  *
754  * @param request request to accept
755  * @param result_mode specified how results will be returned,
756  *        see 'GNUNET_SET_ResultMode'.
757  * @param result_cb callback for the results
758  * @param result_cls closure for result_cb
759  * @return a handle to cancel the operation
760  */
761 struct GNUNET_SET_OperationHandle *
762 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
763                    enum GNUNET_SET_ResultMode result_mode,
764                    GNUNET_SET_ResultIterator result_cb,
765                    void *result_cls)
766 {
767   struct GNUNET_MQ_Envelope *mqm;
768   struct GNUNET_SET_OperationHandle *oh;
769   struct GNUNET_SET_AcceptRejectMessage *msg;
770
771   GNUNET_assert (NULL != request);
772   GNUNET_assert (GNUNET_NO == request->accepted);
773   request->accepted = GNUNET_YES;
774
775   oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
776   oh->result_cb = result_cb;
777   oh->result_cls = result_cls;
778
779   mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_ACCEPT);
780   msg->accept_reject_id = htonl (request->accept_id);
781   msg->result_mode = htons (result_mode);
782
783   oh->conclude_mqm = mqm;
784   oh->request_id_addr = &msg->request_id;
785
786   return oh;
787 }
788
789
790 /**
791  * Commit a set to be used with a set operation.
792  * This function is called once we have fully constructed
793  * the set that we want to use for the operation.  At this
794  * time, the P2P protocol can then begin to exchange the
795  * set information and call the result callback with the
796  * result information.
797  *
798  * @param oh handle to the set operation
799  * @param set the set to use for the operation
800  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
801  *         set is invalid (e.g. the set service crashed)
802  */
803 int
804 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
805                    struct GNUNET_SET_Handle *set)
806 {
807   GNUNET_assert (NULL == oh->set);
808   if (GNUNET_YES == set->invalid)
809     return GNUNET_SYSERR;
810   GNUNET_assert (NULL != oh->conclude_mqm);
811   oh->set = set;
812   GNUNET_CONTAINER_DLL_insert (set->ops_head, set->ops_tail, oh);
813   oh->request_id = GNUNET_MQ_assoc_add (set->mq, oh);
814   *oh->request_id_addr = htonl (oh->request_id);
815   GNUNET_MQ_send (set->mq, oh->conclude_mqm);
816   oh->conclude_mqm = NULL;
817   oh->request_id_addr = NULL;
818   return GNUNET_OK;
819 }
820
821
822 /**
823  * Iterate over all elements in the given set.
824  * Note that this operation involves transferring every element of the set
825  * from the service to the client, and is thus costly.
826  *
827  * @param set the set to iterate over
828  * @param iter the iterator to call for each element
829  * @param cls closure for @a iter
830  * @return #GNUNET_YES if the iteration started successfuly,
831  *         #GNUNET_NO if another iteration is active
832  *         #GNUNET_SYSERR if the set is invalid (e.g. the server crashed, disconnected)
833  */
834 int
835 GNUNET_SET_iterate (struct GNUNET_SET_Handle *set, GNUNET_SET_ElementIterator iter, void *cls)
836 {
837   struct GNUNET_MQ_Envelope *ev;
838
839   GNUNET_assert (NULL != iter);
840
841   if (GNUNET_YES == set->invalid)
842     return GNUNET_SYSERR;
843   if (NULL != set->iterator)
844     return GNUNET_NO;
845
846   set->iterator = iter;
847   set->iterator_cls = cls;
848   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST);
849   GNUNET_MQ_send (set->mq, ev);
850   return GNUNET_YES;
851 }
852
853 /* end of set_api.c */