2 This file is part of GNUnet.
3 (C) 2012 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 * @author Florian Dold
24 * @brief general purpose request queue
27 #include "gnunet_util_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "mq",__VA_ARGS__)
32 struct GNUNET_MQ_Envelope
35 * Messages are stored in a linked list.
36 * Each queue has its own list of envelopes.
38 struct GNUNET_MQ_Envelope *next;
41 * Messages are stored in a linked list
42 * Each queue has its own list of envelopes.
44 struct GNUNET_MQ_Envelope *prev;
47 * Actual allocated message header,
48 * usually points to the end of the containing GNUNET_MQ_Envelope
50 struct GNUNET_MessageHeader *mh;
53 * Queue the message is queued in, NULL if message is not queued.
55 struct GNUNET_MQ_Handle *parent_queue;
58 * Called after the message was sent irrevocably.
60 GNUNET_MQ_NotifyCallback sent_cb;
70 * Handle to a message queue.
72 struct GNUNET_MQ_Handle
75 * Handlers array, or NULL if the queue should not receive messages
77 const struct GNUNET_MQ_MessageHandler *handlers;
80 * Closure for the handler callbacks,
81 * as well as for the error handler.
86 * Actual implementation of message sending,
87 * called when a message is added
89 GNUNET_MQ_SendImpl send_impl;
92 * Implementation-dependent queue destruction function
94 GNUNET_MQ_DestroyImpl destroy_impl;
97 * Implementation-dependent send cancel function
99 GNUNET_MQ_CancelImpl cancel_impl;
102 * Implementation-specific state
107 * Callback will be called when an error occurs.
109 GNUNET_MQ_ErrorHandler error_handler;
112 * Linked list of messages pending to be sent
114 struct GNUNET_MQ_Envelope *envelope_head;
117 * Linked list of messages pending to be sent
119 struct GNUNET_MQ_Envelope *envelope_tail;
122 * Message that is currently scheduled to be
123 * sent. Not the head of the message queue, as the implementation
124 * needs to know if sending has been already scheduled or not.
126 struct GNUNET_MQ_Envelope *current_envelope;
129 * Map of associations, lazily allocated
131 struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
134 * Task scheduled during #GNUNET_MQ_impl_send_continue.
136 GNUNET_SCHEDULER_TaskIdentifier continue_task;
139 * Next id that should be used for the assoc_map,
140 * initialized lazily to a random value together with
149 struct ServerClientSocketState
151 struct GNUNET_SERVER_Client *client;
152 struct GNUNET_SERVER_TransmitHandle* th;
156 struct ClientConnectionState
159 * Did we call receive alread alreadyy?
164 * Do we also want to receive?
166 int receive_requested;
167 struct GNUNET_CLIENT_Connection *connection;
168 struct GNUNET_CLIENT_TransmitHandle *th;
173 * Call the message message handler that was registered
174 * for the type of the given message in the given message queue.
176 * This function is indended to be used for the implementation
179 * @param mq message queue with the handlers
180 * @param mh message to dispatch
183 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *mh)
185 const struct GNUNET_MQ_MessageHandler *handler;
186 int handled = GNUNET_NO;
188 handler = mq->handlers;
191 for (; NULL != handler->cb; handler++)
193 if (handler->type == ntohs (mh->type))
195 handler->cb (mq->handlers_cls, mh);
196 handled = GNUNET_YES;
200 if (GNUNET_NO == handled)
201 LOG (GNUNET_ERROR_TYPE_WARNING, "no handler for message of type %d\n", ntohs (mh->type));
206 * Call the error handler of a message queue with the given
207 * error code. If there is no error handler, log a warning.
209 * This function is intended to be used by the implementation
212 * @param mq message queue
213 * @param error the error type
216 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
217 enum GNUNET_MQ_Error error)
219 if (NULL == mq->error_handler)
221 /* FIXME: log what kind of error occured */
222 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "mq: got error, but no handler installed\n");
225 mq->error_handler (mq->handlers_cls, error);
230 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm)
232 GNUNET_assert (NULL == mqm->parent_queue);
238 * Send a message with the give message queue.
239 * May only be called once per message.
241 * @param mq message queue
242 * @param ev the envelope with the message to send.
245 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
247 GNUNET_assert (NULL != mq);
248 GNUNET_assert (NULL == ev->parent_queue);
250 ev->parent_queue = mq;
252 /* is the implementation busy? queue it! */
253 if (NULL != mq->current_envelope)
255 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head, mq->envelope_tail, ev);
258 mq->current_envelope = ev;
259 mq->send_impl (mq, ev->mh, mq->impl_state);
264 * Task run to call the send implementation for the next queued
265 * message, if any. Only useful for implementing message queues,
266 * results in undefined behavior if not used carefully.
268 * @param cls message queue to send the next message with
269 * @param tc scheduler context
272 impl_send_continue (void *cls,
273 const struct GNUNET_SCHEDULER_TaskContext *tc)
275 struct GNUNET_MQ_Handle *mq = cls;
276 struct GNUNET_MQ_Envelope *current_envelope;
278 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
281 mq->continue_task = GNUNET_SCHEDULER_NO_TASK;
282 /* call is only valid if we're actually currently sending
284 current_envelope = mq->current_envelope;
285 GNUNET_assert (NULL != current_envelope);
286 current_envelope->parent_queue = NULL;
287 if (NULL == mq->envelope_head)
289 mq->current_envelope = NULL;
293 mq->current_envelope = mq->envelope_head;
294 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
296 mq->current_envelope);
297 mq->send_impl (mq, mq->current_envelope->mh, mq->impl_state);
299 if (NULL != current_envelope->sent_cb)
300 current_envelope->sent_cb (current_envelope->sent_cls);
301 GNUNET_free (current_envelope);
306 * Call the send implementation for the next queued message,
308 * Only useful for implementing message queues,
309 * results in undefined behavior if not used carefully.
311 * @param mq message queue to send the next message with
314 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
316 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == mq->continue_task);
317 mq->continue_task = GNUNET_SCHEDULER_add_now (&impl_send_continue,
323 * Create a message queue for the specified handlers.
325 * @param send function the implements sending messages
326 * @param destroy function that implements destroying the queue
327 * @param cancel function that implements canceling a message
328 * @param impl_state for the queue, passed to 'send' and 'destroy'
329 * @param handlers array of message handlers
330 * @param error_handler handler for read and write errors
331 * @param cls closure for message handlers and error handler
332 * @return a new message queue
334 struct GNUNET_MQ_Handle *
335 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
336 GNUNET_MQ_DestroyImpl destroy,
337 GNUNET_MQ_CancelImpl cancel,
339 const struct GNUNET_MQ_MessageHandler *handlers,
340 GNUNET_MQ_ErrorHandler error_handler,
343 struct GNUNET_MQ_Handle *mq;
345 mq = GNUNET_new (struct GNUNET_MQ_Handle);
346 mq->send_impl = send;
347 mq->destroy_impl = destroy;
348 mq->cancel_impl = cancel;
349 mq->handlers = handlers;
350 mq->handlers_cls = cls;
351 mq->impl_state = impl_state;
358 * Get the message that should currently be sent.
359 * Fails if there is no current message.
360 * Only useful for implementing message queues,
361 * results in undefined behavior if not used carefully.
363 * @param mq message queue with the current message
364 * @return message to send, never NULL
366 const struct GNUNET_MessageHeader *
367 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
369 if (NULL == mq->current_envelope)
371 if (NULL == mq->current_envelope->mh)
373 return mq->current_envelope->mh;
378 * Get the implementation state associated with the
381 * While the GNUNET_MQ_Impl* callbacks receive the
382 * implementation state, continuations that are scheduled
383 * by the implementation function often only have one closure
384 * argument, with this function it is possible to get at the
385 * implementation state when only passing the GNUNET_MQ_Handle
388 * @param mq message queue with the current message
389 * @return message to send, never NULL
392 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
394 return mq->impl_state;
398 struct GNUNET_MQ_Envelope *
399 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type)
401 struct GNUNET_MQ_Envelope *mqm;
403 mqm = GNUNET_malloc (sizeof *mqm + size);
404 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
405 mqm->mh->size = htons (size);
406 mqm->mh->type = htons (type);
414 * Implementation of the GNUNET_MQ_msg_nested_mh macro.
416 * @param mhp pointer to the message header pointer that will be changed to allocate at
417 * the newly allocated space for the message.
418 * @param base_size size of the data before the nested message
419 * @param type type of the message in the envelope
420 * @param nested_mh the message to append to the message after base_size
422 struct GNUNET_MQ_Envelope *
423 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp, uint16_t base_size, uint16_t type,
424 const struct GNUNET_MessageHeader *nested_mh)
426 struct GNUNET_MQ_Envelope *mqm;
429 if (NULL == nested_mh)
430 return GNUNET_MQ_msg_ (mhp, base_size, type);
432 size = base_size + ntohs (nested_mh->size);
434 /* check for uint16_t overflow */
435 if (size < base_size)
438 mqm = GNUNET_MQ_msg_ (mhp, size, type);
439 memcpy ((char *) mqm->mh + base_size, nested_mh, ntohs (nested_mh->size));
446 * Transmit a queued message to the session's client.
448 * @param cls consensus session
449 * @param size number of bytes available in buf
450 * @param buf where the callee should write the message
451 * @return number of bytes written to buf
454 transmit_queued (void *cls, size_t size,
457 struct GNUNET_MQ_Handle *mq = cls;
458 struct ServerClientSocketState *state = GNUNET_MQ_impl_state (mq);
459 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
462 GNUNET_assert (NULL != buf);
464 msg_size = ntohs (msg->size);
465 GNUNET_assert (size >= msg_size);
466 memcpy (buf, msg, msg_size);
469 GNUNET_MQ_impl_send_continue (mq);
476 server_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
479 struct ServerClientSocketState *state = impl_state;
481 if (NULL != state->th)
483 GNUNET_SERVER_notify_transmit_ready_cancel (state->th);
487 GNUNET_assert (NULL != mq);
488 GNUNET_assert (NULL != state);
489 GNUNET_SERVER_client_drop (state->client);
495 server_client_send_impl (struct GNUNET_MQ_Handle *mq,
496 const struct GNUNET_MessageHeader *msg, void *impl_state)
498 struct ServerClientSocketState *state = impl_state;
500 GNUNET_assert (NULL != mq);
501 GNUNET_assert (NULL != state);
503 GNUNET_SERVER_notify_transmit_ready (state->client, ntohs (msg->size),
504 GNUNET_TIME_UNIT_FOREVER_REL,
505 &transmit_queued, mq);
509 struct GNUNET_MQ_Handle *
510 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client)
512 struct GNUNET_MQ_Handle *mq;
513 struct ServerClientSocketState *scss;
515 mq = GNUNET_new (struct GNUNET_MQ_Handle);
516 scss = GNUNET_new (struct ServerClientSocketState);
517 mq->impl_state = scss;
518 scss->client = client;
519 GNUNET_SERVER_client_keep (client);
520 mq->send_impl = server_client_send_impl;
521 mq->destroy_impl = server_client_destroy_impl;
527 * Type of a function to call when we receive a message
531 * @param msg message received, NULL on timeout or fatal error
534 handle_client_message (void *cls,
535 const struct GNUNET_MessageHeader *msg)
537 struct GNUNET_MQ_Handle *mq = cls;
538 struct ClientConnectionState *state;
540 state = mq->impl_state;
544 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
548 GNUNET_CLIENT_receive (state->connection, handle_client_message, mq,
549 GNUNET_TIME_UNIT_FOREVER_REL);
551 GNUNET_MQ_inject_message (mq, msg);
556 * Transmit a queued message to the session's client.
558 * @param cls consensus session
559 * @param size number of bytes available in @a buf
560 * @param buf where the callee should write the message
561 * @return number of bytes written to buf
564 connection_client_transmit_queued (void *cls,
568 struct GNUNET_MQ_Handle *mq = cls;
569 const struct GNUNET_MessageHeader *msg;
570 struct ClientConnectionState *state = mq->impl_state;
573 GNUNET_assert (NULL != mq);
574 msg = GNUNET_MQ_impl_current (mq);
578 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
582 if ( (GNUNET_YES == state->receive_requested) &&
583 (GNUNET_NO == state->receive_active) )
585 state->receive_active = GNUNET_YES;
586 GNUNET_CLIENT_receive (state->connection, handle_client_message, mq,
587 GNUNET_TIME_UNIT_FOREVER_REL);
591 msg_size = ntohs (msg->size);
592 GNUNET_assert (size >= msg_size);
593 memcpy (buf, msg, msg_size);
596 GNUNET_MQ_impl_send_continue (mq);
603 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
605 GNUNET_free (impl_state);
610 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
611 const struct GNUNET_MessageHeader *msg, void *impl_state)
613 struct ClientConnectionState *state = impl_state;
615 GNUNET_assert (NULL != state);
616 GNUNET_assert (NULL == state->th);
618 GNUNET_CLIENT_notify_transmit_ready (state->connection, ntohs (msg->size),
619 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
620 &connection_client_transmit_queued, mq);
621 GNUNET_assert (NULL != state->th);
626 connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
629 struct ClientConnectionState *state = impl_state;
630 GNUNET_assert (NULL != state->th);
631 GNUNET_CLIENT_notify_transmit_ready_cancel (state->th);
636 struct GNUNET_MQ_Handle *
637 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
638 const struct GNUNET_MQ_MessageHandler *handlers,
639 GNUNET_MQ_ErrorHandler error_handler,
642 struct GNUNET_MQ_Handle *mq;
643 struct ClientConnectionState *state;
645 GNUNET_assert (NULL != connection);
647 mq = GNUNET_new (struct GNUNET_MQ_Handle);
648 mq->handlers = handlers;
649 mq->error_handler = error_handler;
650 mq->handlers_cls = cls;
651 state = GNUNET_new (struct ClientConnectionState);
652 state->connection = connection;
653 mq->impl_state = state;
654 mq->send_impl = connection_client_send_impl;
655 mq->destroy_impl = connection_client_destroy_impl;
656 mq->cancel_impl = connection_client_cancel_impl;
657 if (NULL != handlers)
658 state->receive_requested = GNUNET_YES;
665 GNUNET_MQ_replace_handlers (struct GNUNET_MQ_Handle *mq,
666 const struct GNUNET_MQ_MessageHandler *new_handlers,
669 /* FIXME: notify implementation? */
670 /* FIXME: what about NULL handlers? abort receive? */
671 mq->handlers = new_handlers;
672 mq->handlers_cls = cls;
677 * Associate the assoc_data in mq with a unique request id.
679 * @param mq message queue, id will be unique for the queue
680 * @param assoc_data to associate
683 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
688 if (NULL == mq->assoc_map)
690 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
694 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map, id, assoc_data,
695 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
701 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq, uint32_t request_id)
703 if (NULL == mq->assoc_map)
705 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
710 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq, uint32_t request_id)
714 if (NULL == mq->assoc_map)
716 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
717 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map, request_id);
723 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm,
724 GNUNET_MQ_NotifyCallback cb,
733 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
735 if (NULL != mq->destroy_impl)
737 mq->destroy_impl (mq, mq->impl_state);
739 if (GNUNET_SCHEDULER_NO_TASK != mq->continue_task)
741 GNUNET_SCHEDULER_cancel (mq->continue_task);
742 mq->continue_task = GNUNET_SCHEDULER_NO_TASK;
744 while (NULL != mq->envelope_head)
746 struct GNUNET_MQ_Envelope *ev;
747 ev = mq->envelope_head;
748 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, mq->envelope_tail, ev);
749 GNUNET_MQ_discard (ev);
752 if (NULL != mq->current_envelope)
754 GNUNET_MQ_discard (mq->current_envelope);
755 mq->current_envelope = NULL;
758 if (NULL != mq->assoc_map)
760 GNUNET_CONTAINER_multihashmap32_destroy (mq->assoc_map);
761 mq->assoc_map = NULL;
768 struct GNUNET_MessageHeader *
769 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh, uint16_t base_size)
772 uint16_t nested_size;
773 struct GNUNET_MessageHeader *nested_msg;
775 whole_size = ntohs (mh->size);
776 GNUNET_assert (whole_size >= base_size);
778 nested_size = whole_size - base_size;
780 if (0 == nested_size)
783 if (nested_size < sizeof (struct GNUNET_MessageHeader))
789 nested_msg = (struct GNUNET_MessageHeader *) ((char *) mh + base_size);
791 if (ntohs (nested_msg->size) != nested_size)
794 nested_msg->size = htons (nested_size);
802 * Cancel sending the message. Message must have been sent with
803 * #GNUNET_MQ_send before. May not be called after the notify sent
804 * callback has been called
806 * @param ev queued envelope to cancel
809 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
811 struct GNUNET_MQ_Handle *mq = ev->parent_queue;
813 GNUNET_assert (NULL != mq);
814 GNUNET_assert (NULL != mq->cancel_impl);
816 if (mq->current_envelope == ev) {
817 // complex case, we already started with transmitting
819 mq->cancel_impl (mq, mq->impl_state);
820 // continue sending the next message, if any
821 if (NULL == mq->envelope_head)
823 mq->current_envelope = NULL;
827 mq->current_envelope = mq->envelope_head;
828 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
830 mq->current_envelope);
831 mq->send_impl (mq, mq->current_envelope->mh, mq->impl_state);
834 // simple case, message is still waiting in the queue
835 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, mq->envelope_tail, ev);
838 ev->parent_queue = NULL;