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 2, 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
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "mq",__VA_ARGS__)
34 struct GNUNET_MQ_Envelope
37 * Messages are stored in a linked list.
38 * Each queue has its own list of envelopes.
40 struct GNUNET_MQ_Envelope *next;
43 * Messages are stored in a linked list
44 * Each queue has its own list of envelopes.
46 struct GNUNET_MQ_Envelope *prev;
49 * Actual allocated message header,
50 * usually points to the end of the containing GNUNET_MQ_Envelope
52 struct GNUNET_MessageHeader *mh;
55 * Queue the message is queued in, NULL if message is not queued.
57 struct GNUNET_MQ_Handle *parent_queue;
60 * Called after the message was sent irrevocably.
62 GNUNET_MQ_NotifyCallback sent_cb;
72 * Handle to a message queue.
74 struct GNUNET_MQ_Handle
77 * Handlers array, or NULL if the queue should not receive messages
79 const struct GNUNET_MQ_MessageHandler *handlers;
82 * Closure for the handler callbacks,
83 * as well as for the error handler.
88 * Actual implementation of message sending,
89 * called when a message is added
91 GNUNET_MQ_SendImpl send_impl;
94 * Implementation-dependent queue destruction function
96 GNUNET_MQ_DestroyImpl destroy_impl;
99 * Implementation-specific state
104 * Callback will be called when an error occurs.
106 GNUNET_MQ_ErrorHandler error_handler;
109 * Linked list of messages pending to be sent
111 struct GNUNET_MQ_Envelope *envelope_head;
114 * Linked list of messages pending to be sent
116 struct GNUNET_MQ_Envelope *envelope_tail;
119 * Message that is currently scheduled to be
120 * sent. Not the head of the message queue, as the implementation
121 * needs to know if sending has been already scheduled or not.
123 struct GNUNET_MQ_Envelope *current_envelope;
126 * Has the current envelope been commited?
127 * Either GNUNET_YES or GNUNET_NO.
132 * Map of associations, lazily allocated
134 struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
137 * Next id that should be used for the assoc_map,
138 * initialized lazily to a random value together with
147 struct ServerClientSocketState
149 struct GNUNET_SERVER_Client *client;
150 struct GNUNET_SERVER_TransmitHandle* th;
154 struct ClientConnectionState
157 * Did we call receive alread alreadyy?
162 * Do we also want to receive?
164 int receive_requested;
165 struct GNUNET_CLIENT_Connection *connection;
166 struct GNUNET_CLIENT_TransmitHandle *th;
171 * Call the message message handler that was registered
172 * for the type of the given message in the given message queue.
174 * This function is indended to be used for the implementation
177 * @param mq message queue with the handlers
178 * @param mh message to dispatch
181 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *mh)
183 const struct GNUNET_MQ_MessageHandler *handler;
184 int handled = GNUNET_NO;
186 handler = mq->handlers;
189 for (; NULL != handler->cb; handler++)
191 if (handler->type == ntohs (mh->type))
193 handler->cb (mq->handlers_cls, mh);
194 handled = GNUNET_YES;
198 if (GNUNET_NO == handled)
199 LOG (GNUNET_ERROR_TYPE_WARNING, "no handler for message of type %d\n", ntohs (mh->type));
204 * Call the error handler of a message queue with the given
205 * error code. If there is no error handler, log a warning.
207 * This function is intended to be used by the implementation
210 * @param mq message queue
211 * @param error the error type
214 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
215 enum GNUNET_MQ_Error error)
217 if (NULL == mq->error_handler)
219 /* FIXME: log what kind of error occured */
220 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "mq: got error, but no handler installed\n");
223 mq->error_handler (mq->handlers_cls, error);
228 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm)
230 GNUNET_assert (NULL == mqm->parent_queue);
236 * Send a message with the give message queue.
237 * May only be called once per message.
239 * @param mq message queue
240 * @param ev the envelope with the message to send.
243 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
245 GNUNET_assert (NULL != mq);
246 GNUNET_assert (NULL == ev->parent_queue);
248 /* is the implementation busy? queue it! */
249 if (NULL != mq->current_envelope)
251 GNUNET_CONTAINER_DLL_insert_tail (mq->envelope_head, mq->envelope_tail, ev);
254 mq->current_envelope = ev;
255 mq->send_impl (mq, ev->mh, mq->impl_state);
260 * Call the send implementation for the next queued message,
262 * Only useful for implementing message queues,
263 * results in undefined behavior if not used carefully.
265 * @param mq message queue to send the next message with
268 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
270 /* call is only valid if we're actually currently sending
272 GNUNET_assert (NULL != mq);
273 GNUNET_assert (NULL != mq->current_envelope);
274 GNUNET_assert (GNUNET_YES == mq->commited);
275 mq->commited = GNUNET_NO;
276 GNUNET_free (mq->current_envelope);
277 if (NULL == mq->envelope_head)
279 mq->current_envelope = NULL;
284 GNUNET_assert (NULL != mq->envelope_tail);
285 GNUNET_assert (NULL != mq->envelope_head);
286 mq->current_envelope = mq->envelope_head;
287 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, mq->envelope_tail,
288 mq->current_envelope);
289 mq->send_impl (mq, mq->current_envelope->mh, mq->impl_state);
294 * Create a message queue for the specified handlers.
296 * @param send function the implements sending messages
297 * @param destroy function that implements destroying the queue
298 * @param cancel function that implements canceling a message
299 * @param impl_state for the queue, passed to 'send' and 'destroy'
300 * @param handlers array of message handlers
301 * @param error_handler handler for read and write errors
302 * @param cls closure for message handlers and error handler
303 * @return a new message queue
305 struct GNUNET_MQ_Handle *
306 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
307 GNUNET_MQ_DestroyImpl destroy,
308 GNUNET_MQ_CancelImpl cancel,
310 const struct GNUNET_MQ_MessageHandler *handlers,
311 GNUNET_MQ_ErrorHandler error_handler,
314 struct GNUNET_MQ_Handle *mq;
316 mq = GNUNET_new (struct GNUNET_MQ_Handle);
317 mq->send_impl = send;
318 mq->destroy_impl = destroy;
319 mq->handlers = handlers;
320 mq->handlers_cls = cls;
321 mq->impl_state = impl_state;
328 * Get the message that should currently be sent.
329 * Fails if there is no current message.
330 * Only useful for implementing message queues,
331 * results in undefined behavior if not used carefully.
333 * @param mq message queue with the current message
334 * @return message to send, never NULL
336 const struct GNUNET_MessageHeader *
337 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
339 if (NULL == mq->current_envelope)
341 if (NULL == mq->current_envelope->mh)
343 return mq->current_envelope->mh;
348 * Get the implementation state associated with the
351 * While the GNUNET_MQ_Impl* callbacks receive the
352 * implementation state, continuations that are scheduled
353 * by the implementation function often only have one closure
354 * argument, with this function it is possible to get at the
355 * implementation state when only passing the GNUNET_MQ_Handle
358 * @param mq message queue with the current message
359 * @return message to send, never NULL
362 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
364 return mq->impl_state;
370 * Mark the current message as irrevocably sent, but do not
371 * proceed with sending the next message.
372 * Will call the appropriate GNUNET_MQ_NotifyCallback, if any.
374 * @param mq message queue
377 GNUNET_MQ_impl_send_commit (struct GNUNET_MQ_Handle *mq)
379 GNUNET_assert (NULL != mq->current_envelope);
380 GNUNET_assert (GNUNET_NO == mq->commited);
381 mq->commited = GNUNET_YES;
382 if (NULL != mq->current_envelope->sent_cb)
383 mq->current_envelope->sent_cb (mq->current_envelope->sent_cls);
387 struct GNUNET_MQ_Envelope *
388 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type)
390 struct GNUNET_MQ_Envelope *mqm;
392 mqm = GNUNET_malloc (sizeof *mqm + size);
393 mqm->mh = (struct GNUNET_MessageHeader *) &mqm[1];
394 mqm->mh->size = htons (size);
395 mqm->mh->type = htons (type);
403 * Implementation of the GNUNET_MQ_msg_nested_mh macro.
405 * @param mhp pointer to the message header pointer that will be changed to allocate at
406 * the newly allocated space for the message.
407 * @param base_size size of the data before the nested message
408 * @param type type of the message in the envelope
409 * @param nested_mh the message to append to the message after base_size
411 struct GNUNET_MQ_Envelope *
412 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp, uint16_t base_size, uint16_t type,
413 const struct GNUNET_MessageHeader *nested_mh)
415 struct GNUNET_MQ_Envelope *mqm;
418 if (NULL == nested_mh)
419 return GNUNET_MQ_msg_ (mhp, base_size, type);
421 size = base_size + ntohs (nested_mh->size);
423 /* check for uint16_t overflow */
424 if (size < base_size)
427 mqm = GNUNET_MQ_msg_ (mhp, size, type);
428 memcpy ((char *) mqm->mh + base_size, nested_mh, ntohs (nested_mh->size));
435 * Transmit a queued message to the session's client.
437 * @param cls consensus session
438 * @param size number of bytes available in buf
439 * @param buf where the callee should write the message
440 * @return number of bytes written to buf
443 transmit_queued (void *cls, size_t size,
446 struct GNUNET_MQ_Handle *mq = cls;
447 struct ServerClientSocketState *state = GNUNET_MQ_impl_state (mq);
448 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
451 GNUNET_assert (NULL != buf);
453 msg_size = ntohs (msg->size);
454 GNUNET_assert (size >= msg_size);
455 memcpy (buf, msg, msg_size);
458 GNUNET_MQ_impl_send_continue (mq);
466 server_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
469 struct ServerClientSocketState *state = impl_state;
471 GNUNET_assert (NULL != mq);
472 GNUNET_assert (NULL != state);
473 GNUNET_SERVER_client_drop (state->client);
478 server_client_send_impl (struct GNUNET_MQ_Handle *mq,
479 const struct GNUNET_MessageHeader *msg, void *impl_state)
481 struct ServerClientSocketState *state = impl_state;
483 GNUNET_assert (NULL != mq);
484 GNUNET_assert (NULL != state);
486 GNUNET_MQ_impl_send_commit (mq);
489 GNUNET_SERVER_notify_transmit_ready (state->client, ntohs (msg->size),
490 GNUNET_TIME_UNIT_FOREVER_REL,
491 &transmit_queued, mq);
495 struct GNUNET_MQ_Handle *
496 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client)
498 struct GNUNET_MQ_Handle *mq;
499 struct ServerClientSocketState *scss;
501 mq = GNUNET_new (struct GNUNET_MQ_Handle);
502 scss = GNUNET_new (struct ServerClientSocketState);
503 mq->impl_state = scss;
504 scss->client = client;
505 GNUNET_SERVER_client_keep (client);
506 mq->send_impl = server_client_send_impl;
507 mq->destroy_impl = server_client_destroy_impl;
513 * Type of a function to call when we receive a message
517 * @param msg message received, NULL on timeout or fatal error
520 handle_client_message (void *cls,
521 const struct GNUNET_MessageHeader *msg)
523 struct GNUNET_MQ_Handle *mq = cls;
524 struct ClientConnectionState *state;
526 state = mq->impl_state;
530 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
534 GNUNET_CLIENT_receive (state->connection, handle_client_message, mq,
535 GNUNET_TIME_UNIT_FOREVER_REL);
537 GNUNET_MQ_inject_message (mq, msg);
542 * Transmit a queued message to the session's client.
544 * @param cls consensus session
545 * @param size number of bytes available in buf
546 * @param buf where the callee should write the message
547 * @return number of bytes written to buf
550 connection_client_transmit_queued (void *cls, size_t size,
553 struct GNUNET_MQ_Handle *mq = cls;
554 const struct GNUNET_MessageHeader *msg;
555 struct ClientConnectionState *state = mq->impl_state;
558 GNUNET_assert (NULL != mq);
559 msg = GNUNET_MQ_impl_current (mq);
563 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_READ);
567 if ( (GNUNET_YES == state->receive_requested) &&
568 (GNUNET_NO == state->receive_active) )
570 state->receive_active = GNUNET_YES;
571 GNUNET_CLIENT_receive (state->connection, handle_client_message, mq,
572 GNUNET_TIME_UNIT_FOREVER_REL);
576 msg_size = ntohs (msg->size);
577 GNUNET_assert (size >= msg_size);
578 memcpy (buf, msg, msg_size);
581 GNUNET_MQ_impl_send_continue (mq);
589 connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
591 GNUNET_free (impl_state);
595 connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
596 const struct GNUNET_MessageHeader *msg, void *impl_state)
598 struct ClientConnectionState *state = impl_state;
600 GNUNET_assert (NULL != state);
601 GNUNET_assert (NULL == state->th);
603 GNUNET_MQ_impl_send_commit (mq);
606 GNUNET_CLIENT_notify_transmit_ready (state->connection, ntohs (msg->size),
607 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
608 &connection_client_transmit_queued, mq);
612 struct GNUNET_MQ_Handle *
613 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
614 const struct GNUNET_MQ_MessageHandler *handlers,
615 GNUNET_MQ_ErrorHandler error_handler,
618 struct GNUNET_MQ_Handle *mq;
619 struct ClientConnectionState *state;
621 GNUNET_assert (NULL != connection);
623 mq = GNUNET_new (struct GNUNET_MQ_Handle);
624 mq->handlers = handlers;
625 mq->error_handler = error_handler;
626 mq->handlers_cls = cls;
627 state = GNUNET_new (struct ClientConnectionState);
628 state->connection = connection;
629 mq->impl_state = state;
630 mq->send_impl = connection_client_send_impl;
631 mq->destroy_impl = connection_client_destroy_impl;
632 if (NULL != handlers)
633 state->receive_requested = GNUNET_YES;
640 GNUNET_MQ_replace_handlers (struct GNUNET_MQ_Handle *mq,
641 const struct GNUNET_MQ_MessageHandler *new_handlers,
644 /* FIXME: notify implementation? */
645 /* FIXME: what about NULL handlers? abort receive? */
646 mq->handlers = new_handlers;
647 mq->handlers_cls = cls;
652 * Associate the assoc_data in mq with a unique request id.
654 * @param mq message queue, id will be unique for the queue
655 * @param assoc_data to associate
658 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
663 if (NULL == mq->assoc_map)
665 mq->assoc_map = GNUNET_CONTAINER_multihashmap32_create (8);
669 GNUNET_CONTAINER_multihashmap32_put (mq->assoc_map, id, assoc_data,
670 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
677 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq, uint32_t request_id)
679 if (NULL == mq->assoc_map)
681 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
686 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq, uint32_t request_id)
690 if (NULL == mq->assoc_map)
692 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id);
693 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map, request_id);
699 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *mqm,
700 GNUNET_MQ_NotifyCallback cb,
709 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
711 if (NULL != mq->destroy_impl)
713 mq->destroy_impl (mq, mq->impl_state);
716 while (NULL != mq->envelope_head)
718 struct GNUNET_MQ_Envelope *ev;
719 ev = mq->envelope_head;
720 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, mq->envelope_tail, ev);
721 GNUNET_MQ_discard (ev);
724 if (NULL != mq->current_envelope)
726 GNUNET_MQ_discard (mq->current_envelope);
727 mq->current_envelope = NULL;
734 struct GNUNET_MessageHeader *
735 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh, uint16_t base_size)
738 uint16_t nested_size;
739 struct GNUNET_MessageHeader *nested_msg;
741 whole_size = ntohs (mh->size);
742 GNUNET_assert (whole_size >= base_size);
744 nested_size = whole_size - base_size;
746 if (0 == nested_size)
749 if (nested_size < sizeof (struct GNUNET_MessageHeader))
755 nested_msg = (struct GNUNET_MessageHeader *) ((char *) mh + base_size);
757 if (ntohs (nested_msg->size) != nested_size)
760 nested_msg->size = htons (nested_size);