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