2 This file is part of GNUnet.
3 Copyright (C) 2012-2014 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @brief api for the set service
23 * @author Florian Dold
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_set_service.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "set-api",__VA_ARGS__)
38 struct SetCopyRequest *next;
40 struct SetCopyRequest *prev;
44 GNUNET_SET_CopyReadyCallback cb;
48 * Opaque handle to a set.
50 struct GNUNET_SET_Handle
53 * Client connected to the set service.
55 struct GNUNET_CLIENT_Connection *client;
58 * Message queue for @e client.
60 struct GNUNET_MQ_Handle *mq;
63 * Linked list of operations on the set.
65 struct GNUNET_SET_OperationHandle *ops_head;
68 * Linked list of operations on the set.
70 struct GNUNET_SET_OperationHandle *ops_tail;
73 * Callback for the current iteration over the set,
74 * NULL if no iterator is active.
76 GNUNET_SET_ElementIterator iterator;
79 * Closure for @e iterator
84 * Should the set be destroyed once all operations are gone?
86 int destroy_requested;
89 * Has the set become invalid (e.g. service died)?
94 * Both client and service count the number of iterators
95 * created so far to match replies with iterators.
97 uint16_t iteration_id;
100 * Configuration, needed when creating (lazy) copies.
102 const struct GNUNET_CONFIGURATION_Handle *cfg;
105 * Doubly linked list of copy requests.
107 struct SetCopyRequest *copy_req_head;
110 * Doubly linked list of copy requests.
112 struct SetCopyRequest *copy_req_tail;
117 * Handle for a set operation request from another peer.
119 struct GNUNET_SET_Request
122 * Id of the request, used to identify the request when
123 * accepting/rejecting it.
128 * Has the request been accepted already?
129 * #GNUNET_YES/#GNUNET_NO
136 * Handle to an operation. Only known to the service after committing
137 * the handle with a set.
139 struct GNUNET_SET_OperationHandle
142 * Function to be called when we have a result,
145 GNUNET_SET_ResultIterator result_cb;
148 * Closure for @e result_cb.
153 * Local set used for the operation,
154 * NULL if no set has been provided by conclude yet.
156 struct GNUNET_SET_Handle *set;
159 * Message sent to the server on calling conclude,
160 * NULL if conclude has been called.
162 struct GNUNET_MQ_Envelope *conclude_mqm;
165 * Address of the request if in the conclude message,
166 * used to patch the request id into the message when the set is known.
168 uint32_t *request_id_addr;
171 * Handles are kept in a linked list.
173 struct GNUNET_SET_OperationHandle *prev;
176 * Handles are kept in a linked list.
178 struct GNUNET_SET_OperationHandle *next;
181 * Request ID to identify the operation within the set.
188 * Opaque handle to a listen operation.
190 struct GNUNET_SET_ListenHandle
193 * Connection to the service.
195 struct GNUNET_CLIENT_Connection *client;
198 * Message queue for the client.
200 struct GNUNET_MQ_Handle* mq;
203 * Configuration handle for the listener, stored
204 * here to be able to reconnect transparently on
205 * connection failure.
207 const struct GNUNET_CONFIGURATION_Handle *cfg;
210 * Function to call on a new incoming request,
213 GNUNET_SET_ListenCallback listen_cb;
216 * Closure for @e listen_cb.
221 * Application ID we listen for.
223 struct GNUNET_HashCode app_id;
226 * Time to wait until we try to reconnect on failure.
228 struct GNUNET_TIME_Relative reconnect_backoff;
231 * Task for reconnecting when the listener fails.
233 struct GNUNET_SCHEDULER_Task *reconnect_task;
236 * Operation we listen for.
238 enum GNUNET_SET_OperationType operation;
242 /* mutual recursion with handle_copy_lazy */
243 static struct GNUNET_SET_Handle *
244 create_internal (const struct GNUNET_CONFIGURATION_Handle *cfg,
245 enum GNUNET_SET_OperationType op,
250 * Handle element for iteration over the set. Notifies the
251 * iterator and sends an acknowledgement to the service.
253 * @param cls the `struct GNUNET_SET_Handle *`
254 * @param mh the message
257 handle_copy_lazy (void *cls,
258 const struct GNUNET_MessageHeader *mh)
260 struct GNUNET_SET_CopyLazyResponseMessage *msg;
261 struct GNUNET_SET_Handle *set = cls;
262 struct SetCopyRequest *req;
263 struct GNUNET_SET_Handle *new_set;
265 msg = (struct GNUNET_SET_CopyLazyResponseMessage *) mh;
267 req = set->copy_req_head;
271 /* Service sent us unsolicited lazy copy response */
276 LOG (GNUNET_ERROR_TYPE_DEBUG,
277 "Handling response to lazy copy\n");
279 GNUNET_CONTAINER_DLL_remove (set->copy_req_head,
284 // We pass none as operation here, since it doesn't matter when
286 new_set = create_internal (set->cfg, GNUNET_SET_OPERATION_NONE, &msg->cookie);
288 req->cb (req->cls, new_set);
295 * Handle element for iteration over the set. Notifies the
296 * iterator and sends an acknowledgement to the service.
298 * @param cls the `struct GNUNET_SET_Handle *`
299 * @param mh the message
302 handle_iter_element (void *cls,
303 const struct GNUNET_MessageHeader *mh)
305 struct GNUNET_SET_Handle *set = cls;
306 GNUNET_SET_ElementIterator iter = set->iterator;
307 struct GNUNET_SET_Element element;
308 const struct GNUNET_SET_IterResponseMessage *msg;
309 struct GNUNET_SET_IterAckMessage *ack_msg;
310 struct GNUNET_MQ_Envelope *ev;
313 msize = ntohs (mh->size);
314 if (msize < sizeof (sizeof (struct GNUNET_SET_IterResponseMessage)))
316 /* message malformed */
318 set->iterator = NULL;
320 iter (set->iterator_cls,
324 msg = (const struct GNUNET_SET_IterResponseMessage *) mh;
325 if (set->iteration_id != ntohs (msg->iteration_id))
327 /* element from a previous iteration, skip! */
332 element.size = msize - sizeof (struct GNUNET_SET_IterResponseMessage);
333 element.element_type = ntohs (msg->element_type);
334 element.data = &msg[1];
335 iter (set->iterator_cls,
338 ev = GNUNET_MQ_msg (ack_msg,
339 GNUNET_MESSAGE_TYPE_SET_ITER_ACK);
340 ack_msg->send_more = htonl ((NULL != iter));
341 GNUNET_MQ_send (set->mq, ev);
346 * Handle message signalling conclusion of iteration over the set.
347 * Notifies the iterator that we are done.
350 * @param mh the message
353 handle_iter_done (void *cls,
354 const struct GNUNET_MessageHeader *mh)
356 struct GNUNET_SET_Handle *set = cls;
357 GNUNET_SET_ElementIterator iter = set->iterator;
361 set->iterator = NULL;
363 iter (set->iterator_cls,
369 * Handle result message for a set operation.
372 * @param mh the message
375 handle_result (void *cls,
376 const struct GNUNET_MessageHeader *mh)
378 struct GNUNET_SET_Handle *set = cls;
379 const struct GNUNET_SET_ResultMessage *msg;
380 struct GNUNET_SET_OperationHandle *oh;
381 struct GNUNET_SET_Element e;
382 enum GNUNET_SET_Status result_status;
384 msg = (const struct GNUNET_SET_ResultMessage *) mh;
385 GNUNET_assert (NULL != set->mq);
386 result_status = ntohs (msg->result_status);
387 LOG (GNUNET_ERROR_TYPE_DEBUG,
388 "Got result message with status %d\n",
391 oh = GNUNET_MQ_assoc_get (set->mq,
392 ntohl (msg->request_id));
395 /* 'oh' can be NULL if we canceled the operation, but the service
396 did not get the cancel message yet. */
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "Ignoring result from canceled operation\n");
402 switch (result_status)
404 case GNUNET_SET_STATUS_OK:
405 case GNUNET_SET_STATUS_ADD_LOCAL:
406 case GNUNET_SET_STATUS_ADD_REMOTE:
408 case GNUNET_SET_STATUS_FAILURE:
409 case GNUNET_SET_STATUS_DONE:
411 case GNUNET_SET_STATUS_HALF_DONE:
412 /* not used anymore */
417 LOG (GNUNET_ERROR_TYPE_DEBUG,
418 "Treating result as final status\n");
419 GNUNET_MQ_assoc_remove (set->mq,
420 ntohl (msg->request_id));
421 GNUNET_CONTAINER_DLL_remove (set->ops_head,
424 if (NULL != oh->result_cb)
426 oh->result_cb (oh->result_cls,
432 LOG (GNUNET_ERROR_TYPE_DEBUG,
433 "No callback for final status\n");
435 if ( (GNUNET_YES == set->destroy_requested) &&
436 (NULL == set->ops_head) )
437 GNUNET_SET_destroy (set);
442 LOG (GNUNET_ERROR_TYPE_DEBUG,
443 "Treating result as element\n");
445 e.size = ntohs (mh->size) - sizeof (struct GNUNET_SET_ResultMessage);
446 e.element_type = ntohs (msg->element_type);
447 if (NULL != oh->result_cb)
448 oh->result_cb (oh->result_cls,
455 * Destroy the given set operation.
457 * @param oh set operation to destroy
460 set_operation_destroy (struct GNUNET_SET_OperationHandle *oh)
462 struct GNUNET_SET_Handle *set = oh->set;
463 struct GNUNET_SET_OperationHandle *h_assoc;
465 if (NULL != oh->conclude_mqm)
466 GNUNET_MQ_discard (oh->conclude_mqm);
467 /* is the operation already commited? */
470 GNUNET_CONTAINER_DLL_remove (set->ops_head,
473 h_assoc = GNUNET_MQ_assoc_remove (set->mq,
475 GNUNET_assert ((NULL == h_assoc) || (h_assoc == oh));
482 * Cancel the given set operation. We need to send an explicit cancel
483 * message, as all operations one one set communicate using one
486 * @param oh set operation to cancel
489 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
491 struct GNUNET_SET_Handle *set = oh->set;
492 struct GNUNET_SET_CancelMessage *m;
493 struct GNUNET_MQ_Envelope *mqm;
497 mqm = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_SET_CANCEL);
498 m->request_id = htonl (oh->request_id);
499 GNUNET_MQ_send (set->mq, mqm);
501 set_operation_destroy (oh);
502 if ( (NULL != set) &&
503 (GNUNET_YES == set->destroy_requested) &&
504 (NULL == set->ops_head) )
506 LOG (GNUNET_ERROR_TYPE_DEBUG,
507 "Destroying set after operation cancel\n");
508 GNUNET_SET_destroy (set);
514 * We encountered an error communicating with the set service while
515 * performing a set operation. Report to the application.
517 * @param cls the `struct GNUNET_SET_Handle`
518 * @param error error code
521 handle_client_set_error (void *cls,
522 enum GNUNET_MQ_Error error)
524 struct GNUNET_SET_Handle *set = cls;
526 LOG (GNUNET_ERROR_TYPE_DEBUG,
527 "Handling client set error %d\n",
529 while (NULL != set->ops_head)
531 if (NULL != set->ops_head->result_cb)
532 set->ops_head->result_cb (set->ops_head->result_cls,
534 GNUNET_SET_STATUS_FAILURE);
535 set_operation_destroy (set->ops_head);
537 set->invalid = GNUNET_YES;
538 if (GNUNET_YES == set->destroy_requested)
540 LOG (GNUNET_ERROR_TYPE_DEBUG,
541 "Destroying set after operation failure\n");
542 GNUNET_SET_destroy (set);
547 static struct GNUNET_SET_Handle *
548 create_internal (const struct GNUNET_CONFIGURATION_Handle *cfg,
549 enum GNUNET_SET_OperationType op,
552 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
554 GNUNET_MESSAGE_TYPE_SET_RESULT,
556 { &handle_iter_element,
557 GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT,
560 GNUNET_MESSAGE_TYPE_SET_ITER_DONE,
561 sizeof (struct GNUNET_MessageHeader) },
563 GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_RESPONSE,
564 sizeof (struct GNUNET_SET_CopyLazyResponseMessage) },
565 GNUNET_MQ_HANDLERS_END
567 struct GNUNET_SET_Handle *set;
568 struct GNUNET_MQ_Envelope *mqm;
569 struct GNUNET_SET_CreateMessage *create_msg;
570 struct GNUNET_SET_CopyLazyConnectMessage *copy_msg;
572 set = GNUNET_new (struct GNUNET_SET_Handle);
573 set->client = GNUNET_CLIENT_connect ("set", cfg);
575 if (NULL == set->client)
580 set->mq = GNUNET_MQ_queue_for_connection_client (set->client,
582 &handle_client_set_error,
584 GNUNET_assert (NULL != set->mq);
588 LOG (GNUNET_ERROR_TYPE_DEBUG,
589 "Creating new set (operation %u)\n",
591 mqm = GNUNET_MQ_msg (create_msg,
592 GNUNET_MESSAGE_TYPE_SET_CREATE);
593 create_msg->operation = htonl (op);
597 LOG (GNUNET_ERROR_TYPE_DEBUG,
598 "Creating new set (lazy copy)\n",
600 mqm = GNUNET_MQ_msg (copy_msg,
601 GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_CONNECT);
602 copy_msg->cookie = *cookie;
604 GNUNET_MQ_send (set->mq, mqm);
610 * Create an empty set, supporting the specified operation.
612 * @param cfg configuration to use for connecting to the
614 * @param op operation supported by the set
615 * Note that the operation has to be specified
616 * beforehand, as certain set operations need to maintain
617 * data structures spefific to the operation
618 * @return a handle to the set
620 struct GNUNET_SET_Handle *
621 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
622 enum GNUNET_SET_OperationType op)
624 return create_internal (cfg, op, NULL);
629 * Add an element to the given set. After the element has been added
630 * (in the sense of being transmitted to the set service), @a cont
631 * will be called. Multiple calls to GNUNET_SET_add_element() can be
634 * @param set set to add element to
635 * @param element element to add to the set
636 * @param cont continuation called after the element has been added
637 * @param cont_cls closure for @a cont
638 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
639 * set is invalid (e.g. the set service crashed)
642 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
643 const struct GNUNET_SET_Element *element,
644 GNUNET_SET_Continuation cont,
647 struct GNUNET_MQ_Envelope *mqm;
648 struct GNUNET_SET_ElementMessage *msg;
650 if (GNUNET_YES == set->invalid)
654 return GNUNET_SYSERR;
656 mqm = GNUNET_MQ_msg_extra (msg, element->size,
657 GNUNET_MESSAGE_TYPE_SET_ADD);
658 msg->element_type = htons (element->element_type);
662 GNUNET_MQ_notify_sent (mqm,
664 GNUNET_MQ_send (set->mq, mqm);
670 * Remove an element to the given set. After the element has been
671 * removed (in the sense of the request being transmitted to the set
672 * service), @a cont will be called. Multiple calls to
673 * GNUNET_SET_remove_element() can be queued
675 * @param set set to remove element from
676 * @param element element to remove from the set
677 * @param cont continuation called after the element has been removed
678 * @param cont_cls closure for @a cont
679 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
680 * set is invalid (e.g. the set service crashed)
683 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
684 const struct GNUNET_SET_Element *element,
685 GNUNET_SET_Continuation cont,
688 struct GNUNET_MQ_Envelope *mqm;
689 struct GNUNET_SET_ElementMessage *msg;
691 if (GNUNET_YES == set->invalid)
695 return GNUNET_SYSERR;
697 mqm = GNUNET_MQ_msg_extra (msg,
699 GNUNET_MESSAGE_TYPE_SET_REMOVE);
700 msg->element_type = htons (element->element_type);
704 GNUNET_MQ_notify_sent (mqm,
706 GNUNET_MQ_send (set->mq, mqm);
712 * Destroy the set handle if no operations are left, mark the set
713 * for destruction otherwise.
715 * @param set set handle to destroy
718 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set)
720 /* destroying set while iterator is active is currently
721 not supported; we should expand the API to allow
722 clients to explicitly cancel the iteration! */
723 GNUNET_assert (NULL == set->iterator);
724 if (NULL != set->ops_head)
726 LOG (GNUNET_ERROR_TYPE_DEBUG,
727 "Set operations are pending, delaying set destruction\n");
728 set->destroy_requested = GNUNET_YES;
731 LOG (GNUNET_ERROR_TYPE_DEBUG,
732 "Really destroying set\n");
733 if (NULL != set->client)
735 GNUNET_CLIENT_disconnect (set->client);
740 GNUNET_MQ_destroy (set->mq);
748 * Prepare a set operation to be evaluated with another peer.
749 * The evaluation will not start until the client provides
750 * a local set with #GNUNET_SET_commit().
752 * @param other_peer peer with the other set
753 * @param app_id hash for the application using the set
754 * @param context_msg additional information for the request
755 * @param result_mode specified how results will be returned,
756 * see `enum GNUNET_SET_ResultMode`.
757 * @param result_cb called on error or success
758 * @param result_cls closure for @e result_cb
759 * @return a handle to cancel the operation
761 struct GNUNET_SET_OperationHandle *
762 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
763 const struct GNUNET_HashCode *app_id,
764 const struct GNUNET_MessageHeader *context_msg,
765 enum GNUNET_SET_ResultMode result_mode,
766 GNUNET_SET_ResultIterator result_cb,
769 struct GNUNET_MQ_Envelope *mqm;
770 struct GNUNET_SET_OperationHandle *oh;
771 struct GNUNET_SET_EvaluateMessage *msg;
773 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
774 oh->result_cb = result_cb;
775 oh->result_cls = result_cls;
776 mqm = GNUNET_MQ_msg_nested_mh (msg,
777 GNUNET_MESSAGE_TYPE_SET_EVALUATE,
779 msg->app_id = *app_id;
780 msg->result_mode = htonl (result_mode);
781 msg->target_peer = *other_peer;
782 oh->conclude_mqm = mqm;
783 oh->request_id_addr = &msg->request_id;
790 * Connect to the set service in order to listen for requests.
792 * @param cls the `struct GNUNET_SET_ListenHandle *` to connect
795 listen_connect (void *cls);
799 * Handle request message for a listen operation
801 * @param cls the listen handle
802 * @param mh the message
805 handle_request (void *cls,
806 const struct GNUNET_MessageHeader *mh)
808 struct GNUNET_SET_ListenHandle *lh = cls;
809 const struct GNUNET_SET_RequestMessage *msg;
810 struct GNUNET_SET_Request req;
811 const struct GNUNET_MessageHeader *context_msg;
813 struct GNUNET_MQ_Envelope *mqm;
814 struct GNUNET_SET_RejectMessage *rmsg;
816 LOG (GNUNET_ERROR_TYPE_DEBUG,
817 "Processing incoming operation request\n");
818 msize = ntohs (mh->size);
819 if (msize < sizeof (struct GNUNET_SET_RequestMessage))
822 GNUNET_CLIENT_disconnect (lh->client);
824 GNUNET_MQ_destroy (lh->mq);
826 lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff,
827 &listen_connect, lh);
828 lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff);
831 /* we got another valid request => reset the backoff */
832 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
833 msg = (const struct GNUNET_SET_RequestMessage *) mh;
834 req.accept_id = ntohl (msg->accept_id);
835 req.accepted = GNUNET_NO;
836 context_msg = GNUNET_MQ_extract_nested_mh (msg);
837 /* calling #GNUNET_SET_accept() in the listen cb will set req->accepted */
838 lh->listen_cb (lh->listen_cls,
842 if (GNUNET_YES == req.accepted)
843 return; /* the accept-case is handled in #GNUNET_SET_accept() */
844 LOG (GNUNET_ERROR_TYPE_DEBUG,
845 "Rejecting request\n");
846 mqm = GNUNET_MQ_msg (rmsg,
847 GNUNET_MESSAGE_TYPE_SET_REJECT);
848 rmsg->accept_reject_id = msg->accept_id;
849 GNUNET_MQ_send (lh->mq, mqm);
854 * Our connection with the set service encountered an error,
855 * re-initialize with exponential back-off.
857 * @param cls the `struct GNUNET_SET_ListenHandle *`
858 * @param error reason for the disconnect
861 handle_client_listener_error (void *cls,
862 enum GNUNET_MQ_Error error)
864 struct GNUNET_SET_ListenHandle *lh = cls;
866 LOG (GNUNET_ERROR_TYPE_DEBUG,
867 "Listener broke down (%d), re-connecting\n",
869 GNUNET_CLIENT_disconnect (lh->client);
871 GNUNET_MQ_destroy (lh->mq);
873 lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff,
874 &listen_connect, lh);
875 lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff);
880 * Connect to the set service in order to listen for requests.
882 * @param cls the `struct GNUNET_SET_ListenHandle *` to connect
885 listen_connect (void *cls)
887 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
888 { &handle_request, GNUNET_MESSAGE_TYPE_SET_REQUEST },
889 GNUNET_MQ_HANDLERS_END
891 struct GNUNET_SET_ListenHandle *lh = cls;
892 struct GNUNET_MQ_Envelope *mqm;
893 struct GNUNET_SET_ListenMessage *msg;
895 lh->reconnect_task = NULL;
896 GNUNET_assert (NULL == lh->client);
897 lh->client = GNUNET_CLIENT_connect ("set", lh->cfg);
898 if (NULL == lh->client)
900 GNUNET_assert (NULL == lh->mq);
901 lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client,
903 &handle_client_listener_error, lh);
904 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
905 msg->operation = htonl (lh->operation);
906 msg->app_id = lh->app_id;
907 GNUNET_MQ_send (lh->mq, mqm);
912 * Wait for set operation requests for the given application id
914 * @param cfg configuration to use for connecting to
915 * the set service, needs to be valid for the lifetime of the listen handle
916 * @param operation operation we want to listen for
917 * @param app_id id of the application that handles set operation requests
918 * @param listen_cb called for each incoming request matching the operation
920 * @param listen_cls handle for @a listen_cb
921 * @return a handle that can be used to cancel the listen operation
923 struct GNUNET_SET_ListenHandle *
924 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
925 enum GNUNET_SET_OperationType operation,
926 const struct GNUNET_HashCode *app_id,
927 GNUNET_SET_ListenCallback listen_cb,
930 struct GNUNET_SET_ListenHandle *lh;
932 lh = GNUNET_new (struct GNUNET_SET_ListenHandle);
933 lh->listen_cb = listen_cb;
934 lh->listen_cls = listen_cls;
936 lh->operation = operation;
937 lh->app_id = *app_id;
938 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
940 if (NULL == lh->client)
950 * Cancel the given listen operation.
952 * @param lh handle for the listen operation
955 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh)
957 LOG (GNUNET_ERROR_TYPE_DEBUG,
958 "Canceling listener\n");
961 GNUNET_MQ_destroy (lh->mq);
964 if (NULL != lh->client)
966 GNUNET_CLIENT_disconnect (lh->client);
969 if (NULL != lh->reconnect_task)
971 GNUNET_SCHEDULER_cancel (lh->reconnect_task);
972 lh->reconnect_task = NULL;
979 * Accept a request we got via #GNUNET_SET_listen. Must be called during
980 * #GNUNET_SET_listen, as the 'struct GNUNET_SET_Request' becomes invalid
982 * Call #GNUNET_SET_commit to provide the local set to use for the operation,
983 * and to begin the exchange with the remote peer.
985 * @param request request to accept
986 * @param result_mode specified how results will be returned,
987 * see `enum GNUNET_SET_ResultMode`.
988 * @param result_cb callback for the results
989 * @param result_cls closure for @a result_cb
990 * @return a handle to cancel the operation
992 struct GNUNET_SET_OperationHandle *
993 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
994 enum GNUNET_SET_ResultMode result_mode,
995 GNUNET_SET_ResultIterator result_cb,
998 struct GNUNET_MQ_Envelope *mqm;
999 struct GNUNET_SET_OperationHandle *oh;
1000 struct GNUNET_SET_AcceptMessage *msg;
1002 GNUNET_assert (GNUNET_NO == request->accepted);
1003 request->accepted = GNUNET_YES;
1004 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_ACCEPT);
1005 msg->accept_reject_id = htonl (request->accept_id);
1006 msg->result_mode = htonl (result_mode);
1007 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
1008 oh->result_cb = result_cb;
1009 oh->result_cls = result_cls;
1010 oh->conclude_mqm = mqm;
1011 oh->request_id_addr = &msg->request_id;
1017 * Commit a set to be used with a set operation.
1018 * This function is called once we have fully constructed
1019 * the set that we want to use for the operation. At this
1020 * time, the P2P protocol can then begin to exchange the
1021 * set information and call the result callback with the
1022 * result information.
1024 * @param oh handle to the set operation
1025 * @param set the set to use for the operation
1026 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
1027 * set is invalid (e.g. the set service crashed)
1030 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
1031 struct GNUNET_SET_Handle *set)
1033 if (NULL != oh->set)
1035 /* Some other set was already commited for this
1036 * operation, there is a logic bug in the client of this API */
1040 if (GNUNET_YES == set->invalid)
1041 return GNUNET_SYSERR;
1042 GNUNET_assert (NULL != oh->conclude_mqm);
1044 GNUNET_CONTAINER_DLL_insert (set->ops_head,
1047 oh->request_id = GNUNET_MQ_assoc_add (set->mq, oh);
1048 *oh->request_id_addr = htonl (oh->request_id);
1049 GNUNET_MQ_send (set->mq, oh->conclude_mqm);
1050 oh->conclude_mqm = NULL;
1051 oh->request_id_addr = NULL;
1057 * Iterate over all elements in the given set. Note that this
1058 * operation involves transferring every element of the set from the
1059 * service to the client, and is thus costly.
1061 * @param set the set to iterate over
1062 * @param iter the iterator to call for each element
1063 * @param iter_cls closure for @a iter
1064 * @return #GNUNET_YES if the iteration started successfuly,
1065 * #GNUNET_NO if another iteration is active
1066 * #GNUNET_SYSERR if the set is invalid (e.g. the server crashed, disconnected)
1069 GNUNET_SET_iterate (struct GNUNET_SET_Handle *set,
1070 GNUNET_SET_ElementIterator iter,
1073 struct GNUNET_MQ_Envelope *ev;
1075 GNUNET_assert (NULL != iter);
1076 if (GNUNET_YES == set->invalid)
1077 return GNUNET_SYSERR;
1078 if (NULL != set->iterator)
1080 LOG (GNUNET_ERROR_TYPE_DEBUG,
1081 "Iterating over set\n");
1082 set->iterator = iter;
1083 set->iterator_cls = iter_cls;
1084 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST);
1085 GNUNET_MQ_send (set->mq, ev);
1091 GNUNET_SET_copy_lazy (struct GNUNET_SET_Handle *set,
1092 GNUNET_SET_CopyReadyCallback cb,
1095 struct GNUNET_MQ_Envelope *ev;
1096 struct SetCopyRequest *req;
1098 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_COPY_LAZY_PREPARE);
1099 GNUNET_MQ_send (set->mq, ev);
1101 req = GNUNET_new (struct SetCopyRequest);
1104 GNUNET_CONTAINER_DLL_insert (set->copy_req_head,
1111 * Create a copy of an element. The copy
1112 * must be GNUNET_free-d by the caller.
1114 * @param element the element to copy
1115 * @return the copied element
1117 struct GNUNET_SET_Element *
1118 GNUNET_SET_element_dup (const struct GNUNET_SET_Element *element)
1120 struct GNUNET_SET_Element *copy;
1122 copy = GNUNET_malloc (element->size + sizeof (struct GNUNET_SET_Element));
1123 copy->size = element->size;
1124 copy->element_type = element->element_type;
1125 copy->data = ©[1];
1126 memcpy ((void *) copy->data, element->data, copy->size);
1133 * Hash a set element.
1135 * @param element the element that should be hashed
1136 * @param ret_hash a pointer to where the hash of @a element
1140 GNUNET_SET_element_hash (const struct GNUNET_SET_Element *element, struct GNUNET_HashCode *ret_hash)
1142 struct GNUNET_HashContext *ctx = GNUNET_CRYPTO_hash_context_start ();
1144 /* It's not guaranteed that the element data is always after the element header,
1145 so we need to hash the chunks separately. */
1146 GNUNET_CRYPTO_hash_context_read (ctx, &element->size, sizeof (uint16_t));
1147 GNUNET_CRYPTO_hash_context_read (ctx, &element->element_type, sizeof (uint16_t));
1148 GNUNET_CRYPTO_hash_context_read (ctx, element->data, element->size);
1149 GNUNET_CRYPTO_hash_context_finish (ctx, ret_hash);
1152 /* end of set_api.c */