aboutsummaryrefslogtreecommitdiff
path: root/src/consensus/consensus_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/consensus/consensus_api.c')
-rw-r--r--src/consensus/consensus_api.c346
1 files changed, 0 insertions, 346 deletions
diff --git a/src/consensus/consensus_api.c b/src/consensus/consensus_api.c
deleted file mode 100644
index 01d0ad3de..000000000
--- a/src/consensus/consensus_api.c
+++ /dev/null
@@ -1,346 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012, 2016 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 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file consensus/consensus_api.c
23 * @brief
24 * @author Florian Dold
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_protocols.h"
29#include "gnunet_consensus_service.h"
30#include "consensus.h"
31
32
33#define LOG(kind, ...) GNUNET_log_from (kind, "consensus-api", __VA_ARGS__)
34
35
36/**
37 * Handle for the service.
38 */
39struct GNUNET_CONSENSUS_Handle
40{
41 /**
42 * Configuration to use.
43 */
44 const struct GNUNET_CONFIGURATION_Handle *cfg;
45
46 /**
47 * Callback for new elements. Not called for elements added locally.
48 */
49 GNUNET_CONSENSUS_ElementCallback new_element_cb;
50
51 /**
52 * Closure for @e new_element_cb
53 */
54 void *new_element_cls;
55
56 /**
57 * The (local) session identifier for the consensus session.
58 */
59 struct GNUNET_HashCode session_id;
60
61 /**
62 * #GNUNET_YES iff the join message has been sent to the service.
63 */
64 int joined;
65
66 /**
67 * Called when the conclude operation finishes or fails.
68 */
69 GNUNET_CONSENSUS_ConcludeCallback conclude_cb;
70
71 /**
72 * Closure for the @e conclude_cb callback.
73 */
74 void *conclude_cls;
75
76 /**
77 * Deadline for the conclude operation.
78 */
79 struct GNUNET_TIME_Absolute conclude_deadline;
80
81 /**
82 * Message queue for the client.
83 */
84 struct GNUNET_MQ_Handle *mq;
85};
86
87
88/**
89 * FIXME: this should not bee necessary when the API
90 * issue has been fixed
91 */
92struct InsertDoneInfo
93{
94 GNUNET_CONSENSUS_InsertDoneCallback idc;
95 void *cls;
96};
97
98
99/**
100 * Called when the server has sent is a new element
101 *
102 * @param cls consensus handle
103 * @param msg element message
104 */
105static int
106check_new_element (void *cls,
107 const struct GNUNET_CONSENSUS_ElementMessage *msg)
108{
109 /* any size is fine, elements are variable-size */
110 return GNUNET_OK;
111}
112
113
114/**
115 * Called when the server has sent is a new element
116 *
117 * @param cls consensus handle
118 * @param msg element message
119 */
120static void
121handle_new_element (void *cls,
122 const struct GNUNET_CONSENSUS_ElementMessage *msg)
123{
124 struct GNUNET_CONSENSUS_Handle *consensus = cls;
125 struct GNUNET_SET_Element element;
126
127 LOG (GNUNET_ERROR_TYPE_DEBUG,
128 "received new element\n");
129 element.element_type = msg->element_type;
130 element.size = ntohs (msg->header.size) - sizeof(struct
131 GNUNET_CONSENSUS_ElementMessage);
132 element.data = &msg[1];
133 consensus->new_element_cb (consensus->new_element_cls,
134 &element);
135}
136
137
138/**
139 * Called when the server has announced
140 * that the conclusion is over.
141 *
142 * @param cls consensus handle
143 * @param msg conclude done message
144 */
145static void
146handle_conclude_done (void *cls,
147 const struct GNUNET_MessageHeader *msg)
148{
149 struct GNUNET_CONSENSUS_Handle *consensus = cls;
150 GNUNET_CONSENSUS_ConcludeCallback cc;
151
152 GNUNET_MQ_destroy (consensus->mq);
153 consensus->mq = NULL;
154 GNUNET_assert (NULL != (cc = consensus->conclude_cb));
155 consensus->conclude_cb = NULL;
156 cc (consensus->conclude_cls);
157}
158
159
160/**
161 * Generic error handler, called with the appropriate
162 * error code and the same closure specified at the creation of
163 * the message queue.
164 * Not every message queue implementation supports an error handler.
165 *
166 * @param cls closure, same closure as for the message handlers
167 * @param error error code
168 */
169static void
170mq_error_handler (void *cls,
171 enum GNUNET_MQ_Error error)
172{
173 LOG (GNUNET_ERROR_TYPE_WARNING,
174 "consensus service disconnected us\n");
175}
176
177
178/**
179 * Create a consensus session.
180 *
181 * @param cfg configuration to use for connecting to the consensus service
182 * @param num_peers number of peers in the peers array
183 * @param peers array of peers participating in this consensus session
184 * Inclusion of the local peer is optional.
185 * @param session_id session identifier
186 * Allows a group of peers to have more than consensus session.
187 * @param start start time of the consensus, conclude should be called before
188 * the start time.
189 * @param deadline time when the consensus should have concluded
190 * @param new_element_cb callback, called when a new element is added to the set by
191 * another peer
192 * @param new_element_cls closure for new_element
193 * @return handle to use, NULL on error
194 */
195struct GNUNET_CONSENSUS_Handle *
196GNUNET_CONSENSUS_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
197 unsigned int num_peers,
198 const struct GNUNET_PeerIdentity *peers,
199 const struct GNUNET_HashCode *session_id,
200 struct GNUNET_TIME_Absolute start,
201 struct GNUNET_TIME_Absolute deadline,
202 GNUNET_CONSENSUS_ElementCallback new_element_cb,
203 void *new_element_cls)
204{
205 struct GNUNET_CONSENSUS_Handle *consensus
206 = GNUNET_new (struct GNUNET_CONSENSUS_Handle);
207 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
208 GNUNET_MQ_hd_var_size (new_element,
209 GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT,
210 struct GNUNET_CONSENSUS_ElementMessage,
211 consensus),
212 GNUNET_MQ_hd_fixed_size (conclude_done,
213 GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE,
214 struct GNUNET_MessageHeader,
215 consensus),
216 GNUNET_MQ_handler_end ()
217 };
218 struct GNUNET_CONSENSUS_JoinMessage *join_msg;
219 struct GNUNET_MQ_Envelope *ev;
220
221 consensus->cfg = cfg;
222 consensus->new_element_cb = new_element_cb;
223 consensus->new_element_cls = new_element_cls;
224 consensus->session_id = *session_id;
225 consensus->mq = GNUNET_CLIENT_connect (cfg,
226 "consensus",
227 mq_handlers,
228 &mq_error_handler,
229 consensus);
230 if (NULL == consensus->mq)
231 {
232 GNUNET_free (consensus);
233 return NULL;
234 }
235 ev = GNUNET_MQ_msg_extra (join_msg,
236 (num_peers * sizeof(struct GNUNET_PeerIdentity)),
237 GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN);
238
239 join_msg->session_id = consensus->session_id;
240 join_msg->start = GNUNET_TIME_absolute_hton (start);
241 join_msg->deadline = GNUNET_TIME_absolute_hton (deadline);
242 join_msg->num_peers = htonl (num_peers);
243 GNUNET_memcpy (&join_msg[1],
244 peers,
245 num_peers * sizeof(struct GNUNET_PeerIdentity));
246
247 GNUNET_MQ_send (consensus->mq, ev);
248 return consensus;
249}
250
251
252static void
253idc_adapter (void *cls)
254{
255 struct InsertDoneInfo *i = cls;
256
257 i->idc (i->cls, GNUNET_OK);
258 GNUNET_free (i);
259}
260
261
262/**
263 * Insert an element in the set being reconsiled. Must not be called after
264 * "GNUNET_CONSENSUS_conclude".
265 *
266 * @param consensus handle for the consensus session
267 * @param element the element to be inserted
268 * @param idc function called when we are done with this element and it
269 * is thus allowed to call GNUNET_CONSENSUS_insert again
270 * @param idc_cls closure for 'idc'
271 */
272void
273GNUNET_CONSENSUS_insert (struct GNUNET_CONSENSUS_Handle *consensus,
274 const struct GNUNET_SET_Element *element,
275 GNUNET_CONSENSUS_InsertDoneCallback idc,
276 void *idc_cls)
277{
278 struct GNUNET_CONSENSUS_ElementMessage *element_msg;
279 struct GNUNET_MQ_Envelope *ev;
280 struct InsertDoneInfo *i;
281
282 LOG (GNUNET_ERROR_TYPE_DEBUG, "inserting, size=%u\n", element->size);
283
284 ev = GNUNET_MQ_msg_extra (element_msg, element->size,
285 GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT);
286
287 GNUNET_memcpy (&element_msg[1], element->data, element->size);
288
289 if (NULL != idc)
290 {
291 i = GNUNET_new (struct InsertDoneInfo);
292 i->idc = idc;
293 i->cls = idc_cls;
294 GNUNET_MQ_notify_sent (ev, idc_adapter, i);
295 }
296 GNUNET_MQ_send (consensus->mq, ev);
297}
298
299
300/**
301 * We are done with inserting new elements into the consensus;
302 * try to conclude the consensus within a given time window.
303 * After conclude has been called, no further elements may be
304 * inserted by the client.
305 *
306 * @param consensus consensus session
307 * @param conclude called when the conclusion was successful
308 * @param conclude_cls closure for the conclude callback
309 */
310void
311GNUNET_CONSENSUS_conclude (struct GNUNET_CONSENSUS_Handle *consensus,
312 GNUNET_CONSENSUS_ConcludeCallback conclude,
313 void *conclude_cls)
314{
315 struct GNUNET_MQ_Envelope *ev;
316
317 GNUNET_assert (NULL != conclude);
318 GNUNET_assert (NULL == consensus->conclude_cb);
319
320 consensus->conclude_cls = conclude_cls;
321 consensus->conclude_cb = conclude;
322
323 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE);
324 GNUNET_MQ_send (consensus->mq, ev);
325}
326
327
328/**
329 * Destroy a consensus handle (free all state associated with
330 * it, no longer call any of the callbacks).
331 *
332 * @param consensus handle to destroy
333 */
334void
335GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus)
336{
337 if (NULL != consensus->mq)
338 {
339 GNUNET_MQ_destroy (consensus->mq);
340 consensus->mq = NULL;
341 }
342 GNUNET_free (consensus);
343}
344
345
346/* end of consensus_api.c */