a2bf69021e36ac4af5ee0fa180bc77f47014b212
[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
42
43 /**
44  * Called when a new element was received from another peer; elements
45  * given to a consensus operation by the local peer are NOT given
46  * to this callback.
47  *
48  * @param cls closure
49  * @param element_size will match the size given to GNUNET_CONSENSUS_create
50  * @param element
51  */
52 typedef void (*GNUNET_CONSENSUS_NewElementCallback) (void *cls,
53                                                      size_t element_size,
54                                                      const void *element);
55
56
57
58 /**
59  * Opaque handle for the consensus service.
60  */
61 struct GNUNET_CONSENSUS_Handle;
62
63
64 /**
65  * Create a consensus session.
66  *
67  * @param cfg
68  * @param num_peers
69  * @param peers array of peers participating in this consensus session
70  * @param session_id session identifier
71  *                   Allows a group of peers to have more than consensus session.
72  * @param element_size size of the elements in the reconciled set in bytes
73  * @param num_initial_elements number of entries in the 'initial_elements' array
74  * @param initial_elements our elements for the consensus (each of 'element_size'
75  * @param new_element callback, called when a new element is added to the set by
76  *                    another peer
77  * @param new_element_cls closure for new_element
78  * @return handle to use, NULL on error
79  */
80 struct GNUNET_CONSENSUS_Handle *
81 GNUNET_CONSENSUS_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
82                          unsigned int num_peers,
83                          const struct GNUNET_PeerIdentity *peers,
84                          const struct GNUNET_HashCode *session_id,
85                          size_t element_size,
86                          unsigned int num_initial_elements,
87                          const void **initial_elements,
88                          GNUNET_CONSENSUS_NewElementCallback new_element,
89                          void *new_element_cls);
90
91
92 /**
93  * Called when an insertion (transmission to consensus service,
94  * which does not imply fully consensus on this element with
95  * all other peers) was successful.
96  *
97  * @param cls
98  * @param success GNUNET_OK on success, GNUNET_SYSERR if 
99  *        the insertion and thus the consensus failed for good
100  */
101 typedef void (*GNUNET_CONSENSUS_InsertDoneCallback) (void *cls,
102                                                      int success);
103
104
105 /**
106  * Insert an element in the set being reconsiled.  Must not be called after
107  * "GNUNET_CONSENSUS_conclude".
108  *
109  * @param consensus handle for the consensus session
110  * @param element_size must match element size from GNUNET_CONSENSUS_create
111  * @param element the element to be inserted
112  * @param idc function called when we are done with this element and it 
113  *            is thus allowed to call GNUNET_CONSENSUS_insert again
114  * @param idc_cls closure for 'idc'
115  */
116 void
117 GNUNET_CONSENSUS_insert (struct GNUNET_CONSENSUS_Handle *consensus,
118                          size_t element_size,
119                          const void *element,
120                          GNUNET_CONSENSUS_InsertDoneCallback idc,
121                          void *idc_cls);
122
123
124 /**
125  * Called when a conclusion was successful.
126  *
127  * @param cls
128  * @param num_peers_in_consensus
129  * @param peers_in_consensus
130  */
131 typedef void (*GNUNET_CONSENSUS_ConcludeCallback) (void *cls, 
132                                                    unsigned int num_peers_in_consensus,
133                                                    const struct GNUNET_PeerIdentity *peers_in_consensus);
134
135
136 /**
137  * We are finished inserting new elements into the consensus;
138  * try to conclude the consensus within a given time window.
139  *
140  * @param consensus consensus session
141  * @param timeout timeout after which the conculde callback
142  *                must be called
143  * @param conclude called when the conclusion was successful
144  * @param conclude_cls closure for the conclude callback
145  */
146 void
147 GNUNET_CONSENSUS_conclude (struct GNUNET_CONSENSUS_Handle *consensus,
148                            struct GNUNET_TIME_Relative timeout,
149                            GNUNET_CONSENSUS_ConcludeCallback conclude,
150                            void *conclude_cls);
151
152
153 /**
154  * Destroy a consensus handle (free all state associated with
155  * it, no longer call any of the callbacks).
156  *
157  * @param consensus handle to destroy
158  */
159 void
160 GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus);
161
162
163 #if 0                           /* keep Emacsens' auto-indent happy */
164 {
165 #endif
166 #ifdef __cplusplus
167 }
168 #endif
169
170 #endif