fixing collect_generation_garbage() complexity, doxygen, etc.
[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  * @author Christian Grothoff
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_cadet_service.h"
36 #include "gnunet_set_service.h"
37 #include "set.h"
38
39
40 /**
41  * Implementation-specific set state.  Used as opaque pointer, and
42  * specified further in the respective implementation.
43  */
44 struct SetState;
45
46 /**
47  * Implementation-specific set operation.  Used as opaque pointer, and
48  * specified further in the respective implementation.
49  */
50 struct OperationState;
51
52 /**
53  * A set that supports a specific operation with other peers.
54  */
55 struct Set;
56
57 /**
58  * Information about an element element in the set.  All elements are
59  * stored in a hash-table from their hash-code to their 'struct
60  * Element', so that the remove and add operations are reasonably
61  * fast.
62  */
63 struct ElementEntry;
64
65 /**
66  * Operation context used to execute a set operation.
67  */
68 struct Operation;
69
70
71 /**
72  * Detail information about an operation.
73  */
74 struct OperationSpecification
75 {
76
77   /**
78    * The remove peer we evaluate the operation with.
79    */
80   struct GNUNET_PeerIdentity peer;
81
82   /**
83    * Application ID for the operation, used to distinguish
84    * multiple operations of the same type with the same peer.
85    */
86   struct GNUNET_HashCode app_id;
87
88   /**
89    * Context message, may be NULL.
90    */
91   struct GNUNET_MessageHeader *context_msg;
92
93   /**
94    * Set associated with the operation, NULL until the spec has been
95    * associated with a set.
96    */
97   struct Set *set;
98
99   /**
100    * Salt to use for the operation.
101    */
102   uint32_t salt;
103
104   /**
105    * Remote peers element count
106    */
107   uint32_t remote_element_count;
108
109   /**
110    * ID used to identify an operation between service and client
111    */
112   uint32_t client_request_id;
113
114   /**
115    * The type of the operation.
116    */
117   enum GNUNET_SET_OperationType operation;
118
119   /**
120    * When are elements sent to the client, and which elements are sent?
121    */
122   enum GNUNET_SET_ResultMode result_mode;
123 };
124
125
126 /**
127  * Signature of functions that create the implementation-specific
128  * state for a set supporting a specific operation.
129  *
130  * @return a set state specific to the supported operation
131  */
132 typedef struct SetState *
133 (*CreateImpl) (void);
134
135
136 /**
137  * Signature of functions that implement the add/remove functionality
138  * for a set supporting a specific operation.
139  *
140  * @param set implementation-specific set state
141  * @param ee element message from the client
142  */
143 typedef void
144 (*AddRemoveImpl) (struct SetState *state,
145                   struct ElementEntry *ee);
146
147
148 /**
149  * Signature of functions that handle disconnection of the remote
150  * peer.
151  *
152  * @param op the set operation, contains implementation-specific data
153  */
154 typedef void
155 (*PeerDisconnectImpl) (struct Operation *op);
156
157
158 /**
159  * Signature of functions that implement the destruction of the
160  * implementation-specific set state.
161  *
162  * @param state the set state, contains implementation-specific data
163  */
164 typedef void
165 (*DestroySetImpl) (struct SetState *state);
166
167
168 /**
169  * Signature of functions that implement the creation of set operations
170  * (currently "evaluate" and "accept").
171  *
172  * @param op operation that is created, should be initialized by the implementation
173  */
174 typedef void
175 (*OpCreateImpl) (struct Operation *op);
176
177
178 /**
179  * Signature of functions that implement the message handling for
180  * the different set operations.
181  *
182  * @param op operation state
183  * @param msg received message
184  * @return #GNUNET_OK on success, #GNUNET_SYSERR to
185  *         destroy the operation and the tunnel
186  */
187 typedef int
188 (*MsgHandlerImpl) (struct Operation *op,
189                    const struct GNUNET_MessageHeader *msg);
190
191
192 /**
193  * Signature of functions that implement operation cancellation
194  *
195  * @param op operation state
196  */
197 typedef void
198 (*CancelImpl) (struct Operation *op);
199
200
201 /**
202  * Dispatch table for a specific set operation.  Every set operation
203  * has to implement the callback in this struct.
204  */
205 struct SetVT
206 {
207   /**
208    * Callback for the set creation.
209    */
210   CreateImpl create;
211
212   /**
213    * Callback for element insertion
214    */
215   AddRemoveImpl add;
216
217   /**
218    * Callback for element removal.
219    */
220   AddRemoveImpl remove;
221
222   /**
223    * Callback for accepting a set operation request
224    */
225   OpCreateImpl accept;
226
227   /**
228    * Callback for starting evaluation with a remote peer.
229    */
230   OpCreateImpl evaluate;
231
232   /**
233    * Callback for destruction of the set state.
234    */
235   DestroySetImpl destroy_set;
236
237   /**
238    * Callback for handling operation-specific messages.
239    */
240   MsgHandlerImpl msg_handler;
241
242   /**
243    * Callback for handling the remote peer's disconnect.
244    */
245   PeerDisconnectImpl peer_disconnect;
246
247   /**
248    * Callback for canceling an operation by its ID.
249    */
250   CancelImpl cancel;
251 };
252
253
254 /**
255  * Information about an element element in the set.  All elements are
256  * stored in a hash-table from their hash-code to their `struct
257  * Element`, so that the remove and add operations are reasonably
258  * fast.
259  */
260 struct ElementEntry
261 {
262   /**
263    * The actual element. The data for the element
264    * should be allocated at the end of this struct.
265    */
266   struct GNUNET_SET_Element element;
267
268   /**
269    * Hash of the element.  For set union: Will be used to derive the
270    * different IBF keys for different salts.
271    */
272   struct GNUNET_HashCode element_hash;
273
274   /**
275    * Generation the element was added by the client.
276    * Operations of earlier generations will not consider the element.
277    */
278   unsigned int generation_added;
279
280   /**
281    * Generation the element was removed by the client.
282    * Operations of later generations will not consider the element.
283    * Only valid if @e removed is #GNUNET_YES.
284    */
285   unsigned int generation_removed;
286
287   /**
288    * #GNUNET_YES if the element has been removed in some generation.
289    */
290   int removed;
291
292   /**
293    * #GNUNET_YES if the element is a remote element, and does not belong
294    * to the operation's set.
295    */
296   int remote;
297 };
298
299
300 /**
301  * Operation context used to execute a set operation.
302  */
303 struct Operation
304 {
305   /**
306    * V-Table for the operation belonging to the tunnel contest.
307    *
308    * Used for all operation specific operations after receiving the ops request
309    */
310   const struct SetVT *vt;
311
312   /**
313    * Tunnel to the peer.
314    */
315   struct GNUNET_CADET_Channel *channel;
316
317   /**
318    * Message queue for the tunnel.
319    */
320   struct GNUNET_MQ_Handle *mq;
321
322   /**
323    * Detail information about the set operation, including the set to
324    * use.  When 'spec' is NULL, the operation is not yet entirely
325    * initialized.
326    */
327   struct OperationSpecification *spec;
328
329   /**
330    * Operation-specific operation state.
331    */
332   struct OperationState *state;
333
334   /**
335    * Evaluate operations are held in a linked list.
336    */
337   struct Operation *next;
338
339   /**
340    * Evaluate operations are held in a linked list.
341    */
342   struct Operation *prev;
343
344   /**
345    * #GNUNET_YES if this is not a "real" set operation yet, and we still
346    * need to wait for the other peer to give us more details.
347    */
348   int is_incoming;
349
350   /**
351    * Generation in which the operation handle
352    * was created.
353    */
354   unsigned int generation_created;
355
356   /**
357    * Set to #GNUNET_YES if the set service should not free the
358    * operation, as it is still needed (e.g. in some scheduled task).
359    */
360   int keep;
361 };
362
363
364 /**
365  * A set that supports a specific operation with other peers.
366  */
367 struct Set
368 {
369
370   /**
371    * Sets are held in a doubly linked list (in `sets_head` and `sets_tail`).
372    */
373   struct Set *next;
374
375   /**
376    * Sets are held in a doubly linked list.
377    */
378   struct Set *prev;
379
380   /**
381    * Client that owns the set.  Only one client may own a set,
382    * and there can only be one set per client.
383    */
384   struct GNUNET_SERVER_Client *client;
385
386   /**
387    * Message queue for the client.
388    */
389   struct GNUNET_MQ_Handle *client_mq;
390
391   /**
392    * Virtual table for this set.  Determined by the operation type of
393    * this set.
394    *
395    * Used only for Add/remove of elements and when receiving an incoming
396    * operation from a remote peer.
397    */
398   const struct SetVT *vt;
399
400   /**
401    * Implementation-specific state.
402    */
403   struct SetState *state;
404
405   /**
406    * Current state of iterating elements for the client.
407    * NULL if we are not currently iterating.
408    */
409   struct GNUNET_CONTAINER_MultiHashMapIterator *iter;
410
411   /**
412    * Maps `struct GNUNET_HashCode *` to `struct ElementEntry *`.
413    */
414   struct GNUNET_CONTAINER_MultiHashMap *elements;
415
416   /**
417    * Evaluate operations are held in a linked list.
418    */
419   struct Operation *ops_head;
420
421   /**
422    * Evaluate operations are held in a linked list.
423    */
424   struct Operation *ops_tail;
425
426   /**
427    * Current generation, that is, number of previously executed
428    * operations on this set
429    */
430   unsigned int current_generation;
431
432   /**
433    * Type of operation supported for this set
434    */
435   enum GNUNET_SET_OperationType operation;
436
437 };
438
439
440 /**
441  * Destroy the given operation.  Call the implementation-specific
442  * cancel function of the operation.  Disconnects from the remote
443  * peer.  Does not disconnect the client, as there may be multiple
444  * operations per set.
445  *
446  * @param op operation to destroy
447  */
448 void
449 _GSS_operation_destroy (struct Operation *op);
450
451
452 /**
453  * Get the table with implementing functions for set union.
454  *
455  * @return the operation specific VTable
456  */
457 const struct SetVT *
458 _GSS_union_vt (void);
459
460
461 /**
462  * Get the table with implementing functions for set intersection.
463  *
464  * @return the operation specific VTable
465  */
466 const struct SetVT *
467 _GSS_intersection_vt (void);
468
469
470 #endif