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