api fix
[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
34
35 #define GNUNET_MQ_msg_extra(mvar, esize, type) GNUNET_MQ_msg_(((void) mvar->header, (struct GNUNET_MessageHeader**) &mvar), (esize) + sizeof *mvar, type)
36
37 #define GNUNET_MQ_msg(mvar, type) GNUNET_MQ_msg_extra(mvar, 0, type)
38
39 #define GNUNET_MQ_msg_raw(type) GNUNET_MQ_msg_ (NULL, sizeof (struct GNUNET_MessageHeader), type)
40
41 #define GNUNET_MQ_HANDLERS_END {NULL, 0}
42
43 struct GNUNET_MQ_MessageQueue;
44
45 struct GNUNET_MQ_Message;
46
47 struct GNUNET_MQ_Handler
48 {
49   void *cb;
50   uint16_t type;
51 };
52
53 /**
54  * Callback used for notifications
55  *
56  * @param cls closure
57  */
58 typedef void (*GNUNET_MQ_NotifyCallback) (void *cls);
59
60 /**
61  * Create a new message for MQ.
62  * 
63  * @param mhp message header to store the allocated message header in, can be NULL
64  * @param size size of the message to allocate
65  * @param type type of the message, will be set in the allocated message
66  * @param return the allocated MQ message
67  */
68 struct GNUNET_MQ_Message *
69 GNUNET_MQ_msg_ (struct GNUNET_MessageHeader **mhp, uint16_t size, uint16_t type);
70
71 /**
72  * Send a message with the give message queue.
73  * May only be called once per message.
74  * 
75  * @param mq message queue
76  * @param mqm the message to send.
77  */
78 void
79 GNUNET_MQ_send (struct GNUNET_MQ_MessageQueue *mq, struct GNUNET_MQ_Message *mqm);
80
81 /**
82  * Cancel sending the message. Message must have been sent with GNUNET_MQ_send before.
83  * May not be called after the notify sent callback has been called
84  *
85  * @param mqm queued message to cancel
86  */
87 void
88 GNUNET_MQ_send_cancel (struct GNUNET_MQ_Message *mqm);
89
90
91 /**
92  * Associate the assoc_data in mq with a unique request id.
93  *
94  * @param mq message queue, id will be unique for the queue
95  * @param mqm message to associate
96  * @param data to associate
97  */
98 uint32_t
99 GNUNET_MQ_assoc_add (struct GNUNET_MQ_MessageQueue *mq,
100                      struct GNUNET_MQ_Message *mqm,
101                      void *assoc_data);
102
103 void *
104 GNUNET_MQ_assoc_get (struct GNUNET_MQ_MessageQueue *mq, uint32_t request_id);
105
106 void *
107 GNUNET_MQ_assoc_remove (struct GNUNET_MQ_MessageQueue *mq, uint32_t request_id);
108
109
110
111 struct GNUNET_MQ_MessageQueue *
112 GNUNET_MQ_queue_for_connection_client (struct GNUNET_CLIENT_Connection *connection,
113                                        const struct GNUNET_MQ_Handler *handlers,
114                                        void *cls);
115
116
117 void
118 GNUNET_MQ_notify_sent (struct GNUNET_MQ_Message *mqm,
119                        GNUNET_MQ_NotifyCallback cb,
120                        void *cls);
121
122
123 void
124 GNUNET_MQ_notify_timeout (struct GNUNET_MQ_Message *mqm,
125                           GNUNET_MQ_NotifyCallback cb,
126                           void *cls);
127
128
129 void
130 GNUNET_MQ_destroy (struct GNUNET_MQ_MessageQueue *mq);
131
132 #endif