65d70d86b9caa3f54965289020799893c2e5a639
[oweals/gnunet.git] / src / include / gnunet_set_service.h
1 /*
2       This file is part of GNUnet
3       (C) 2013 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  * @file include/gnunet_set_service.h
23  * @brief two-peer set operations
24  * @author Florian Dold
25  */
26
27 #ifndef GNUNET_SET_SERVICE_H
28 #define GNUNET_SET_SERVICE_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 #include "platform.h"
39 #include "gnunet_common.h"
40 #include "gnunet_time_lib.h"
41 #include "gnunet_configuration_lib.h"
42
43
44 /**
45  * Opaque handle to a set.
46  */
47 struct GNUNET_SET_Handle;
48
49 /**
50  * Opaque handle to a set operation request from another peer.
51  */
52 struct GNUNET_SET_Request;
53
54 /**
55  * Opaque handle to a listen operation.
56  */
57 struct GNUNET_SET_ListenHandle;
58
59 /**
60  * Opaque handle to a set operation.
61  */
62 struct GNUNET_SET_OperationHandle;
63
64
65 /**
66  * The operation that a set set supports.
67  */
68 enum GNUNET_SET_OperationType
69 {
70   /**
71    * Set intersection, only return elements that are in both sets.
72    */
73   GNUNET_SET_OPERATION_INTERSECTION,
74   /**
75    * Set union, return all elements that are in at least one of the sets.
76    */
77   GNUNET_SET_OPERATION_UNION
78 };
79
80 /**
81  * Status for the result callback
82  */
83 enum GNUNET_SET_Status
84 {
85   /**
86    * Everything went ok.
87    */
88   GNUNET_SET_STATUS_OK,
89   /**
90    * There was a timeout.
91    */
92   GNUNET_SET_STATUS_TIMEOUT,
93   /**
94    * The other peer refused to to the operation with us
95    */
96   GNUNET_SET_STATUS_REFUSED,
97   /**
98    * Success, all elements have been sent.
99    */
100   GNUNET_SET_STATUS_DONE
101 };
102
103 /**
104  * The way results are given to the client.
105  */
106 enum GNUNET_SET_ResultMode
107 {
108   /**
109    * Client gets every element in the resulting set.
110    */
111   GNUNET_SET_RESULT_FULL,
112   /**
113    * Client gets only elements that have been added to the set.
114    * Only works with set union.
115    */
116   GNUNET_SET_RESULT_ADDED,
117   /**
118    * Client gets only elements that have been removed from the set.
119    * Only works with set intersection.
120    */
121   GNUNET_SET_RESULT_REMOVED
122 };
123
124 /**
125  * Element stored in a set.
126  */
127 struct GNUNET_SET_Element
128 {
129   /**
130    * Number of bytes in the buffer pointed to by data.
131    */
132   uint16_t size;
133
134   /**
135    * Application-specific element type.
136    */
137   uint16_t type;
138
139   /**
140    * Actual data of the element
141    */
142   void *data;
143 };
144
145
146 /**
147  * Continuation used for some of the set operations
148  *
149  * @cls closure
150  */
151 typedef void (*GNUNET_SET_Continuation) (void *cls);
152
153
154 /**
155  * Callback for set operation results. Called for each element
156  * in the result set.
157  *
158  * @param cls closure
159  * @param element a result element, only valid if status is GNUNET_SET_STATUS_OK
160  * @param status see enum GNUNET_SET_Status
161  */
162 typedef void (*GNUNET_SET_ResultIterator) (void *cls,
163                                            struct GNUNET_SET_Element *element,
164                                            enum GNUNET_SET_Status status);
165
166
167 /**
168  * Called when another peer wants to do a set operation with the
169  * local peer
170  *
171  * @param other_peer the other peer
172  * @param context_msg message with application specific information from
173  *        the other peer
174  * @param request request from the other peer, use GNUNET_SET_accept
175  *        to accept it, otherwise the request will be refused
176  *        Note that we don't use a return value here, as it is also
177  *        necessary to specify the set we want to do the operation with,
178  *        whith sometimes can be derived from the context message.
179  *        Also necessary to specify the timeout.
180  */
181 typedef void
182 (*GNUNET_SET_ListenCallback) (void *cls,
183                               const struct GNUNET_PeerIdentity *other_peer,
184                               const struct GNUNET_MessageHeader *context_msg,
185                               struct GNUNET_SET_Request *request);
186
187
188
189 /**
190  * Create an empty set, supporting the specified operation.
191  *
192  * @param cfg configuration to use for connecting to the
193  *        set service
194  * @param op operation supported by the set
195  *        Note that the operation has to be specified
196  *        beforehand, as certain set operations need to maintain
197  *        data structures spefific to the operation
198  * @return a handle to the set
199  */
200 struct GNUNET_SET_Handle *
201 GNUNET_SET_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
202                    enum GNUNET_SET_OperationType op);
203
204
205 /**
206  * Add an element to the given set.
207  * After the element has been added (in the sense of being
208  * transmitted to the set service), cont will be called.
209  * Calls to add_element can be queued
210  *
211  * @param set set to add element to
212  * @param element element to add to the set
213  * @param cont continuation called after the element has been added
214  * @param cont_cls closure for cont
215  */
216 void
217 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
218                         const struct GNUNET_SET_Element *element,
219                         GNUNET_SET_Continuation cont,
220                         void *cont_cls);
221
222
223 /**
224  * Remove an element to the given set.
225  * After the element has been removed (in the sense of the
226  * request being transmitted to the set service), cont will be called.
227  * Calls to remove_element can be queued
228  *
229  * @param set set to remove element from
230  * @param element element to remove from the set
231  * @param cont continuation called after the element has been removed
232  * @param cont_cls closure for cont
233  */
234 void
235 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
236                            const struct GNUNET_SET_Element *element,
237                            GNUNET_SET_Continuation cont,
238                            void *cont_cls);
239
240
241 /**
242  * Destroy the set handle, and free all associated resources.
243  */
244 void
245 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set);
246
247
248 /**
249  * Evaluate a set operation with our set and the set of another peer.
250  *
251  * @param set set to use
252  * @param salt salt for HKDF (explain more here)
253  * @param other_peer peer with the other set
254  * @param app_id hash for the application using the set
255  * @param context_msg additional information for the request
256  * @param result_cb called on error or success
257  * @param result_cls closure for result_cb
258  * @return a handle to cancel the operation
259  */
260 struct GNUNET_SET_OperationHandle *
261 GNUNET_SET_evaluate (struct GNUNET_SET_Handle *set,
262                      const struct GNUNET_PeerIdentity *other_peer,
263                      const struct GNUNET_HashCode *app_id,
264                      const struct GNUNET_MessageHeader *context_msg,
265                      struct GNUNET_TIME_Relative timeout,
266                      enum GNUNET_SET_ResultMode result_mode,
267                      GNUNET_SET_ResultIterator result_cb,
268                      void *result_cls);
269
270
271 /**
272  * Wait for set operation requests for the given application id
273  * 
274  * @param cfg configuration to use for connecting to
275  *            the set service
276  * @param operation operation we want to listen for
277  * @param app_id id of the application that handles set operation requests
278  * @param listen_cb called for each incoming request matching the operation
279  *                  and application id
280  * @param listen_cls handle for listen_cb
281  * @return a handle that can be used to cancel the listen operation
282  */
283 struct GNUNET_SET_ListenHandle *
284 GNUNET_SET_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
285                    enum GNUNET_SET_OperationType op_type,
286                    const struct GNUNET_HashCode *app_id,
287                    GNUNET_SET_ListenCallback listen_cb,
288                    void *listen_cls);
289
290
291
292 /**
293  * Cancel the given listen operation.
294  *
295  * @param lh handle for the listen operation
296  */
297 void
298 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh);
299
300
301 /**
302  * Accept a request we got via GNUNET_SET_listen
303  *
304  * @param request request to accept
305  * @param set set used for the requested operation 
306  * @param timeout timeout for the set operation
307  * @param result_cb callback for the results
308  * @param cls closure for result_cb
309  */
310 struct GNUNET_SET_OperationHandle *
311 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
312                    struct GNUNET_SET_Handle *set,
313                    struct GNUNET_TIME_Relative timeout,
314                    enum GNUNET_SET_ResultMode result_mode,
315                    GNUNET_SET_ResultIterator result_cb,
316                    void *cls);
317
318
319 /**
320  * Cancel the given set operation.
321  *
322  * @param op set operation to cancel
323  */
324 void
325 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *oh);
326
327
328 #if 0                           /* keep Emacsens' auto-indent happy */
329 {
330 #endif
331 #ifdef __cplusplus
332 }
333 #endif
334
335 #endif