psycstore service skeleton
[oweals/gnunet.git] / src / include / gnunet_psycstore_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 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 include/gnunet_psycstore_service.h
23  * @brief PSYCstore service; implements persistent storage for the PSYC service
24  * @author Gabor X Toth
25  * @author Christian Grothoff
26  */
27 #ifndef GNUNET_PSYCSTORE_SERVICE_H
28 #define GNUNET_PSYCSTORE_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 "gnunet_util_lib.h"
39 #include "gnunet_env_lib.h"
40 #include "gnunet_multicast_service.h"
41 #include "gnunet_psyc_service.h"
42
43 /** 
44  * Version number of GNUnet PSYCstore API.
45  */
46 #define GNUNET_PSYCSTORE_VERSION 0x00000000
47
48 /** 
49  * Handle for a PSYCstore
50  */
51 struct GNUNET_PSYCSTORE_Handle;
52
53
54 /** 
55  * Connect to the PSYCstore service.
56  *
57  * @param cfg Configuration to use.
58  *
59  * @return Handle for the connecton.
60  */
61 struct GNUNET_PSYCSTORE_Handle *
62 GNUNET_PSYCSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
63
64
65 /** 
66  * Disconnect from the PSYCstore service.
67  *
68  * @param h Handle for the connection.
69  */
70 void
71 GNUNET_PSYCSTORE_disconnect (struct GNUNET_PSYCSTORE_Handle *h);
72
73
74 /** 
75  * Handle for an operation on the PSYCSTORE (useful to cancel the operation).
76  */
77 struct GNUNET_PSYCSTORE_OperationHandle;
78
79
80 /** 
81  * Callback used to return the latest value of counters of a channel.
82  *
83  * @see GNUNET_PSYCSTORE_counters_get()
84  *
85  * @param *cls Closure.
86  * @param fragment_id Latest message fragment ID, used by multicast.
87  * @param message_id Latest message ID, used by PSYC.
88  * @param group_generation Latest group generation, used by PSYC.
89  */
90 typedef void
91 (*GNUNET_PSYCSTORE_CountersCallback) (void *cls,
92                                       uint64_t fragment_id,
93                                       uint64_t message_id,
94                                       uint64_t group_generation);
95
96
97 /** 
98  * Retrieve latest values of counters for a channel.
99  *
100  * The current value of counters are needed when a channel master is restarted,
101  * so that it can continue incrementing the counters from their last value.
102  *
103  * @param h Handle for the PSYCstore.
104  * @param channel_key Public key that identifies the channel.
105  * @param cb Callback to call with the result.
106  * @param cb_cls Closure for the callback.
107  * 
108  * @return 
109  */
110 struct GNUNET_PSYCSTORE_OperationHandle *
111 GNUNET_PSYCSTORE_counters_get (struct GNUNET_PSYCSTORE_Handle *h,
112                                struct GNUNET_CRYPTO_EccPublicKey *channel_key,
113                                GNUNET_PSYCSTORE_CountersCallback *cb,
114                                void *cb_cls);
115
116
117 /** 
118  * Function called with the result of an asynchronous operation.
119  * 
120  * @param result #GNUNET_SYSERR on error,
121  *        #GNUNET_YES on success or if the peer was a member,
122  *        #GNUNET_NO if the peer was not a member
123  */
124 typedef void
125 (*GNUNET_PSYCSTORE_ResultCallback) (void *cls,
126                                     int result,
127                                     const char *err_msg);
128
129
130 /** 
131  * Store join/leave events for a PSYC channel in order to be able to answer
132  * membership test queries later.
133  *
134  * @param h Handle for the PSYCstore.
135  * @param channel_key The channel where the event happened.
136  * @param slave_key Public key of joining/leaving slave.
137  * @param did_join #GNUNET_YES on join, #GNUNET_NO on part.
138  * @param announced_at ID of the message that announced the membership change.
139  * @param effective_since Message ID this membership change is in effect since.
140  *        For joins it is <= announced_at, for parts it is always 0.
141  * @param group_generation In case of a part, the last group generation the
142  *        slave has access to.  It has relevance when a larger message have
143  *        fragments with different group generations.
144  * @param rcb Callback to call with the result of the storage operation.
145  * @param rcb_cls Closure for the callback.
146  *
147  * @return Operation handle that can be used to cancel the operation.
148  */
149 struct GNUNET_PSYCSTORE_OperationHandle *
150 GNUNET_PSYCSTORE_membership_store (struct GNUNET_PSYCSTORE_Handle *h,
151                                    const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
152                                    const struct GNUNET_CRYPTO_EccPublicKey *slave_key,
153                                    int did_join,
154                                    uint64_t announced_at,
155                                    uint64_t effective_since,
156                                    uint64_t group_generation,
157                                    GNUNET_PSYCSTORE_ResultCallback rcb,
158                                    void *rcb_cls);
159
160
161 /** 
162  * Test if a peer was a member of the channel during the given period specified by the group generation.
163  *
164  * This is useful when relaying and replaying messages to check if a particular slave has access to the message fragment with a given group generation.  It is also used when handling join requests to determine whether the slave is currently admitted to the channel.
165  *
166  * @param h Handle for the PSYCstore.
167  * @param channel_key The channel we are interested in.
168  * @param slave_key Public key of slave whose membership to check.
169  * @param message_id Message ID for which to do the membership test.
170  * @param group_generation Group generation of the fragment of the message to
171  *        test.  It has relevance if the message consists of multiple fragments
172  *        with different group generations.
173  * @param rcb Callback to call with the test result.
174  * @param rcb_cls Closure for the callback.
175  *
176  * @return Operation handle that can be used to cancel the operation.
177  */
178 struct GNUNET_PSYCSTORE_OperationHandle *
179 GNUNET_PSYCSTORE_membership_test (struct GNUNET_PSYCSTORE_Handle *h,
180                                   const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
181                                   const struct GNUNET_CRYPTO_EccPublicKey *slave_key,
182                                   uint64_t message_id,
183                                   uint64_t group_generation,
184                                   GNUNET_PSYCSTORE_ResultCallback rcb,
185                                   void *rcb_cls);
186
187
188 /** 
189  * Store a message fragment sent to a channel.
190  *
191  * @param h Handle for the PSYCstore.
192  * @param channel_key The channel the message belongs to.
193  * @param message Message to store.
194  * @param rcb Callback to call with the result of the operation.
195  * @param rcb_cls Closure for the callback.
196  * 
197  * @return Handle that can be used to cancel the operation.
198  */
199 struct GNUNET_PSYCSTORE_OperationHandle *
200 GNUNET_PSYCSTORE_fragment_store (struct GNUNET_PSYCSTORE_Handle *h,
201                                  const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
202                                  const struct GNUNET_MULTICAST_MessageHeader *message,
203                                  GNUNET_PSYCSTORE_ResultCallback rcb,
204                                  void *rcb_cls);
205
206
207 /** 
208  * Function called with one message fragment, as the result of a
209  * GNUNET_PSYCSTORE_fragment_get() or GNUNET_PSYCSTORE_message_get() call.
210  *
211  * @param cls Closure.
212  * @param message The retrieved message fragment.  A NULL value indicates that
213  *        there are no more results to be returned.
214  * @param flags Message flags indicating fragmentation status.
215  */
216 typedef void
217 (*GNUNET_PSYCSTORE_FragmentCallback) (void *cls,
218                                       const struct GNUNET_MULTICAST_MessageHeader *message,
219                                       enum GNUNET_PSYC_MessageFlags flags);
220
221
222 /** 
223  * Retrieve a message fragment by fragment ID.
224  *
225  * @param h Handle for the PSYCstore.
226  * @param channel_key The channel we are interested in.
227  * @param fragment_id Fragment ID to check.  Use 0 to get the latest message fragment.
228  * @param cb Callback to call with the retrieved fragment.
229  * @param cb_cls Closure for the callback.
230  * 
231  * @return Handle that can be used to cancel the operation.
232  */
233 struct GNUNET_PSYCSTORE_OperationHandle *
234 GNUNET_PSYCSTORE_fragment_get (struct GNUNET_PSYCSTORE_Handle *h,
235                                const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
236                                uint64_t fragment_id,
237                                GNUNET_PSYCSTORE_FragmentCallback cb,
238                                void *cb_cls);
239
240
241 /** 
242  * Retrieve a message by ID.
243  *
244  * @param h Handle for the PSYCstore.
245  * @param channel_key The channel we are interested in.
246  * @param message_id Message ID to check.  Use 0 to get the latest message.
247  * @param cb Callback to call with the retrieved fragments.
248  * @param cb_cls Closure for the callback.
249  * 
250  * @return Handle that can be used to cancel the operation.
251  */
252 struct GNUNET_PSYCSTORE_OperationHandle *
253 GNUNET_PSYCSTORE_message_get (struct GNUNET_PSYCSTORE_Handle *h,
254                               const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
255                               uint64_t message_id,
256                               GNUNET_PSYCSTORE_FragmentCallback cb,
257                               void *cb_cls);
258
259
260 /** 
261  * Apply modifiers of a message to the current channel state.
262  *
263  * An error is returned if there are missing messages containing state
264  * operations before the current one.
265  *
266  * @param h Handle for the PSYCstore.
267  * @param channel_key The channel we are interested in.
268  * @param message_id ID of the message that contains the @a modifiers.
269  * @param state_delta Value of the _state_delta PSYC header variable of the message.
270  * @param modifier_count Number of elements in the @a modifiers array.
271  * @param modifiers List of modifiers to apply.
272  * @param rcb Callback to call with the result of the operation.
273  * @param rcb_cls Closure for the callback.
274  * 
275  * @return Handle that can be used to cancel the operation.
276  */
277 struct GNUNET_PSYCSTORE_OperationHandle *
278 GNUNET_PSYCSTORE_state_modify (struct GNUNET_PSYCSTORE_Handle *h,
279                                const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
280                                uint64_t message_id,
281                                uint64_t state_delta,
282                                size_t modifier_count,
283                                const struct GNUNET_ENV_Modifier *modifiers,
284                                GNUNET_PSYCSTORE_ResultCallback rcb,
285                                void *rcb_cls);
286
287
288 /** 
289  * Update signed values of state variables in the state store.
290  *
291  * @param h Handle for the PSYCstore.
292  * @param channel_key The channel we are interested in.
293  * @param message_id Message ID that contained the state @a hash.
294  * @param hash Hash of the serialized full state.
295  * @param rcb Callback to call with the result of the operation.
296  * @param rcb_cls Closure for the callback.
297  *
298  */
299 struct GNUNET_PSYCSTORE_OperationHandle *
300 GNUNET_PSYCSTORE_state_hash_update (struct GNUNET_PSYCSTORE_Handle *h,
301                                     const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
302                                     uint64_t message_id,
303                                     const struct GNUNET_HashCode *hash,
304                                     GNUNET_PSYCSTORE_ResultCallback rcb,
305                                     void *rcb_cls);
306
307
308 /** 
309  * Function called with the value of a state variable.
310  *
311  * @param cls Closure.
312  * @param name Name of the state variable.  A NULL value indicates that there are no more
313  *        state variables to be returned.
314  * @param value_size Number of bytes in @a value.
315  * @param value Value of the state variable.
316 t * 
317  */
318 typedef void
319 (*GNUNET_PSYCSTORE_StateCallback) (void *cls,
320                                    const char *name,
321                                    size_t value_size,
322                                    const void *value);
323
324
325 /** 
326  * Retrieve the best matching state variable.
327  *
328  * @param h Handle for the PSYCstore.
329  * @param channel_key The channel we are interested in.
330  * @param name Name of variable to match, the returned variable might be less specific.
331  * @param cb Callback to return matching state variables.
332  * @param cb_cls Closure for the callback.
333  * 
334  * @return Handle that can be used to cancel the operation.
335  */
336 struct GNUNET_PSYCSTORE_OperationHandle *
337 GNUNET_PSYCSTORE_state_get (struct GNUNET_PSYCSTORE_Handle *h,
338                             const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
339                             const char *name,
340                             GNUNET_PSYCSTORE_StateCallback cb,
341                             void *cb_cls);
342
343
344 /** 
345  * Retrieve all state variables for a channel with the given prefix.
346  *
347  * @param h Handle for the PSYCstore.
348  * @param channel_key The channel we are interested in.
349  * @param name_prefix Prefix of state variable names to match.
350  * @param cb Callback to return matching state variables.
351  * @param cb_cls Closure for the callback.
352  * 
353  * @return Handle that can be used to cancel the operation.
354  */
355 struct GNUNET_PSYCSTORE_OperationHandle *
356 GNUNET_PSYCSTORE_state_get_all (struct GNUNET_PSYCSTORE_Handle *h,
357                                 const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
358                                 const char *name_prefix,
359                                 GNUNET_PSYCSTORE_StateCallback cb,
360                                 void *cb_cls);
361
362
363 /** 
364  * Cancel an operation.
365  *
366  * @param oh Handle for the operation to cancel.
367  */
368 void
369 GNUNET_PSYCSTORE_operation_cancel (struct GNUNET_PSYCSTORE_OperationHandle *oh);
370
371
372
373
374 #if 0                           /* keep Emacsens' auto-indent happy */
375 {
376 #endif
377 #ifdef __cplusplus
378 }
379 #endif
380
381 /* ifndef GNUNET_PSYCSTORE_SERVICE_H */
382 #endif
383 /* end of gnunet_psycstore_service.h */