- Remove printf, use GNUNET_log INFO
[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 multi-peer set reconciliation
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  * May deliver duplicate values.
69  * Elements given to a consensus operation by the local peer are NOT given
70  * to this callback.
71  *
72  * @param cls closure
73  * @param element new element, NULL on error
74  * @return GNUNET_OK if the valid is well-formed and should be added to the consensus,
75  *         GNUNET_SYSERR if the element should be ignored and not be propagated
76  */
77 typedef int (*GNUNET_CONSENSUS_ElementCallback) (void *cls,
78                                                  struct GNUNET_CONSENSUS_Element *element);
79
80
81
82 /**
83  * Opaque handle for the consensus service.
84  */
85 struct GNUNET_CONSENSUS_Handle;
86
87
88 /**
89  * Create a consensus session.
90  * The set being reconciled is initially empty.  Only reconcile with other peers
91  * after GNUNET_CONSENSUS_reconcile has been called.
92  *
93  * @param cfg
94  * @param num_peers
95  * @param peers array of peers participating in this consensus session
96  *              Inclusion of the local peer is optional.
97  * @param session_id session identifier
98  *                   Allows a group of peers to have more than consensus session.
99  * @param new_element_cb callback, called when a new element is added to the set by
100  *                    another peer
101  * @param new_element_cls closure for new_element
102  * @return handle to use, NULL on error
103  */
104 struct GNUNET_CONSENSUS_Handle *
105 GNUNET_CONSENSUS_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
106                          unsigned int num_peers,
107                          const struct GNUNET_PeerIdentity *peers,
108                          const struct GNUNET_HashCode *session_id,
109                          GNUNET_CONSENSUS_ElementCallback new_element_cb,
110                          void *new_element_cls);
111
112
113 /**
114  * Called when an insertion (transmission to consensus service,
115  * which does not imply fully consensus on this element with
116  * all other peers) was successful.
117  * May not call GNUNET_CONSENSUS_destroy; schedule a task to call
118  * GNUNET_CONSENSUS_destroy instead.
119  *
120  * @param cls
121  * @param success GNUNET_OK on success, GNUNET_SYSERR if 
122  *        the insertion and thus the consensus failed for good
123  */
124 typedef void (*GNUNET_CONSENSUS_InsertDoneCallback) (void *cls,
125                                                      int success);
126
127
128 /**
129  * Insert an element in the set being reconsiled.  Only transmit changes to
130  * other peers if "GNUNET_CONSENSUS_begin" has been called.
131  * Must not be called after "GNUNET_CONSENSUS_conclude".
132  * May not call GNUNET_CONSENSUS_destroy; schedule a task to call
133  * GNUNET_CONSENSUS_destroy instead.
134  *
135  * @param consensus handle for the consensus session
136  * @param element the element to be inserted
137  * @param idc function called when we are done with this element and it 
138  *            is thus allowed to call GNUNET_CONSENSUS_insert again
139  * @param idc_cls closure for 'idc'
140  */
141 void
142 GNUNET_CONSENSUS_insert (struct GNUNET_CONSENSUS_Handle *consensus,
143                          const struct GNUNET_CONSENSUS_Element *element,
144                          GNUNET_CONSENSUS_InsertDoneCallback idc,
145                          void *idc_cls);
146
147
148 struct GNUNET_CONSENSUS_DeltaRequest;
149
150 /**
151  * FIXME
152  *
153  * @param consensus consensus session
154  * @param remove_element_cb callback that receives the removed elements
155  * @return a handle to cancel the request
156  */
157 struct GNUNET_CONSENSUS_DeltaRequest *
158 GNUNET_CONSENSUS_get_delta (struct GNUNET_CONSENSUS_Handle *consensus,
159                             GNUNET_CONSENSUS_ElementCallback remove_element_cb,
160                             void *remove_element_cb_cls);
161
162
163 void
164 GNUNET_CONSENSUS_get_delta_cancel (struct GNUNET_CONSENSUS_DeltaRequest *dr);
165
166
167 struct GNUNET_CONSENSUS_Group
168 {
169   unsigned int num_members;
170   uint64_t total_elements_in_group;
171   const struct GNUNET_PeerIdentity **members;
172 };
173                                        
174
175 /**
176  * Called when a conclusion was successful.
177  *
178  * @param cls
179  * @param group
180  */
181 typedef void (*GNUNET_CONSENSUS_ConcludeCallback) (void *cls, const struct GNUNET_CONSENSUS_Group *group);
182
183
184 /**
185  * We are finished inserting new elements into the consensus;
186  * try to conclude the consensus within a given time window.
187  *
188  * @param consensus consensus session
189  * @param timeout timeout after which the conculde callback
190  *                must be called
191  * @param conclude called when the conclusion was successful
192  * @param conclude_cls closure for the conclude callback
193  */
194 void
195 GNUNET_CONSENSUS_conclude (struct GNUNET_CONSENSUS_Handle *consensus,
196                            struct GNUNET_TIME_Relative timeout,
197                            unsigned int min_group_size_in_consensus,
198                            GNUNET_CONSENSUS_ConcludeCallback conclude,
199                            void *conclude_cls);
200
201
202 /**
203  * Destroy a consensus handle (free all state associated with
204  * it, no longer call any of the callbacks).
205  *
206  * @param consensus handle to destroy
207  */
208 void
209 GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus);
210
211
212 #if 0                           /* keep Emacsens' auto-indent happy */
213 {
214 #endif
215 #ifdef __cplusplus
216 }
217 #endif
218
219 #endif