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