- some of the missing set union functionality implemented
[oweals/gnunet.git] / src / set / gnunet-service-set.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 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., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file set/gnunet-service-set.h
23  * @brief common components for the implementation the different set operations
24  * @author Florian Dold
25  */
26
27 #ifndef GNUNET_SERVICE_SET_H_PRIVATE
28 #define GNUNET_SERVICE_SET_H_PRIVATE
29
30 #include "platform.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_applications.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_mesh_service.h"
36 #include "gnunet_set_service.h"
37 #include "set.h"
38
39
40 /**
41  * Implementation-specific set state.
42  * Used as opaque pointer, and specified further
43  * in the respective implementation.
44  */
45 struct SetState;
46
47
48 /**
49  * Implementation-specific set operation.
50  * Used as opaque pointer, and specified further
51  * in the respective implementation.
52  */
53 struct OperationState;
54
55
56 /* forward declarations */
57 struct Set;
58 struct TunnelContext;
59 struct ElementEntry;
60
61
62 /**
63  * Detail information about an operation.
64  */
65 struct OperationSpecification
66 {
67   /**
68    * The type of the operation.
69    */
70   enum GNUNET_SET_OperationType operation;
71
72   /**
73    * The remove peer we evaluate the operation with
74    */
75   struct GNUNET_PeerIdentity peer;
76
77   /**
78    * Application ID for the operation, used to distinguish
79    * multiple operations of the same type with the same peer.
80    */
81   struct GNUNET_HashCode app_id;
82
83   /**
84    * Context message, may be NULL.
85    */
86   struct GNUNET_MessageHeader *context_msg;
87
88   /**
89    * Salt to use for the operation.
90    */
91   uint32_t salt;
92
93   /**
94    * ID used to identify responses to a client.
95    */
96   uint32_t client_request_id;
97
98   /**
99    * Set associated with the operation, NULL until the spec has been associated
100    * with a set.
101    */
102   struct Set *set;
103
104   /**
105    * When are elements sent to the client, and which elements are sent?
106    */
107   enum GNUNET_SET_ResultMode result_mode;
108 };
109
110
111
112
113 /**
114  * Signature of functions that create the implementation-specific
115  * state for a set supporting a specific operation.
116  *
117  * @return a set state specific to the supported operation
118  */
119 typedef struct SetState *(*CreateImpl) (void);
120
121
122 /**
123  * Signature of functions that implement the add/remove functionality
124  * for a set supporting a specific operation.
125  *
126  * @param set implementation-specific set state
127  * @param msg element message from the client
128  */
129 typedef void (*AddRemoveImpl) (struct SetState *state, struct ElementEntry *ee);
130
131
132 /**
133  * Signature of functions that handle disconnection
134  * of the remote peer.
135  *
136  * @param op the set operation, contains implementation-specific data
137  */
138 typedef void (*PeerDisconnectImpl) (struct OperationState *op);
139
140
141 /**
142  * Signature of functions that implement the destruction of the
143  * implementation-specific set state.
144  *
145  * @param state the set state, contains implementation-specific data
146  */
147 typedef void (*DestroySetImpl) (struct SetState *state);
148
149
150 /**
151  * Signature of functions that implement the creation of set operations
152  * (currently evaluate and accept).
153  *
154  * @param spec specification of the set operation to be created
155  * @param tunnel the tunnel with the other peer
156  * @param tc tunnel context
157  */
158 typedef void (*OpCreateImpl) (struct OperationSpecification *spec,
159                               struct GNUNET_MESH_Tunnel *tunnel,
160                               struct TunnelContext *tc);
161
162
163 /**
164  * Signature of functions that implement the message handling for
165  * the different set operations.
166  *
167  * @param op operation state
168  * @param msg received message
169  * @return GNUNET_OK on success, GNUNET_SYSERR to
170  *         destroy the operation and the tunnel
171  */
172 typedef int (*MsgHandlerImpl) (struct OperationState *op,
173                                const struct GNUNET_MessageHeader *msg);
174
175 typedef void (*CancelImpl) (struct SetState *set,
176                             uint32_t request_id);
177
178
179 /**
180  * Signature of functions that implement sending all the set's
181  * elements to the client.
182  *
183  * @param set set that should be iterated over
184  */
185 typedef void (*IterateImpl) (struct Set *set);
186
187
188 /**
189  * Dispatch table for a specific set operation.
190  * Every set operation has to implement the callback
191  * in this struct.
192  */
193 struct SetVT
194 {
195   /**
196    * Callback for the set creation.
197    */
198   CreateImpl create;
199
200   /**
201    * Callback for element insertion
202    */
203   AddRemoveImpl add;
204
205   /**
206    * Callback for element removal.
207    */
208   AddRemoveImpl remove;
209
210   /**
211    * Callback for accepting a set operation request
212    */
213   OpCreateImpl accept;
214
215   /**
216    * Callback for starting evaluation with a remote peer.
217    */
218   OpCreateImpl evaluate;
219
220   /**
221    * Callback for destruction of the set state.
222    */
223   DestroySetImpl destroy_set;
224
225   /**
226    * Callback for handling operation-specific messages.
227    */
228   MsgHandlerImpl msg_handler;
229
230   /**
231    * Callback for handling the remote peer's
232    * disconnect.
233    */
234   PeerDisconnectImpl peer_disconnect;
235
236   /**
237    * Callback for canceling an operation by
238    * its ID.
239    */
240   CancelImpl cancel;
241
242   /**
243    * Callback for iterating over all set elements.
244    */
245   IterateImpl iterate;
246 };
247
248
249 /**
250  * Information about an element element in the set.
251  * All elements are stored in a hash-table
252  * from their hash-code to their 'struct Element',
253  * so that the remove and add operations are reasonably
254  * fast.
255  */
256 struct ElementEntry
257 {
258   /**
259    * The actual element. The data for the element
260    * should be allocated at the end of this struct.
261    */
262   struct GNUNET_SET_Element element;
263
264   /**
265    * Hash of the element.
266    * Will be used to derive the different IBF keys
267    * for different salts.
268    */
269   struct GNUNET_HashCode element_hash;
270
271   /**
272    * Generation the element was added by the client.
273    * Operations of earlier generations will not consider the element.
274    */
275   unsigned int generation_added;
276
277   /**
278    * GNUNET_YES if the element has been removed in some generation.
279    */
280   int removed;
281
282   /**
283    * Generation the element was removed by the client.
284    * Operations of later generations will not consider the element.
285    * Only valid if is_removed is GNUNET_YES.
286    */
287   unsigned int generation_removed;
288
289   /**
290    * GNUNET_YES if the element is a remote element, and does not belong
291    * to the operation's set.
292    */
293   int remote;
294 };
295
296
297 /**
298  * A set that supports a specific operation
299  * with other peers.
300  */
301 struct Set
302 {
303   /**
304    * Client that owns the set.
305    * Only one client may own a set.
306    */
307   struct GNUNET_SERVER_Client *client;
308
309   /**
310    * Message queue for the client
311    */
312   struct GNUNET_MQ_Handle *client_mq;
313
314   /**
315    * Type of operation supported for this set
316    */
317   enum GNUNET_SET_OperationType operation;
318
319   /**
320    * Virtual table for this set.
321    * Determined by the operation type of this set.
322    */
323   const struct SetVT *vt;
324
325   /**
326    * Sets are held in a doubly linked list.
327    */
328   struct Set *next;
329
330   /**
331    * Sets are held in a doubly linked list.
332    */
333   struct Set *prev;
334
335   /**
336    * Implementation-specific state.
337    */
338   struct SetState *state;
339
340   /**
341    * Current state of iterating elements for the client.
342    * NULL if we are not currently iterating.
343    */
344   struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
345
346   /**
347    * Maps 'struct GNUNET_HashCode' to 'struct ElementEntry'.
348    */
349   struct GNUNET_CONTAINER_MultiHashMap *elements;
350
351   /**
352    * Current generation, that is, number of
353    * previously executed operations on this set
354    */
355   unsigned int current_generation;
356 };
357
358
359 /**
360  * Information about a tunnel we are connected to.
361  * Used as tunnel context with mesh.
362  */
363 struct TunnelContext
364 {
365   /**
366    * V-Table for the operation belonging
367    * to the tunnel contest.
368    */
369   const struct SetVT *vt;
370
371   /**
372    * Implementation-specific operation state.
373    */
374   struct OperationState *op;
375 };
376
377
378 /**
379  * Get the table with implementing functions for
380  * set union.
381  */
382 const struct SetVT *
383 _GSS_union_vt (void);
384
385 /**
386  * Get the table with implementing functions for
387  * set intersection.
388  */
389 const struct SetVT *
390 _GSS_intersection_vt (void);
391
392
393 #endif