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