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