2 This file is part of GNUnet.
3 Copyright (C) 2012, 2016 GNUnet e.V.
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.
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.
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.
22 * @file consensus/consensus_api.c
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_client_lib.h"
30 #include "gnunet_consensus_service.h"
31 #include "consensus.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "consensus-api",__VA_ARGS__)
38 * Handle for the service.
40 struct GNUNET_CONSENSUS_Handle
43 * Configuration to use.
45 const struct GNUNET_CONFIGURATION_Handle *cfg;
48 * Client connected to the consensus service, may be NULL if not connected.
50 struct GNUNET_CLIENT_Connection *client;
53 * Callback for new elements. Not called for elements added locally.
55 GNUNET_CONSENSUS_ElementCallback new_element_cb;
58 * Closure for new_element_cb
60 void *new_element_cls;
63 * The (local) session identifier for the consensus session.
65 struct GNUNET_HashCode session_id;
68 * GNUNES_YES iff the join message has been sent to the service.
73 * Called when the conclude operation finishes or fails.
75 GNUNET_CONSENSUS_ConcludeCallback conclude_cb;
78 * Closure for the conclude callback.
83 * Deadline for the conclude operation.
85 struct GNUNET_TIME_Absolute conclude_deadline;
88 * Message queue for the client.
90 struct GNUNET_MQ_Handle *mq;
95 * FIXME: this should not bee necessary when the API
96 * issue has been fixed
100 GNUNET_CONSENSUS_InsertDoneCallback idc;
106 * Called when the server has sent is a new element
108 * @param cls consensus handle
109 * @param msg element message
112 check_new_element (void *cls,
113 const struct GNUNET_CONSENSUS_ElementMessage *msg)
115 /* any size is fine, elements are variable-size */
121 * Called when the server has sent is a new element
123 * @param cls consensus handle
124 * @param msg element message
127 handle_new_element (void *cls,
128 const struct GNUNET_CONSENSUS_ElementMessage *msg)
130 struct GNUNET_CONSENSUS_Handle *consensus = cls;
131 struct GNUNET_SET_Element element;
133 LOG (GNUNET_ERROR_TYPE_DEBUG,
134 "received new element\n");
135 element.element_type = msg->element_type;
136 element.size = ntohs (msg->header.size) - sizeof (struct GNUNET_CONSENSUS_ElementMessage);
137 element.data = &msg[1];
138 consensus->new_element_cb (consensus->new_element_cls,
144 * Called when the server has announced
145 * that the conclusion is over.
147 * @param cls consensus handle
148 * @param msg conclude done message
151 handle_conclude_done (void *cls,
152 const struct GNUNET_MessageHeader *msg)
154 struct GNUNET_CONSENSUS_Handle *consensus = cls;
155 GNUNET_CONSENSUS_ConcludeCallback cc;
157 GNUNET_MQ_destroy (consensus->mq);
158 consensus->mq = NULL;
159 GNUNET_CLIENT_disconnect (consensus->client);
160 consensus->client = NULL;
161 GNUNET_assert (NULL != (cc = consensus->conclude_cb));
162 consensus->conclude_cb = NULL;
163 cc (consensus->conclude_cls);
168 * Generic error handler, called with the appropriate
169 * error code and the same closure specified at the creation of
171 * Not every message queue implementation supports an error handler.
173 * @param cls closure, same closure as for the message handlers
174 * @param error error code
177 mq_error_handler (void *cls,
178 enum GNUNET_MQ_Error error)
180 LOG (GNUNET_ERROR_TYPE_WARNING,
181 "consensus service disconnected us\n");
186 * Create a consensus session.
188 * @param cfg configuration to use for connecting to the consensus service
189 * @param num_peers number of peers in the peers array
190 * @param peers array of peers participating in this consensus session
191 * Inclusion of the local peer is optional.
192 * @param session_id session identifier
193 * Allows a group of peers to have more than consensus session.
194 * @param start start time of the consensus, conclude should be called before
196 * @param deadline time when the consensus should have concluded
197 * @param new_element_cb callback, called when a new element is added to the set by
199 * @param new_element_cls closure for new_element
200 * @return handle to use, NULL on error
202 struct GNUNET_CONSENSUS_Handle *
203 GNUNET_CONSENSUS_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
204 unsigned int num_peers,
205 const struct GNUNET_PeerIdentity *peers,
206 const struct GNUNET_HashCode *session_id,
207 struct GNUNET_TIME_Absolute start,
208 struct GNUNET_TIME_Absolute deadline,
209 GNUNET_CONSENSUS_ElementCallback new_element_cb,
210 void *new_element_cls)
212 GNUNET_MQ_hd_var_size (new_element,
213 GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT,
214 struct GNUNET_CONSENSUS_ElementMessage);
215 GNUNET_MQ_hd_fixed_size (conclude_done,
216 GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE,
217 struct GNUNET_MessageHeader);
218 struct GNUNET_CONSENSUS_Handle *consensus
219 = GNUNET_new (struct GNUNET_CONSENSUS_Handle);
220 struct GNUNET_MQ_MessageHandler mq_handlers[] = {
221 make_new_element_handler (consensus),
222 make_conclude_done_handler (consensus),
223 GNUNET_MQ_handler_end ()
225 struct GNUNET_CONSENSUS_JoinMessage *join_msg;
226 struct GNUNET_MQ_Envelope *ev;
228 consensus->cfg = cfg;
229 consensus->new_element_cb = new_element_cb;
230 consensus->new_element_cls = new_element_cls;
231 consensus->session_id = *session_id;
232 consensus->client = GNUNET_CLIENT_connect ("consensus", cfg);
233 consensus->mq = GNUNET_MQ_queue_for_connection_client (consensus->client,
234 mq_handlers, mq_error_handler, consensus);
236 GNUNET_assert (consensus->client != NULL);
238 ev = GNUNET_MQ_msg_extra (join_msg,
239 (num_peers * sizeof (struct GNUNET_PeerIdentity)),
240 GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN);
242 join_msg->session_id = consensus->session_id;
243 join_msg->start = GNUNET_TIME_absolute_hton (start);
244 join_msg->deadline = GNUNET_TIME_absolute_hton (deadline);
245 join_msg->num_peers = htonl (num_peers);
248 num_peers * sizeof (struct GNUNET_PeerIdentity));
250 GNUNET_MQ_send (consensus->mq, ev);
256 idc_adapter (void *cls)
258 struct InsertDoneInfo *i = cls;
259 i->idc (i->cls, GNUNET_OK);
264 * Insert an element in the set being reconsiled. Must not be called after
265 * "GNUNET_CONSENSUS_conclude".
267 * @param consensus handle for the consensus session
268 * @param element the element to be inserted
269 * @param idc function called when we are done with this element and it
270 * is thus allowed to call GNUNET_CONSENSUS_insert again
271 * @param idc_cls closure for 'idc'
274 GNUNET_CONSENSUS_insert (struct GNUNET_CONSENSUS_Handle *consensus,
275 const struct GNUNET_SET_Element *element,
276 GNUNET_CONSENSUS_InsertDoneCallback idc,
279 struct GNUNET_CONSENSUS_ElementMessage *element_msg;
280 struct GNUNET_MQ_Envelope *ev;
281 struct InsertDoneInfo *i;
283 LOG (GNUNET_ERROR_TYPE_DEBUG, "inserting, size=%llu\n", element->size);
285 ev = GNUNET_MQ_msg_extra (element_msg, element->size,
286 GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT);
288 memcpy (&element_msg[1], element->data, element->size);
292 i = GNUNET_new (struct InsertDoneInfo);
295 GNUNET_MQ_notify_sent (ev, idc_adapter, i);
297 GNUNET_MQ_send (consensus->mq, ev);
302 * We are done with inserting new elements into the consensus;
303 * try to conclude the consensus within a given time window.
304 * After conclude has been called, no further elements may be
305 * inserted by the client.
307 * @param consensus consensus session
308 * @param deadline deadline after which the conculde callback
310 * @param conclude called when the conclusion was successful
311 * @param conclude_cls closure for the conclude callback
314 GNUNET_CONSENSUS_conclude (struct GNUNET_CONSENSUS_Handle *consensus,
315 GNUNET_CONSENSUS_ConcludeCallback conclude,
318 struct GNUNET_MQ_Envelope *ev;
320 GNUNET_assert (NULL != conclude);
321 GNUNET_assert (NULL == consensus->conclude_cb);
323 consensus->conclude_cls = conclude_cls;
324 consensus->conclude_cb = conclude;
326 ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE);
327 GNUNET_MQ_send (consensus->mq, ev);
332 * Destroy a consensus handle (free all state associated with
333 * it, no longer call any of the callbacks).
335 * @param consensus handle to destroy
338 GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus)
340 if (NULL != consensus->mq)
342 GNUNET_MQ_destroy (consensus->mq);
343 consensus->mq = NULL;
345 if (NULL != consensus->client)
347 GNUNET_CLIENT_disconnect (consensus->client);
348 consensus->client = NULL;
350 GNUNET_free (consensus);