add function conv param string
[oweals/gnunet.git] / src / include / gnunet_mq_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012-2016 GNUnet e.V.
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @author Florian Dold
23  * @author Christian Grothoff
24  *
25  * @file
26  * General-purpose message queue
27  *
28  * @defgroup mq  MQ library
29  * General-purpose message queue
30  *
31  * @see [Documentation](https://gnunet.org/message-queue-api)
32  *
33  * @{
34  */
35 #ifndef GNUNET_MQ_H
36 #define GNUNET_MQ_H
37
38
39 /**
40  * Allocate an envelope, with extra space allocated after the space needed
41  * by the message struct.
42  * The allocated message will already have the type and size field set.
43  *
44  * @param mvar variable to store the allocated message in;
45  *             must have a header field
46  * @param esize extra space to allocate after the message
47  * @param type type of the message
48  * @return the MQ message
49  */
50 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
51
52 /**
53  * Allocate a GNUNET_MQ_Envelope.
54  * The contained message will already have the type and size field set.
55  *
56  * @param mvar variable to store the allocated message in;
57  *             must have a header field
58  * @param type type of the message
59  * @return the allocated envelope
60  */
61 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
62
63
64 /**
65  * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
66  * The allocated message will already have the type and size field set.
67  *
68  * @param type type of the message
69  */
70 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
71
72
73 /**
74  * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
75  * The allocated message will already have the type and size field set.
76  *
77  * @param mh pointer that will changed to point at to the allocated message header
78  * @param esize extra space to allocate after the message header
79  * @param type type of the message
80  */
81 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
82
83
84 /**
85  * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
86  * message struct.
87  *
88  * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
89  *        whose size is 'sizeof(*mvar) + ntohs (mh->size)'
90  * @param type message type of the allocated message, has no effect on the nested message
91  * @param mh message to nest
92  * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
93  */
94 #define GNUNET_MQ_msg_nested_mh(mvar, type, mh) GNUNET_MQ_msg_nested_mh_((((void)(mvar)->header), (struct GNUNET_MessageHeader**) &(mvar)), sizeof (*(mvar)), (type), mh)
95
96
97 /**
98  * Return a pointer to the message at the end of the given message.
99  *
100  * @param var pointer to a message struct, the type of the expression determines the base size,
101  *        the space after the base size is the nested message
102  * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
103  *         or NULL if the given message in @a var does not have any space after the message struct
104  */
105 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
106
107
108 /**
109  * Implementation of the GNUNET_MQ_extract_nexted_mh macro.
110  *
111  * @param mh message header to extract nested message header from
112  * @param base_size size of the message before the nested message's header appears
113  * @return pointer to the nested message, does not copy the message
114  */
115 const struct GNUNET_MessageHeader *
116 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
117                               uint16_t base_size);
118
119
120 /**
121  * Implementation of the #GNUNET_MQ_msg_nested_mh macro.
122  *
123  * @param mhp pointer to the message header pointer that will be changed to allocate at
124  *        the newly allocated space for the message.
125  * @param base_size size of the data before the nested message
126  * @param type type of the message in the envelope
127  * @param nested_mh the message to append to the message after base_size
128  */
129 struct GNUNET_MQ_Envelope *
130 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
131                           uint16_t base_size,
132                           uint16_t type,
133                           const struct GNUNET_MessageHeader *nested_mh);
134
135
136 /**
137  * Opaque handle to a message queue.
138  */
139 struct GNUNET_MQ_Handle;
140
141 /**
142  * Opaque handle to an envelope.
143  */
144 struct GNUNET_MQ_Envelope;
145
146
147 /**
148  * Error codes for the queue.
149  */
150 enum GNUNET_MQ_Error
151 {
152   /**
153    * Failed to read message from the network.
154    * FIXME: Likely not properly distinguished
155    * from TIMEOUT case in the code!
156    */
157   GNUNET_MQ_ERROR_READ = 1,
158
159   /**
160    * FIXME: document!
161    */
162   GNUNET_MQ_ERROR_WRITE = 2,
163
164   /**
165    * FIXME: document!
166    */
167   GNUNET_MQ_ERROR_TIMEOUT = 4,
168
169   /**
170    * We received a message that was malformed and thus
171    * could not be passed to its handler.
172    */
173   GNUNET_MQ_ERROR_MALFORMED = 8
174 };
175
176
177 /**
178  * Called when a message has been received.
179  *
180  * @param cls closure
181  * @param msg the received message
182  */
183 typedef void
184 (*GNUNET_MQ_MessageCallback) (void *cls,
185                               const struct GNUNET_MessageHeader *msg);
186
187
188 /**
189  * Called when a message needs to be validated.
190  *
191  * @param cls closure
192  * @param msg the received message
193  * @return #GNUNET_OK if the message is well-formed,
194  *         #GNUNET_SYSERR if not
195  */
196 typedef int
197 (*GNUNET_MQ_MessageValidationCallback) (void *cls,
198                                         const struct GNUNET_MessageHeader *msg);
199
200
201 /**
202  * Signature of functions implementing the
203  * sending functionality of a message queue.
204  *
205  * @param mq the message queue
206  * @param msg the message to send
207  * @param impl_state state of the implementation
208  */
209 typedef void
210 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
211                        const struct GNUNET_MessageHeader *msg,
212                        void *impl_state);
213
214
215 /**
216  * Signature of functions implementing the
217  * destruction of a message queue.
218  * Implementations must not free @a mq, but should
219  * take care of @a impl_state.
220  *
221  * @param mq the message queue to destroy
222  * @param impl_state state of the implementation
223  */
224 typedef void
225 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
226                           void *impl_state);
227
228
229 /**
230  * Implementation function that cancels the currently sent message.
231  *
232  * @param mq message queue
233  * @param impl_state state specific to the implementation
234  */
235 typedef void
236 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
237                          void *impl_state);
238
239
240 /**
241  * Callback used for notifications
242  *
243  * @param cls closure
244  */
245 typedef void
246 (*GNUNET_MQ_NotifyCallback) (void *cls);
247
248
249 /**
250  * Generic error handler, called with the appropriate
251  * error code and the same closure specified at the creation of
252  * the message queue.
253  * Not every message queue implementation supports an error handler.
254  *
255  * @param cls closure
256  * @param error error code
257  */
258 typedef void
259 (*GNUNET_MQ_ErrorHandler) (void *cls,
260                            enum GNUNET_MQ_Error error);
261
262
263 /**
264  * Message handler for a specific message type.
265  */
266 struct GNUNET_MQ_MessageHandler
267 {
268   /**
269    * Callback to validate a message of the specified @e type.
270    * The closure given to @e mv will be this struct (not @e ctx).
271    * Using NULL means only size-validation using
272    * @e expected_size.  In this case, @e expected_size must
273    * be non-zero.
274    */
275   GNUNET_MQ_MessageValidationCallback mv;
276
277   /**
278    * Callback, called every time a new message of
279    * the specified @e type has been receied.
280    * The closure given to @e mv will be this struct (not @e ctx).
281    */
282   GNUNET_MQ_MessageCallback cb;
283
284   /**
285    * Closure for @e mv and @e cb.
286    */
287   void *cls;
288
289   /**
290    * Type of the message this handler covers, in host byte order.
291    */
292   uint16_t type;
293
294   /**
295    * Expected size of messages of this type.  Minimum size of the
296    * message if @e mv is non-NULL.  Messages of the given type will be
297    * discarded (and the connection closed with an error reported to
298    * the application) if they do not have the right size.
299    */
300   uint16_t expected_size;
301 };
302
303
304 /**
305  * End-marker for the handlers array
306  */
307 #define GNUNET_MQ_handler_end() {NULL, NULL, NULL, 0, 0}
308
309
310 /**
311  * Defines a static function @a name which takes as a single argument
312  * a message handler for fixed-sized messages of type @a code and with
313  * a message type argument of @a str.  Given such an argument, the
314  * function @name will return a `struct GNUNET_MQ_MessageHandler`
315  * for the given message type.
316  *
317  * The macro is to be used as follows:
318  * <code>
319  * struct GNUNET_MessageTest { ... }; // must be fixed size
320  * static void
321  * handle_test_message (void *cls,
322  *                      const struct GNUNET_MessageTest *msg)
323  * { ... }
324  *
325  * GNUNET_MQ_hd_fixed_size(test_message,
326  *                         GNUNET_MESSAGE_TYPE_TEST,
327  *                         struct GNUNET_MessageTest);
328  * struct GNUNET_MQ_MessageHandler handlers[] = {
329  *   make_test_message_handler (),
330  *   GNUNET_MQ_handler_end()
331  * };
332  *
333  * @param name unique basename for the functions
334  * @param code message type constant
335  * @param str type of the message (a struct)
336  */
337 #define GNUNET_MQ_hd_fixed_size(name,code,str)   \
338   struct GNUNET_MQ_MessageHandler                            \
339   make_##name##_handler (void *cls) {                        \
340     void (*cb)(void *cls, const str *msg) = &handle_##name;  \
341     struct GNUNET_MQ_MessageHandler mh = {                   \
342       NULL, (GNUNET_MQ_MessageCallback) cb,                  \
343       cls, code, sizeof (str) };                             \
344     return mh;                                               \
345   }
346
347
348 /**
349  * Defines a static function @a name which takes two arguments and a
350  * context-pointer for validating and handling variable-sized messages
351  * of type @a code and with a message type argument of @a str.  Given
352  * such arguments, the function @name will return a `struct
353  * GNUNET_MQ_MessageHandler` for the given message type.
354  *
355  * The macro is to be used as follows:
356  * <code>
357  * struct GNUNET_MessageTest { ... }; // can be variable size
358  * GNUNET_MQ_hd_var_size(test_message,
359  *                       GNUNET_MESSAGE_TYPE_TEST,
360  *                       struct GNUNET_MessageTest);
361  * static int
362  * check_test (void *cls,
363  *             const struct GNUNET_MessageTest *msg)
364  * {
365  *   const char *ctx = cls;
366  *   GNUNET_assert (0 == strcmp ("context", ctx));
367  *   // ...
368  * }
369  * static void
370  * handle_test (void *cls,
371  *              const struct GNUNET_MessageTest *msg)
372  * {
373  *   const char *ctx = cls;
374  *   GNUNET_assert (0 == strcmp ("context", ctx));
375  *   // ...
376  * }
377  *
378  * struct GNUNET_MQ_MessageHandler handlers[] = {
379  *   make_test_message_handler ("context"),
380  *   GNUNET_MQ_handler_end()
381  * };
382  *
383  * @param name unique basename for the functions
384  * @param code message type constant
385  * @param str type of the message (a struct)
386  */
387 #define GNUNET_MQ_hd_var_size(name,code,str)               \
388   struct GNUNET_MQ_MessageHandler                          \
389   make_##name##_handler (void *ctx) {                      \
390     int (*mv)(void *cls, const str *msg) = &check_##name;  \
391     void (*cb)(void *cls, const str *msg) = &handle_##name;\
392     struct GNUNET_MQ_MessageHandler mh =                   \
393       { (GNUNET_MQ_MessageValidationCallback) mv,          \
394         (GNUNET_MQ_MessageCallback) cb,                    \
395         ctx, code, sizeof (str) };                         \
396     return mh;                                             \
397   }
398
399
400 /**
401  * Create a new envelope.
402  *
403  * @param mhp message header to store the allocated message header in, can be NULL
404  * @param size size of the message to allocate
405  * @param type type of the message, will be set in the allocated message
406  * @return the allocated MQ message
407  */
408 struct GNUNET_MQ_Envelope *
409 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
410                 uint16_t size,
411                 uint16_t type);
412
413
414 /**
415  * Create a new envelope by copying an existing message.
416  *
417  * @param hdr header of the message to copy
418  * @return envelope containing @a hdr
419  */
420 struct GNUNET_MQ_Envelope *
421 GNUNET_MQ_msg_copy (const struct GNUNET_MessageHeader *hdr);
422
423
424 /**
425  * Discard the message queue message, free all
426  * allocated resources. Must be called in the event
427  * that a message is created but should not actually be sent.
428  *
429  * @param mqm the message to discard
430  */
431 void
432 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
433
434
435 /**
436  * Obtain the current length of the message queue.
437  *
438  * @param mq queue to inspect
439  * @return number of queued, non-transmitted messages
440  */
441 unsigned int
442 GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq);
443
444
445 /**
446  * Send a message with the give message queue.
447  * May only be called once per message.
448  *
449  * @param mq message queue
450  * @param ev the envelope with the message to send.
451  */
452 void
453 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
454                 struct GNUNET_MQ_Envelope *ev);
455
456
457 /**
458  * Send a copy of a message with the give message queue.
459  * Can be called repeatedly on the same envelope.
460  *
461  * @param mq message queue
462  * @param ev the envelope with the message to send.
463  */
464 void
465 GNUNET_MQ_send_copy (struct GNUNET_MQ_Handle *mq,
466                      const struct GNUNET_MQ_Envelope *ev);
467
468
469 /**
470  * Cancel sending the message. Message must have been sent with
471  * #GNUNET_MQ_send before.  May not be called after the notify sent
472  * callback has been called
473  *
474  * @param ev queued envelope to cancel
475  */
476 void
477 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
478
479
480 /**
481  * Associate the assoc_data in @a mq with a unique request id.
482  *
483  * @param mq message queue, id will be unique for the queue
484  * @param assoc_data to associate
485  */
486 uint32_t
487 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq,
488                      void *assoc_data);
489
490
491 /**
492  * Get the data associated with a @a request_id in a queue
493  *
494  * @param mq the message queue with the association
495  * @param request_id the request id we are interested in
496  * @return the associated data
497  */
498 void *
499 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
500                      uint32_t request_id);
501
502
503 /**
504  * Remove the association for a @a request_id
505  *
506  * @param mq the message queue with the association
507  * @param request_id the request id we want to remove
508  * @return the associated data
509  */
510 void *
511 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
512                         uint32_t request_id);
513
514
515 /**
516  * Create a message queue for the specified handlers.
517  *
518  * @param send function the implements sending messages
519  * @param destroy function that implements destroying the queue
520  * @param cancel function that implements canceling a message
521  * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
522  * @param handlers array of message handlers
523  * @param error_handler handler for read and write errors
524  * @param cls closure for message handlers and error handler
525  * @return a new message queue
526  */
527 struct GNUNET_MQ_Handle *
528 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
529                                GNUNET_MQ_DestroyImpl destroy,
530                                GNUNET_MQ_CancelImpl cancel,
531                                void *impl_state,
532                                const struct GNUNET_MQ_MessageHandler *handlers,
533                                GNUNET_MQ_ErrorHandler error_handler,
534                                void *cls);
535
536
537 /**
538  * Call a callback once the envelope has been sent, that is,
539  * sending it can not be canceled anymore.
540  * There can be only one notify sent callback per envelope.
541  *
542  * @param ev message to call the notify callback for
543  * @param cb the notify callback
544  * @param cls closure for the callback
545  */
546 void
547 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
548                        GNUNET_MQ_NotifyCallback cb,
549                        void *cls);
550
551
552 /**
553  * Destroy the message queue.
554  *
555  * @param mq message queue to destroy
556  */
557 void
558 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
559
560
561 /**
562  * Call the message message handler that was registered
563  * for the type of the given message in the given message queue.
564  *
565  * This function is indended to be used for the implementation
566  * of message queues.
567  *
568  * @param mq message queue with the handlers
569  * @param mh message to dispatch
570  */
571 void
572 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
573                           const struct GNUNET_MessageHeader *mh);
574
575
576 /**
577  * Call the error handler of a message queue with the given
578  * error code.  If there is no error handler, log a warning.
579  *
580  * This function is intended to be used for the implementation
581  * of message queues.
582  *
583  * @param mq message queue
584  * @param error the error type
585  */
586 void
587 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
588                         enum GNUNET_MQ_Error error);
589
590
591 /**
592  * Call the send implementation for the next queued message, if any.
593  * Only useful for implementing message queues, results in undefined
594  * behavior if not used carefully.
595  *
596  * @param mq message queue to send the next message with
597  */
598 void
599 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
600
601
602 /**
603  * Get the message that should currently be sent.  The returned
604  * message is only valid until #GNUNET_MQ_impl_send_continue is
605  * called.  Fails if there is no current message.  Only useful for
606  * implementing message queues, results in undefined behavior if not
607  * used carefully.
608  *
609  * @param mq message queue with the current message, only valid
610  *        until #GNUNET_MQ_impl_send_continue() is called
611  * @return message to send, never NULL
612  */
613 const struct GNUNET_MessageHeader *
614 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
615
616
617 /**
618  * Get the implementation state associated with the
619  * message queue.
620  *
621  * While the GNUNET_MQ_Impl* callbacks receive the
622  * implementation state, continuations that are scheduled
623  * by the implementation function often only have one closure
624  * argument, with this function it is possible to get at the
625  * implementation state when only passing the `struct GNUNET_MQ_Handle`
626  * as closure.
627  *
628  * @param mq message queue with the current message
629  * @return message to send, never NULL
630  */
631 void *
632 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
633
634
635 #endif
636
637 /** @} */ /* end of group mq */