psycstore: sqlite plugin
[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  * Function called with the result of an asynchronous operation.
82  * 
83  * @param result #GNUNET_SYSERR on error,
84  *        #GNUNET_YES on success or if the peer was a member,
85  *        #GNUNET_NO if the peer was not a member
86  */
87 typedef void
88 (*GNUNET_PSYCSTORE_ResultCallback) (void *cls,
89                                     int result,
90                                     const char *err_msg);
91
92
93 /** 
94  * Store join/leave events for a PSYC channel in order to be able to answer
95  * membership test queries later.
96  *
97  * @param h Handle for the PSYCstore.
98  * @param channel_key The channel where the event happened.
99  * @param slave_key Public key of joining/leaving slave.
100  * @param did_join #GNUNET_YES on join, #GNUNET_NO on part.
101  * @param announced_at ID of the message that announced the membership change.
102  * @param effective_since Message ID this membership change is in effect since.
103  *        For joins it is <= announced_at, for parts it is always 0.
104  * @param group_generation In case of a part, the last group generation the
105  *        slave has access to.  It has relevance when a larger message have
106  *        fragments with different group generations.
107  * @param rcb Callback to call with the result of the storage operation.
108  * @param rcb_cls Closure for the callback.
109  *
110  * @return Operation handle that can be used to cancel the operation.
111  */
112 struct GNUNET_PSYCSTORE_OperationHandle *
113 GNUNET_PSYCSTORE_membership_store (struct GNUNET_PSYCSTORE_Handle *h,
114                                    const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
115                                    const struct GNUNET_CRYPTO_EccPublicKey *slave_key,
116                                    int did_join,
117                                    uint64_t announced_at,
118                                    uint64_t effective_since,
119                                    uint64_t group_generation,
120                                    GNUNET_PSYCSTORE_ResultCallback rcb,
121                                    void *rcb_cls);
122
123
124 /** 
125  * Test if a member was admitted to the channel at the given message ID.
126  *
127  * This is useful when relaying and replaying messages to check if a particular
128  * slave has access to the message fragment with a given group generation.  It
129  * is also used when handling join requests to determine whether the slave is
130  * currently admitted to the channel.
131  *
132  * @param h Handle for the PSYCstore.
133  * @param channel_key The channel we are interested in.
134  * @param slave_key Public key of slave whose membership to check.
135  * @param message_id Message ID for which to do the membership test.
136  * @param group_generation Group generation of the fragment of the message to
137  *        test.  It has relevance if the message consists of multiple fragments
138  *        with different group generations.
139  * @param rcb Callback to call with the test result.
140  * @param rcb_cls Closure for the callback.
141  *
142  * @return Operation handle that can be used to cancel the operation.
143  */
144 struct GNUNET_PSYCSTORE_OperationHandle *
145 GNUNET_PSYCSTORE_membership_test (struct GNUNET_PSYCSTORE_Handle *h,
146                                   const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
147                                   const struct GNUNET_CRYPTO_EccPublicKey *slave_key,
148                                   uint64_t message_id,
149                                   uint64_t group_generation,
150                                   GNUNET_PSYCSTORE_ResultCallback rcb,
151                                   void *rcb_cls);
152
153
154 /** 
155  * Store a message fragment sent to a channel.
156  *
157  * @param h Handle for the PSYCstore.
158  * @param channel_key The channel the message belongs to.
159  * @param message Message to store.
160  * @param rcb Callback to call with the result of the operation.
161  * @param rcb_cls Closure for the callback.
162  * 
163  * @return Handle that can be used to cancel the operation.
164  */
165 struct GNUNET_PSYCSTORE_OperationHandle *
166 GNUNET_PSYCSTORE_fragment_store (struct GNUNET_PSYCSTORE_Handle *h,
167                                  const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
168                                  const struct GNUNET_MULTICAST_MessageHeader *message,
169                                  GNUNET_PSYCSTORE_ResultCallback rcb,
170                                  void *rcb_cls);
171
172
173 /** 
174  * Function called with one message fragment, as the result of a
175  * GNUNET_PSYCSTORE_fragment_get() or GNUNET_PSYCSTORE_message_get() call.
176  *
177  * @param cls Closure.
178  * @param message The retrieved message fragment.  A NULL value indicates that
179  *        there are no more results to be returned.
180  * @param flags Message flags indicating fragmentation status.
181  */
182 typedef void
183 (*GNUNET_PSYCSTORE_FragmentCallback) (void *cls,
184                                       const struct GNUNET_MULTICAST_MessageHeader *message,
185                                       enum GNUNET_PSYC_MessageFlags flags);
186
187
188 /** 
189  * Retrieve a message fragment by fragment ID.
190  *
191  * @param h Handle for the PSYCstore.
192  * @param channel_key The channel we are interested in.
193  * @param fragment_id Fragment ID to check.  Use 0 to get the latest message fragment.
194  * @param cb Callback to call with the retrieved fragment.
195  * @param cb_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_get (struct GNUNET_PSYCSTORE_Handle *h,
201                                const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
202                                uint64_t fragment_id,
203                                GNUNET_PSYCSTORE_FragmentCallback cb,
204                                void *cb_cls);
205
206
207 /** 
208  * Retrieve all fragments of a message.
209  *
210  * @param h Handle for the PSYCstore.
211  * @param channel_key The channel we are interested in.
212  * @param message_id Message ID to check.  Use 0 to get the latest message.
213  * @param cb Callback to call with the retrieved fragments.
214  * @param cb_cls Closure for the callback.
215  * 
216  * @return Handle that can be used to cancel the operation.
217  */
218 struct GNUNET_PSYCSTORE_OperationHandle *
219 GNUNET_PSYCSTORE_message_get (struct GNUNET_PSYCSTORE_Handle *h,
220                               const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
221                               uint64_t message_id,
222                               GNUNET_PSYCSTORE_FragmentCallback cb,
223                               void *cb_cls);
224
225
226 /** 
227  * Retrieve a fragment of message specified by its message ID and fragment offset.
228  *
229  * @param h Handle for the PSYCstore.
230  * @param channel_key The channel we are interested in.
231  * @param message_id Message ID to check.  Use 0 to get the latest message.
232  * @param fragment_offset Offset of the fragment to retrieve.
233  * @param cb Callback to call with the retrieved fragments.
234  * @param cb_cls Closure for the callback.
235  * 
236  * @return Handle that can be used to cancel the operation.
237  */
238 struct GNUNET_PSYCSTORE_OperationHandle *
239 GNUNET_PSYCSTORE_message_get_fragment (struct GNUNET_PSYCSTORE_Handle *h,
240                                        const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
241                                        uint64_t message_id,
242                                        uint64_t fragment_offset,
243                                        GNUNET_PSYCSTORE_FragmentCallback cb,
244                                        void *cb_cls);
245
246
247 /** 
248  * Callback used to return the latest value of counters for the channel master.
249  *
250  * @see GNUNET_PSYCSTORE_counters_get_master()
251  *
252  * @param cls Closure.
253  * @param fragment_id Latest message fragment ID, used by multicast.
254  * @param message_id Latest message ID, used by PSYC.
255  * @param group_generation Latest group generation, used by PSYC.
256  */
257 typedef void
258 (*GNUNET_PSYCSTORE_MasterCountersCallback) (void *cls,
259                                             uint64_t fragment_id,
260                                             uint64_t message_id,
261                                             uint64_t group_generation);
262
263
264 /** 
265  * Callback used to return the latest value of counters for a channel slave.
266  *
267  * @see GNUNET_PSYCSTORE_counters_get_slave()
268  *
269  * @param cls Closure.
270  * @param max_state_msg_id Latest message ID containing state modifiers that was applied to the state store.  Used for the state sync process.
271  */
272 typedef void
273 (*GNUNET_PSYCSTORE_SlaveCountersCallback) (void *cls,
274                                            uint64_t max_state_msg_id);
275
276
277 /** 
278  * Retrieve latest values of counters for a channel master.
279  *
280  * The current value of counters are needed when a channel master is restarted,
281  * so that it can continue incrementing the counters from their last value.
282  *
283  * @param h Handle for the PSYCstore.
284  * @param channel_key Public key that identifies the channel.
285  * @param cb Callback to call with the result.
286  * @param cb_cls Closure for the callback.
287  * 
288  * @return 
289  */
290 struct GNUNET_PSYCSTORE_OperationHandle *
291 GNUNET_PSYCSTORE_counters_get_master (struct GNUNET_PSYCSTORE_Handle *h,
292                                       struct GNUNET_CRYPTO_EccPublicKey *channel_key,
293                                       GNUNET_PSYCSTORE_MasterCountersCallback *cb,
294                                       void *cb_cls);
295
296
297 /** 
298  * Retrieve latest values of counters for a channel slave.
299  *
300  * The current value of counters are needed when a channel slave rejoins
301  * and starts the state synchronization process.
302  *
303  * @param h Handle for the PSYCstore.
304  * @param channel_key Public key that identifies the channel.
305  * @param cb Callback to call with the result.
306  * @param cb_cls Closure for the callback.
307  * 
308  * @return 
309  */
310 struct GNUNET_PSYCSTORE_OperationHandle *
311 GNUNET_PSYCSTORE_counters_get_slave (struct GNUNET_PSYCSTORE_Handle *h,
312                                      struct GNUNET_CRYPTO_EccPublicKey *channel_key,
313                                      GNUNET_PSYCSTORE_SlaveCountersCallback *cb,
314                                      void *cb_cls);
315
316
317 /** 
318  * Apply modifiers of a message to the current channel state.
319  *
320  * An error is returned if there are missing messages containing state
321  * operations before the current one.
322  *
323  * @param h Handle for the PSYCstore.
324  * @param channel_key The channel we are interested in.
325  * @param message_id ID of the message that contains the @a modifiers.
326  * @param state_delta Value of the _state_delta PSYC header variable of the message.
327  * @param modifier_count Number of elements in the @a modifiers array.
328  * @param modifiers List of modifiers to apply.
329  * @param rcb Callback to call with the result of the operation.
330  * @param rcb_cls Closure for the callback.
331  * 
332  * @return Handle that can be used to cancel the operation.
333  */
334 struct GNUNET_PSYCSTORE_OperationHandle *
335 GNUNET_PSYCSTORE_state_modify (struct GNUNET_PSYCSTORE_Handle *h,
336                                const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
337                                uint64_t message_id,
338                                uint64_t state_delta,
339                                size_t modifier_count,
340                                const struct GNUNET_ENV_Modifier *modifiers,
341                                GNUNET_PSYCSTORE_ResultCallback rcb,
342                                void *rcb_cls);
343
344
345 /** 
346  * Update signed values of state variables in the state store.
347  *
348  * @param h Handle for the PSYCstore.
349  * @param channel_key The channel we are interested in.
350  * @param message_id Message ID that contained the state @a hash.
351  * @param hash Hash of the serialized full state.
352  * @param rcb Callback to call with the result of the operation.
353  * @param rcb_cls Closure for the callback.
354  *
355  */
356 struct GNUNET_PSYCSTORE_OperationHandle *
357 GNUNET_PSYCSTORE_state_hash_update (struct GNUNET_PSYCSTORE_Handle *h,
358                                     const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
359                                     uint64_t message_id,
360                                     const struct GNUNET_HashCode *hash,
361                                     GNUNET_PSYCSTORE_ResultCallback rcb,
362                                     void *rcb_cls);
363
364
365 /** 
366  * Function called with the value of a state variable.
367  *
368  * @param cls Closure.
369  * @param name Name of the state variable.  A NULL value indicates that there are no more
370  *        state variables to be returned.
371  * @param value_size Number of bytes in @a value.
372  * @param value Value of the state variable.
373 t * 
374  */
375 typedef void
376 (*GNUNET_PSYCSTORE_StateCallback) (void *cls,
377                                    const char *name,
378                                    size_t value_size,
379                                    const void *value);
380
381
382 /** 
383  * Retrieve the best matching state variable.
384  *
385  * @param h Handle for the PSYCstore.
386  * @param channel_key The channel we are interested in.
387  * @param name Name of variable to match, the returned variable might be less specific.
388  * @param cb Callback to return matching state variables.
389  * @param cb_cls Closure for the callback.
390  * 
391  * @return Handle that can be used to cancel the operation.
392  */
393 struct GNUNET_PSYCSTORE_OperationHandle *
394 GNUNET_PSYCSTORE_state_get (struct GNUNET_PSYCSTORE_Handle *h,
395                             const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
396                             const char *name,
397                             GNUNET_PSYCSTORE_StateCallback cb,
398                             void *cb_cls);
399
400
401 /** 
402  * Retrieve all state variables for a channel with the given prefix.
403  *
404  * @param h Handle for the PSYCstore.
405  * @param channel_key The channel we are interested in.
406  * @param name_prefix Prefix of state variable names to match.
407  * @param cb Callback to return matching state variables.
408  * @param cb_cls Closure for the callback.
409  * 
410  * @return Handle that can be used to cancel the operation.
411  */
412 struct GNUNET_PSYCSTORE_OperationHandle *
413 GNUNET_PSYCSTORE_state_get_all (struct GNUNET_PSYCSTORE_Handle *h,
414                                 const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
415                                 const char *name_prefix,
416                                 GNUNET_PSYCSTORE_StateCallback cb,
417                                 void *cb_cls);
418
419
420 /** 
421  * Cancel an operation.
422  *
423  * @param oh Handle for the operation to cancel.
424  */
425 void
426 GNUNET_PSYCSTORE_operation_cancel (struct GNUNET_PSYCSTORE_OperationHandle *oh);
427
428
429
430
431 #if 0                           /* keep Emacsens' auto-indent happy */
432 {
433 #endif
434 #ifdef __cplusplus
435 }
436 #endif
437
438 /* ifndef GNUNET_PSYCSTORE_SERVICE_H */
439 #endif
440 /* end of gnunet_psycstore_service.h */