Replace mesh with new version
[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 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 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_common.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_applications.h"
34 #include "gnunet_util_lib.h"
35 #include "gnunet_core_service.h"
36 #include "gnunet_mesh_service.h"
37 #include "gnunet_set_service.h"
38 #include "set.h"
39
40
41 /* FIXME: cfuchs */
42 struct IntersectionState;
43
44
45 /* FIXME: cfuchs */
46 struct IntersectionOperation;
47
48
49 /**
50  * Extra state required for set union.
51  */
52 struct UnionState;
53
54 /**
55  * State of a union operation being evaluated.
56  */
57 struct UnionEvaluateOperation;
58
59
60
61 /**
62  * A set that supports a specific operation
63  * with other peers.
64  */
65 struct Set
66 {
67   /**
68    * Client that owns the set.
69    * Only one client may own a set.
70    */
71   struct GNUNET_SERVER_Client *client;
72
73   /**
74    * Message queue for the client
75    */
76   struct GNUNET_MQ_Handle *client_mq;
77
78   /**
79    * Type of operation supported for this set
80    */
81   uint32_t operation; // use enum from API
82
83   /**
84    * Sets are held in a doubly linked list.
85    */
86   struct Set *next;
87
88   /**
89    * Sets are held in a doubly linked list.
90    */
91   struct Set *prev;
92
93   /**
94    * Appropriate state for each type of
95    * operation.
96    */
97   union {
98     struct IntersectionState *i;
99     struct UnionState *u;
100   } state;
101 };
102
103
104 /**
105  * Detail information about an operation.
106  */
107 struct OperationSpecification
108 {
109   /**
110    * The type of the operation.
111    */
112   enum GNUNET_SET_OperationType operation;
113
114   /**
115    * The remove peer we evaluate the operation with
116    */
117   struct GNUNET_PeerIdentity peer;
118
119   /**
120    * Application ID for the operation, used to distinguish
121    * multiple operations of the same type with the same peer.
122    */
123   struct GNUNET_HashCode app_id;
124
125   /**
126    * Context message, may be NULL.
127    */
128   struct GNUNET_MessageHeader *context_msg;
129
130   /**
131    * Salt to use for the operation.
132    */
133   uint32_t salt;
134
135   /**
136    * ID used to identify responses to a client.
137    */
138   uint32_t client_request_id;
139
140   /**
141    * Set associated with the operation, NULL until the spec has been associated
142    * with a set.
143    */
144   struct Set *set;
145 };
146
147
148 /**
149  * A listener is inhabited by a client, and
150  * waits for evaluation requests from remote peers.
151  */
152 struct Listener
153 {
154   /**
155    * Listeners are held in a doubly linked list.
156    */
157   struct Listener *next;
158
159   /**
160    * Listeners are held in a doubly linked list.
161    */
162   struct Listener *prev;
163
164   /**
165    * Client that owns the listener.
166    * Only one client may own a listener.
167    */
168   struct GNUNET_SERVER_Client *client;
169
170   /**
171    * Message queue for the client
172    */
173   struct GNUNET_MQ_Handle *client_mq;
174
175   /**
176    * The type of the operation.
177    */
178   enum GNUNET_SET_OperationType operation;
179
180   /**
181    * Application ID for the operation, used to distinguish
182    * multiple operations of the same type with the same peer.
183    */
184   struct GNUNET_HashCode app_id;
185 };
186
187
188 /**
189  * Peer that has connected to us, but is not yet evaluating a set operation.
190  * Once the peer has sent a request, and the client has
191  * accepted or rejected it, this information will be deleted.
192  */
193 struct Incoming;
194
195
196 /**
197  * Different types a tunnel can be.
198  */
199 enum TunnelContextType 
200 {
201   /**
202    * Tunnel is waiting for a set request from the tunnel,
203    * or for the ack/nack of the client for a received request.
204    */
205   CONTEXT_INCOMING,
206
207   /**
208    * The tunnel performs a union operation.
209    */
210   CONTEXT_OPERATION_UNION,
211
212   /**
213    * The tunnel performs an intersection operation.
214    */
215   CONTEXT_OPERATION_INTERSECTION,
216 };
217
218
219 /**
220  * State associated with the tunnel, dependent on
221  * tunnel type.
222  */
223 union TunnelContextData
224 {
225   /**
226    * Valid for tag 'CONTEXT_INCOMING'
227    */
228   struct Incoming *incoming;
229
230   /**
231    * Valid for tag 'CONTEXT_OPERATION_UNION'
232    */
233   struct UnionEvaluateOperation *union_op;
234
235   /**
236    * Valid for tag 'CONTEXT_OPERATION_INTERSECTION'
237    */
238   struct IntersectionEvaluateOperation *intersection_op;
239 };
240
241
242 /**
243  * Information about a tunnel we are connected to.
244  * Used as tunnel context with mesh.
245  */
246 struct TunnelContext
247 {
248   /**
249    * Type of the tunnel.
250    */
251   enum TunnelContextType type;
252
253   /**
254    * State associated with the tunnel, dependent on
255    * tunnel type.
256    */
257   union TunnelContextData data;
258 };
259
260
261
262 /**
263  * Configuration of the local peer.
264  */
265 extern const struct GNUNET_CONFIGURATION_Handle *configuration;
266
267 /**
268  * Handle to the mesh service.
269  */
270 extern struct GNUNET_MESH_Handle *mesh;
271
272
273 /**
274  * Create a new set supporting the union operation
275  *
276  * @return the newly created set
277  */
278 struct Set *
279 _GSS_union_set_create (void);
280
281
282 /**
283  * Evaluate a union operation with
284  * a remote peer.
285  *
286  * @param spec specification of the operation the evaluate
287  * @param tunnel tunnel already connected to the partner peer
288  * @return a handle to the operation
289  */
290 struct UnionEvaluateOperation *
291 _GSS_union_evaluate (struct OperationSpecification *spec,
292                      struct GNUNET_MESH_Tunnel *tunnel);
293
294
295 /**
296  * Add the element from the given element message to the set.
297  *
298  * @param m message with the element
299  * @param set set to add the element to
300  */
301 void
302 _GSS_union_add (struct GNUNET_SET_ElementMessage *m, struct Set *set);
303
304
305 /**
306  * Remove the element given in the element message from the set.
307  * Only marks the element as removed, so that older set operations can still exchange it.
308  *
309  * @param m message with the element
310  * @param set set to remove the element from
311  */
312 void
313 _GSS_union_remove (struct GNUNET_SET_ElementMessage *m, struct Set *set);
314
315
316 /**
317  * Destroy a set that supports the union operation
318  *
319  * @param set the set to destroy, must be of type GNUNET_SET_OPERATION_UNION
320  */
321 void
322 _GSS_union_set_destroy (struct Set *set);
323
324
325 /**
326  * Accept an union operation request from a remote peer
327  *
328  * @param spec all necessary information about the operation
329  * @param tunnel open tunnel to the partner's peer
330  * @return operation
331  */
332 struct UnionEvaluateOperation *
333 _GSS_union_accept (struct OperationSpecification *spec,
334                    struct GNUNET_MESH_Tunnel *tunnel);
335
336
337 /**
338  * Destroy a union operation, and free all resources
339  * associated with it.
340  *
341  * @param eo the union operation to destroy
342  */
343 void
344 _GSS_union_operation_destroy (struct UnionEvaluateOperation *eo);
345
346
347 /**
348  * Dispatch messages for a union operation.
349  *
350  * @param cls closure
351  * @param tunnel mesh tunnel
352  * @param tunnel_ctx tunnel context
353  * @param mh message to process
354  * @return GNUNET_SYSERR if the tunnel should be disconnected,
355  *         GNUNET_OK otherwise
356  */
357 int
358 _GSS_union_handle_p2p_message (void *cls,
359                                struct GNUNET_MESH_Tunnel *tunnel,
360                                void **tunnel_ctx,
361                                const struct GNUNET_MessageHeader *mh);
362
363
364 #endif