2 This file is part of GNUnet.
3 Copyright (C) 2012-2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @author Florian Dold
22 * @brief general purpose request queue
25 #include "gnunet_util_lib.h"
27 #define LOG(kind,...) GNUNET_log_from (kind, "util-mq",__VA_ARGS__)
30 struct GNUNET_MQ_Envelope
33 * Messages are stored in a linked list.
34 * Each queue has its own list of envelopes.
36 struct GNUNET_MQ_Envelope *next;
39 * Messages are stored in a linked list
40 * Each queue has its own list of envelopes.
42 struct GNUNET_MQ_Envelope *prev;
45 * Actual allocated message header.
46 * The GNUNET_MQ_Envelope header is allocated at
47 * the end of the message.
49 struct GNUNET_MessageHeader *mh;
52 * Queue the message is queued in, NULL if message is not queued.
54 struct GNUNET_MQ_Handle *parent_queue;
57 * Called after the message was sent irrevocably.
59 GNUNET_SCHEDULER_TaskCallback sent_cb;
62 * Closure for @e send_cb
67 * Flags that were set for this envelope by
68 * #GNUNET_MQ_env_set_options(). Only valid if
69 * @e have_custom_options is set.
74 * Additional options buffer set for this envelope by
75 * #GNUNET_MQ_env_set_options(). Only valid if
76 * @e have_custom_options is set.
81 * Did the application call #GNUNET_MQ_env_set_options()?
83 int have_custom_options;
88 * Handle to a message queue.
90 struct GNUNET_MQ_Handle
93 * Handlers array, or NULL if the queue should not receive messages
95 struct GNUNET_MQ_MessageHandler *handlers;
98 * Actual implementation of message sending,
99 * called when a message is added
101 GNUNET_MQ_SendImpl send_impl;
104 * Implementation-dependent queue destruction function
106 GNUNET_MQ_DestroyImpl destroy_impl;
109 * Implementation-dependent send cancel function
111 GNUNET_MQ_CancelImpl cancel_impl;
114 * Implementation-specific state
119 * Callback will be called when an error occurs.
121 GNUNET_MQ_ErrorHandler error_handler;
124 * Closure for the error handler.
126 void *error_handler_cls;
129 * Task to asynchronously run #impl_send_continue().
131 struct GNUNET_SCHEDULER_Task *send_task;
134 * Linked list of messages pending to be sent
136 struct GNUNET_MQ_Envelope *envelope_head;
139 * Linked list of messages pending to be sent
141 struct GNUNET_MQ_Envelope *envelope_tail;
144 * Message that is currently scheduled to be
145 * sent. Not the head of the message queue, as the implementation
146 * needs to know if sending has been already scheduled or not.
148 struct GNUNET_MQ_Envelope *current_envelope;
151 * Map of associations, lazily allocated
153 struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
156 * Functions to call on queue destruction; kept in a DLL.
158 struct GNUNET_MQ_DestroyNotificationHandle *dnh_head;
161 * Functions to call on queue destruction; kept in a DLL.
163 struct GNUNET_MQ_DestroyNotificationHandle *dnh_tail;
166 * Additional options buffer set for this queue by
167 * #GNUNET_MQ_set_options(). Default is 0.
169 const void *default_extra;
172 * Flags that were set for this queue by
173 * #GNUNET_MQ_set_options(). Default is 0.
175 uint64_t default_flags;
178 * Next id that should be used for the @e assoc_map,
179 * initialized lazily to a random value together with
185 * Number of entries we have in the envelope-DLL.
187 unsigned int queue_length;
190 * #GNUNET_YES if GNUNET_MQ_impl_evacuate was called.
191 * FIXME: is this dead?
196 * #GNUNET_YES if GNUNET_MQ_impl_send_in_flight() was called.
203 * Call the message message handler that was registered
204 * for the type of the given message in the given message queue.
206 * This function is indended to be used for the implementation
209 * @param mq message queue with the handlers
210 * @param mh message to dispatch
213 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
214 const struct GNUNET_MessageHeader *mh)
216 const struct GNUNET_MQ_MessageHandler *handler;
217 int handled = GNUNET_NO;
218 uint16_t msize = ntohs (mh->size);
219 uint16_t mtype = ntohs (mh->type);
221 LOG (GNUNET_ERROR_TYPE_DEBUG,
222 "Received message of type %u and size %u\n",
225 if (NULL == mq->handlers)
227 for (handler = mq->handlers; NULL != handler->cb; handler++)
229 if (handler->type == mtype)
231 handled = GNUNET_YES;
232 if ( (handler->expected_size > msize) ||
233 ( (handler->expected_size != msize) &&
234 (NULL == handler->mv) ) )
236 /* Too small, or not an exact size and
237 no 'mv' handler to check rest */
238 LOG (GNUNET_ERROR_TYPE_ERROR,
239 "Received malformed message of type %u\n",
240 (unsigned int) handler->type);
241 GNUNET_MQ_inject_error (mq,
242 GNUNET_MQ_ERROR_MALFORMED);
245 if ( (NULL == handler->mv) ||
247 handler->mv (handler->cls, mh)) )
249 /* message well-formed, pass to handler */
250 handler->cb (handler->cls, mh);
254 /* Message rejected by check routine */
255 LOG (GNUNET_ERROR_TYPE_ERROR,
256 "Received malformed message of type %u\n",
257 (unsigned int) handler->type);
258 GNUNET_MQ_inject_error (mq,
259 GNUNET_MQ_ERROR_MALFORMED);
265 if (GNUNET_NO == handled)
266 LOG (GNUNET_ERROR_TYPE_INFO,
267 "No handler for message of type %u and size %u\n",
273 * Call the error handler of a message queue with the given
274 * error code. If there is no error handler, log a warning.
276 * This function is intended to be used by the implementation
279 * @param mq message queue
280 * @param error the error type
283 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
284 enum GNUNET_MQ_Error error)
286 if (NULL == mq->error_handler)
288 LOG (GNUNET_ERROR_TYPE_WARNING,
289 "Got error %d, but no handler installed\n",
293 mq->error_handler (mq->error_handler_cls,
299 * Discard the message queue message, free all
300 * allocated resources. Must be called in the event
301 * that a message is created but should not actually be sent.
303 * @param mqm the message to discard
306 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *ev)
308 GNUNET_assert (NULL == ev->parent_queue);
314 * Obtain the current length of the message queue.
316 * @param mq queue to inspect
317 * @return number of queued, non-transmitted messages
320 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
322 if (GNUNET_YES != mq->in_flight)
324 return mq->queue_length;
326 return mq->queue_length - 1;
331 * Send a message with the given message queue.
332 * May only be called once per message.
334 * @param mq message queue
335 * @param ev the envelope with the message to send.
338 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
339 struct GNUNET_MQ_Envelope *ev)
341 GNUNET_assert (NULL != mq);
342 GNUNET_assert (NULL == ev->parent_queue);
345 GNUNET_break (mq->queue_length < 10000); /* This would seem like a bug... */
346 ev->parent_queue = mq;
347 /* is the implementation busy? queue it! */
348 if ( (NULL != mq->current_envelope) ||
349 (NULL != mq->send_task) )
351 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head,
356 GNUNET_assert (NULL == mq->envelope_head);
357 mq->current_envelope = ev;
359 LOG (GNUNET_ERROR_TYPE_DEBUG,
360 "sending message of type %u, queue empty (MQ: %p)\n",
371 * Remove the first envelope that has not yet been sent from the message
372 * queue and return it.
374 * @param mq queue to remove envelope from
375 * @return NULL if queue is empty (or has no envelope that is not under transmission)
377 struct GNUNET_MQ_Envelope *
378 GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq)
380 struct GNUNET_MQ_Envelope *env;
382 env = mq->envelope_head;
383 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
387 env->parent_queue = NULL;
393 * Function to copy an envelope. The envelope must not yet
394 * be in any queue or have any options or callbacks set.
396 * @param env envelope to copy
397 * @return copy of @a env
399 struct GNUNET_MQ_Envelope *
400 GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env)
402 GNUNET_assert (NULL == env->next);
403 GNUNET_assert (NULL == env->parent_queue);
404 GNUNET_assert (NULL == env->sent_cb);
405 GNUNET_assert (GNUNET_NO == env->have_custom_options);
406 return GNUNET_MQ_msg_copy (env->mh);
411 * Send a copy of a message with the given message queue.
412 * Can be called repeatedly on the same envelope.
414 * @param mq message queue
415 * @param ev the envelope with the message to send.
418 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
419 const struct GNUNET_MQ_Envelope *ev)
421 struct GNUNET_MQ_Envelope *env;
424 msize = ntohs (ev->mh->size);
425 env = GNUNET_malloc (sizeof (struct GNUNET_MQ_Envelope) +
427 env->mh = (struct GNUNET_MessageHeader *) &env[1];
428 env->sent_cb = ev->sent_cb;
429 env->sent_cls = ev->sent_cls;
430 GNUNET_memcpy (&env[1],
439 * Task run to call the send implementation for the next queued
440 * message, if any. Only useful for implementing message queues,
441 * results in undefined behavior if not used carefully.
443 * @param cls message queue to send the next message with
446 impl_send_continue (void *cls)
448 struct GNUNET_MQ_Handle *mq = cls;
450 mq->send_task = NULL;
451 /* call is only valid if we're actually currently sending
453 if (NULL == mq->envelope_head)
455 mq->current_envelope = mq->envelope_head;
456 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
458 mq->current_envelope);
460 LOG (GNUNET_ERROR_TYPE_DEBUG,
461 "sending message of type %u from queue\n",
462 ntohs(mq->current_envelope->mh->type));
465 mq->current_envelope->mh,
471 * Call the send implementation for the next queued message, if any.
472 * Only useful for implementing message queues, results in undefined
473 * behavior if not used carefully.
475 * @param mq message queue to send the next message with
478 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
480 struct GNUNET_MQ_Envelope *current_envelope;
481 GNUNET_SCHEDULER_TaskCallback cb;
483 GNUNET_assert (0 < mq->queue_length);
485 mq->in_flight = GNUNET_NO;
486 current_envelope = mq->current_envelope;
487 current_envelope->parent_queue = NULL;
488 mq->current_envelope = NULL;
489 GNUNET_assert (NULL == mq->send_task);
490 mq->send_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
492 if (NULL != (cb = current_envelope->sent_cb))
494 current_envelope->sent_cb = NULL;
495 cb (current_envelope->sent_cls);
497 GNUNET_free (current_envelope);
502 * Call the send notification for the current message, but do not
503 * try to send the next message until #GNUNET_MQ_impl_send_continue
506 * Only useful for implementing message queues, results in undefined
507 * behavior if not used carefully.
509 * @param mq message queue to send the next message with
512 GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq)
514 struct GNUNET_MQ_Envelope *current_envelope;
515 GNUNET_SCHEDULER_TaskCallback cb;
517 mq->in_flight = GNUNET_YES;
518 /* call is only valid if we're actually currently sending
520 current_envelope = mq->current_envelope;
521 GNUNET_assert (NULL != current_envelope);
522 /* can't call cancel from now on anymore */
523 current_envelope->parent_queue = NULL;
524 if (NULL != (cb = current_envelope->sent_cb))
526 current_envelope->sent_cb = NULL;
527 cb (current_envelope->sent_cls);
533 * Create a message queue for the specified handlers.
535 * @param send function the implements sending messages
536 * @param destroy function that implements destroying the queue
537 * @param cancel function that implements canceling a message
538 * @param impl_state for the queue, passed to 'send' and 'destroy'
539 * @param handlers array of message handlers
540 * @param error_handler handler for read and write errors
541 * @param error_handler_cls closure for @a error_handler
542 * @return a new message queue
544 struct GNUNET_MQ_Handle *
545 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
546 GNUNET_MQ_DestroyImpl destroy,
547 GNUNET_MQ_CancelImpl cancel,
549 const struct GNUNET_MQ_MessageHandler *handlers,
550 GNUNET_MQ_ErrorHandler error_handler,
551 void *error_handler_cls)
553 struct GNUNET_MQ_Handle *mq;
555 mq = GNUNET_new (struct GNUNET_MQ_Handle);
556 mq->send_impl = send;
557 mq->destroy_impl = destroy;
558 mq->cancel_impl = cancel;
559 mq->handlers = GNUNET_MQ_copy_handlers (handlers);
560 mq->error_handler = error_handler;
561 mq->error_handler_cls = error_handler_cls;
562 mq->impl_state = impl_state;
569 * Change the closure argument in all of the `handlers` of the
572 * @param mq to modify
573 * @param handlers_cls new closure to use
576 GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
579 if (NULL == mq->handlers)
581 for (unsigned int i=0;NULL != mq->handlers[i].cb; i++)
582 mq->handlers[i].cls = handlers_cls;
587 * Get the message that should currently be sent.
588 * Fails if there is no current message.
589 * Only useful for implementing message queues,
590 * results in undefined behavior if not used carefully.
592 * @param mq message queue with the current message
593 * @return message to send, never NULL
595 const struct GNUNET_MessageHeader *
596 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
598 GNUNET_assert (NULL != mq->current_envelope);
599 GNUNET_assert (NULL != mq->current_envelope->mh);
600 return mq->current_envelope->mh;
605 * Get the implementation state associated with the
608 * While the GNUNET_MQ_Impl* callbacks receive the
609 * implementation state, continuations that are scheduled
610 * by the implementation function often only have one closure
611 * argument, with this function it is possible to get at the
612 * implementation state when only passing the GNUNET_MQ_Handle
615 * @param mq message queue with the current message
616 * @return message to send, never NULL
619 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
621 return mq->impl_state;
625 struct GNUNET_MQ_Envelope *
626 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
630 struct GNUNET_MQ_Envelope *ev;
632 ev = GNUNET_malloc (size + sizeof (struct GNUNET_MQ_Envelope));
633 ev->mh = (struct GNUNET_MessageHeader *) &ev[1];
634 ev->mh->size = htons (size);
635 ev->mh->type = htons (type);
643 * Create a new envelope by copying an existing message.
645 * @param hdr header of the message to copy
646 * @return envelope containing @a hdr
648 struct GNUNET_MQ_Envelope *
649 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
651 struct GNUNET_MQ_Envelope *mqm;
652 uint16_t size = ntohs (hdr->size);
654 mqm = GNUNET_malloc (sizeof (*mqm) + size);
655 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
656 GNUNET_memcpy (mqm->mh,
664 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
666 * @param mhp pointer to the message header pointer that will be changed to allocate at
667 * the newly allocated space for the message.
668 * @param base_size size of the data before the nested message
669 * @param type type of the message in the envelope
670 * @param nested_mh the message to append to the message after base_size
672 struct GNUNET_MQ_Envelope *
673 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
676 const struct GNUNET_MessageHeader *nested_mh)
678 struct GNUNET_MQ_Envelope *mqm;
681 if (NULL == nested_mh)
682 return GNUNET_MQ_msg_ (mhp, base_size, type);
684 size = base_size + ntohs (nested_mh->size);
686 /* check for uint16_t overflow */
687 if (size < base_size)
690 mqm = GNUNET_MQ_msg_ (mhp, size, type);
691 GNUNET_memcpy ((char *) mqm->mh + base_size,
693 ntohs (nested_mh->size));
700 * Associate the assoc_data in mq with a unique request id.
702 * @param mq message queue, id will be unique for the queue
703 * @param assoc_data to associate
706 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
711 if (NULL == mq->assoc_map)
713 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
717 GNUNET_assert (GNUNET_OK ==
718 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map,
721 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
727 * Get the data associated with a @a request_id in a queue
729 * @param mq the message queue with the association
730 * @param request_id the request id we are interested in
731 * @return the associated data
734 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
737 if (NULL == mq->assoc_map)
739 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
745 * Remove the association for a @a request_id
747 * @param mq the message queue with the association
748 * @param request_id the request id we want to remove
749 * @return the associated data
752 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
757 if (NULL == mq->assoc_map)
759 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
761 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
768 * Call a callback once the envelope has been sent, that is,
769 * sending it can not be canceled anymore.
770 * There can be only one notify sent callback per envelope.
772 * @param ev message to call the notify callback for
773 * @param cb the notify callback
774 * @param cb_cls closure for the callback
777 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
778 GNUNET_SCHEDULER_TaskCallback cb,
781 /* allow setting *OR* clearing callback */
782 GNUNET_assert ( (NULL == ev->sent_cb) ||
785 ev->sent_cls = cb_cls;
790 * Handle we return for callbacks registered to be
791 * notified when #GNUNET_MQ_destroy() is called on a queue.
793 struct GNUNET_MQ_DestroyNotificationHandle
798 struct GNUNET_MQ_DestroyNotificationHandle *prev;
803 struct GNUNET_MQ_DestroyNotificationHandle *next;
806 * Queue to notify about.
808 struct GNUNET_MQ_Handle *mq;
813 GNUNET_SCHEDULER_TaskCallback cb;
823 * Destroy the message queue.
825 * @param mq message queue to destroy
828 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
830 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
832 if (NULL != mq->destroy_impl)
834 mq->destroy_impl (mq, mq->impl_state);
836 if (NULL != mq->send_task)
838 GNUNET_SCHEDULER_cancel (mq->send_task);
839 mq->send_task = NULL;
841 while (NULL != mq->envelope_head)
843 struct GNUNET_MQ_Envelope *ev;
845 ev = mq->envelope_head;
846 ev->parent_queue = NULL;
847 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
850 GNUNET_assert (0 < mq->queue_length);
852 LOG (GNUNET_ERROR_TYPE_DEBUG,
853 "MQ destroy drops message of type %u\n",
854 ntohs (ev->mh->type));
855 GNUNET_MQ_discard (ev);
857 if (NULL != mq->current_envelope)
859 /* we can only discard envelopes that
861 mq->current_envelope->parent_queue = NULL;
862 LOG (GNUNET_ERROR_TYPE_DEBUG,
863 "MQ destroy drops current message of type %u\n",
864 ntohs (mq->current_envelope->mh->type));
865 GNUNET_MQ_discard (mq->current_envelope);
866 mq->current_envelope = NULL;
867 GNUNET_assert (0 < mq->queue_length);
870 GNUNET_assert (0 == mq->queue_length);
871 while (NULL != (dnh = mq->dnh_head))
873 dnh->cb (dnh->cb_cls);
874 GNUNET_MQ_destroy_notify_cancel (dnh);
876 if (NULL != mq->assoc_map)
878 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
879 mq->assoc_map = NULL;
881 GNUNET_free_non_null (mq->handlers);
886 const struct GNUNET_MessageHeader *
887 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
891 uint16_t nested_size;
892 const struct GNUNET_MessageHeader *nested_msg;
894 whole_size = ntohs (mh->size);
895 GNUNET_assert (whole_size >= base_size);
896 nested_size = whole_size - base_size;
897 if (0 == nested_size)
899 if (nested_size < sizeof (struct GNUNET_MessageHeader))
904 nested_msg = (const struct GNUNET_MessageHeader *) ((char *) mh + base_size);
905 if (ntohs (nested_msg->size) != nested_size)
915 * Cancel sending the message. Message must have been sent with
916 * #GNUNET_MQ_send before. May not be called after the notify sent
917 * callback has been called
919 * @param ev queued envelope to cancel
922 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
924 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
926 GNUNET_assert (NULL != mq);
927 GNUNET_assert (NULL != mq->cancel_impl);
929 mq->evacuate_called = GNUNET_NO;
931 if (mq->current_envelope == ev)
933 /* complex case, we already started with transmitting
934 the message using the callbacks. */
935 GNUNET_assert (0 < mq->queue_length);
939 /* continue sending the next message, if any */
940 mq->current_envelope = mq->envelope_head;
941 if (NULL != mq->current_envelope)
943 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
945 mq->current_envelope);
947 LOG (GNUNET_ERROR_TYPE_DEBUG,
948 "sending canceled message of type %u queue\n",
949 ntohs(ev->mh->type));
952 mq->current_envelope->mh,
958 /* simple case, message is still waiting in the queue */
959 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
962 GNUNET_assert (0 < mq->queue_length);
966 if (GNUNET_YES != mq->evacuate_called)
968 ev->parent_queue = NULL;
977 * Function to obtain the current envelope
978 * from within #GNUNET_MQ_SendImpl implementations.
980 * @param mq message queue to interrogate
981 * @return the current envelope
983 struct GNUNET_MQ_Envelope *
984 GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
986 return mq->current_envelope;
991 * Function to obtain the last envelope in the queue.
993 * @param mq message queue to interrogate
994 * @return the last envelope in the queue
996 struct GNUNET_MQ_Envelope *
997 GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
999 if (NULL != mq->envelope_tail)
1000 return mq->envelope_tail;
1002 return mq->current_envelope;
1007 * Set application-specific options for this envelope.
1008 * Overrides the options set for the queue with
1009 * #GNUNET_MQ_set_options() for this message only.
1011 * @param env message to set options for
1012 * @param flags flags to use (meaning is queue-specific)
1013 * @param extra additional buffer for further data (also queue-specific)
1016 GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
1022 env->have_custom_options = GNUNET_YES;
1027 * Get application-specific options for this envelope.
1029 * @param env message to set options for
1030 * @param[out] flags set to flags to use (meaning is queue-specific)
1031 * @return extra additional buffer for further data (also queue-specific)
1034 GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env,
1037 struct GNUNET_MQ_Handle *mq = env->parent_queue;
1039 if (GNUNET_YES == env->have_custom_options)
1041 *flags = env->flags;
1049 *flags = mq->default_flags;
1050 return mq->default_extra;
1055 * Set application-specific options for this queue.
1057 * @param mq message queue to set options for
1058 * @param flags flags to use (meaning is queue-specific)
1059 * @param extra additional buffer for further data (also queue-specific)
1062 GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1066 mq->default_flags = flags;
1067 mq->default_extra = extra;
1072 * Obtain message contained in envelope.
1074 * @param env the envelope
1075 * @return message contained in the envelope
1077 const struct GNUNET_MessageHeader *
1078 GNUNET_MQ_env_get_msg (const struct GNUNET_MQ_Envelope *env)
1085 * Return next envelope in queue.
1087 * @param env a queued envelope
1088 * @return next one, or NULL
1090 const struct GNUNET_MQ_Envelope *
1091 GNUNET_MQ_env_next (const struct GNUNET_MQ_Envelope *env)
1098 * Register function to be called whenever @a mq is being
1101 * @param mq message queue to watch
1102 * @param cb function to call on @a mq destruction
1103 * @param cb_cls closure for @a cb
1104 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1106 struct GNUNET_MQ_DestroyNotificationHandle *
1107 GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1108 GNUNET_SCHEDULER_TaskCallback cb,
1111 struct GNUNET_MQ_DestroyNotificationHandle *dnh;
1113 dnh = GNUNET_new (struct GNUNET_MQ_DestroyNotificationHandle);
1116 dnh->cb_cls = cb_cls;
1117 GNUNET_CONTAINER_DLL_insert (mq->dnh_head,
1125 * Cancel registration from #GNUNET_MQ_destroy_notify().
1127 * @param dnh handle for registration to cancel
1130 GNUNET_MQ_destroy_notify_cancel (struct GNUNET_MQ_DestroyNotificationHandle *dnh)
1132 struct GNUNET_MQ_Handle *mq = dnh->mq;
1134 GNUNET_CONTAINER_DLL_remove (mq->dnh_head,
1142 * Insert @a env into the envelope DLL starting at @a env_head
1143 * Note that @a env must not be in any MQ while this function
1144 * is used with DLLs defined outside of the MQ module. This
1145 * is just in case some application needs to also manage a
1146 * FIFO of envelopes independent of MQ itself and wants to
1147 * re-use the pointers internal to @a env. Use with caution.
1149 * @param[in|out] env_head of envelope DLL
1150 * @param[in|out] env_tail tail of envelope DLL
1151 * @param[in|out] env element to insert at the tail
1154 GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
1155 struct GNUNET_MQ_Envelope **env_tail,
1156 struct GNUNET_MQ_Envelope *env)
1158 GNUNET_CONTAINER_DLL_insert_tail (*env_head,
1165 * Remove @a env from the envelope DLL starting at @a env_head.
1166 * Note that @a env must not be in any MQ while this function
1167 * is used with DLLs defined outside of the MQ module. This
1168 * is just in case some application needs to also manage a
1169 * FIFO of envelopes independent of MQ itself and wants to
1170 * re-use the pointers internal to @a env. Use with caution.
1172 * @param[in|out] env_head of envelope DLL
1173 * @param[in|out] env_tail tail of envelope DLL
1174 * @param[in|out] env element to remove from the DLL
1177 GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
1178 struct GNUNET_MQ_Envelope **env_tail,
1179 struct GNUNET_MQ_Envelope *env)
1181 GNUNET_CONTAINER_DLL_remove (*env_head,
1188 * Copy an array of handlers.
1190 * Useful if the array has been delared in local memory and needs to be
1191 * persisted for future use.
1193 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
1194 * @return A newly allocated array of handlers.
1195 * Needs to be freed with #GNUNET_free.
1197 struct GNUNET_MQ_MessageHandler *
1198 GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1200 struct GNUNET_MQ_MessageHandler *copy;
1203 if (NULL == handlers)
1206 count = GNUNET_MQ_count_handlers (handlers);
1207 copy = GNUNET_new_array (count + 1,
1208 struct GNUNET_MQ_MessageHandler);
1209 GNUNET_memcpy (copy,
1211 count * sizeof (struct GNUNET_MQ_MessageHandler));
1217 * Copy an array of handlers, appending AGPL handler.
1219 * Useful if the array has been delared in local memory and needs to be
1220 * persisted for future use.
1222 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
1223 * @param agpl_handler function to call for AGPL handling
1224 * @param agpl_cls closure for @a agpl_handler
1225 * @return A newly allocated array of handlers.
1226 * Needs to be freed with #GNUNET_free.
1228 struct GNUNET_MQ_MessageHandler *
1229 GNUNET_MQ_copy_handlers2 (const struct GNUNET_MQ_MessageHandler *handlers,
1230 GNUNET_MQ_MessageCallback agpl_handler,
1233 struct GNUNET_MQ_MessageHandler *copy;
1236 if (NULL == handlers)
1238 count = GNUNET_MQ_count_handlers (handlers);
1239 copy = GNUNET_new_array (count + 2,
1240 struct GNUNET_MQ_MessageHandler);
1241 GNUNET_memcpy (copy,
1243 count * sizeof (struct GNUNET_MQ_MessageHandler));
1244 copy[count].mv = NULL;
1245 copy[count].cb = agpl_handler;
1246 copy[count].cls = agpl_cls;
1247 copy[count].type = GNUNET_MESSAGE_TYPE_REQUEST_AGPL;
1248 copy[count].expected_size = sizeof (struct GNUNET_MessageHeader);
1254 * Count the handlers in a handler array.
1256 * @param handlers Array of handlers to be counted.
1257 * @return The number of handlers in the array.
1260 GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1264 if (NULL == handlers)
1267 for (i=0; NULL != handlers[i].cb; i++) ;
1274 * Convert an `enum GNUNET_MQ_PreferenceType` to a string
1276 * @param type the preference type
1277 * @return a string or NULL if invalid
1280 GNUNET_MQ_preference_to_string (enum GNUNET_MQ_PreferenceKind type)
1283 case GNUNET_MQ_PREFERENCE_NONE:
1285 case GNUNET_MQ_PREFERENCE_BANDWIDTH:
1287 case GNUNET_MQ_PREFERENCE_LATENCY:
1289 case GNUNET_MQ_PREFERENCE_RELIABILITY:
1290 return "RELIABILITY";