90cba446cbc1ada0924cb629ad9df68d35c4d92f
[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.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   // 'oh' can be NULL if we canceled the operation, but the service
287   // did not get the cancel message yet.
288   if (NULL == oh)
289   {
290     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ignoring result from canceled operation\n");
291     return;
292   }
293   /* status is not STATUS_OK => there's no attached element,
294    * and this is the last result message we get */
295   if (GNUNET_SET_STATUS_OK != result_status)
296   {
297     GNUNET_MQ_assoc_remove (set->mq, ntohl (msg->request_id));
298     GNUNET_CONTAINER_DLL_remove (oh->set->ops_head, oh->set->ops_tail, oh);
299     if (GNUNET_YES == oh->set->destroy_requested)
300       GNUNET_SET_destroy (oh->set);
301     if (NULL != oh->result_cb)
302       oh->result_cb (oh->result_cls, NULL, result_status);
303     GNUNET_free (oh);
304     return;
305   }
306
307   e.data = &msg[1];
308   e.size = ntohs (mh->size) - sizeof (struct GNUNET_SET_ResultMessage);
309   e.element_type = msg->element_type;
310   if (NULL != oh->result_cb)
311     oh->result_cb (oh->result_cls, &e, result_status);
312 }
313
314
315 /**
316  * Handle request message for a listen operation
317  *
318  * @param cls the listen handle
319  * @param mh the message
320  */
321 static void
322 handle_request (void *cls,
323                 const struct GNUNET_MessageHeader *mh)
324 {
325   const struct GNUNET_SET_RequestMessage *msg = (const struct GNUNET_SET_RequestMessage *) mh;
326   struct GNUNET_SET_ListenHandle *lh = cls;
327   struct GNUNET_SET_Request *req;
328   const struct GNUNET_MessageHeader *context_msg;
329
330   LOG (GNUNET_ERROR_TYPE_DEBUG,
331        "processing operation request\n");
332   req = GNUNET_new (struct GNUNET_SET_Request);
333   req->accept_id = ntohl (msg->accept_id);
334   context_msg = GNUNET_MQ_extract_nested_mh (msg);
335   /* calling #GNUNET_SET_accept() in the listen cb will set req->accepted */
336   lh->listen_cb (lh->listen_cls, &msg->peer_id, context_msg, req);
337
338   /* we got another request => reset the backoff */
339   lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
340
341   if (GNUNET_NO == req->accepted)
342   {
343     struct GNUNET_MQ_Envelope *mqm;
344     struct GNUNET_SET_RejectMessage *rmsg;
345
346     mqm = GNUNET_MQ_msg (rmsg,
347                          GNUNET_MESSAGE_TYPE_SET_REJECT);
348     rmsg->accept_reject_id = msg->accept_id;
349     GNUNET_MQ_send (lh->mq, mqm);
350     LOG (GNUNET_ERROR_TYPE_DEBUG,
351          "rejecting request\n");
352   }
353   GNUNET_free (req);
354
355   LOG (GNUNET_ERROR_TYPE_DEBUG,
356        "processed op request from service\n");
357
358   /* the accept-case is handled in GNUNET_SET_accept,
359    * as we have the accept message available there */
360 }
361
362
363 static void
364 handle_client_listener_error (void *cls,
365                               enum GNUNET_MQ_Error error)
366 {
367   struct GNUNET_SET_ListenHandle *lh = cls;
368
369   LOG (GNUNET_ERROR_TYPE_DEBUG,
370        "listener broke down, re-connecting\n");
371   GNUNET_CLIENT_disconnect (lh->client);
372   lh->client = NULL;
373   GNUNET_MQ_destroy (lh->mq);
374   lh->mq = NULL;
375   lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff,
376                                                      &listen_connect, lh);
377   lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff);
378 }
379
380
381 /**
382  * Destroy the set handle if no operations are left, mark the set
383  * for destruction otherwise.
384  *
385  * @param set set handle to destroy
386  */
387 static int
388 set_destroy (struct GNUNET_SET_Handle *set)
389 {
390   if (NULL != set->ops_head)
391   {
392     set->destroy_requested = GNUNET_YES;
393     return GNUNET_NO;
394   }
395   LOG (GNUNET_ERROR_TYPE_DEBUG, "Really destroying set\n");
396   GNUNET_CLIENT_disconnect (set->client);
397   set->client = NULL;
398   GNUNET_MQ_destroy (set->mq);
399   set->mq = NULL;
400   GNUNET_free (set);
401   return GNUNET_YES;
402 }
403
404
405 /**
406  * Cancel the given set operation.  We need to send an explicit cancel message,
407  * as all operations one one set communicate using one handle.
408  *
409  * In contrast to #GNUNET_SET_operation_cancel(), this function indicates whether
410  * the set of the operation has been destroyed because all operations are done and
411  * the set's destruction was requested before.
412  *
413  * @param oh set operation to cancel
414  * @return #GNUNET_YES if the set of the operation was destroyed
415  */
416 static int
417 set_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
418 {
419   int ret = GNUNET_NO;
420
421   if (NULL != oh->conclude_mqm)
422     GNUNET_MQ_discard (oh->conclude_mqm);
423
424   /* is the operation already commited? */
425   if (NULL != oh->set)
426   {
427     struct GNUNET_SET_OperationHandle *h_assoc;
428     struct GNUNET_SET_CancelMessage *m;
429     struct GNUNET_MQ_Envelope *mqm;
430
431     GNUNET_CONTAINER_DLL_remove (oh->set->ops_head,
432                                  oh->set->ops_tail,
433                                  oh);
434     h_assoc = GNUNET_MQ_assoc_remove (oh->set->mq,
435                                       oh->request_id);
436     GNUNET_assert ((h_assoc == NULL) || (h_assoc == oh));
437     mqm = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_SET_CANCEL);
438     m->request_id = htonl (oh->request_id);
439     GNUNET_MQ_send (oh->set->mq, mqm);
440
441     if (GNUNET_YES == oh->set->destroy_requested)
442     {
443       LOG (GNUNET_ERROR_TYPE_DEBUG,
444            "Destroying set after operation cancel\n");
445       ret = set_destroy (oh->set);
446     }
447   }
448   GNUNET_free (oh);
449   return ret;
450 }
451
452
453 /**
454  * Cancel the given set operation.  We need to send an explicit cancel
455  * message, as all operations one one set communicate using one
456  * handle.
457  *
458  * @param oh set operation to cancel
459  */
460 void
461 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
462 {
463   (void) set_operation_cancel (oh);
464 }
465
466
467 static void
468 handle_client_set_error (void *cls, enum GNUNET_MQ_Error error)
469 {
470   struct GNUNET_SET_Handle *set = cls;
471
472   LOG (GNUNET_ERROR_TYPE_DEBUG,
473        "handling client set error\n");
474
475   while (NULL != set->ops_head)
476   {
477     if (NULL != set->ops_head->result_cb)
478       set->ops_head->result_cb (set->ops_head->result_cls, NULL,
479                                 GNUNET_SET_STATUS_FAILURE);
480     if (GNUNET_YES == set_operation_cancel (set->ops_head))
481       return; /* stop if the set is destroyed */
482   }
483   set->invalid = GNUNET_YES;
484 }
485
486
487 /**
488  * Create an empty set, supporting the specified operation.
489  *
490  * @param cfg configuration to use for connecting to the
491  *        set service
492  * @param op operation supported by the set
493  *        Note that the operation has to be specified
494  *        beforehand, as certain set operations need to maintain
495  *        data structures spefific to the operation
496  * @return a handle to the set
497  */
498 struct GNUNET_SET_Handle *
499 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
500                    enum GNUNET_SET_OperationType op)
501 {
502   static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
503     {handle_result, GNUNET_MESSAGE_TYPE_SET_RESULT, 0},
504     {handle_iter_element, GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT, 0},
505     {handle_iter_done, GNUNET_MESSAGE_TYPE_SET_ITER_DONE, 0},
506     GNUNET_MQ_HANDLERS_END
507   };
508   struct GNUNET_SET_Handle *set;
509   struct GNUNET_MQ_Envelope *mqm;
510   struct GNUNET_SET_CreateMessage *msg;
511
512   set = GNUNET_new (struct GNUNET_SET_Handle);
513   set->client = GNUNET_CLIENT_connect ("set", cfg);
514   LOG (GNUNET_ERROR_TYPE_DEBUG, "set client created\n");
515   GNUNET_assert (NULL != set->client);
516   set->mq = GNUNET_MQ_queue_for_connection_client (set->client, mq_handlers,
517                                                    handle_client_set_error, set);
518   GNUNET_assert (NULL != set->mq);
519   mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_CREATE);
520   msg->operation = htonl (op);
521   GNUNET_MQ_send (set->mq, mqm);
522   return set;
523 }
524
525
526 /**
527  * Add an element to the given set.
528  * After the element has been added (in the sense of being
529  * transmitted to the set service), cont will be called.
530  * Calls to add_element can be queued
531  *
532  * @param set set to add element to
533  * @param element element to add to the set
534  * @param cont continuation called after the element has been added
535  * @param cont_cls closure for @a cont
536  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
537  *         set is invalid (e.g. the set service crashed)
538  */
539 int
540 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
541                         const struct GNUNET_SET_Element *element,
542                         GNUNET_SET_Continuation cont,
543                         void *cont_cls)
544 {
545   struct GNUNET_MQ_Envelope *mqm;
546   struct GNUNET_SET_ElementMessage *msg;
547
548   if (GNUNET_YES == set->invalid)
549   {
550     if (NULL != cont)
551       cont (cont_cls);
552     return GNUNET_SYSERR;
553   }
554
555   mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_ADD);
556   msg->element_type = element->element_type;
557   memcpy (&msg[1], element->data, element->size);
558   GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
559   GNUNET_MQ_send (set->mq, mqm);
560   return GNUNET_OK;
561 }
562
563
564 /**
565  * Remove an element to the given set.
566  * After the element has been removed (in the sense of the
567  * request being transmitted to the set service), cont will be called.
568  * Calls to remove_element can be queued
569  *
570  * @param set set to remove element from
571  * @param element element to remove from the set
572  * @param cont continuation called after the element has been removed
573  * @param cont_cls closure for @a cont
574  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
575  *         set is invalid (e.g. the set service crashed)
576  */
577 int
578 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
579                            const struct GNUNET_SET_Element *element,
580                            GNUNET_SET_Continuation cont,
581                            void *cont_cls)
582 {
583   struct GNUNET_MQ_Envelope *mqm;
584   struct GNUNET_SET_ElementMessage *msg;
585
586   if (GNUNET_YES == set->invalid)
587   {
588     if (NULL != cont)
589       cont (cont_cls);
590     return GNUNET_SYSERR;
591   }
592
593   mqm = GNUNET_MQ_msg_extra (msg,
594                              element->size,
595                              GNUNET_MESSAGE_TYPE_SET_REMOVE);
596   msg->element_type = element->element_type;
597   memcpy (&msg[1], element->data, element->size);
598   GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
599   GNUNET_MQ_send (set->mq, mqm);
600   return GNUNET_OK;
601 }
602
603
604 /**
605  * Destroy the set handle, and free all associated resources.
606  *
607  * @param set set handle to destroy
608  */
609 void
610 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set)
611 {
612   (void) set_destroy (set);
613 }
614
615
616 /**
617  * Prepare a set operation to be evaluated with another peer.
618  * The evaluation will not start until the client provides
619  * a local set with #GNUNET_SET_commit().
620  *
621  * @param other_peer peer with the other set
622  * @param app_id hash for the application using the set
623  * @param context_msg additional information for the request
624  * @param result_mode specified how results will be returned,
625  *        see `enum GNUNET_SET_ResultMode`.
626  * @param result_cb called on error or success
627  * @param result_cls closure for @e result_cb
628  * @return a handle to cancel the operation
629  */
630 struct GNUNET_SET_OperationHandle *
631 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
632                     const struct GNUNET_HashCode *app_id,
633                     const struct GNUNET_MessageHeader *context_msg,
634                     enum GNUNET_SET_ResultMode result_mode,
635                     GNUNET_SET_ResultIterator result_cb,
636                     void *result_cls)
637 {
638   struct GNUNET_MQ_Envelope *mqm;
639   struct GNUNET_SET_OperationHandle *oh;
640   struct GNUNET_SET_EvaluateMessage *msg;
641
642   oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
643   oh->result_cb = result_cb;
644   oh->result_cls = result_cls;
645   mqm = GNUNET_MQ_msg_nested_mh (msg,
646                                  GNUNET_MESSAGE_TYPE_SET_EVALUATE,
647                                  context_msg);
648   msg->app_id = *app_id;
649   msg->result_mode = htonl (result_mode);
650   msg->target_peer = *other_peer;
651   oh->conclude_mqm = mqm;
652   oh->request_id_addr = &msg->request_id;
653
654   return oh;
655 }
656
657
658 /**
659  * Connect to the set service in order to listen
660  * for request.
661  *
662  * @param cls the listen handle to connect
663  * @param tc task context if invoked as a task, NULL otherwise
664  */
665 static void
666 listen_connect (void *cls,
667                 const struct GNUNET_SCHEDULER_TaskContext *tc)
668 {
669   struct GNUNET_MQ_Envelope *mqm;
670   struct GNUNET_SET_ListenMessage *msg;
671   struct GNUNET_SET_ListenHandle *lh = cls;
672   static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
673     {handle_request, GNUNET_MESSAGE_TYPE_SET_REQUEST},
674     GNUNET_MQ_HANDLERS_END
675   };
676
677   if ((tc != NULL) &&(tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
678   {
679     LOG (GNUNET_ERROR_TYPE_DEBUG, "listener not reconnecting due to shutdown\n");
680     return;
681   }
682
683   lh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
684
685   GNUNET_assert (NULL == lh->client);
686   lh->client = GNUNET_CLIENT_connect ("set", lh->cfg);
687   if (NULL == lh->client)
688   {
689     LOG (GNUNET_ERROR_TYPE_ERROR,
690          "could not connect to set (wrong configuration?), giving up listening\n");
691     return;
692   }
693   GNUNET_assert (NULL == lh->mq);
694   lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client, mq_handlers,
695                                                   handle_client_listener_error, lh);
696   mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
697   msg->operation = htonl (lh->operation);
698   msg->app_id = lh->app_id;
699   GNUNET_MQ_send (lh->mq, mqm);
700 }
701
702
703 /**
704  * Wait for set operation requests for the given application id
705  *
706  * @param cfg configuration to use for connecting to
707  *            the set service, needs to be valid for the lifetime of the listen handle
708  * @param operation operation we want to listen for
709  * @param app_id id of the application that handles set operation requests
710  * @param listen_cb called for each incoming request matching the operation
711  *                  and application id
712  * @param listen_cls handle for listen_cb
713  * @return a handle that can be used to cancel the listen operation
714  */
715 struct GNUNET_SET_ListenHandle *
716 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
717                    enum GNUNET_SET_OperationType operation,
718                    const struct GNUNET_HashCode *app_id,
719                    GNUNET_SET_ListenCallback listen_cb,
720                    void *listen_cls)
721 {
722   struct GNUNET_SET_ListenHandle *lh;
723
724   lh = GNUNET_new (struct GNUNET_SET_ListenHandle);
725   lh->listen_cb = listen_cb;
726   lh->listen_cls = listen_cls;
727   lh->cfg = cfg;
728   lh->operation = operation;
729   lh->app_id = *app_id;
730   lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
731   listen_connect (lh, NULL);
732   return lh;
733 }
734
735
736 /**
737  * Cancel the given listen operation.
738  *
739  * @param lh handle for the listen operation
740  */
741 void
742 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh)
743 {
744   LOG (GNUNET_ERROR_TYPE_DEBUG, "canceling listener\n");
745   /* listener's connection may have failed, thus mq/client could be NULL */
746   if (NULL != lh->mq)
747   {
748     GNUNET_MQ_destroy (lh->mq);
749     lh->mq = NULL;
750   }
751   if (NULL != lh->client)
752   {
753     GNUNET_CLIENT_disconnect (lh->client);
754     lh->client = NULL;
755   }
756   if (GNUNET_SCHEDULER_NO_TASK != lh->reconnect_task)
757   {
758     GNUNET_SCHEDULER_cancel (lh->reconnect_task);
759     lh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
760   }
761   GNUNET_free (lh);
762 }
763
764
765 /**
766  * Accept a request we got via #GNUNET_SET_listen.  Must be called during
767  * #GNUNET_SET_listen, as the 'struct GNUNET_SET_Request' becomes invalid
768  * afterwards.
769  * Call #GNUNET_SET_commit to provide the local set to use for the operation,
770  * and to begin the exchange with the remote peer.
771  *
772  * @param request request to accept
773  * @param result_mode specified how results will be returned,
774  *        see `enum GNUNET_SET_ResultMode`.
775  * @param result_cb callback for the results
776  * @param result_cls closure for @a result_cb
777  * @return a handle to cancel the operation
778  */
779 struct GNUNET_SET_OperationHandle *
780 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
781                    enum GNUNET_SET_ResultMode result_mode,
782                    GNUNET_SET_ResultIterator result_cb,
783                    void *result_cls)
784 {
785   struct GNUNET_MQ_Envelope *mqm;
786   struct GNUNET_SET_OperationHandle *oh;
787   struct GNUNET_SET_AcceptMessage *msg;
788
789   GNUNET_assert (NULL != request);
790   GNUNET_assert (GNUNET_NO == request->accepted);
791   request->accepted = GNUNET_YES;
792
793   oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
794   oh->result_cb = result_cb;
795   oh->result_cls = result_cls;
796
797   mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_ACCEPT);
798   msg->accept_reject_id = htonl (request->accept_id);
799   msg->result_mode = htonl (result_mode);
800
801   oh->conclude_mqm = mqm;
802   oh->request_id_addr = &msg->request_id;
803
804   return oh;
805 }
806
807
808 /**
809  * Commit a set to be used with a set operation.
810  * This function is called once we have fully constructed
811  * the set that we want to use for the operation.  At this
812  * time, the P2P protocol can then begin to exchange the
813  * set information and call the result callback with the
814  * result information.
815  *
816  * @param oh handle to the set operation
817  * @param set the set to use for the operation
818  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
819  *         set is invalid (e.g. the set service crashed)
820  */
821 int
822 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
823                    struct GNUNET_SET_Handle *set)
824 {
825   GNUNET_assert (NULL == oh->set);
826   if (GNUNET_YES == set->invalid)
827     return GNUNET_SYSERR;
828   GNUNET_assert (NULL != oh->conclude_mqm);
829   oh->set = set;
830   GNUNET_CONTAINER_DLL_insert (set->ops_head,
831                                set->ops_tail,
832                                oh);
833   oh->request_id = GNUNET_MQ_assoc_add (set->mq, oh);
834   *oh->request_id_addr = htonl (oh->request_id);
835   GNUNET_MQ_send (set->mq, oh->conclude_mqm);
836   oh->conclude_mqm = NULL;
837   oh->request_id_addr = NULL;
838   return GNUNET_OK;
839 }
840
841
842 /**
843  * Iterate over all elements in the given set.
844  * Note that this operation involves transferring every element of the set
845  * from the service to the client, and is thus costly.
846  *
847  * @param set the set to iterate over
848  * @param iter the iterator to call for each element
849  * @param iter_cls closure for @a iter
850  * @return #GNUNET_YES if the iteration started successfuly,
851  *         #GNUNET_NO if another iteration is active
852  *         #GNUNET_SYSERR if the set is invalid (e.g. the server crashed, disconnected)
853  */
854 int
855 GNUNET_SET_iterate (struct GNUNET_SET_Handle *set,
856                     GNUNET_SET_ElementIterator iter,
857                     void *iter_cls)
858 {
859   struct GNUNET_MQ_Envelope *ev;
860
861
862   GNUNET_assert (NULL != iter);
863
864   if (GNUNET_YES == set->invalid)
865     return GNUNET_SYSERR;
866   if (NULL != set->iterator)
867     return GNUNET_NO;
868   LOG (GNUNET_ERROR_TYPE_DEBUG,
869        "iterating set\n");
870   set->iterator = iter;
871   set->iterator_cls = iter_cls;
872   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST);
873   GNUNET_MQ_send (set->mq, ev);
874   return GNUNET_YES;
875 }
876
877 /* end of set_api.c */