aboutsummaryrefslogtreecommitdiff
path: root/src/include/gnunet_mq_lib.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/include/gnunet_mq_lib.h')
-rw-r--r--src/include/gnunet_mq_lib.h309
1 files changed, 162 insertions, 147 deletions
diff --git a/src/include/gnunet_mq_lib.h b/src/include/gnunet_mq_lib.h
index 54ea806a5..b73cab8d8 100644
--- a/src/include/gnunet_mq_lib.h
+++ b/src/include/gnunet_mq_lib.h
@@ -21,7 +21,7 @@
21/** 21/**
22 * @author Florian Dold 22 * @author Florian Dold
23 * @file set/mq.h 23 * @file set/mq.h
24 * @brief general purpose request queue 24 * @brief general purpose message queue
25 */ 25 */
26#ifndef GNUNET_MQ_H 26#ifndef GNUNET_MQ_H
27#define GNUNET_MQ_H 27#define GNUNET_MQ_H
@@ -30,7 +30,7 @@
30 30
31 31
32/** 32/**
33 * Allocate a GNUNET_MQ_Message, with extra space allocated after the space needed 33 * Allocate an envelope, with extra space allocated after the space needed
34 * by the message struct. 34 * by the message struct.
35 * The allocated message will already have the type and size field set. 35 * The allocated message will already have the type and size field set.
36 * 36 *
@@ -43,19 +43,19 @@
43#define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_((((void)(mvar)->header), (struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type)) 43#define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_((((void)(mvar)->header), (struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
44 44
45/** 45/**
46 * Allocate a GNUNET_MQ_Message. 46 * Allocate a GNUNET_MQ_Envelope.
47 * The allocated message will already have the type and size field set. 47 * The contained message will already have the type and size field set.
48 * 48 *
49 * @param mvar variable to store the allocated message in; 49 * @param mvar variable to store the allocated message in;
50 * must have a header field 50 * must have a header field
51 * @param type type of the message 51 * @param type type of the message
52 * @return the MQ message 52 * @return the allocated envelope
53 */ 53 */
54#define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type) 54#define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
55 55
56 56
57/** 57/**
58 * Allocate a GNUNET_MQ_Message, where the message only consists of a header. 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. 59 * The allocated message will already have the type and size field set.
60 * 60 *
61 * @param type type of the message 61 * @param type type of the message
@@ -64,7 +64,7 @@
64 64
65 65
66/** 66/**
67 * Allocate a GNUNET_MQ_Message, where the message only consists of a header and extra space. 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. 68 * The allocated message will already have the type and size field set.
69 * 69 *
70 * @param mh pointer that will changed to point at to the allocated message header 70 * @param mh pointer that will changed to point at to the allocated message header
@@ -75,14 +75,14 @@
75 75
76 76
77/** 77/**
78 * Allocate a GNUNET_MQ_Message, and append a payload message after the given 78 * Allocate a GNUNET_MQ_Envelope, and append a payload message after the given
79 * message struct. 79 * message struct.
80 * 80 *
81 * @param mvar pointer to a message struct, will be changed to point at the newly allocated message, 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)' 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 83 * @param type message type of the allocated message, has no effect on the nested message
84 * @param mh message to nest 84 * @param mh message to nest
85 * @return a newly allocated 'struct GNUNET_MQ_Message *' 85 * @return a newly allocated 'struct GNUNET_MQ_Envelope *'
86 */ 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) 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 88
@@ -98,11 +98,24 @@
98#define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var))) 98#define GNUNET_MQ_extract_nested_mh(var) GNUNET_MQ_extract_nested_mh_ ((struct GNUNET_MessageHeader *) (var), sizeof (*(var)))
99 99
100 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 */
101struct GNUNET_MessageHeader * 108struct GNUNET_MessageHeader *
102GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh, uint16_t base_size); 109GNUNET_MQ_extract_nested_mh_ (const struct GNUNET_MessageHeader *mh, uint16_t base_size);
103 110
104 111
105struct GNUNET_MQ_Message * 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 */
118struct GNUNET_MQ_Envelope *
106GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp, uint16_t base_size, uint16_t type, 119GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp, uint16_t base_size, uint16_t type,
107 const struct GNUNET_MessageHeader *nested_mh); 120 const struct GNUNET_MessageHeader *nested_mh);
108 121
@@ -114,9 +127,15 @@ GNUNET_MQ_msg_nested_mh_ (struct GNUNET_MessageHeader **mhp, uint16_t base_size,
114#define GNUNET_MQ_HANDLERS_END {NULL, 0, 0} 127#define GNUNET_MQ_HANDLERS_END {NULL, 0, 0}
115 128
116 129
117struct GNUNET_MQ_MessageQueue; 130/**
131 * Opaque handle to a message queue.
132 */
133struct GNUNET_MQ_Handle;
118 134
119struct GNUNET_MQ_Message; 135/**
136 * Opaque handle to an envelope.
137 */
138struct GNUNET_MQ_Envelope;
120 139
121enum GNUNET_MQ_Error 140enum GNUNET_MQ_Error
122{ 141{
@@ -133,22 +152,45 @@ enum GNUNET_MQ_Error
133 * @param msg the received message 152 * @param msg the received message
134 */ 153 */
135typedef void 154typedef void
136(*GNUNET_MQ_MessageCallback) (void *cls, const struct GNUNET_MessageHeader *msg); 155(*GNUNET_MQ_MessageCallback) (void *cls,
156 const struct GNUNET_MessageHeader *msg);
137 157
138 158
139/** 159/**
140 * Signature of functions implementing the 160 * Signature of functions implementing the
141 * sending part of a message queue 161 * sending functionality of a message queue.
142 * 162 *
143 * @param q the message queue 163 * @param mq the message queue
144 * @param m the message 164 * @param msg the message to send
165 * @param impl_state state of the implementation
166 */
167typedef void
168(*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_Handle *mq,
169 const struct GNUNET_MessageHeader *msg,
170 void *impl_state);
171
172
173/**
174 * Signature of functions implementing the
175 * destruction of a message queue.
176 * Implementations must not free 'mq', but should
177 * take care of 'impl_state'.
178 *
179 * @param mq the message queue to destroy
180 * @param impl_state state of the implementation
145 */ 181 */
146typedef void 182typedef void
147(*GNUNET_MQ_SendImpl) (struct GNUNET_MQ_MessageQueue *q, struct GNUNET_MQ_Message *m); 183(*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_Handle *mq, void *impl_state);
148 184
149 185
186/**
187 * Implementation function that cancels the currently sent message.
188 *
189 * @param mq message queue
190 * @param impl_state state specific to the implementation
191 */
150typedef void 192typedef void
151(*GNUNET_MQ_DestroyImpl) (struct GNUNET_MQ_MessageQueue *q); 193(*GNUNET_MQ_CancelImpl) (struct GNUNET_MQ_Handle *mq, void *impl_state);
152 194
153 195
154/** 196/**
@@ -160,117 +202,23 @@ typedef void
160(*GNUNET_MQ_NotifyCallback) (void *cls); 202(*GNUNET_MQ_NotifyCallback) (void *cls);
161 203
162 204
163typedef void
164(*GNUNET_MQ_ErrorHandler) (void *cls, enum GNUNET_MQ_Error error);
165
166
167struct GNUNET_MQ_Message
168{
169 /**
170 * Messages are stored in a linked list
171 */
172 struct GNUNET_MQ_Message *next;
173
174 /**
175 * Messages are stored in a linked list
176 */
177 struct GNUNET_MQ_Message *prev;
178
179 /**
180 * Actual allocated message header,
181 * usually points to the end of the containing GNUNET_MQ_Message
182 */
183 struct GNUNET_MessageHeader *mh;
184
185 /**
186 * Queue the message is queued in, NULL if message is not queued.
187 */
188 struct GNUNET_MQ_MessageQueue *parent_queue;
189
190 /**
191 * Called after the message was sent irrevokably
192 */
193 GNUNET_MQ_NotifyCallback sent_cb;
194
195 /**
196 * Closure for send_cb
197 */
198 void *sent_cls;
199};
200
201
202/** 205/**
203 * Handle to a message queue. 206 * Generic error handler, called with the appropriate
207 * error code and the same closure specified at the creation of
208 * the message queue.
209 * Not every message queue implementation supports an error handler.
210 *
211 * @param cls closure, same closure as for the message handlers
212 * @param error error code
204 */ 213 */
205struct GNUNET_MQ_MessageQueue 214typedef void
206{ 215(*GNUNET_MQ_ErrorHandler) (void *cls, enum GNUNET_MQ_Error error);
207 /**
208 * Handlers array, or NULL if the queue should not receive messages
209 */
210 const struct GNUNET_MQ_Handler *handlers;
211
212 /**
213 * Closure for the handler callbacks,
214 * as well as for the error handler.
215 */
216 void *handlers_cls;
217
218 /**
219 * Actual implementation of message sending,
220 * called when a message is added
221 */
222 GNUNET_MQ_SendImpl send_impl;
223
224 /**
225 * Implementation-dependent queue destruction function
226 */
227 GNUNET_MQ_DestroyImpl destroy_impl;
228
229 /**
230 * Implementation-specific state
231 */
232 void *impl_state;
233
234 /**
235 * Callback will be called when an error occurs.
236 */
237 GNUNET_MQ_ErrorHandler error_handler;
238
239 /**
240 * Linked list of messages pending to be sent
241 */
242 struct GNUNET_MQ_Message *msg_head;
243
244 /**
245 * Linked list of messages pending to be sent
246 */
247 struct GNUNET_MQ_Message *msg_tail;
248
249 /**
250 * Message that is currently scheduled to be
251 * sent. Not the head of the message queue, as the implementation
252 * needs to know if sending has been already scheduled or not.
253 */
254 struct GNUNET_MQ_Message *current_msg;
255
256 /**
257 * Map of associations, lazily allocated
258 */
259 struct GNUNET_CONTAINER_MultiHashMap32 *assoc_map;
260
261 /**
262 * Next id that should be used for the assoc_map,
263 * initialized lazily to a random value together with
264 * assoc_map
265 */
266 uint32_t assoc_id;
267};
268 216
269 217
270/** 218/**
271 * Message handler for a specific message type. 219 * Message handler for a specific message type.
272 */ 220 */
273struct GNUNET_MQ_Handler 221struct GNUNET_MQ_MessageHandler
274{ 222{
275 /** 223 /**
276 * Callback, called every time a new message of 224 * Callback, called every time a new message of
@@ -296,14 +244,14 @@ struct GNUNET_MQ_Handler
296 244
297 245
298/** 246/**
299 * Create a new message for MQ. 247 * Create a new envelope.
300 * 248 *
301 * @param mhp message header to store the allocated message header in, can be NULL 249 * @param mhp message header to store the allocated message header in, can be NULL
302 * @param size size of the message to allocate 250 * @param size size of the message to allocate
303 * @param type type of the message, will be set in the allocated message 251 * @param type type of the message, will be set in the allocated message
304 * @return the allocated MQ message 252 * @return the allocated MQ message
305 */ 253 */
306struct GNUNET_MQ_Message * 254struct GNUNET_MQ_Envelope *
307GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type); 255GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type);
308 256
309 257
@@ -315,7 +263,7 @@ GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type)
315 * @param mqm the message to discard 263 * @param mqm the message to discard
316 */ 264 */
317void 265void
318GNUNET_MQ_discard (struct GNUNET_MQ_Message *mqm); 266GNUNET_MQ_discard (struct GNUNET_MQ_Envelope *mqm);
319 267
320 268
321/** 269/**
@@ -326,7 +274,7 @@ GNUNET_MQ_discard (struct GNUNET_MQ_Message *mqm);
326 * @param mqm the message to send. 274 * @param mqm the message to send.
327 */ 275 */
328void 276void
329GNUNET_MQ_send (struct GNUNET_MQ_MessageQueue *mq, struct GNUNET_MQ_Message *mqm); 277GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev);
330 278
331 279
332/** 280/**
@@ -336,7 +284,7 @@ GNUNET_MQ_send (struct GNUNET_MQ_MessageQueue *mq, struct GNUNET_MQ_Message *mqm
336 * @param mqm queued message to cancel 284 * @param mqm queued message to cancel
337 */ 285 */
338void 286void
339GNUNET_MQ_send_cancel (struct GNUNET_MQ_Message *mqm); 287GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev);
340 288
341 289
342/** 290/**
@@ -347,9 +295,7 @@ GNUNET_MQ_send_cancel (struct GNUNET_MQ_Message *mqm);
347 * @param assoc_data to associate 295 * @param assoc_data to associate
348 */ 296 */
349uint32_t 297uint32_t
350GNUNET_MQ_assoc_add (struct GNUNET_MQ_MessageQueue *mq, 298GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data);
351 struct GNUNET_MQ_Message *mqm,
352 void *assoc_data);
353 299
354/** 300/**
355 * Get the data associated with a request id in a queue 301 * Get the data associated with a request id in a queue
@@ -359,7 +305,7 @@ GNUNET_MQ_assoc_add (struct GNUNET_MQ_MessageQueue *mq,
359 * @return the associated data 305 * @return the associated data
360 */ 306 */
361void * 307void *
362GNUNET_MQ_assoc_get (struct GNUNET_MQ_MessageQueue *mq, uint32_t request_id); 308GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq, uint32_t request_id);
363 309
364 310
365/** 311/**
@@ -370,7 +316,7 @@ GNUNET_MQ_assoc_get (struct GNUNET_MQ_MessageQueue *mq, uint32_t request_id);
370 * @return the associated data 316 * @return the associated data
371 */ 317 */
372void * 318void *
373GNUNET_MQ_assoc_remove (struct GNUNET_MQ_MessageQueue *mq, uint32_t request_id); 319GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq, uint32_t request_id);
374 320
375 321
376 322
@@ -383,9 +329,9 @@ GNUNET_MQ_assoc_remove (struct GNUNET_MQ_MessageQueue *mq, uint32_t request_id);
383 * @param cls closure for the handlers 329 * @param cls closure for the handlers
384 * @return the message queue 330 * @return the message queue
385 */ 331 */
386struct GNUNET_MQ_MessageQueue * 332struct GNUNET_MQ_Handle *
387GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection, 333GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
388 const struct GNUNET_MQ_Handler *handlers, 334 const struct GNUNET_MQ_MessageHandler *handlers,
389 void *cls); 335 void *cls);
390 336
391 337
@@ -395,7 +341,7 @@ GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connecti
395 * @param client the client 341 * @param client the client
396 * @return the message queue 342 * @return the message queue
397 */ 343 */
398struct GNUNET_MQ_MessageQueue * 344struct GNUNET_MQ_Handle *
399GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client); 345GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client);
400 346
401 347
@@ -404,16 +350,19 @@ GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client);
404 * 350 *
405 * @param send function the implements sending messages 351 * @param send function the implements sending messages
406 * @param destroy function that implements destroying the queue 352 * @param destroy function that implements destroying the queue
353 * @param destroy function that implements canceling a message
407 * @param state for the queue, passed to 'send' and 'destroy' 354 * @param state for the queue, passed to 'send' and 'destroy'
408 * @param handlers array of message handlers 355 * @param handlers array of message handlers
409 * @param error_handler handler for read and write errors 356 * @param error_handler handler for read and write errors
357 * @param cls closure for handlers
410 * @return a new message queue 358 * @return a new message queue
411 */ 359 */
412struct GNUNET_MQ_MessageQueue * 360struct GNUNET_MQ_Handle *
413GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send, 361GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
414 GNUNET_MQ_DestroyImpl destroy, 362 GNUNET_MQ_DestroyImpl destroy,
363 GNUNET_MQ_CancelImpl cancel,
415 void *impl_state, 364 void *impl_state,
416 struct GNUNET_MQ_Handler *handlers, 365 const struct GNUNET_MQ_MessageHandler *handlers,
417 GNUNET_MQ_ErrorHandler error_handler, 366 GNUNET_MQ_ErrorHandler error_handler,
418 void *cls); 367 void *cls);
419 368
@@ -424,27 +373,30 @@ GNUNET_MQ_queue_for_callbacks (GNUNET_MQ_SendImpl send,
424 * Takes effect immediately, even for messages that already have been received, but for 373 * Takes effect immediately, even for messages that already have been received, but for
425 * with the handler has not been called. 374 * with the handler has not been called.
426 * 375 *
376 * If the message queue does not support receiving messages,
377 * this function has no effect.
378 *
427 * @param mq message queue 379 * @param mq message queue
428 * @param new_handlers new handlers 380 * @param new_handlers new handlers
429 * @param cls new closure for the handlers 381 * @param cls new closure for the handlers
430 */ 382 */
431void 383void
432GNUNET_MQ_replace_handlers (struct GNUNET_MQ_MessageQueue *mq, 384GNUNET_MQ_replace_handlers (struct GNUNET_MQ_Handle *mq,
433 const struct GNUNET_MQ_Handler *new_handlers, 385 const struct GNUNET_MQ_MessageHandler *new_handlers,
434 void *cls); 386 void *cls);
435 387
436 388
437/** 389/**
438 * Call a callback once the message has been sent, that is, the message 390 * Call a callback once the envelope has been sent, that is,
439 * can not be canceled anymore. 391 * sending it can not be canceled anymore.
440 * There can be only one notify sent callback per message. 392 * There can be only one notify sent callback per envelope.
441 * 393 *
442 * @param mqm message to call the notify callback for 394 * @param ev message to call the notify callback for
443 * @param cb the notify callback 395 * @param cb the notify callback
444 * @param cls closure for the callback 396 * @param cls closure for the callback
445 */ 397 */
446void 398void
447GNUNET_MQ_notify_sent (struct GNUNET_MQ_Message *mqm, 399GNUNET_MQ_notify_sent (struct GNUNET_MQ_Envelope *ev,
448 GNUNET_MQ_NotifyCallback cb, 400 GNUNET_MQ_NotifyCallback cb,
449 void *cls); 401 void *cls);
450 402
@@ -455,7 +407,7 @@ GNUNET_MQ_notify_sent (struct GNUNET_MQ_Message *mqm,
455 * @param mq message queue to destroy 407 * @param mq message queue to destroy
456 */ 408 */
457void 409void
458GNUNET_MQ_destroy (struct GNUNET_MQ_MessageQueue *mq); 410GNUNET_MQ_destroy (struct GNUNET_MQ_Handle *mq);
459 411
460 412
461/** 413/**
@@ -465,7 +417,70 @@ GNUNET_MQ_destroy (struct GNUNET_MQ_MessageQueue *mq);
465 * @param mh message to dispatch 417 * @param mh message to dispatch
466 */ 418 */
467void 419void
468GNUNET_MQ_dispatch (struct GNUNET_MQ_MessageQueue *mq, 420GNUNET_MQ_inject_message (struct GNUNET_MQ_Handle *mq,
469 const struct GNUNET_MessageHeader *mh); 421 const struct GNUNET_MessageHeader *mh);
422
423
424/**
425 * Call the right callback for an error condition.
426 *
427 * @param mq message queue
428 */
429void
430GNUNET_MQ_inject_error (struct GNUNET_MQ_Handle *mq,
431 enum GNUNET_MQ_Error error);
432
433
434/**
435 * Call the send implementation for the next queued message,
436 * if any.
437 * Only useful for implementing message queues,
438 * results in undefined behavior if not used carefully.
439 *
440 * @param mq message queue to send the next message with
441 */
442void
443GNUNET_MQ_impl_send_continue (struct GNUNET_MQ_Handle *mq);
444
445
446/**
447 * Get the message that should currently be sent.
448 * Fails if there is no current message.
449 * Only useful for implementing message queues,
450 * results in undefined behavior if not used carefully.
451 *
452 * @param mq message queue with the current message
453 * @return message to send, never NULL
454 */
455const struct GNUNET_MessageHeader *
456GNUNET_MQ_impl_current (struct GNUNET_MQ_Handle *mq);
457
458
459/**
460 * Get the implementation state associated with the
461 * message queue.
462 *
463 * While the GNUNET_MQ_Impl* callbacks receive the
464 * implementation state, continuations that are scheduled
465 * by the implementation function often only have one closure
466 * argument, with this function it is possible to get at the
467 * implementation state when only passing the GNUNET_MQ_Handle
468 * as closure.
469 *
470 * @param mq message queue with the current message
471 * @return message to send, never NULL
472 */
473void *
474GNUNET_MQ_impl_state (struct GNUNET_MQ_Handle *mq);
475
476/**
477 * Mark the current message as irrevocably sent, but do not
478 * proceed with sending the next message.
479 * Will call the appropriate GNUNET_MQ_NotifyCallback, if any.
480 *
481 * @param mq message queue
482 */
483void
484GNUNET_MQ_impl_send_commit (struct GNUNET_MQ_Handle *mq);
470 485
471#endif 486#endif