77607a6e2ebf8eb00fbf16e4a8ad5e0d60d41976
[oweals/gnunet.git] / src / include / gnunet_consensus_service.h
1 /*
2       This file is part of GNUnet
3       Copyright (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 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., 51 Franklin Street, Fifth Floor,
18       Boston, MA 02110-1301, 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 #include "gnunet_set_service.h"
43
44
45 /**
46  * Elements inserted into the consensus set by the client
47  * may not be larger than this constant, since types in
48  * the upper range are used by CONSENSUS internally.
49  */
50 #define GNUNET_CONSENSUS_ELEMENT_TYPE_USER_MAX 0xFFF0
51
52
53 /**
54  * Called when a new element was received from another peer, or an error occured.
55  * May deliver duplicate values.
56  * Elements given to a consensus operation by the local peer are NOT given
57  * to this callback.
58  *
59  * @param cls closure
60  * @param element new element, NULL on error
61  */
62 typedef void (*GNUNET_CONSENSUS_ElementCallback) (void *cls,
63                                                   const struct GNUNET_SET_Element *element);
64
65
66
67 /**
68  * Opaque handle for the consensus service.
69  */
70 struct GNUNET_CONSENSUS_Handle;
71
72
73 /**
74  * Create a consensus session.  The set being reconciled is initially
75  * empty.
76  *
77  * @param cfg
78  * @param num_peers
79  * @param peers array of peers participating in this consensus session
80  *              Inclusion of the local peer is optional.
81  * @param session_id session identifier
82  *                   Allows a group of peers to have more than consensus session.
83  * @param start start time of the consensus, conclude should be called before
84  *              the start time.
85  * @param deadline time when the consensus should have concluded
86  * @param new_element_cb callback, called when a new element is added to the set by
87  *                    another peer. Also called when an error occurs.
88  * @param new_element_cls closure for new_element
89  * @return handle to use, NULL on error
90  */
91 struct GNUNET_CONSENSUS_Handle *
92 GNUNET_CONSENSUS_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
93                          unsigned int num_peers,
94                          const struct GNUNET_PeerIdentity *peers,
95                          const struct GNUNET_HashCode *session_id,
96                          struct GNUNET_TIME_Absolute start,
97                          struct GNUNET_TIME_Absolute deadline,
98                          GNUNET_CONSENSUS_ElementCallback new_element_cb,
99                          void *new_element_cls);
100
101
102 /**
103  * Called when an insertion (transmission to consensus service, which
104  * does not imply fully consensus on this element with all other
105  * peers) was successful.  May not call GNUNET_CONSENSUS_destroy();
106  * schedule a task to call GNUNET_CONSENSUS_destroy() instead (if
107  * needed).
108  *
109  * @param cls
110  * @param success #GNUNET_OK on success, #GNUNET_SYSERR if
111  *        the insertion and thus the consensus failed for good
112  */
113 typedef void (*GNUNET_CONSENSUS_InsertDoneCallback) (void *cls,
114                                                      int success);
115
116
117 /**
118  * Insert an element in the set being reconsiled.  Only transmit changes to
119  * other peers if GNUNET_CONSENSUS_begin() has been called.
120  * Must not be called after GNUNET_CONSENSUS_conclude().
121  * May not call GNUNET_CONSENSUS_destroy(); schedule a task to call
122  * GNUNET_CONSENSUS_destroy() instead (if needed).
123  *
124  * @param consensus handle for the consensus session
125  * @param element the element to be inserted
126  * @param idc function called when we are done with this element and it
127  *            is thus allowed to call GNUNET_CONSENSUS_insert() again
128  * @param idc_cls closure for @a idc
129  */
130 void
131 GNUNET_CONSENSUS_insert (struct GNUNET_CONSENSUS_Handle *consensus,
132                          const struct GNUNET_SET_Element *element,
133                          GNUNET_CONSENSUS_InsertDoneCallback idc,
134                          void *idc_cls);
135
136
137
138 /**
139  * Called when a conclusion was successful.
140  *
141  * @param cls
142  */
143 typedef void (*GNUNET_CONSENSUS_ConcludeCallback) (void *cls);
144
145
146 /**
147  * We are finished inserting new elements into the consensus;
148  * try to conclude the consensus within a given time window.
149  *
150  * @param consensus consensus session
151  * @param conclude called when the conclusion was successful
152  * @param conclude_cls closure for the conclude callback
153  */
154 void
155 GNUNET_CONSENSUS_conclude (struct GNUNET_CONSENSUS_Handle *consensus,
156                            GNUNET_CONSENSUS_ConcludeCallback conclude,
157                            void *conclude_cls);
158
159
160 /**
161  * Destroy a consensus handle (free all state associated with
162  * it, no longer call any of the callbacks).
163  *
164  * @param consensus handle to destroy
165  */
166 void
167 GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus);
168
169
170 #if 0                           /* keep Emacsens' auto-indent happy */
171 {
172 #endif
173 #ifdef __cplusplus
174 }
175 #endif
176
177 #endif