-rps: rename and doxygen
[oweals/gnunet.git] / src / include / gnunet_mq_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012-2013 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  *
24  * @file
25  * General-purpose message queue
26  *
27  * @defgroup mq  MQ library
28  * General-purpose message queue
29  *
30  * @see [Documentation](https://gnunet.org/message-queue-api)
31  *
32  * @{
33  */
34 #ifndef GNUNET_MQ_H
35 #define GNUNET_MQ_H
36
37
38 /**
39  * Allocate an envelope, with extra space allocated after the space needed
40  * by the message struct.
41  * The allocated message will already have the type and size field set.
42  *
43  * @param mvar variable to store the allocated message in;
44  *             must have a header field
45  * @param esize extra space to allocate after the message
46  * @param type type of the message
47  * @return the MQ message
48  */
49 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
50
51 /**
52  * Allocate a GNUNET_MQ_Envelope.
53  * The contained message will already have the type and size field set.
54  *
55  * @param mvar variable to store the allocated message in;
56  *             must have a header field
57  * @param type type of the message
58  * @return the allocated envelope
59  */
60 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
61
62
63 /**
64  * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
65  * The allocated message will already have the type and size field set.
66  *
67  * @param type type of the message
68  */
69 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
70
71
72 /**
73  * Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
74  * The allocated message will already have the type and size field set.
75  *
76  * @param mh pointer that will changed to point at to the allocated message header
77  * @param esize extra space to allocate after the message header
78  * @param type type of the message
79  */
80 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, (esize) + sizeof (struct GNUNET_MessageHeader), type)
81
82
83 /**
84  * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
85  * message struct.
86  *
87  * @param mvar pointer to a message struct, will be changed to point at the newly allocated message,
88  *        whose size is 'sizeof(*mvar) + ntohs (mh->size)'
89  * @param type message type of the allocated message, has no effect on the nested message
90  * @param mh message to nest
91  * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
92  */
93 #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)
94
95
96 /**
97  * Return a pointer to the message at the end of the given message.
98  *
99  * @param var pointer to a message struct, the type of the expression determines the base size,
100  *        the space after the base size is the nested message
101  * @return a 'struct GNUNET_MessageHeader *' that points at the nested message of the given message,
102  *         or NULL if the given message in @a var does not have any space after the message struct
103  */
104 #define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
105
106
107 /**
108  * Implementation of the GNUNET_MQ_extract_nexted_mh macro.
109  *
110  * @param mh message header to extract nested message header from
111  * @param base_size size of the message before the nested message's header appears
112  * @return pointer to the nested message, does not copy the message
113  */
114 const struct GNUNET_MessageHeader *
115 GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh,
116                               uint16_t base_size);
117
118
119 /**
120  * Implementation of the GNUNET_MQ_msg_nested_mh macro.
121  *
122  * @param mhp pointer to the message header pointer that will be changed to allocate at
123  *        the newly allocated space for the message.
124  * @param base_size size of the data before the nested message
125  * @param type type of the message in the envelope
126  * @param nested_mh the message to append to the message after base_size
127  */
128 struct GNUNET_MQ_Envelope *
129 GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp,
130                           uint16_t base_size,
131                           uint16_t type,
132                           const struct GNUNET_MessageHeader *nested_mh);
133
134
135
136 /**
137  * End-marker for the handlers array
138  */
139 #define GNUNET_MQ_HANDLERS_END {NULL, 0, 0}
140
141
142 /**
143  * Opaque handle to a message queue.
144  */
145 struct GNUNET_MQ_Handle;
146
147 /**
148  * Opaque handle to an envelope.
149  */
150 struct GNUNET_MQ_Envelope;
151
152
153 /**
154  * Error codes for the queue.
155  */
156 enum GNUNET_MQ_Error
157 {
158   /**
159    * FIXME: document!
160    */
161   GNUNET_MQ_ERROR_READ = 1,
162
163   /**
164    * FIXME: document!
165    */
166   GNUNET_MQ_ERROR_WRITE = 2,
167
168   /**
169    * FIXME: document!
170    */
171   GNUNET_MQ_ERROR_TIMEOUT = 4
172 };
173
174
175 /**
176  * Called when a message has been received.
177  *
178  * @param cls closure
179  * @param msg the received message
180  */
181 typedef void
182 (*GNUNET_MQ_MessageCallback) (void *cls,
183                               const struct GNUNET_MessageHeader *msg);
184
185
186 /**
187  * Signature of functions implementing the
188  * sending functionality of a message queue.
189  *
190  * @param mq the message queue
191  * @param msg the message to send
192  * @param impl_state state of the implementation
193  */
194 typedef void
195 (*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
196                        const struct GNUNET_MessageHeader *msg,
197                        void *impl_state);
198
199
200 /**
201  * Signature of functions implementing the
202  * destruction of a message queue.
203  * Implementations must not free @a mq, but should
204  * take care of @a impl_state.
205  *
206  * @param mq the message queue to destroy
207  * @param impl_state state of the implementation
208  */
209 typedef void
210 (*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq,
211                           void *impl_state);
212
213
214 /**
215  * Implementation function that cancels the currently sent message.
216  *
217  * @param mq message queue
218  * @param impl_state state specific to the implementation
219  */
220 typedef void
221 (*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq,
222                          void *impl_state);
223
224
225 /**
226  * Callback used for notifications
227  *
228  * @param cls closure
229  */
230 typedef void
231 (*GNUNET_MQ_NotifyCallback) (void *cls);
232
233
234 /**
235  * Generic error handler, called with the appropriate
236  * error code and the same closure specified at the creation of
237  * the message queue.
238  * Not every message queue implementation supports an error handler.
239  *
240  * @param cls closure, same closure as for the message handlers
241  * @param error error code
242  */
243 typedef void
244 (*GNUNET_MQ_ErrorHandler) (void *cls,
245                            enum GNUNET_MQ_Error error);
246
247
248 /**
249  * Message handler for a specific message type.
250  */
251 struct GNUNET_MQ_MessageHandler
252 {
253   /**
254    * Callback, called every time a new message of
255    * the specified type has been receied.
256    */
257   GNUNET_MQ_MessageCallback cb;
258
259   /**
260    * Type of the message this handler covers.
261    */
262   uint16_t type;
263
264   /**
265    * Expected size of messages of this type.  Use 0 for
266    * variable-size.  If non-zero, messages of the given
267    * type will be discarded (and the connection closed)
268    * if they do not have the right size.
269    */
270   uint16_t expected_size;
271 };
272
273
274
275 /**
276  * Create a new envelope.
277  *
278  * @param mhp message header to store the allocated message header in, can be NULL
279  * @param size size of the message to allocate
280  * @param type type of the message, will be set in the allocated message
281  * @return the allocated MQ message
282  */
283 struct GNUNET_MQ_Envelope *
284 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp,
285                 uint16_t size,
286                 uint16_t type);
287
288
289 /**
290  * Discard the message queue message, free all
291  * allocated resources. Must be called in the event
292  * that a message is created but should not actually be sent.
293  *
294  * @param mqm the message to discard
295  */
296 void
297 GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
298
299
300 /**
301  * Send a message with the give message queue.
302  * May only be called once per message.
303  *
304  * @param mq message queue
305  * @param ev the envelope with the message to send.
306  */
307 void
308 GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq,
309                 struct GNUNET_MQ_Envelope *ev);
310
311
312 /**
313  * Cancel sending the message. Message must have been sent with
314  * #GNUNET_MQ_send before.  May not be called after the notify sent
315  * callback has been called
316  *
317  * @param ev queued envelope to cancel
318  */
319 void
320 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
321
322
323 /**
324  * Associate the assoc_data in mq with a unique request id.
325  *
326  * @param mq message queue, id will be unique for the queue
327  * @param assoc_data to associate
328  */
329 uint32_t
330 GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data);
331
332
333 /**
334  * Get the data associated with a request id in a queue
335  *
336  * @param mq the message queue with the association
337  * @param request_id the request id we are interested in
338  * @return the associated data
339  */
340 void *
341 GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq,
342                      uint32_t request_id);
343
344
345 /**
346  * Remove the association for a request id
347  *
348  * @param mq the message queue with the association
349  * @param request_id the request id we want to remove
350  * @return the associated data
351  */
352 void *
353 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq,
354                         uint32_t request_id);
355
356
357 /**
358  * Create a message queue for a GNUNET_CLIENT_Connection.
359  * If handlers are specfied, receive messages from the connection.
360  *
361  * @param connection the client connection
362  * @param handlers handlers for receiving messages
363  * @param error_handler error handler
364  * @param cls closure for the handlers
365  * @return the message queue
366  */
367 struct GNUNET_MQ_Handle *
368 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
369                                        const struct GNUNET_MQ_MessageHandler *handlers,
370                                        GNUNET_MQ_ErrorHandler error_handler,
371                                        void *cls);
372
373
374 /**
375  * Create a message queue for a GNUNET_SERVER_Client.
376  *
377  * @param client the client
378  * @return the message queue
379  */
380 struct GNUNET_MQ_Handle *
381 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client);
382
383
384 /**
385  * Create a message queue for the specified handlers.
386  *
387  * @param send function the implements sending messages
388  * @param destroy function that implements destroying the queue
389  * @param cancel function that implements canceling a message
390  * @param impl_state for the queue, passed to @a send, @a destroy and @a cancel
391  * @param handlers array of message handlers
392  * @param error_handler handler for read and write errors
393  * @param cls closure for message handlers and error handler
394  * @return a new message queue
395  */
396 struct GNUNET_MQ_Handle *
397 GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
398                                GNUNET_MQ_DestroyImpl destroy,
399                                GNUNET_MQ_CancelImpl cancel,
400                                void *impl_state,
401                                const struct GNUNET_MQ_MessageHandler *handlers,
402                                GNUNET_MQ_ErrorHandler error_handler,
403                                void *cls);
404
405
406 /**
407  * Replace the handlers of a message queue with new handlers.  Takes
408  * effect immediately, even for messages that already have been
409  * received, but for with the handler has not been called.
410  *
411  * If the message queue does not support receiving messages,
412  * this function has no effect.
413  *
414  * @param mq message queue
415  * @param new_handlers new handlers
416  * @param cls new closure for the handlers
417  */
418 void
419 GNUNET_MQ_replace_handlers (struct GNUNET_MQ_Handle *mq,
420                             const struct GNUNET_MQ_MessageHandler *new_handlers,
421                             void *cls);
422
423
424 /**
425  * Call a callback once the envelope has been sent, that is,
426  * sending it can not be canceled anymore.
427  * There can be only one notify sent callback per envelope.
428  *
429  * @param ev message to call the notify callback for
430  * @param cb the notify callback
431  * @param cls closure for the callback
432  */
433 void
434 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
435                        GNUNET_MQ_NotifyCallback cb,
436                        void *cls);
437
438
439 /**
440  * Destroy the message queue.
441  *
442  * @param mq message queue to destroy
443  */
444 void
445 GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
446
447
448 /**
449  * Call the message message handler that was registered
450  * for the type of the given message in the given message queue.
451  *
452  * This function is indended to be used for the implementation
453  * of message queues.
454  *
455  * @param mq message queue with the handlers
456  * @param mh message to dispatch
457  */
458 void
459 GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
460                           const struct GNUNET_MessageHeader *mh);
461
462
463 /**
464  * Call the error handler of a message queue with the given
465  * error code.  If there is no error handler, log a warning.
466  *
467  * This function is intended to be used for the implementation
468  * of message queues.
469  *
470  * @param mq message queue
471  * @param error the error type
472  */
473 void
474 GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
475                         enum GNUNET_MQ_Error error);
476
477
478 /**
479  * Call the send implementation for the next queued message,
480  * if any.
481  * Only useful for implementing message queues,
482  * results in undefined behavior if not used carefully.
483  *
484  * @param mq message queue to send the next message with
485  */
486 void
487 GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
488
489
490 /**
491  * Get the message that should currently be sent.
492  * The returned message is only valid until #GNUNET_MQ_impl_send_continue
493  * is called.
494  * Fails if there is no current message.
495  * Only useful for implementing message queues,
496  * results in undefined behavior if not used carefully.
497  *
498  * @param mq message queue with the current message, only valid
499  *        until #GNUNET_MQ_impl_send_continue is called
500  * @return message to send, never NULL
501  */
502 const struct GNUNET_MessageHeader *
503 GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
504
505
506 /**
507  * Get the implementation state associated with the
508  * message queue.
509  *
510  * While the GNUNET_MQ_Impl* callbacks receive the
511  * implementation state, continuations that are scheduled
512  * by the implementation function often only have one closure
513  * argument, with this function it is possible to get at the
514  * implementation state when only passing the GNUNET_MQ_Handle
515  * as closure.
516  *
517  * @param mq message queue with the current message
518  * @return message to send, never NULL
519  */
520 void *
521 GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
522
523
524 #endif
525
526 /** @} */ /* end of group mq */