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