Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / include / gnunet_consensus_service.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2012 GNUnet e.V.
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14      
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @author Florian Dold
21  *
22  * @file
23  * Multi-peer set reconciliation
24  *
25  * @defgroup consensus  Consensus service
26  * Multi-peer set reconciliation
27  * @{
28  */
29
30 #ifndef GNUNET_CONSENSUS_SERVICE_H
31 #define GNUNET_CONSENSUS_SERVICE_H
32
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #if 0                           /* keep Emacsens' auto-indent happy */
37 }
38 #endif
39 #endif
40
41 #include "gnunet_common.h"
42 #include "gnunet_time_lib.h"
43 #include "gnunet_configuration_lib.h"
44 #include "gnunet_set_service.h"
45
46
47 /**
48  * Elements inserted into the consensus set by the client
49  * may not be larger than this constant, since types in
50  * the upper range are used by CONSENSUS internally.
51  */
52 #define GNUNET_CONSENSUS_ELEMENT_TYPE_USER_MAX 0xFFF0
53
54
55 /**
56  * Called when a new element was received from another peer, or an error occurred.
57  * May deliver duplicate values.
58  * Elements given to a consensus operation by the local peer are NOT given
59  * to this callback.
60  *
61  * @param cls closure
62  * @param element new element, NULL on error
63  */
64 typedef void (*GNUNET_CONSENSUS_ElementCallback) (void *cls,
65                                                   const struct GNUNET_SET_Element *element);
66
67
68
69 /**
70  * Opaque handle for the consensus service.
71  */
72 struct GNUNET_CONSENSUS_Handle;
73
74
75 /**
76  * Create a consensus session.  The set being reconciled is initially
77  * empty.
78  *
79  * @param cfg
80  * @param num_peers
81  * @param peers array of peers participating in this consensus session
82  *              Inclusion of the local peer is optional.
83  * @param session_id session identifier
84  *                   Allows a group of peers to have more than consensus session.
85  * @param start start time of the consensus, conclude should be called before
86  *              the start time.
87  * @param deadline time when the consensus should have concluded
88  * @param new_element_cb callback, called when a new element is added to the set by
89  *                    another peer. Also called when an error occurs.
90  * @param new_element_cls closure for new_element
91  * @return handle to use, NULL on error
92  */
93 struct GNUNET_CONSENSUS_Handle *
94 GNUNET_CONSENSUS_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
95                          unsigned int num_peers,
96                          const struct GNUNET_PeerIdentity *peers,
97                          const struct GNUNET_HashCode *session_id,
98                          struct GNUNET_TIME_Absolute start,
99                          struct GNUNET_TIME_Absolute deadline,
100                          GNUNET_CONSENSUS_ElementCallback new_element_cb,
101                          void *new_element_cls);
102
103
104 /**
105  * Called when an insertion (transmission to consensus service, which
106  * does not imply fully consensus on this element with all other
107  * peers) was successful.  May not call GNUNET_CONSENSUS_destroy();
108  * schedule a task to call GNUNET_CONSENSUS_destroy() instead (if
109  * needed).
110  *
111  * @param cls
112  * @param success #GNUNET_OK on success, #GNUNET_SYSERR if
113  *        the insertion and thus the consensus failed for good
114  */
115 typedef void (*GNUNET_CONSENSUS_InsertDoneCallback) (void *cls,
116                                                      int success);
117
118
119 /**
120  * Insert an element in the set being reconsiled.  Only transmit changes to
121  * other peers if GNUNET_CONSENSUS_begin() has been called.
122  * Must not be called after GNUNET_CONSENSUS_conclude().
123  * May not call GNUNET_CONSENSUS_destroy(); schedule a task to call
124  * GNUNET_CONSENSUS_destroy() instead (if needed).
125  *
126  * @param consensus handle for the consensus session
127  * @param element the element to be inserted
128  * @param idc function called when we are done with this element and it
129  *            is thus allowed to call GNUNET_CONSENSUS_insert() again
130  * @param idc_cls closure for @a idc
131  */
132 void
133 GNUNET_CONSENSUS_insert (struct GNUNET_CONSENSUS_Handle *consensus,
134                          const struct GNUNET_SET_Element *element,
135                          GNUNET_CONSENSUS_InsertDoneCallback idc,
136                          void *idc_cls);
137
138
139
140 /**
141  * Called when a conclusion was successful.
142  *
143  * @param cls
144  */
145 typedef void (*GNUNET_CONSENSUS_ConcludeCallback) (void *cls);
146
147
148 /**
149  * We are finished inserting new elements into the consensus;
150  * try to conclude the consensus within a given time window.
151  *
152  * @param consensus consensus session
153  * @param conclude called when the conclusion was successful
154  * @param conclude_cls closure for the conclude callback
155  */
156 void
157 GNUNET_CONSENSUS_conclude (struct GNUNET_CONSENSUS_Handle *consensus,
158                            GNUNET_CONSENSUS_ConcludeCallback conclude,
159                            void *conclude_cls);
160
161
162 /**
163  * Destroy a consensus handle (free all state associated with
164  * it, no longer call any of the callbacks).
165  *
166  * @param consensus handle to destroy
167  */
168 void
169 GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus);
170
171
172 #if 0                           /* keep Emacsens' auto-indent happy */
173 {
174 #endif
175 #ifdef __cplusplus
176 }
177 #endif
178
179 #endif
180
181 /** @} */  /* end of group */