- rename
[oweals/gnunet.git] / src / include / gnunet_consensus_service.h
1 /*
2       This file is part of GNUnet
3       (C) 2012 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 include/gnunet_consensus_service.h
23  * @brief 
24  * @author Florian Dold
25  */
26
27 #ifndef GNUNET_CONSENSUS_SERVICE_H
28 #define GNUNET_CONSENSUS_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 "platform.h"
39 #include "gnunet_common.h"
40 #include "gnunet_time_lib.h"
41 #include "gnunet_configuration_lib.h"
42
43
44 /**
45  * An element of the consensus set.
46  */
47 struct GNUNET_CONSENSUS_Element
48 {
49   /**
50    * The actual data of the element.
51    */
52    const void *data;
53
54    /**
55     * Size of the element's data.
56     */
57    uint16_t size;
58
59    /**
60     * Application specific element type
61     */
62    uint16_t type;
63 };
64
65
66 /**
67  * Called when a new element was received from another peer, or an error occured.
68  *
69  * May deliver duplicate values.
70  *
71  * Elements given to a consensus operation by the local peer are NOT given
72  * to this callback.
73  *
74  * @param cls closure
75  * @param element new element, NULL on error
76  * @return GNUNET_OK if the valid is well-formed and should be added to the consensus,
77  *         GNUNET_SYSERR if the element should be ignored and not be propagated
78  */
79 typedef int (*GNUNET_CONSENSUS_ElementCallback) (void *cls,
80                                                  struct GNUNET_CONSENSUS_Element *element);
81
82
83
84 /**
85  * Opaque handle for the consensus service.
86  */
87 struct GNUNET_CONSENSUS_Handle;
88
89
90 /**
91  * Create a consensus session.
92  * The set being reconciled is initially empty.  Only reconcile with other peers
93  * after GNUNET_CONSENSUS_reconcile has been called.
94  *
95  * @param cfg
96  * @param num_peers
97  * @param peers array of peers participating in this consensus session
98  *              Inclusion of the local peer is optional.
99  * @param session_id session identifier
100  *                   Allows a group of peers to have more than consensus session.
101  * @param new_element_cb callback, called when a new element is added to the set by
102  *                    another peer
103  * @param new_element_cls closure for new_element
104  * @return handle to use, NULL on error
105  */
106 struct GNUNET_CONSENSUS_Handle *
107 GNUNET_CONSENSUS_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
108                          unsigned int num_peers,
109                          const struct GNUNET_PeerIdentity *peers,
110                          const struct GNUNET_HashCode *session_id,
111                          GNUNET_CONSENSUS_ElementCallback new_element_cb,
112                          void *new_element_cls);
113
114
115 /**
116  * Called when an insertion (transmission to consensus service,
117  * which does not imply fully consensus on this element with
118  * all other peers) was successful.
119  * May not call GNUNET_CONSENSUS_destroy; schedule a task to call
120  * GNUNET_CONSENSUS_destroy instead.
121  *
122  * @param cls
123  * @param success GNUNET_OK on success, GNUNET_SYSERR if 
124  *        the insertion and thus the consensus failed for good
125  */
126 typedef void (*GNUNET_CONSENSUS_InsertDoneCallback) (void *cls,
127                                                      int success);
128
129
130 /**
131  * Insert an element in the set being reconsiled.  Only transmit changes to
132  * other peers if "GNUNET_CONSENSUS_begin" has been called.
133  * Must not be called after "GNUNET_CONSENSUS_conclude".
134  * May not call GNUNET_CONSENSUS_destroy; schedule a task to call
135  * GNUNET_CONSENSUS_destroy instead.
136  *
137  * @param consensus handle for the consensus session
138  * @param element the element to be inserted
139  * @param idc function called when we are done with this element and it 
140  *            is thus allowed to call GNUNET_CONSENSUS_insert again
141  * @param idc_cls closure for 'idc'
142  */
143 void
144 GNUNET_CONSENSUS_insert (struct GNUNET_CONSENSUS_Handle *consensus,
145                          const struct GNUNET_CONSENSUS_Element *element,
146                          GNUNET_CONSENSUS_InsertDoneCallback idc,
147                          void *idc_cls);
148
149
150 /**
151  * Begin reconciling elements with other peers.
152  * May not be called if an insert operation has not yet finished.
153  *
154  * @param consensus handle for the consensus session
155  */
156 void
157 GNUNET_CONSENSUS_begin (struct GNUNET_CONSENSUS_Handle *consensus);
158
159
160
161 struct GNUNET_CONSENSUS_DeltaRequest;
162
163 /**
164  * We are finished inserting new elements into the consensus;
165  * try to conclude the consensus within a given time window.
166  *
167  * @param consensus consensus session
168  * @param timeout timeout after which the conculde callback
169  *                must be called
170  * @param conclude called when the conclusion was successful
171  * @param conclude_cls closure for the conclude callback
172  */
173 struct GNUNET_CONSENSUS_DeltaRequest *
174 GNUNET_CONSENSUS_get_delta (struct GNUNET_CONSENSUS_Handle *consensus,
175                             uint32_t group_id,
176                             GNUNET_CONSENSUS_ElementCallback remove_element_cb,
177                             void *remove_element_cb_cls);
178
179
180 void
181 GNUNET_CONSENSUS_get_delta_cancel (struct GNUNET_CONSENSUS_DeltaRequest *dr);
182
183
184 struct GNUNET_CONSENSUS_Group
185 {
186   unsigned int num_members;
187   uint64_t total_elements_in_group;
188   const struct GNUNET_PeerIdentity **members;
189 };
190                                        
191
192 /**
193  * Called when a conclusion was successful.
194  *
195  * @param cls
196  * @param group
197  * @return GNUNET_YES if more consensus groups should be offered, GNUNET_NO if not
198  */
199 typedef int (*GNUNET_CONSENSUS_ConcludeCallback) (void *cls, const struct GNUNET_CONSENSUS_Group *group);
200
201
202 /**
203  * We are finished inserting new elements into the consensus;
204  * try to conclude the consensus within a given time window.
205  *
206  * @param consensus consensus session
207  * @param timeout timeout after which the conculde callback
208  *                must be called
209  * @param conclude called when the conclusion was successful
210  * @param conclude_cls closure for the conclude callback
211  */
212 void
213 GNUNET_CONSENSUS_conclude (struct GNUNET_CONSENSUS_Handle *consensus,
214                            struct GNUNET_TIME_Relative timeout,
215                            unsigned int min_group_size_in_consensus,
216                            GNUNET_CONSENSUS_ConcludeCallback conclude,
217                            void *conclude_cls);
218
219
220 /**
221  * Destroy a consensus handle (free all state associated with
222  * it, no longer call any of the callbacks).
223  *
224  * @param consensus handle to destroy
225  */
226 void
227 GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus);
228
229
230 #if 0                           /* keep Emacsens' auto-indent happy */
231 {
232 #endif
233 #ifdef __cplusplus
234 }
235 #endif
236
237 #endif