6ababe92fe2f05958927c875f1b8d83b2e68eded
[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 /**
42  * Implementation-specific set state.
43  * Used as opaque pointer, and specified further
44  * in the respective implementation.
45  */
46 struct SetState;
47
48
49 /**
50  * Implementation-specific set operation.
51  * Used as opaque pointer, and specified further
52  * in the respective implementation.
53  */
54 struct OperationState;
55
56
57 /* forward declarations */
58 struct Set;
59 struct TunnelContext;
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
106 /**
107  * Signature of functions that create the implementation-specific
108  * state for a set supporting a specific operation.
109  *
110  * @return a set state specific to the supported operation
111  */
112 typedef struct SetState *(*CreateImpl) (void);
113
114
115 /**
116  * Signature of functions that implement the add/remove functionality
117  * for a set supporting a specific operation.
118  *
119  * @param set implementation-specific set state
120  * @param msg element message from the client
121  */
122 typedef void (*AddRemoveImpl) (struct SetState *state, const struct GNUNET_SET_Element *element);
123
124
125 /**
126  * Signature of functions that handle disconnection
127  * of the remote peer.
128  *
129  * @param op the set operation, contains implementation-specific data
130  */
131 typedef void (*PeerDisconnectImpl) (struct OperationState *op);
132
133
134 /**
135  * Signature of functions that implement the destruction of the
136  * implementation-specific set state.
137  *
138  * @param state the set state, contains implementation-specific data
139  */
140 typedef void (*DestroySetImpl) (struct SetState *state);
141
142
143 /**
144  * Signature of functions that implement the creation of set operations
145  * (currently evaluate and accept).
146  *
147  * @param spec specification of the set operation to be created
148  * @param tunnel the tunnel with the other peer
149  * @param tc tunnel context
150  */
151 typedef void (*OpCreateImpl) (struct OperationSpecification *spec,
152                               struct GNUNET_MESH_Tunnel *tunnel,
153                               struct TunnelContext *tc);
154
155
156 /**
157  * Signature of functions that implement the message handling for
158  * the different set operations.
159  *
160  * @param op operation state
161  * @param msg received message
162  * @return GNUNET_OK on success, GNUNET_SYSERR to
163  *         destroy the operation and the tunnel
164  */
165 typedef int (*MsgHandlerImpl) (struct OperationState *op,
166                                const struct GNUNET_MessageHeader *msg);
167
168 typedef void (*CancelImpl) (struct SetState *set,
169                             uint32_t request_id);
170
171
172 /**
173  * Dispatch table for a specific set operation.
174  * Every set operation has to implement the callback
175  * in this struct.
176  */
177 struct SetVT
178 {
179   /**
180    * Callback for the set creation.
181    */
182   CreateImpl create;
183
184   /**
185    * Callback for element insertion
186    */
187   AddRemoveImpl add;
188
189   /**
190    * Callback for element removal.
191    */
192   AddRemoveImpl remove;
193
194   /**
195    * Callback for accepting a set operation request
196    */
197   OpCreateImpl accept;
198
199   /**
200    * Callback for starting evaluation with a remote peer.
201    */
202   OpCreateImpl evaluate;
203
204   /**
205    * Callback for destruction of the set state.
206    */
207   DestroySetImpl destroy_set;
208
209   /**
210    * Callback for handling operation-specific messages.
211    */
212   MsgHandlerImpl msg_handler;
213
214   /**
215    * Callback for handling the remote peer's
216    * disconnect.
217    */
218   PeerDisconnectImpl peer_disconnect;
219
220   /**
221    * Callback for canceling an operation by
222    * its ID.
223    */
224   CancelImpl cancel;
225 };
226
227
228 /**
229  * A set that supports a specific operation
230  * with other peers.
231  */
232 struct Set
233 {
234   /**
235    * Client that owns the set.
236    * Only one client may own a set.
237    */
238   struct GNUNET_SERVER_Client *client;
239
240   /**
241    * Message queue for the client
242    */
243   struct GNUNET_MQ_Handle *client_mq;
244
245   /**
246    * Type of operation supported for this set
247    */
248   enum GNUNET_SET_OperationType operation;
249
250   /**
251    * Virtual table for this set.
252    * Determined by the operation type of this set.
253    */
254   const struct SetVT *vt;
255
256   /**
257    * Sets are held in a doubly linked list.
258    */
259   struct Set *next;
260
261   /**
262    * Sets are held in a doubly linked list.
263    */
264   struct Set *prev;
265
266   /**
267    * Implementation-specific state.
268    */
269   struct SetState *state;
270 };
271
272
273 /**
274  * Information about a tunnel we are connected to.
275  * Used as tunnel context with mesh.
276  */
277 struct TunnelContext
278 {
279   /**
280    * V-Table for the operation belonging
281    * to the tunnel contest.
282    */
283   const struct SetVT *vt;
284
285   /**
286    * Implementation-specific operation state.
287    */
288   struct OperationState *op;
289 };
290
291
292 /**
293  * Get the table with implementing functions for
294  * set union.
295  */
296 const struct SetVT *
297 _GSS_union_vt (void);
298
299
300 #endif