aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/util/mq.c430
1 files changed, 68 insertions, 362 deletions
diff --git a/src/util/mq.c b/src/util/mq.c
index 0d0ac3fc3..8749d5d21 100644
--- a/src/util/mq.c
+++ b/src/util/mq.c
@@ -182,49 +182,28 @@ struct GNUNET_MQ_Handle
182}; 182};
183 183
184 184
185/**
186 * Call the message message handler that was registered
187 * for the type of the given message in the given message queue.
188 *
189 * This function is intended to be used for the implementation
190 * of message queues.
191 *
192 * @param mq message queue with the handlers
193 * @param mh message to dispatch
194 */
195void 185void
196GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq, 186GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
197 const struct GNUNET_MessageHeader *mh) 187 const struct GNUNET_MessageHeader *mh)
198{ 188{
199 int ret; 189 enum GNUNET_GenericReturnValue ret;
200 190
201 ret = GNUNET_MQ_handle_message (mq->handlers, mh); 191 ret = GNUNET_MQ_handle_message (mq->handlers,
192 mh);
202 if (GNUNET_SYSERR == ret) 193 if (GNUNET_SYSERR == ret)
203 { 194 {
204 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_MALFORMED); 195 GNUNET_MQ_inject_error (mq,
196 GNUNET_MQ_ERROR_MALFORMED);
205 return; 197 return;
206 } 198 }
207} 199}
208 200
209 201
210/** 202enum GNUNET_GenericReturnValue
211 * Call the message message handler that was registered
212 * for the type of the given message in the given @a handlers list.
213 *
214 * This function is intended to be used for the implementation
215 * of message queues.
216 *
217 * @param handlers a set of handlers
218 * @param mh message to dispatch
219 * @return #GNUNET_OK on success, #GNUNET_NO if no handler matched,
220 * #GNUNET_SYSERR if message was rejected by check function
221 */
222int
223GNUNET_MQ_handle_message (const struct GNUNET_MQ_MessageHandler *handlers, 203GNUNET_MQ_handle_message (const struct GNUNET_MQ_MessageHandler *handlers,
224 const struct GNUNET_MessageHeader *mh) 204 const struct GNUNET_MessageHeader *mh)
225{ 205{
226 const struct GNUNET_MQ_MessageHandler *handler; 206 bool handled = false;
227 int handled = GNUNET_NO;
228 uint16_t msize = ntohs (mh->size); 207 uint16_t msize = ntohs (mh->size);
229 uint16_t mtype = ntohs (mh->type); 208 uint16_t mtype = ntohs (mh->type);
230 209
@@ -232,16 +211,18 @@ GNUNET_MQ_handle_message (const struct GNUNET_MQ_MessageHandler *handlers,
232 "Received message of type %u and size %u\n", 211 "Received message of type %u and size %u\n",
233 mtype, 212 mtype,
234 msize); 213 msize);
235
236 if (NULL == handlers) 214 if (NULL == handlers)
237 goto done; 215 goto done;
238 for (handler = handlers; NULL != handler->cb; handler++) 216 for (const struct GNUNET_MQ_MessageHandler *handler = handlers;
217 NULL != handler->cb;
218 handler++)
239 { 219 {
240 if (handler->type == mtype) 220 if (handler->type == mtype)
241 { 221 {
242 handled = GNUNET_YES; 222 handled = true;
243 if ((handler->expected_size > msize) || 223 if ( (handler->expected_size > msize) ||
244 ((handler->expected_size != msize) && (NULL == handler->mv))) 224 ( (handler->expected_size != msize) &&
225 (NULL == handler->mv) ) )
245 { 226 {
246 /* Too small, or not an exact size and 227 /* Too small, or not an exact size and
247 no 'mv' handler to check rest */ 228 no 'mv' handler to check rest */
@@ -250,8 +231,10 @@ GNUNET_MQ_handle_message (const struct GNUNET_MQ_MessageHandler *handlers,
250 (unsigned int) handler->type); 231 (unsigned int) handler->type);
251 return GNUNET_SYSERR; 232 return GNUNET_SYSERR;
252 } 233 }
253 if ((NULL == handler->mv) || 234 if ( (NULL == handler->mv) ||
254 (GNUNET_OK == handler->mv (handler->cls, mh))) 235 (GNUNET_OK ==
236 handler->mv (handler->cls,
237 mh)) )
255 { 238 {
256 /* message well-formed, pass to handler */ 239 /* message well-formed, pass to handler */
257 handler->cb (handler->cls, mh); 240 handler->cb (handler->cls, mh);
@@ -268,7 +251,7 @@ GNUNET_MQ_handle_message (const struct GNUNET_MQ_MessageHandler *handlers,
268 } 251 }
269 } 252 }
270done: 253done:
271 if (GNUNET_NO == handled) 254 if (! handled)
272 { 255 {
273 LOG (GNUNET_ERROR_TYPE_INFO, 256 LOG (GNUNET_ERROR_TYPE_INFO,
274 "No handler for message of type %u and size %u\n", 257 "No handler for message of type %u and size %u\n",
@@ -280,16 +263,6 @@ done:
280} 263}
281 264
282 265
283/**
284 * Call the error handler of a message queue with the given
285 * error code. If there is no error handler, log a warning.
286 *
287 * This function is intended to be used by the implementation
288 * of message queues.
289 *
290 * @param mq message queue
291 * @param error the error type
292 */
293void 266void
294GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq, 267GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
295 enum GNUNET_MQ_Error error) 268 enum GNUNET_MQ_Error error)
@@ -306,13 +279,6 @@ GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
306} 279}
307 280
308 281
309/**
310 * Discard the message queue message, free all
311 * allocated resources. Must be called in the event
312 * that a message is created but should not actually be sent.
313 *
314 * @param mqm the message to discard
315 */
316void 282void
317GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *ev) 283GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *ev)
318{ 284{
@@ -321,12 +287,6 @@ GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *ev)
321} 287}
322 288
323 289
324/**
325 * Obtain the current length of the message queue.
326 *
327 * @param mq queue to inspect
328 * @return number of queued, non-transmitted messages
329 */
330unsigned int 290unsigned int
331GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq) 291GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
332{ 292{
@@ -338,13 +298,6 @@ GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq)
338} 298}
339 299
340 300
341/**
342 * Send a message with the given message queue.
343 * May only be called once per message.
344 *
345 * @param mq message queue
346 * @param ev the envelope with the message to send.
347 */
348void 301void
349GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq, 302GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
350 struct GNUNET_MQ_Envelope *ev) 303 struct GNUNET_MQ_Envelope *ev)
@@ -389,33 +342,21 @@ GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
389} 342}
390 343
391 344
392/**
393 * Remove the first envelope that has not yet been sent from the message
394 * queue and return it.
395 *
396 * @param mq queue to remove envelope from
397 * @return NULL if queue is empty (or has no envelope that is not under transmission)
398 */
399struct GNUNET_MQ_Envelope * 345struct GNUNET_MQ_Envelope *
400GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq) 346GNUNET_MQ_unsent_head (struct GNUNET_MQ_Handle *mq)
401{ 347{
402 struct GNUNET_MQ_Envelope *env; 348 struct GNUNET_MQ_Envelope *env;
403 349
404 env = mq->envelope_head; 350 env = mq->envelope_head;
405 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, mq->envelope_tail, env); 351 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
352 mq->envelope_tail,
353 env);
406 mq->queue_length--; 354 mq->queue_length--;
407 env->parent_queue = NULL; 355 env->parent_queue = NULL;
408 return env; 356 return env;
409} 357}
410 358
411 359
412/**
413 * Function to copy an envelope. The envelope must not yet
414 * be in any queue or have any options or callbacks set.
415 *
416 * @param env envelope to copy
417 * @return copy of @a env
418 */
419struct GNUNET_MQ_Envelope * 360struct GNUNET_MQ_Envelope *
420GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env) 361GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env)
421{ 362{
@@ -427,13 +368,6 @@ GNUNET_MQ_env_copy (struct GNUNET_MQ_Envelope *env)
427} 368}
428 369
429 370
430/**
431 * Send a copy of a message with the given message queue.
432 * Can be called repeatedly on the same envelope.
433 *
434 * @param mq message queue
435 * @param ev the envelope with the message to send.
436 */
437void 371void
438GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq, 372GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
439 const struct GNUNET_MQ_Envelope *ev) 373 const struct GNUNET_MQ_Envelope *ev)
@@ -485,13 +419,6 @@ impl_send_continue (void *cls)
485} 419}
486 420
487 421
488/**
489 * Call the send implementation for the next queued message, if any.
490 * Only useful for implementing message queues, results in undefined
491 * behavior if not used carefully.
492 *
493 * @param mq message queue to send the next message with
494 */
495void 422void
496GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq) 423GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
497{ 424{
@@ -515,16 +442,6 @@ GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq)
515} 442}
516 443
517 444
518/**
519 * Call the send notification for the current message, but do not
520 * try to send the next message until #GNUNET_MQ_impl_send_continue
521 * is called.
522 *
523 * Only useful for implementing message queues, results in undefined
524 * behavior if not used carefully.
525 *
526 * @param mq message queue to send the next message with
527 */
528void 445void
529GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq) 446GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq)
530{ 447{
@@ -546,18 +463,6 @@ GNUNET_MQ_impl_send_in_flight (struct GNUNET_MQ_Handle *mq)
546} 463}
547 464
548 465
549/**
550 * Create a message queue for the specified handlers.
551 *
552 * @param send function the implements sending messages
553 * @param destroy function that implements destroying the queue
554 * @param cancel function that implements canceling a message
555 * @param impl_state for the queue, passed to 'send' and 'destroy'
556 * @param handlers array of message handlers
557 * @param error_handler handler for read and write errors
558 * @param error_handler_cls closure for @a error_handler
559 * @return a new message queue
560 */
561struct GNUNET_MQ_Handle * 466struct GNUNET_MQ_Handle *
562GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send, 467GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
563 GNUNET_MQ_DestroyImpl destroy, 468 GNUNET_MQ_DestroyImpl destroy,
@@ -582,15 +487,9 @@ GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
582} 487}
583 488
584 489
585/**
586 * Change the closure argument in all of the `handlers` of the
587 * @a mq.
588 *
589 * @param mq to modify
590 * @param handlers_cls new closure to use
591 */
592void 490void
593GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq, void *handlers_cls) 491GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq,
492 void *handlers_cls)
594{ 493{
595 if (NULL == mq->handlers) 494 if (NULL == mq->handlers)
596 return; 495 return;
@@ -599,15 +498,6 @@ GNUNET_MQ_set_handlers_closure (struct GNUNET_MQ_Handle *mq, void *handlers_cls)
599} 498}
600 499
601 500
602/**
603 * Get the message that should currently be sent.
604 * Fails if there is no current message.
605 * Only useful for implementing message queues,
606 * results in undefined behavior if not used carefully.
607 *
608 * @param mq message queue with the current message
609 * @return message to send, never NULL
610 */
611const struct GNUNET_MessageHeader * 501const struct GNUNET_MessageHeader *
612GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq) 502GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
613{ 503{
@@ -617,20 +507,6 @@ GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq)
617} 507}
618 508
619 509
620/**
621 * Get the implementation state associated with the
622 * message queue.
623 *
624 * While the GNUNET_MQ_Impl* callbacks receive the
625 * implementation state, continuations that are scheduled
626 * by the implementation function often only have one closure
627 * argument, with this function it is possible to get at the
628 * implementation state when only passing the GNUNET_MQ_Handle
629 * as closure.
630 *
631 * @param mq message queue with the current message
632 * @return message to send, never NULL
633 */
634void * 510void *
635GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq) 511GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
636{ 512{
@@ -639,7 +515,9 @@ GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq)
639 515
640 516
641struct GNUNET_MQ_Envelope * 517struct GNUNET_MQ_Envelope *
642GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type) 518GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
519 uint16_t size,
520 uint16_t type)
643{ 521{
644 struct GNUNET_MQ_Envelope *ev; 522 struct GNUNET_MQ_Envelope *ev;
645 523
@@ -653,12 +531,6 @@ GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type)
653} 531}
654 532
655 533
656/**
657 * Create a new envelope by copying an existing message.
658 *
659 * @param hdr header of the message to copy
660 * @return envelope containing @a hdr
661 */
662struct GNUNET_MQ_Envelope * 534struct GNUNET_MQ_Envelope *
663GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr) 535GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
664{ 536{
@@ -674,15 +546,6 @@ GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr)
674} 546}
675 547
676 548
677/**
678 * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
679 *
680 * @param mhp pointer to the message header pointer that will be changed to allocate at
681 * the newly allocated space for the message.
682 * @param base_size size of the data before the nested message
683 * @param type type of the message in the envelope
684 * @param nested_mh the message to append to the message after base_size
685 */
686struct GNUNET_MQ_Envelope * 549struct GNUNET_MQ_Envelope *
687GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp, 550GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
688 uint16_t base_size, 551 uint16_t base_size,
@@ -693,31 +556,26 @@ GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
693 uint16_t size; 556 uint16_t size;
694 557
695 if (NULL == nested_mh) 558 if (NULL == nested_mh)
696 return GNUNET_MQ_msg_ (mhp, base_size, type); 559 return GNUNET_MQ_msg_ (mhp,
697 560 base_size,
561 type);
698 size = base_size + ntohs (nested_mh->size); 562 size = base_size + ntohs (nested_mh->size);
699
700 /* check for uint16_t overflow */ 563 /* check for uint16_t overflow */
701 if (size < base_size) 564 if (size < base_size)
702 return NULL; 565 return NULL;
703 566 mqm = GNUNET_MQ_msg_ (mhp,
704 mqm = GNUNET_MQ_msg_ (mhp, size, type); 567 size,
568 type);
705 GNUNET_memcpy ((char *) mqm->mh + base_size, 569 GNUNET_memcpy ((char *) mqm->mh + base_size,
706 nested_mh, 570 nested_mh,
707 ntohs (nested_mh->size)); 571 ntohs (nested_mh->size));
708
709 return mqm; 572 return mqm;
710} 573}
711 574
712 575
713/**
714 * Associate the assoc_data in mq with a unique request id.
715 *
716 * @param mq message queue, id will be unique for the queue
717 * @param assoc_data to associate
718 */
719uint32_t 576uint32_t
720GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data) 577GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
578 void *assoc_data)
721{ 579{
722 uint32_t id; 580 uint32_t id;
723 581
@@ -745,11 +603,13 @@ GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data)
745 * @return the associated data 603 * @return the associated data
746 */ 604 */
747void * 605void *
748GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq, uint32_t request_id) 606GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
607 uint32_t request_id)
749{ 608{
750 if (NULL == mq->assoc_map) 609 if (NULL == mq->assoc_map)
751 return NULL; 610 return NULL;
752 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id); 611 return GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
612 request_id);
753} 613}
754 614
755 615
@@ -761,27 +621,21 @@ GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq, uint32_t request_id)
761 * @return the associated data 621 * @return the associated data
762 */ 622 */
763void * 623void *
764GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq, uint32_t request_id) 624GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
625 uint32_t request_id)
765{ 626{
766 void *val; 627 void *val;
767 628
768 if (NULL == mq->assoc_map) 629 if (NULL == mq->assoc_map)
769 return NULL; 630 return NULL;
770 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map, request_id); 631 val = GNUNET_CONTAINER_multihashmap32_get (mq->assoc_map,
771 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map, request_id); 632 request_id);
633 GNUNET_CONTAINER_multihashmap32_remove_all (mq->assoc_map,
634 request_id);
772 return val; 635 return val;
773} 636}
774 637
775 638
776/**
777 * Call a callback once the envelope has been sent, that is,
778 * sending it can not be canceled anymore.
779 * There can be only one notify sent callback per envelope.
780 *
781 * @param ev message to call the notify callback for
782 * @param cb the notify callback
783 * @param cb_cls closure for the callback
784 */
785void 639void
786GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev, 640GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
787 GNUNET_SCHEDULER_TaskCallback cb, 641 GNUNET_SCHEDULER_TaskCallback cb,
@@ -827,11 +681,6 @@ struct GNUNET_MQ_DestroyNotificationHandle
827}; 681};
828 682
829 683
830/**
831 * Destroy the message queue.
832 *
833 * @param mq message queue to destroy
834 */
835void 684void
836GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq) 685GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq)
837{ 686{
@@ -917,13 +766,6 @@ GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
917} 766}
918 767
919 768
920/**
921 * Cancel sending the message. Message must have been sent with
922 * #GNUNET_MQ_send before. May not be called after the notify sent
923 * callback has been called
924 *
925 * @param ev queued envelope to cancel
926 */
927void 769void
928GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev) 770GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
929{ 771{
@@ -931,14 +773,13 @@ GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
931 773
932 GNUNET_assert (NULL != mq); 774 GNUNET_assert (NULL != mq);
933 GNUNET_assert (NULL != mq->cancel_impl); 775 GNUNET_assert (NULL != mq->cancel_impl);
934 776 GNUNET_assert (0 < mq->queue_length);
777 mq->queue_length--;
935 if (mq->current_envelope == ev) 778 if (mq->current_envelope == ev)
936 { 779 {
937 /* complex case, we already started with transmitting 780 /* complex case, we already started with transmitting
938 the message using the callbacks. */ 781 the message using the callbacks. */
939 GNUNET_assert (! mq->in_flight); 782 GNUNET_assert (! mq->in_flight);
940 GNUNET_assert (0 < mq->queue_length);
941 mq->queue_length--;
942 mq->cancel_impl (mq, 783 mq->cancel_impl (mq,
943 mq->impl_state); 784 mq->impl_state);
944 /* continue sending the next message, if any */ 785 /* continue sending the next message, if any */
@@ -948,7 +789,6 @@ GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
948 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, 789 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
949 mq->envelope_tail, 790 mq->envelope_tail,
950 mq->current_envelope); 791 mq->current_envelope);
951
952 LOG (GNUNET_ERROR_TYPE_DEBUG, 792 LOG (GNUNET_ERROR_TYPE_DEBUG,
953 "sending canceled message of type %u queue\n", 793 "sending canceled message of type %u queue\n",
954 ntohs (ev->mh->type)); 794 ntohs (ev->mh->type));
@@ -963,8 +803,6 @@ GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
963 GNUNET_CONTAINER_DLL_remove (mq->envelope_head, 803 GNUNET_CONTAINER_DLL_remove (mq->envelope_head,
964 mq->envelope_tail, 804 mq->envelope_tail,
965 ev); 805 ev);
966 GNUNET_assert (0 < mq->queue_length);
967 mq->queue_length--;
968 } 806 }
969 ev->parent_queue = NULL; 807 ev->parent_queue = NULL;
970 ev->mh = NULL; 808 ev->mh = NULL;
@@ -973,13 +811,6 @@ GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev)
973} 811}
974 812
975 813
976/**
977 * Function to obtain the current envelope
978 * from within #GNUNET_MQ_SendImpl implementations.
979 *
980 * @param mq message queue to interrogate
981 * @return the current envelope
982 */
983struct GNUNET_MQ_Envelope * 814struct GNUNET_MQ_Envelope *
984GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq) 815GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
985{ 816{
@@ -987,12 +818,6 @@ GNUNET_MQ_get_current_envelope (struct GNUNET_MQ_Handle *mq)
987} 818}
988 819
989 820
990/**
991 * Function to obtain the last envelope in the queue.
992 *
993 * @param mq message queue to interrogate
994 * @return the last envelope in the queue
995 */
996struct GNUNET_MQ_Envelope * 821struct GNUNET_MQ_Envelope *
997GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq) 822GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
998{ 823{
@@ -1003,14 +828,6 @@ GNUNET_MQ_get_last_envelope (struct GNUNET_MQ_Handle *mq)
1003} 828}
1004 829
1005 830
1006/**
1007 * Set application-specific preferences for this envelope.
1008 * Overrides the options set for the queue with
1009 * #GNUNET_MQ_set_options() for this message only.
1010 *
1011 * @param env message to set options for
1012 * @param pp priorities and preferences to apply
1013 */
1014void 831void
1015GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env, 832GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
1016 enum GNUNET_MQ_PriorityPreferences pp) 833 enum GNUNET_MQ_PriorityPreferences pp)
@@ -1020,12 +837,6 @@ GNUNET_MQ_env_set_options (struct GNUNET_MQ_Envelope *env,
1020} 837}
1021 838
1022 839
1023/**
1024 * Get application-specific options for this envelope.
1025 *
1026 * @param env message to set options for
1027 * @return priorities and preferences to apply for @a env
1028 */
1029enum GNUNET_MQ_PriorityPreferences 840enum GNUNET_MQ_PriorityPreferences
1030GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env) 841GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env)
1031{ 842{
@@ -1039,14 +850,6 @@ GNUNET_MQ_env_get_options (struct GNUNET_MQ_Envelope *env)
1039} 850}
1040 851
1041 852
1042/**
1043 * Combine performance preferences set for different
1044 * envelopes that are being combined into one larger envelope.
1045 *
1046 * @param p1 one set of preferences
1047 * @param p2 second set of preferences
1048 * @return combined priority and preferences to use
1049 */
1050enum GNUNET_MQ_PriorityPreferences 853enum GNUNET_MQ_PriorityPreferences
1051GNUNET_MQ_env_combine_options (enum GNUNET_MQ_PriorityPreferences p1, 854GNUNET_MQ_env_combine_options (enum GNUNET_MQ_PriorityPreferences p1,
1052 enum GNUNET_MQ_PriorityPreferences p2) 855 enum GNUNET_MQ_PriorityPreferences p2)
@@ -1066,12 +869,6 @@ GNUNET_MQ_env_combine_options (enum GNUNET_MQ_PriorityPreferences p1,
1066} 869}
1067 870
1068 871
1069/**
1070 * Set application-specific default options for this queue.
1071 *
1072 * @param mq message queue to set options for
1073 * @param pp priorities and preferences to apply
1074 */
1075void 872void
1076GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq, 873GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1077 enum GNUNET_MQ_PriorityPreferences pp) 874 enum GNUNET_MQ_PriorityPreferences pp)
@@ -1080,12 +877,6 @@ GNUNET_MQ_set_options (struct GNUNET_MQ_Handle *mq,
1080} 877}
1081 878
1082 879
1083/**
1084 * Obtain message contained in envelope.
1085 *
1086 * @param env the envelope
1087 * @return message contained in the envelope
1088 */
1089const struct GNUNET_MessageHeader * 880const struct GNUNET_MessageHeader *
1090GNUNET_MQ_env_get_msg (const struct GNUNET_MQ_Envelope *env) 881GNUNET_MQ_env_get_msg (const struct GNUNET_MQ_Envelope *env)
1091{ 882{
@@ -1093,12 +884,6 @@ GNUNET_MQ_env_get_msg (const struct GNUNET_MQ_Envelope *env)
1093} 884}
1094 885
1095 886
1096/**
1097 * Return next envelope in queue.
1098 *
1099 * @param env a queued envelope
1100 * @return next one, or NULL
1101 */
1102const struct GNUNET_MQ_Envelope * 887const struct GNUNET_MQ_Envelope *
1103GNUNET_MQ_env_next (const struct GNUNET_MQ_Envelope *env) 888GNUNET_MQ_env_next (const struct GNUNET_MQ_Envelope *env)
1104{ 889{
@@ -1106,15 +891,6 @@ GNUNET_MQ_env_next (const struct GNUNET_MQ_Envelope *env)
1106} 891}
1107 892
1108 893
1109/**
1110 * Register function to be called whenever @a mq is being
1111 * destroyed.
1112 *
1113 * @param mq message queue to watch
1114 * @param cb function to call on @a mq destruction
1115 * @param cb_cls closure for @a cb
1116 * @return handle for #GNUNET_MQ_destroy_notify_cancel().
1117 */
1118struct GNUNET_MQ_DestroyNotificationHandle * 894struct GNUNET_MQ_DestroyNotificationHandle *
1119GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq, 895GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1120 GNUNET_SCHEDULER_TaskCallback cb, 896 GNUNET_SCHEDULER_TaskCallback cb,
@@ -1126,100 +902,59 @@ GNUNET_MQ_destroy_notify (struct GNUNET_MQ_Handle *mq,
1126 dnh->mq = mq; 902 dnh->mq = mq;
1127 dnh->cb = cb; 903 dnh->cb = cb;
1128 dnh->cb_cls = cb_cls; 904 dnh->cb_cls = cb_cls;
1129 GNUNET_CONTAINER_DLL_insert (mq->dnh_head, mq->dnh_tail, dnh); 905 GNUNET_CONTAINER_DLL_insert (mq->dnh_head,
906 mq->dnh_tail,
907 dnh);
1130 return dnh; 908 return dnh;
1131} 909}
1132 910
1133 911
1134/**
1135 * Cancel registration from #GNUNET_MQ_destroy_notify().
1136 *
1137 * @param dnh handle for registration to cancel
1138 */
1139void 912void
1140GNUNET_MQ_destroy_notify_cancel (struct 913GNUNET_MQ_destroy_notify_cancel (
1141 GNUNET_MQ_DestroyNotificationHandle *dnh) 914 struct GNUNET_MQ_DestroyNotificationHandle *dnh)
1142{ 915{
1143 struct GNUNET_MQ_Handle *mq = dnh->mq; 916 struct GNUNET_MQ_Handle *mq = dnh->mq;
1144 917
1145 GNUNET_CONTAINER_DLL_remove (mq->dnh_head, mq->dnh_tail, dnh); 918 GNUNET_CONTAINER_DLL_remove (mq->dnh_head,
919 mq->dnh_tail,
920 dnh);
1146 GNUNET_free (dnh); 921 GNUNET_free (dnh);
1147} 922}
1148 923
1149 924
1150/**
1151 * Insert @a env into the envelope DLL starting at @a env_head
1152 * Note that @a env must not be in any MQ while this function
1153 * is used with DLLs defined outside of the MQ module. This
1154 * is just in case some application needs to also manage a
1155 * FIFO of envelopes independent of MQ itself and wants to
1156 * re-use the pointers internal to @a env. Use with caution.
1157 *
1158 * @param[in|out] env_head of envelope DLL
1159 * @param[in|out] env_tail tail of envelope DLL
1160 * @param[in|out] env element to insert at the tail
1161 */
1162void 925void
1163GNUNET_MQ_dll_insert_head (struct GNUNET_MQ_Envelope **env_head, 926GNUNET_MQ_dll_insert_head (struct GNUNET_MQ_Envelope **env_head,
1164 struct GNUNET_MQ_Envelope **env_tail, 927 struct GNUNET_MQ_Envelope **env_tail,
1165 struct GNUNET_MQ_Envelope *env) 928 struct GNUNET_MQ_Envelope *env)
1166{ 929{
1167 GNUNET_CONTAINER_DLL_insert (*env_head, *env_tail, env); 930 GNUNET_CONTAINER_DLL_insert (*env_head,
931 *env_tail,
932 env);
1168} 933}
1169 934
1170 935
1171/**
1172 * Insert @a env into the envelope DLL starting at @a env_head
1173 * Note that @a env must not be in any MQ while this function
1174 * is used with DLLs defined outside of the MQ module. This
1175 * is just in case some application needs to also manage a
1176 * FIFO of envelopes independent of MQ itself and wants to
1177 * re-use the pointers internal to @a env. Use with caution.
1178 *
1179 * @param[in|out] env_head of envelope DLL
1180 * @param[in|out] env_tail tail of envelope DLL
1181 * @param[in|out] env element to insert at the tail
1182 */
1183void 936void
1184GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head, 937GNUNET_MQ_dll_insert_tail (struct GNUNET_MQ_Envelope **env_head,
1185 struct GNUNET_MQ_Envelope **env_tail, 938 struct GNUNET_MQ_Envelope **env_tail,
1186 struct GNUNET_MQ_Envelope *env) 939 struct GNUNET_MQ_Envelope *env)
1187{ 940{
1188 GNUNET_CONTAINER_DLL_insert_tail (*env_head, *env_tail, env); 941 GNUNET_CONTAINER_DLL_insert_tail (*env_head,
942 *env_tail,
943 env);
1189} 944}
1190 945
1191 946
1192/**
1193 * Remove @a env from the envelope DLL starting at @a env_head.
1194 * Note that @a env must not be in any MQ while this function
1195 * is used with DLLs defined outside of the MQ module. This
1196 * is just in case some application needs to also manage a
1197 * FIFO of envelopes independent of MQ itself and wants to
1198 * re-use the pointers internal to @a env. Use with caution.
1199 *
1200 * @param[in|out] env_head of envelope DLL
1201 * @param[in|out] env_tail tail of envelope DLL
1202 * @param[in|out] env element to remove from the DLL
1203 */
1204void 947void
1205GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head, 948GNUNET_MQ_dll_remove (struct GNUNET_MQ_Envelope **env_head,
1206 struct GNUNET_MQ_Envelope **env_tail, 949 struct GNUNET_MQ_Envelope **env_tail,
1207 struct GNUNET_MQ_Envelope *env) 950 struct GNUNET_MQ_Envelope *env)
1208{ 951{
1209 GNUNET_CONTAINER_DLL_remove (*env_head, *env_tail, env); 952 GNUNET_CONTAINER_DLL_remove (*env_head,
953 *env_tail,
954 env);
1210} 955}
1211 956
1212 957
1213/**
1214 * Copy an array of handlers.
1215 *
1216 * Useful if the array has been declared in local memory and needs to be
1217 * persisted for future use.
1218 *
1219 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
1220 * @return A newly allocated array of handlers.
1221 * Needs to be freed with #GNUNET_free.
1222 */
1223struct GNUNET_MQ_MessageHandler * 958struct GNUNET_MQ_MessageHandler *
1224GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers) 959GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1225{ 960{
@@ -1228,9 +963,9 @@ GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1228 963
1229 if (NULL == handlers) 964 if (NULL == handlers)
1230 return NULL; 965 return NULL;
1231
1232 count = GNUNET_MQ_count_handlers (handlers); 966 count = GNUNET_MQ_count_handlers (handlers);
1233 copy = GNUNET_new_array (count + 1, struct GNUNET_MQ_MessageHandler); 967 copy = GNUNET_new_array (count + 1,
968 struct GNUNET_MQ_MessageHandler);
1234 GNUNET_memcpy (copy, 969 GNUNET_memcpy (copy,
1235 handlers, 970 handlers,
1236 count * sizeof(struct GNUNET_MQ_MessageHandler)); 971 count * sizeof(struct GNUNET_MQ_MessageHandler));
@@ -1238,18 +973,6 @@ GNUNET_MQ_copy_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1238} 973}
1239 974
1240 975
1241/**
1242 * Copy an array of handlers, appending AGPL handler.
1243 *
1244 * Useful if the array has been declared in local memory and needs to be
1245 * persisted for future use.
1246 *
1247 * @param handlers Array of handlers to be copied. Can be NULL (nothing done).
1248 * @param agpl_handler function to call for AGPL handling
1249 * @param agpl_cls closure for @a agpl_handler
1250 * @return A newly allocated array of handlers.
1251 * Needs to be freed with #GNUNET_free.
1252 */
1253struct GNUNET_MQ_MessageHandler * 976struct GNUNET_MQ_MessageHandler *
1254GNUNET_MQ_copy_handlers2 (const struct GNUNET_MQ_MessageHandler *handlers, 977GNUNET_MQ_copy_handlers2 (const struct GNUNET_MQ_MessageHandler *handlers,
1255 GNUNET_MQ_MessageCallback agpl_handler, 978 GNUNET_MQ_MessageCallback agpl_handler,
@@ -1261,7 +984,8 @@ GNUNET_MQ_copy_handlers2 (const struct GNUNET_MQ_MessageHandler *handlers,
1261 if (NULL == handlers) 984 if (NULL == handlers)
1262 return NULL; 985 return NULL;
1263 count = GNUNET_MQ_count_handlers (handlers); 986 count = GNUNET_MQ_count_handlers (handlers);
1264 copy = GNUNET_new_array (count + 2, struct GNUNET_MQ_MessageHandler); 987 copy = GNUNET_new_array (count + 2,
988 struct GNUNET_MQ_MessageHandler);
1265 GNUNET_memcpy (copy, 989 GNUNET_memcpy (copy,
1266 handlers, 990 handlers,
1267 count * sizeof(struct GNUNET_MQ_MessageHandler)); 991 count * sizeof(struct GNUNET_MQ_MessageHandler));
@@ -1274,12 +998,6 @@ GNUNET_MQ_copy_handlers2 (const struct GNUNET_MQ_MessageHandler *handlers,
1274} 998}
1275 999
1276 1000
1277/**
1278 * Count the handlers in a handler array.
1279 *
1280 * @param handlers Array of handlers to be counted.
1281 * @return The number of handlers in the array.
1282 */
1283unsigned int 1001unsigned int
1284GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers) 1002GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1285{ 1003{
@@ -1287,20 +1005,12 @@ GNUNET_MQ_count_handlers (const struct GNUNET_MQ_MessageHandler *handlers)
1287 1005
1288 if (NULL == handlers) 1006 if (NULL == handlers)
1289 return 0; 1007 return 0;
1290
1291 for (i = 0; NULL != handlers[i].cb; i++) 1008 for (i = 0; NULL != handlers[i].cb; i++)
1292 ; 1009 ;
1293
1294 return i; 1010 return i;
1295} 1011}
1296 1012
1297 1013
1298/**
1299 * Convert an `enum GNUNET_MQ_PreferenceType` to a string
1300 *
1301 * @param type the preference type
1302 * @return a string or NULL if invalid
1303 */
1304const char * 1014const char *
1305GNUNET_MQ_preference_to_string (enum GNUNET_MQ_PreferenceKind type) 1015GNUNET_MQ_preference_to_string (enum GNUNET_MQ_PreferenceKind type)
1306{ 1016{
@@ -1308,17 +1018,13 @@ GNUNET_MQ_preference_to_string (enum GNUNET_MQ_PreferenceKind type)
1308 { 1018 {
1309 case GNUNET_MQ_PREFERENCE_NONE: 1019 case GNUNET_MQ_PREFERENCE_NONE:
1310 return "NONE"; 1020 return "NONE";
1311
1312 case GNUNET_MQ_PREFERENCE_BANDWIDTH: 1021 case GNUNET_MQ_PREFERENCE_BANDWIDTH:
1313 return "BANDWIDTH"; 1022 return "BANDWIDTH";
1314
1315 case GNUNET_MQ_PREFERENCE_LATENCY: 1023 case GNUNET_MQ_PREFERENCE_LATENCY:
1316 return "LATENCY"; 1024 return "LATENCY";
1317
1318 case GNUNET_MQ_PREFERENCE_RELIABILITY: 1025 case GNUNET_MQ_PREFERENCE_RELIABILITY:
1319 return "RELIABILITY"; 1026 return "RELIABILITY";
1320 } 1027 }
1321 ;
1322 return NULL; 1028 return NULL;
1323} 1029}
1324 1030