-comments
[oweals/gnunet.git] / src / set / mq.h
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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 2, 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 mq/mq.h
24  * @brief general purpose request queue
25  */
26 #ifndef MQ_H
27 #define MQ_H
28
29 #include "platform.h"
30 #include "gnunet_common.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_connection_lib.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_stream_lib.h"
35
36
37 /**
38  * Allocate a GNUNET_MQ_Message, with extra space allocated after the space needed
39  * by the message struct.
40  * The allocated message will already have the type and size field set.
41  *
42  * @param mvar variable to store the allocated message in;
43  *             must have a header field
44  * @param esize extra space to allocate after the message
45  * @param type type of the message
46  * @return the MQ message
47  */
48 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_((((void)(mvar)->header), (struct GNUNET_MessageHeader**) &(mvar)), (esize) + sizeof *(mvar), (type))
49
50 /**
51  * Allocate a GNUNET_MQ_Message.
52  * The allocated message will already have the type and size field set.
53  *
54  * @param mvar variable to store the allocated message in;
55  *             must have a header field
56  * @param type type of the message
57  * @return the MQ message
58  */
59 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
60
61 /**
62  * Allocate a GNUNET_MQ_Message, and concatenate another message
63  * after the space needed by the message struct.
64  * // nest?
65  *
66  * @param mvar variable to store the allocated message in;
67  *             must have a header field
68  * @param mc message to concatenate, can be NULL
69  * @param type type of the message
70  * @return the MQ message, NULL if mc is to large to be concatenated
71  */
72 #define GNUNET_MQ_msg_concat(mvar, mc, t) GNUNET_MQ_msg_concat_(((void) mvar->header, (struct GNUNET_MessageHeader **) &(mvar)), \
73       sizeof *mvar, (struct GNUNET_MessageHeader *) mc, t)
74
75
76 /**
77  * Allocate a GNUNET_MQ_Message, where the message only consists of a header.
78  * The allocated message will already have the type and size field set.
79  *
80  * @param mvar variable to store the allocated message in;
81  *             must have a header field
82  * @param type type of the message
83  */
84 #define GNUNET_MQ_msg_header(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
85
86
87 /**
88  * Allocate a GNUNET_MQ_Message, where the message only consists of a header and extra space.
89  * The allocated message will already have the type and size field set.
90  *
91  * @param mvar variable to store the allocated message in;
92  *             must have a header field
93  * @param esize extra space to allocate after the message header
94  * @param type type of the message
95  */
96 #define GNUNET_MQ_msg_header_extra(mh, esize, type) GNUNET_MQ_msg_ (&mh, sizeof (struct GNUNET_MessageHeader), type)
97
98
99 /**
100  * End-marker for the handlers array
101  */
102 #define GNUNET_MQ_HANDLERS_END {NULL, 0}
103
104 /**
105  * Opaque handle to a message queue
106  */
107 struct GNUNET_MQ_MessageQueue;
108
109 /**
110  * Opaque handle to an allocated message
111  */
112 struct GNUNET_MQ_Message; // Entry (/ Request)
113
114 /**
115  * Called when a message has been received.
116  *
117  * @param cls closure
118  * @param msg the received message
119  */
120 typedef void (*GNUNET_MQ_MessageCallback) (void *cls, const struct GNUNET_MessageHeader *msg);
121
122
123 /**
124  * Message handler for a specific message type.
125  */
126 struct GNUNET_MQ_Handler
127 {
128   /**
129    * Callback, called every time a new message of 
130    * the specified type has been receied.
131    */
132   GNUNET_MQ_MessageCallback cb;
133
134
135   /**
136    * Type of the message we are interested in
137    */
138   uint16_t type;
139 };
140
141 /**
142  * Callback used for notifications
143  *
144  * @param cls closure
145  */
146 typedef void (*GNUNET_MQ_NotifyCallback) (void *cls);
147
148 /**
149  * Create a new message for MQ.
150  * 
151  * @param mhp message header to store the allocated message header in, can be NULL
152  * @param size size of the message to allocate
153  * @param type type of the message, will be set in the allocated message
154  * @return the allocated MQ message
155  */
156 struct GNUNET_MQ_Message *
157 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type);
158
159
160 /**
161  * Create a new message for MQ, by concatenating another message
162  * after a message of the specified type.
163  *
164  * @retrn the allocated MQ message
165  */
166 struct GNUNET_MQ_Message *
167 GNUNET_MQ_msg_concat_ (struct GNUNET_MessageHeader **mhp, uint16_t base_size, struct GNUNET_MessageHeader *m, uint16_t type);
168
169
170 /**
171  * Discard the message queue message, free all
172  * allocated resources. Must be called in the event
173  * that a message is created but should not actually be sent.
174  *
175  * @param mqm the message to discard
176  */
177 void
178 GNUNET_MQ_discard (struct GNUNET_MQ_Message *mqm);
179
180
181 /**
182  * Send a message with the give message queue.
183  * May only be called once per message.
184  * 
185  * @param mq message queue
186  * @param mqm the message to send.
187  */
188 void
189 GNUNET_MQ_send (struct GNUNET_MQ_MessageQueue *mq, struct GNUNET_MQ_Message *mqm);
190
191
192 /**
193  * Cancel sending the message. Message must have been sent with GNUNET_MQ_send before.
194  * May not be called after the notify sent callback has been called
195  *
196  * @param mqm queued message to cancel
197  */
198 void
199 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Message *mqm);
200
201
202 /**
203  * Associate the assoc_data in mq with a unique request id.
204  *
205  * @param mq message queue, id will be unique for the queue
206  * @param mqm message to associate
207  * @param data to associate
208  */
209 uint32_t
210 GNUNET_MQ_assoc_add (struct GNUNET_MQ_MessageQueue *mq,
211                      struct GNUNET_MQ_Message *mqm,
212                      void *assoc_data);
213
214 /**
215  * Get the data associated with a request id in a queue
216  *
217  * @param mq the message queue with the association
218  * @param request_id the request id we are interested in
219  * @return the associated data
220  */
221 void *
222 GNUNET_MQ_assoc_get (struct GNUNET_MQ_MessageQueue *mq, uint32_t request_id);
223
224
225 /**
226  * Remove the association for a request id
227  *
228  * @param mq the message queue with the association
229  * @param request_id the request id we want to remove
230  * @return the associated data
231  */
232 void *
233 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_MessageQueue *mq, uint32_t request_id);
234
235
236
237 /**
238  * Create a message queue for a GNUNET_CLIENT_Connection.
239  * If handlers are specfied, receive messages from the connection.
240  *
241  * @param connection the client connection
242  * @param handlers handlers for receiving messages
243  * @param cls closure for the handlers
244  * @return the message queue
245  */
246 struct GNUNET_MQ_MessageQueue *
247 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
248                                        const struct GNUNET_MQ_Handler *handlers,
249                                        void *cls);
250
251
252 /**
253  * Create a message queue for a GNUNET_STREAM_Socket.
254  *
255  * @param param client the client
256  * @return the message queue
257  */
258 struct GNUNET_MQ_MessageQueue *
259 GNUNET_MQ_queue_for_server_client (struct GNUNET_SERVER_Client *client);
260
261
262
263 /**
264  * Create a message queue for a GNUNET_STREAM_Socket.
265  * If handlers are specfied, receive messages from the stream socket.
266  *
267  * @param socket the stream socket
268  * @param handlers handlers for receiving messages
269  * @param cls closure for the handlers
270  * @return the message queue
271  */
272 struct GNUNET_MQ_MessageQueue *
273 GNUNET_MQ_queue_for_stream_socket (struct GNUNET_STREAM_Socket *socket,
274                                    const struct GNUNET_MQ_Handler *handlers,
275                                    void *cls);
276
277 /**
278  * Replace the handlers of a message queue with new handlers.
279  * Takes effect immediately, even for messages that already have been received, but for
280  * with the handler has not been called.
281  *
282  * @param mq message queue
283  * @param new_handlers new handlers
284  * @param cls new closure for the handlers
285  */
286 void
287 GNUNET_MQ_replace_handlers (struct GNUNET_MQ_MessageQueue *mq,
288                             const struct GNUNET_MQ_Handler *new_handlers,
289                             void *cls);
290
291
292
293 /**
294  * Call a callback once the message has been sent, that is, the message
295  * can not be canceled anymore.
296  * There can be only one notify sent callback per message.
297  *
298  * @param mqm message to call the notify callback for
299  * @param cb the notify callback
300  * @param cls closure for the callback
301  */
302 void
303 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Message *mqm,
304                        GNUNET_MQ_NotifyCallback cb,
305                        void *cls);
306
307
308 /**
309  * Destroy the message queue.
310  *
311  * @param mq message queue to destroy
312  */
313 void
314 GNUNET_MQ_destroy (struct GNUNET_MQ_MessageQueue *mq);
315
316 #endif