multicast: removed replay cancellation as responses are limited
[oweals/gnunet.git] / src / include / gnunet_set_service.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2013, 2014 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 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  * @file include/gnunet_set_service.h
23  * @brief two-peer set operations
24  * @author Florian Dold
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_SET_SERVICE_H
29 #define GNUNET_SET_SERVICE_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_common.h"
40 #include "gnunet_time_lib.h"
41 #include "gnunet_configuration_lib.h"
42
43
44 /**
45  * Maximum size of a context message for set operation requests.
46  */
47 #define GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE ((1<<16) - 1024)
48
49
50 /**
51  * Opaque handle to a set.
52  */
53 struct GNUNET_SET_Handle;
54
55 /**
56  * Opaque handle to a set operation request from another peer.
57  */
58 struct GNUNET_SET_Request;
59
60 /**
61  * Opaque handle to a listen operation.
62  */
63 struct GNUNET_SET_ListenHandle;
64
65 /**
66  * Opaque handle to a set operation.
67  */
68 struct GNUNET_SET_OperationHandle;
69
70
71 /**
72  * The operation that a set set supports.
73  */
74 enum GNUNET_SET_OperationType
75 {
76   /**
77    * A purely local set that does not support any operation.
78    */
79   GNUNET_SET_OPERATION_NONE,
80
81   /**
82    * Set intersection, only return elements that are in both sets.
83    */
84   GNUNET_SET_OPERATION_INTERSECTION,
85
86   /**
87    * Set union, return all elements that are in at least one of the sets.
88    */
89   GNUNET_SET_OPERATION_UNION
90 };
91
92
93 /**
94  * Status for the result callback
95  */
96 enum GNUNET_SET_Status
97 {
98   /**
99    * Everything went ok, we are transmitting an element of the
100    * result (in set, or to be removed from set, depending on
101    * the `enum GNUNET_SET_ResultMode`).
102    *
103    * Only applies to
104    * #GNUNET_SET_RESULT_FULL,
105    * #GNUNET_SET_RESULT_ADDED,
106    * #GNUNET_SET_RESULT_REMOVED,
107    */
108   GNUNET_SET_STATUS_OK,
109
110   /**
111    * Element should be added to the result set
112    * of the local peer, i.e. the local peer is
113    * missing an element.
114    *
115    * Only applies to #GNUNET_SET_RESULT_SYMMETRIC
116    */
117   GNUNET_SET_STATUS_ADD_LOCAL,
118
119   /**
120    * Element should be added to the result set
121    * of the remove peer, i.e. the remote peer is
122    * missing an element.
123    *
124    * Only applies to #GNUNET_SET_RESULT_SYMMETRIC
125    */
126   GNUNET_SET_STATUS_ADD_REMOTE,
127
128   /**
129    * The other peer refused to to the operation with us,
130    * or something went wrong.
131    */
132   GNUNET_SET_STATUS_FAILURE,
133
134   /**
135    * Success, all elements have been returned (but the other peer
136    * might still be receiving some from us, so we are not done).  Only
137    * used during UNION operation.
138    */
139   GNUNET_SET_STATUS_HALF_DONE,
140
141   /**
142    * Success, all elements have been sent (and received).
143    */
144   GNUNET_SET_STATUS_DONE
145 };
146
147
148 /**
149  * The way results are given to the client.
150  */
151 enum GNUNET_SET_ResultMode
152 {
153   /**
154    * Client gets every element in the resulting set.
155    *
156    * Only supported for set intersection.
157    */
158   GNUNET_SET_RESULT_FULL,
159
160   /**
161    * Client gets notified of the required changes
162    * for both the local and the remote set.
163    *
164    * Only supported for set 
165    */
166   GNUNET_SET_RESULT_SYMMETRIC,
167
168   /**
169    * Client gets only elements that have been removed from the set.
170    *
171    * Only supported for set intersection.
172    */
173   GNUNET_SET_RESULT_REMOVED,
174
175   /**
176    * Client gets only elements that have been removed from the set.
177    *
178    * Only supported for set union.
179    */
180   GNUNET_SET_RESULT_ADDED
181 };
182
183
184 /**
185  * Element stored in a set.
186  */
187 struct GNUNET_SET_Element
188 {
189   /**
190    * Number of bytes in the buffer pointed to by data.
191    */
192   uint16_t size;
193
194   /**
195    * Application-specific element type.
196    */
197   uint16_t element_type;
198
199   /**
200    * Actual data of the element
201    */
202   const void *data;
203 };
204
205
206 /**
207  * Continuation used for some of the set operations
208  *
209  * @param cls closure
210  */
211 typedef void (*GNUNET_SET_Continuation) (void *cls);
212
213
214 /**
215  * Callback for set operation results. Called for each element
216  * in the result set.
217  *
218  * @param cls closure
219  * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
220  * @param status see `enum GNUNET_SET_Status`
221  */
222 typedef void (*GNUNET_SET_ResultIterator) (void *cls,
223                                            const struct GNUNET_SET_Element *element,
224                                            enum GNUNET_SET_Status status);
225
226 /**
227  * Iterator for set elements.
228  *
229  * @param cls closure
230  * @param element the current element, NULL if all elements have been
231  *        iterated over
232  * @return #GNUNET_YES to continue iterating, #GNUNET_NO to stop.
233  */
234 typedef int (*GNUNET_SET_ElementIterator) (void *cls,
235                                            const struct GNUNET_SET_Element *element);
236
237
238 /**
239  * Called when another peer wants to do a set operation with the
240  * local peer. If a listen error occurs, the @a request is NULL.
241  *
242  * @param cls closure
243  * @param other_peer the other peer
244  * @param context_msg message with application specific information from
245  *        the other peer
246  * @param request request from the other peer (never NULL), use GNUNET_SET_accept()
247  *        to accept it, otherwise the request will be refused
248  *        Note that we can't just return value from the listen callback,
249  *        as it is also necessary to specify the set we want to do the
250  *        operation with, whith sometimes can be derived from the context
251  *        message. It's necessary to specify the timeout.
252  */
253 typedef void
254 (*GNUNET_SET_ListenCallback) (void *cls,
255                               const struct GNUNET_PeerIdentity *other_peer,
256                               const struct GNUNET_MessageHeader *context_msg,
257                               struct GNUNET_SET_Request *request);
258
259
260
261 typedef void
262 (*GNUNET_SET_CopyReadyCallback) (void *cls,
263                                  struct GNUNET_SET_Handle *copy);
264
265
266 /**
267  * Create an empty set, supporting the specified operation.
268  *
269  * @param cfg configuration to use for connecting to the
270  *        set service
271  * @param op operation supported by the set
272  *        Note that the operation has to be specified
273  *        beforehand, as certain set operations need to maintain
274  *        data structures spefific to the operation
275  * @return a handle to the set
276  */
277 struct GNUNET_SET_Handle *
278 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
279                    enum GNUNET_SET_OperationType op);
280
281
282 /**
283  * Add an element to the given set.
284  * After the element has been added (in the sense of being
285  * transmitted to the set service), @a cont will be called.
286  * Calls to #GNUNET_SET_add_element can be queued
287  *
288  * @param set set to add element to
289  * @param element element to add to the set
290  * @param cont continuation called after the element has been added
291  * @param cont_cls closure for @a cont
292  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
293  *         set is invalid (e.g. the set service crashed)
294  */
295 int
296 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
297                         const struct GNUNET_SET_Element *element,
298                         GNUNET_SET_Continuation cont,
299                         void *cont_cls);
300
301
302 /**
303  * Remove an element to the given set.
304  * After the element has been removed (in the sense of the
305  * request being transmitted to the set service), cont will be called.
306  * Calls to remove_element can be queued
307  *
308  * @param set set to remove element from
309  * @param element element to remove from the set
310  * @param cont continuation called after the element has been removed
311  * @param cont_cls closure for @a cont
312  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
313  *         set is invalid (e.g. the set service crashed)
314  */
315 int
316 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
317                            const struct GNUNET_SET_Element *element,
318                            GNUNET_SET_Continuation cont,
319                            void *cont_cls);
320
321
322 void
323 GNUNET_SET_copy_lazy (struct GNUNET_SET_Handle *set,
324                       GNUNET_SET_CopyReadyCallback cb,
325                       void *cls);
326
327
328 /**
329  * Destroy the set handle, and free all associated resources.
330  * Iterations must have completed (or be explicitly canceled)
331  * before destroying the corresponding set.  Operations may
332  * still be pending when a set is destroyed.
333  *
334  * @param set set to destroy
335  */
336 void
337 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set);
338
339
340 /**
341  * Prepare a set operation to be evaluated with another peer.
342  * The evaluation will not start until the client provides
343  * a local set with GNUNET_SET_commit().
344  *
345  * @param other_peer peer with the other set
346  * @param app_id hash for the application using the set
347  * @param context_msg additional information for the request
348  * @param result_mode specified how results will be returned,
349  *        see `enum GNUNET_SET_ResultMode`.
350  * @param result_cb called on error or success
351  * @param result_cls closure for @a result_cb
352  * @return a handle to cancel the operation
353  */
354 struct GNUNET_SET_OperationHandle *
355 GNUNET_SET_prepare (const struct GNUNET_PeerIdentity *other_peer,
356                     const struct GNUNET_HashCode *app_id,
357                     const struct GNUNET_MessageHeader *context_msg,
358                     enum GNUNET_SET_ResultMode result_mode,
359                     GNUNET_SET_ResultIterator result_cb,
360                     void *result_cls);
361
362
363 /**
364  * Wait for set operation requests for the given application ID.
365  * If the connection to the set service is lost, the listener is
366  * re-created transparently with exponential backoff.
367  *
368  * @param cfg configuration to use for connecting to
369  *            the set service
370  * @param operation operation we want to listen for
371  * @param app_id id of the application that handles set operation requests
372  * @param listen_cb called for each incoming request matching the operation
373  *                  and application id
374  * @param listen_cls handle for @a listen_cb
375  * @return a handle that can be used to cancel the listen operation
376  */
377 struct GNUNET_SET_ListenHandle *
378 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
379                    enum GNUNET_SET_OperationType op_type,
380                    const struct GNUNET_HashCode *app_id,
381                    GNUNET_SET_ListenCallback listen_cb,
382                    void *listen_cls);
383
384
385 /**
386  * Cancel the given listen operation.  After calling cancel, the
387  * listen callback for this listen handle will not be called again.
388  *
389  * @param lh handle for the listen operation
390  */
391 void
392 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh);
393
394
395 /**
396  * Accept a request we got via GNUNET_SET_listen().  Must be called during
397  * GNUNET_SET_listen(), as the `struct GNUNET_SET_Request` becomes invalid
398  * afterwards.
399  * Call GNUNET_SET_commit() to provide the local set to use for the operation,
400  * and to begin the exchange with the remote peer.
401  *
402  * @param request request to accept
403  * @param result_mode specified how results will be returned,
404  *        see `enum GNUNET_SET_ResultMode`.
405  * @param result_cb callback for the results
406  * @param result_cls closure for @a result_cb
407  * @return a handle to cancel the operation
408  */
409 struct GNUNET_SET_OperationHandle *
410 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
411                    enum GNUNET_SET_ResultMode result_mode,
412                    GNUNET_SET_ResultIterator result_cb,
413                    void *result_cls);
414
415
416 /**
417  * Commit a set to be used with a set operation.
418  * This function is called once we have fully constructed
419  * the set that we want to use for the operation.  At this
420  * time, the P2P protocol can then begin to exchange the
421  * set information and call the result callback with the
422  * result information.
423  *
424  * @param oh handle to the set operation
425  * @param set the set to use for the operation
426  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
427  *         set is invalid (e.g. the set service crashed)
428  */
429 int
430 GNUNET_SET_commit (struct GNUNET_SET_OperationHandle *oh,
431                    struct GNUNET_SET_Handle *set);
432
433
434 /**
435  * Cancel the given set operation.  May not be called after the
436  * operation's `GNUNET_SET_ResultIterator` has been called with a
437  * status that indicates error, timeout or done.
438  *
439  * @param oh set operation to cancel
440  */
441 void
442 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh);
443
444
445 /**
446  * Iterate over all elements in the given set.
447  * Note that this operation involves transferring every element of the set
448  * from the service to the client, and is thus costly.
449  * Only one iteration per set may be active at the same time.
450  *
451  * @param set the set to iterate over
452  * @param iter the iterator to call for each element
453  * @param iter_cls closure for @a iter
454  * @return #GNUNET_YES if the iteration started successfuly,
455  *         #GNUNET_NO if another iteration was still active,
456  *         #GNUNET_SYSERR if the set is invalid (e.g. the server crashed, disconnected)
457  */
458 int
459 GNUNET_SET_iterate (struct GNUNET_SET_Handle *set,
460                     GNUNET_SET_ElementIterator iter,
461                     void *iter_cls);
462
463 /**
464  * Stop iteration over all elements in the given set.  Can only
465  * be called before the iteration has "naturally" completed its
466  * turn.
467  *
468  * @param set the set to stop iterating over
469  */
470 void
471 GNUNET_SET_iterate_cancel (struct GNUNET_SET_Handle *set);
472
473
474 #if 0                           /* keep Emacsens' auto-indent happy */
475 {
476 #endif
477 #ifdef __cplusplus
478 }
479 #endif
480
481 #endif