2 This file is part of GNUnet.
3 (C) 2012-2014 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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__)
37 * Opaque handle to a set.
39 struct GNUNET_SET_Handle
42 * Client connected to the set service.
44 struct GNUNET_CLIENT_Connection *client;
47 * Message queue for @e client.
49 struct GNUNET_MQ_Handle *mq;
52 * Linked list of operations on the set.
54 struct GNUNET_SET_OperationHandle *ops_head;
57 * Linked list of operations on the set.
59 struct GNUNET_SET_OperationHandle *ops_tail;
62 * Callback for the current iteration over the set,
63 * NULL if no iterator is active.
65 GNUNET_SET_ElementIterator iterator;
68 * Closure for @e iterator
73 * Should the set be destroyed once all operations are gone?
75 int destroy_requested;
78 * Has the set become invalid (e.g. service died)?
83 * Both client and service count the number of iterators
84 * created so far to match replies with iterators.
86 uint16_t iteration_id;
91 * Handle for a set operation request from another peer.
93 struct GNUNET_SET_Request
96 * Id of the request, used to identify the request when
97 * accepting/rejecting it.
102 * Has the request been accepted already?
103 * #GNUNET_YES/#GNUNET_NO
110 * Handle to an operation. Only known to the service after committing
111 * the handle with a set.
113 struct GNUNET_SET_OperationHandle
116 * Function to be called when we have a result,
119 GNUNET_SET_ResultIterator result_cb;
122 * Closure for @e result_cb.
127 * Local set used for the operation,
128 * NULL if no set has been provided by conclude yet.
130 struct GNUNET_SET_Handle *set;
133 * Message sent to the server on calling conclude,
134 * NULL if conclude has been called.
136 struct GNUNET_MQ_Envelope *conclude_mqm;
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.
142 uint32_t *request_id_addr;
145 * Handles are kept in a linked list.
147 struct GNUNET_SET_OperationHandle *prev;
150 * Handles are kept in a linked list.
152 struct GNUNET_SET_OperationHandle *next;
155 * Request ID to identify the operation within the set.
162 * Opaque handle to a listen operation.
164 struct GNUNET_SET_ListenHandle
167 * Connection to the service.
169 struct GNUNET_CLIENT_Connection *client;
172 * Message queue for the client.
174 struct GNUNET_MQ_Handle* mq;
177 * Configuration handle for the listener, stored
178 * here to be able to reconnect transparently on
179 * connection failure.
181 const struct GNUNET_CONFIGURATION_Handle *cfg;
184 * Function to call on a new incoming request,
187 GNUNET_SET_ListenCallback listen_cb;
190 * Closure for @e listen_cb.
195 * Application ID we listen for.
197 struct GNUNET_HashCode app_id;
200 * Time to wait until we try to reconnect on failure.
202 struct GNUNET_TIME_Relative reconnect_backoff;
205 * Task for reconnecting when the listener fails.
207 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
210 * Operation we listen for.
212 enum GNUNET_SET_OperationType operation;
217 * Handle element for iteration over the set. Notifies the
218 * iterator and sends an acknowledgement to the service.
220 * @param cls the `struct GNUNET_SET_Handle *`
221 * @param mh the message
224 handle_iter_element (void *cls,
225 const struct GNUNET_MessageHeader *mh)
227 struct GNUNET_SET_Handle *set = cls;
228 GNUNET_SET_ElementIterator iter = set->iterator;
229 struct GNUNET_SET_Element element;
230 const struct GNUNET_SET_IterResponseMessage *msg;
231 struct GNUNET_SET_IterAckMessage *ack_msg;
232 struct GNUNET_MQ_Envelope *ev;
235 msize = ntohs (mh->size);
236 if (msize < sizeof (sizeof (struct GNUNET_SET_IterResponseMessage)))
238 /* message malformed */
240 set->iterator = NULL;
242 iter (set->iterator_cls,
246 msg = (const struct GNUNET_SET_IterResponseMessage *) mh;
247 if (set->iteration_id != ntohs (msg->iteration_id))
249 /* element from a previous iteration, skip! */
254 element.size = msize - sizeof (struct GNUNET_SET_IterResponseMessage);
255 element.element_type = htons (msg->element_type);
256 element.data = &msg[1];
257 iter (set->iterator_cls,
260 ev = GNUNET_MQ_msg (ack_msg,
261 GNUNET_MESSAGE_TYPE_SET_ITER_ACK);
262 ack_msg->send_more = htonl ((NULL != iter));
263 GNUNET_MQ_send (set->mq, ev);
268 * Handle message signalling conclusion of iteration over the set.
269 * Notifies the iterator that we are done.
272 * @param mh the message
275 handle_iter_done (void *cls,
276 const struct GNUNET_MessageHeader *mh)
278 struct GNUNET_SET_Handle *set = cls;
279 GNUNET_SET_ElementIterator iter = set->iterator;
283 set->iterator = NULL;
285 iter (set->iterator_cls,
291 * Handle result message for a set operation.
294 * @param mh the message
297 handle_result (void *cls,
298 const struct GNUNET_MessageHeader *mh)
300 struct GNUNET_SET_Handle *set = cls;
301 const struct GNUNET_SET_ResultMessage *msg;
302 struct GNUNET_SET_OperationHandle *oh;
303 struct GNUNET_SET_Element e;
304 enum GNUNET_SET_Status result_status;
306 msg = (const struct GNUNET_SET_ResultMessage *) mh;
307 GNUNET_assert (NULL != set->mq);
308 result_status = ntohs (msg->result_status);
309 oh = GNUNET_MQ_assoc_get (set->mq,
310 ntohl (msg->request_id));
313 /* 'oh' can be NULL if we canceled the operation, but the service
314 did not get the cancel message yet. */
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Ignoring result from canceled operation\n");
319 if (GNUNET_SET_STATUS_OK != result_status)
321 /* status is not #GNUNET_SET_STATUS_OK => there's no attached element,
322 * and this is the last result message we get */
323 GNUNET_MQ_assoc_remove (set->mq, ntohl (msg->request_id));
324 GNUNET_CONTAINER_DLL_remove (set->ops_head,
327 if ( (GNUNET_YES == set->destroy_requested) &&
328 (NULL == set->ops_head) )
329 GNUNET_SET_destroy (set);
330 if (NULL != oh->result_cb)
331 oh->result_cb (oh->result_cls,
338 e.size = ntohs (mh->size) - sizeof (struct GNUNET_SET_ResultMessage);
339 e.element_type = msg->element_type;
340 if (NULL != oh->result_cb)
341 oh->result_cb (oh->result_cls,
348 * Destroy the given set operation.
350 * @param oh set operation to destroy
353 set_operation_destroy (struct GNUNET_SET_OperationHandle *oh)
355 struct GNUNET_SET_Handle *set = oh->set;
356 struct GNUNET_SET_OperationHandle *h_assoc;
358 if (NULL != oh->conclude_mqm)
359 GNUNET_MQ_discard (oh->conclude_mqm);
360 /* is the operation already commited? */
363 GNUNET_CONTAINER_DLL_remove (set->ops_head,
366 h_assoc = GNUNET_MQ_assoc_remove (set->mq,
368 GNUNET_assert ((NULL == h_assoc) || (h_assoc == oh));
375 * Cancel the given set operation. We need to send an explicit cancel
376 * message, as all operations one one set communicate using one
379 * @param oh set operation to cancel
382 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh)
384 struct GNUNET_SET_Handle *set = oh->set;
385 struct GNUNET_SET_CancelMessage *m;
386 struct GNUNET_MQ_Envelope *mqm;
390 mqm = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_SET_CANCEL);
391 m->request_id = htonl (oh->request_id);
392 GNUNET_MQ_send (set->mq, mqm);
394 set_operation_destroy (oh);
395 if ( (NULL != set) &&
396 (GNUNET_YES == set->destroy_requested) &&
397 (NULL == set->ops_head) )
399 LOG (GNUNET_ERROR_TYPE_DEBUG,
400 "Destroying set after operation cancel\n");
401 GNUNET_SET_destroy (set);
407 * We encountered an error communicating with the set service while
408 * performing a set operation. Report to the application.
410 * @param cls the `struct GNUNET_SET_Handle`
411 * @param error error code
414 handle_client_set_error (void *cls,
415 enum GNUNET_MQ_Error error)
417 struct GNUNET_SET_Handle *set = cls;
419 LOG (GNUNET_ERROR_TYPE_DEBUG,
420 "Handling client set error\n");
421 while (NULL != set->ops_head)
423 if (NULL != set->ops_head->result_cb)
424 set->ops_head->result_cb (set->ops_head->result_cls,
426 GNUNET_SET_STATUS_FAILURE);
427 set_operation_destroy (set->ops_head);
429 set->invalid = GNUNET_YES;
430 if (GNUNET_YES == set->destroy_requested)
432 LOG (GNUNET_ERROR_TYPE_DEBUG,
433 "Destroying set after operation failure\n");
434 GNUNET_SET_destroy (set);
440 * Create an empty set, supporting the specified operation.
442 * @param cfg configuration to use for connecting to the
444 * @param op operation supported by the set
445 * Note that the operation has to be specified
446 * beforehand, as certain set operations need to maintain
447 * data structures spefific to the operation
448 * @return a handle to the set
450 struct GNUNET_SET_Handle *
451 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
452 enum GNUNET_SET_OperationType op)
454 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
456 GNUNET_MESSAGE_TYPE_SET_RESULT,
458 { &handle_iter_element,
459 GNUNET_MESSAGE_TYPE_SET_ITER_ELEMENT,
462 GNUNET_MESSAGE_TYPE_SET_ITER_DONE,
463 sizeof (struct GNUNET_MessageHeader) },
464 GNUNET_MQ_HANDLERS_END
466 struct GNUNET_SET_Handle *set;
467 struct GNUNET_MQ_Envelope *mqm;
468 struct GNUNET_SET_CreateMessage *msg;
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "Creating new set (operation %u)\n",
473 set = GNUNET_new (struct GNUNET_SET_Handle);
474 set->client = GNUNET_CLIENT_connect ("set", cfg);
475 if (NULL == set->client)
480 set->mq = GNUNET_MQ_queue_for_connection_client (set->client,
482 &handle_client_set_error, set);
483 GNUNET_assert (NULL != set->mq);
484 mqm = GNUNET_MQ_msg (msg,
485 GNUNET_MESSAGE_TYPE_SET_CREATE);
486 msg->operation = htonl (op);
487 GNUNET_MQ_send (set->mq, mqm);
493 * Add an element to the given set. After the element has been added
494 * (in the sense of being transmitted to the set service), @a cont
495 * will be called. Multiple calls to GNUNET_SET_add_element() can be
498 * @param set set to add element to
499 * @param element element to add to the set
500 * @param cont continuation called after the element has been added
501 * @param cont_cls closure for @a cont
502 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
503 * set is invalid (e.g. the set service crashed)
506 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
507 const struct GNUNET_SET_Element *element,
508 GNUNET_SET_Continuation cont,
511 struct GNUNET_MQ_Envelope *mqm;
512 struct GNUNET_SET_ElementMessage *msg;
514 if (GNUNET_YES == set->invalid)
518 return GNUNET_SYSERR;
520 mqm = GNUNET_MQ_msg_extra (msg, element->size,
521 GNUNET_MESSAGE_TYPE_SET_ADD);
522 msg->element_type = element->element_type;
526 GNUNET_MQ_notify_sent (mqm,
528 GNUNET_MQ_send (set->mq, mqm);
534 * Remove an element to the given set. After the element has been
535 * removed (in the sense of the request being transmitted to the set
536 * service), @a cont will be called. Multiple calls to
537 * GNUNET_SET_remove_element() can be queued
539 * @param set set to remove element from
540 * @param element element to remove from the set
541 * @param cont continuation called after the element has been removed
542 * @param cont_cls closure for @a cont
543 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
544 * set is invalid (e.g. the set service crashed)
547 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
548 const struct GNUNET_SET_Element *element,
549 GNUNET_SET_Continuation cont,
552 struct GNUNET_MQ_Envelope *mqm;
553 struct GNUNET_SET_ElementMessage *msg;
555 if (GNUNET_YES == set->invalid)
559 return GNUNET_SYSERR;
561 mqm = GNUNET_MQ_msg_extra (msg,
563 GNUNET_MESSAGE_TYPE_SET_REMOVE);
564 msg->element_type = element->element_type;
568 GNUNET_MQ_notify_sent (mqm,
570 GNUNET_MQ_send (set->mq, mqm);
576 * Destroy the set handle if no operations are left, mark the set
577 * for destruction otherwise.
579 * @param set set handle to destroy
582 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set)
584 /* destroying set while iterator is active is currently
585 not supported; we should expand the API to allow
586 clients to explicitly cancel the iteration! */
587 GNUNET_assert (NULL == set->iterator);
588 if (NULL != set->ops_head)
590 LOG (GNUNET_ERROR_TYPE_DEBUG,
591 "Set operations are pending, delaying set destruction\n");
592 set->destroy_requested = GNUNET_YES;
595 LOG (GNUNET_ERROR_TYPE_DEBUG,
596 "Really destroying set\n");
597 if (NULL != set->client)
599 GNUNET_CLIENT_disconnect (set->client);
604 GNUNET_MQ_destroy (set->mq);
612 * Prepare a set operation to be evaluated with another peer.
613 * The evaluation will not start until the client provides
614 * a local set with #GNUNET_SET_commit().
616 * @param other_peer peer with the other set
617 * @param app_id hash for the application using the set
618 * @param context_msg additional information for the request
619 * @param result_mode specified how results will be returned,
620 * see `enum GNUNET_SET_ResultMode`.
621 * @param result_cb called on error or success
622 * @param result_cls closure for @e result_cb
623 * @return a handle to cancel the operation
625 struct GNUNET_SET_OperationHandle *
626 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
627 const struct GNUNET_HashCode *app_id,
628 const struct GNUNET_MessageHeader *context_msg,
629 enum GNUNET_SET_ResultMode result_mode,
630 GNUNET_SET_ResultIterator result_cb,
633 struct GNUNET_MQ_Envelope *mqm;
634 struct GNUNET_SET_OperationHandle *oh;
635 struct GNUNET_SET_EvaluateMessage *msg;
637 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
638 oh->result_cb = result_cb;
639 oh->result_cls = result_cls;
640 mqm = GNUNET_MQ_msg_nested_mh (msg,
641 GNUNET_MESSAGE_TYPE_SET_EVALUATE,
643 msg->app_id = *app_id;
644 msg->result_mode = htonl (result_mode);
645 msg->target_peer = *other_peer;
646 oh->conclude_mqm = mqm;
647 oh->request_id_addr = &msg->request_id;
654 * Connect to the set service in order to listen for requests.
656 * @param cls the `struct GNUNET_SET_ListenHandle *` to connect
657 * @param tc task context if invoked as a task, NULL otherwise
660 listen_connect (void *cls,
661 const struct GNUNET_SCHEDULER_TaskContext *tc);
665 * Handle request message for a listen operation
667 * @param cls the listen handle
668 * @param mh the message
671 handle_request (void *cls,
672 const struct GNUNET_MessageHeader *mh)
674 struct GNUNET_SET_ListenHandle *lh = cls;
675 const struct GNUNET_SET_RequestMessage *msg;
676 struct GNUNET_SET_Request req;
677 const struct GNUNET_MessageHeader *context_msg;
679 struct GNUNET_MQ_Envelope *mqm;
680 struct GNUNET_SET_RejectMessage *rmsg;
682 LOG (GNUNET_ERROR_TYPE_DEBUG,
683 "Processing incoming operation request\n");
684 msize = ntohs (mh->size);
685 if (msize < sizeof (struct GNUNET_SET_RequestMessage))
688 GNUNET_CLIENT_disconnect (lh->client);
690 GNUNET_MQ_destroy (lh->mq);
692 lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff,
693 &listen_connect, lh);
694 lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff);
697 /* we got another valid request => reset the backoff */
698 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
699 msg = (const struct GNUNET_SET_RequestMessage *) mh;
700 req.accept_id = ntohl (msg->accept_id);
701 req.accepted = GNUNET_NO;
702 context_msg = GNUNET_MQ_extract_nested_mh (msg);
703 /* calling #GNUNET_SET_accept() in the listen cb will set req->accepted */
704 lh->listen_cb (lh->listen_cls,
708 if (GNUNET_YES == req.accepted)
709 return; /* the accept-case is handled in #GNUNET_SET_accept() */
710 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 "Rejecting request\n");
712 mqm = GNUNET_MQ_msg (rmsg,
713 GNUNET_MESSAGE_TYPE_SET_REJECT);
714 rmsg->accept_reject_id = msg->accept_id;
715 GNUNET_MQ_send (lh->mq, mqm);
720 * Our connection with the set service encountered an error,
721 * re-initialize with exponential back-off.
723 * @param cls the `struct GNUNET_SET_ListenHandle *`
724 * @param error reason for the disconnect
727 handle_client_listener_error (void *cls,
728 enum GNUNET_MQ_Error error)
730 struct GNUNET_SET_ListenHandle *lh = cls;
732 LOG (GNUNET_ERROR_TYPE_DEBUG,
733 "Listener broke down (%d), re-connecting\n",
735 GNUNET_CLIENT_disconnect (lh->client);
737 GNUNET_MQ_destroy (lh->mq);
739 lh->reconnect_task = GNUNET_SCHEDULER_add_delayed (lh->reconnect_backoff,
740 &listen_connect, lh);
741 lh->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (lh->reconnect_backoff);
746 * Connect to the set service in order to listen for requests.
748 * @param cls the `struct GNUNET_SET_ListenHandle *` to connect
749 * @param tc task context if invoked as a task, NULL otherwise
752 listen_connect (void *cls,
753 const struct GNUNET_SCHEDULER_TaskContext *tc)
755 static const struct GNUNET_MQ_MessageHandler mq_handlers[] = {
756 { &handle_request, GNUNET_MESSAGE_TYPE_SET_REQUEST },
757 GNUNET_MQ_HANDLERS_END
759 struct GNUNET_SET_ListenHandle *lh = cls;
760 struct GNUNET_MQ_Envelope *mqm;
761 struct GNUNET_SET_ListenMessage *msg;
764 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) )
766 LOG (GNUNET_ERROR_TYPE_DEBUG,
767 "Listener not reconnecting due to shutdown\n");
770 lh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
771 GNUNET_assert (NULL == lh->client);
772 lh->client = GNUNET_CLIENT_connect ("set", lh->cfg);
773 if (NULL == lh->client)
775 GNUNET_assert (NULL == lh->mq);
776 lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client,
778 &handle_client_listener_error, lh);
779 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
780 msg->operation = htonl (lh->operation);
781 msg->app_id = lh->app_id;
782 GNUNET_MQ_send (lh->mq, mqm);
787 * Wait for set operation requests for the given application id
789 * @param cfg configuration to use for connecting to
790 * the set service, needs to be valid for the lifetime of the listen handle
791 * @param operation operation we want to listen for
792 * @param app_id id of the application that handles set operation requests
793 * @param listen_cb called for each incoming request matching the operation
795 * @param listen_cls handle for @a listen_cb
796 * @return a handle that can be used to cancel the listen operation
798 struct GNUNET_SET_ListenHandle *
799 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
800 enum GNUNET_SET_OperationType operation,
801 const struct GNUNET_HashCode *app_id,
802 GNUNET_SET_ListenCallback listen_cb,
805 struct GNUNET_SET_ListenHandle *lh;
807 lh = GNUNET_new (struct GNUNET_SET_ListenHandle);
808 lh->listen_cb = listen_cb;
809 lh->listen_cls = listen_cls;
811 lh->operation = operation;
812 lh->app_id = *app_id;
813 lh->reconnect_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
814 listen_connect (lh, NULL);
815 if (NULL == lh->client)
825 * Cancel the given listen operation.
827 * @param lh handle for the listen operation
830 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh)
832 LOG (GNUNET_ERROR_TYPE_DEBUG,
833 "Canceling listener\n");
836 GNUNET_MQ_destroy (lh->mq);
839 if (NULL != lh->client)
841 GNUNET_CLIENT_disconnect (lh->client);
844 if (GNUNET_SCHEDULER_NO_TASK != lh->reconnect_task)
846 GNUNET_SCHEDULER_cancel (lh->reconnect_task);
847 lh->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
854 * Accept a request we got via #GNUNET_SET_listen. Must be called during
855 * #GNUNET_SET_listen, as the 'struct GNUNET_SET_Request' becomes invalid
857 * Call #GNUNET_SET_commit to provide the local set to use for the operation,
858 * and to begin the exchange with the remote peer.
860 * @param request request to accept
861 * @param result_mode specified how results will be returned,
862 * see `enum GNUNET_SET_ResultMode`.
863 * @param result_cb callback for the results
864 * @param result_cls closure for @a result_cb
865 * @return a handle to cancel the operation
867 struct GNUNET_SET_OperationHandle *
868 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
869 enum GNUNET_SET_ResultMode result_mode,
870 GNUNET_SET_ResultIterator result_cb,
873 struct GNUNET_MQ_Envelope *mqm;
874 struct GNUNET_SET_OperationHandle *oh;
875 struct GNUNET_SET_AcceptMessage *msg;
877 GNUNET_assert (GNUNET_NO == request->accepted);
878 request->accepted = GNUNET_YES;
879 mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_ACCEPT);
880 msg->accept_reject_id = htonl (request->accept_id);
881 msg->result_mode = htonl (result_mode);
882 oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
883 oh->result_cb = result_cb;
884 oh->result_cls = result_cls;
885 oh->conclude_mqm = mqm;
886 oh->request_id_addr = &msg->request_id;
892 * Commit a set to be used with a set operation.
893 * This function is called once we have fully constructed
894 * the set that we want to use for the operation. At this
895 * time, the P2P protocol can then begin to exchange the
896 * set information and call the result callback with the
897 * result information.
899 * @param oh handle to the set operation
900 * @param set the set to use for the operation
901 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
902 * set is invalid (e.g. the set service crashed)
905 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
906 struct GNUNET_SET_Handle *set)
908 GNUNET_assert (NULL == oh->set);
909 if (GNUNET_YES == set->invalid)
910 return GNUNET_SYSERR;
911 GNUNET_assert (NULL != oh->conclude_mqm);
913 GNUNET_CONTAINER_DLL_insert (set->ops_head,
916 oh->request_id = GNUNET_MQ_assoc_add (set->mq, oh);
917 *oh->request_id_addr = htonl (oh->request_id);
918 GNUNET_MQ_send (set->mq, oh->conclude_mqm);
919 oh->conclude_mqm = NULL;
920 oh->request_id_addr = NULL;
926 * Iterate over all elements in the given set. Note that this
927 * operation involves transferring every element of the set from the
928 * service to the client, and is thus costly.
930 * @param set the set to iterate over
931 * @param iter the iterator to call for each element
932 * @param iter_cls closure for @a iter
933 * @return #GNUNET_YES if the iteration started successfuly,
934 * #GNUNET_NO if another iteration is active
935 * #GNUNET_SYSERR if the set is invalid (e.g. the server crashed, disconnected)
938 GNUNET_SET_iterate (struct GNUNET_SET_Handle *set,
939 GNUNET_SET_ElementIterator iter,
942 struct GNUNET_MQ_Envelope *ev;
944 GNUNET_assert (NULL != iter);
945 if (GNUNET_YES == set->invalid)
946 return GNUNET_SYSERR;
947 if (NULL != set->iterator)
949 LOG (GNUNET_ERROR_TYPE_DEBUG,
950 "Iterating over set\n");
951 set->iterator = iter;
952 set->iterator_cls = iter_cls;
953 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_SET_ITER_REQUEST);
954 GNUNET_MQ_send (set->mq, ev);
960 * Stop iteration over all elements in the given set. Can only
961 * be called before the iteration has "naturally" completed its
964 * @param set the set to stop iterating over
967 GNUNET_SET_iterate_cancel (struct GNUNET_SET_Handle *set)
969 GNUNET_assert (NULL != set->iterator);
970 set->iterator = NULL;
975 /* end of set_api.c */