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