05cfc011eb0b1d187d18c8e8767323c8b7b48b38
[oweals/gnunet.git] / src / include / gnunet_psyc_message.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012, 2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @author Gabor X Toth
21  *
22  * @file
23  * PSYC message utilities; receiving/transmitting/logging PSYC messages
24  *
25  * @defgroup psyc-util-message  PSYC Utilities library: Messages
26  * Receiving, transmitting, logging PSYC messages.
27  * @{
28  */
29
30 #ifndef GNUNET_PSYC_MESSAGE_H
31 #define GNUNET_PSYC_MESSAGE_H
32
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #if 0                           /* keep Emacsens' auto-indent happy */
37 }
38 #endif
39 #endif
40
41
42 #include "gnunet_util_lib.h"
43 #include "gnunet_psyc_util_lib.h"
44 #include "gnunet_psyc_service.h"
45
46
47 /**
48  * Create a PSYC message.
49  *
50  * @param method_name
51  *        PSYC method for the message.
52  * @param env
53  *        Environment for the message.
54  * @param data
55  *        Data payload for the message.
56  * @param data_size
57  *        Size of @a data.
58  *
59  * @return Message header with size information,
60  *         followed by the message parts.
61  *
62  * FIXME: arg order
63  */
64 struct GNUNET_PSYC_Message *
65 GNUNET_PSYC_message_create (const char *method_name,
66                             const struct GNUNET_PSYC_Environment *env,
67                             const void *data,
68                             size_t data_size);
69
70 /**
71  * Parse PSYC message.
72  *
73  * @param msg
74  *        The PSYC message to parse.
75  * @param env
76  *        The environment for the message with a list of modifiers.
77  * @param[out] method_name
78  *        Pointer to the method name inside @a pmsg.
79  * @param[out] data
80  *        Pointer to data inside @a pmsg.
81  * @param[out] data_size
82  *        Size of @data is written here.
83  *
84  * @return #GNUNET_OK on success,
85  *         #GNUNET_SYSERR on parse error.
86  *
87  * FIXME: arg order
88  */
89 int
90 GNUNET_PSYC_message_parse (const struct GNUNET_PSYC_MessageHeader *msg,
91                            const char **method_name,
92                            struct GNUNET_PSYC_Environment *env,
93                            const void **data,
94                            uint16_t *data_size);
95
96
97 void
98 GNUNET_PSYC_log_message (enum GNUNET_ErrorType kind,
99                          const struct GNUNET_MessageHeader *msg);
100
101
102 struct GNUNET_PSYC_TransmitHandle;
103
104 /**
105  * Create a transmission handle.
106  */
107 struct GNUNET_PSYC_TransmitHandle *
108 GNUNET_PSYC_transmit_create (struct GNUNET_MQ_Handle *mq);
109
110
111 /**
112  * Destroy a transmission handle.
113  */
114 void
115 GNUNET_PSYC_transmit_destroy (struct GNUNET_PSYC_TransmitHandle *tmit);
116
117
118 /**
119  * Transmit a message.
120  *
121  * @param tmit
122  *        Transmission handle.
123  * @param method_name
124  *        Which method should be invoked.
125  * @param env
126  *        Environment for the message.
127  *        Should stay available until the first call to notify_data.
128  *        Can be NULL if there are no modifiers or @a notify_mod is
129  *        provided instead.
130  * @param notify_mod
131  *        Function to call to obtain modifiers.
132  *        Can be NULL if there are no modifiers or @a env is provided instead.
133  * @param notify_data
134  *        Function to call to obtain fragments of the data.
135  * @param notify_cls
136  *        Closure for @a notify_mod and @a notify_data.
137  * @param flags
138  *        Flags for the message being transmitted.
139  *
140  * @return #GNUNET_OK if the transmission was started.
141  *         #GNUNET_SYSERR if another transmission is already going on.
142  */
143 int
144 GNUNET_PSYC_transmit_message (struct GNUNET_PSYC_TransmitHandle *tmit,
145                               const char *method_name,
146                               const struct GNUNET_PSYC_Environment *env,
147                               GNUNET_PSYC_TransmitNotifyModifier notify_mod,
148                               GNUNET_PSYC_TransmitNotifyData notify_data,
149                               void *notify_cls,
150                               uint32_t flags);
151
152
153 /**
154  * Resume transmission.
155  *
156  * @param tmit  Transmission handle.
157  */
158 void
159 GNUNET_PSYC_transmit_resume (struct GNUNET_PSYC_TransmitHandle *tmit);
160
161
162 /**
163  * Abort transmission request.
164  *
165  * @param tmit  Transmission handle.
166  */
167 void
168 GNUNET_PSYC_transmit_cancel (struct GNUNET_PSYC_TransmitHandle *tmit);
169
170
171 /**
172  * Got acknowledgement of a transmitted message part, continue transmission.
173  *
174  * @param tmit  Transmission handle.
175  */
176 void
177 GNUNET_PSYC_transmit_got_ack (struct GNUNET_PSYC_TransmitHandle *tmit);
178
179
180 struct GNUNET_PSYC_ReceiveHandle;
181
182
183 /**
184  * Create handle for receiving messages.
185  */
186 struct GNUNET_PSYC_ReceiveHandle *
187 GNUNET_PSYC_receive_create (GNUNET_PSYC_MessageCallback message_cb,
188                             GNUNET_PSYC_MessagePartCallback message_part_cb,
189                             void *cb_cls);
190
191
192 /**
193  * Destroy handle for receiving messages.
194  */
195 void
196 GNUNET_PSYC_receive_destroy (struct GNUNET_PSYC_ReceiveHandle *recv);
197
198
199 /**
200  * Reset stored data related to the last received message.
201  */
202 void
203 GNUNET_PSYC_receive_reset (struct GNUNET_PSYC_ReceiveHandle *recv);
204
205
206 /**
207  * Handle incoming PSYC message.
208  *
209  * @param recv
210  *        Receive handle.
211  * @param msg
212  *        The message.
213  *
214  * @return #GNUNET_OK on success,
215  *         #GNUNET_SYSERR on receive error.
216  */
217 int
218 GNUNET_PSYC_receive_message (struct GNUNET_PSYC_ReceiveHandle *recv,
219                              const struct GNUNET_PSYC_MessageHeader *msg);
220
221
222 /**
223  * Check if @a data contains a series of valid message parts.
224  *
225  * @param data_size
226  *        Size of @a data.
227  * @param data
228  *        Data.
229  * @param[out] first_ptype
230  *        Type of first message part.
231  * @param[out] last_ptype
232  *        Type of last message part.
233  *
234  * @return Number of message parts found in @a data.
235  *         or GNUNET_SYSERR if the message contains invalid parts.
236  */
237 int
238 GNUNET_PSYC_receive_check_parts (uint16_t data_size, const char *data,
239                                  uint16_t *first_ptype, uint16_t *last_ptype);
240
241
242 /**
243  * Initialize PSYC message header.
244  */
245 void
246 GNUNET_PSYC_message_header_init (struct GNUNET_PSYC_MessageHeader *pmsg,
247                                  const struct GNUNET_MULTICAST_MessageHeader *mmsg,
248                                  uint32_t flags);
249
250
251 /**
252  * Create a new PSYC message header from a multicast message for sending it to clients.
253  */
254 struct GNUNET_PSYC_MessageHeader *
255 GNUNET_PSYC_message_header_create (const struct GNUNET_MULTICAST_MessageHeader *mmsg,
256                                    uint32_t flags);
257
258
259 /**
260  * Create a new PSYC message header from a PSYC message.
261  */
262 struct GNUNET_PSYC_MessageHeader *
263 GNUNET_PSYC_message_header_create_from_psyc (const struct GNUNET_PSYC_Message *msg);
264
265
266 #if 0                           /* keep Emacsens' auto-indent happy */
267 {
268 #endif
269 #ifdef __cplusplus
270 }
271 #endif
272
273 /* ifndef GNUNET_PSYC_MESSAGE_H */
274 #endif
275
276 /** @} */  /* end of group */