2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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__)
37 * Actions that can be queued.
42 * Queued messages are stored in a doubly linked list.
44 struct QueuedMessage *next;
47 * Queued messages are stored in a doubly linked list.
49 struct QueuedMessage *prev;
52 * The actual queued message.
54 struct GNUNET_MessageHeader *msg;
57 * Will be called after transmit, if not NULL
59 GNUNET_CONSENSUS_InsertDoneCallback idc;
69 * Handle for the service.
71 struct GNUNET_CONSENSUS_Handle
74 * Configuration to use.
76 const struct GNUNET_CONFIGURATION_Handle *cfg;
79 * Client connected to the consensus service, may be NULL if not connected.
81 struct GNUNET_CLIENT_Connection *client;
84 * Callback for new elements. Not called for elements added locally.
86 GNUNET_CONSENSUS_ElementCallback new_element_cb;
89 * Closure for new_element_cb
91 void *new_element_cls;
94 * The (local) session identifier for the consensus session.
96 struct GNUNET_HashCode session_id;
99 * Number of peers in the consensus. Optionally includes the local peer.
104 * Peer identities of peers participating in the consensus, includes the local peer.
106 struct GNUNET_PeerIdentity **peers;
109 * Currently active transmit request.
111 struct GNUNET_CLIENT_TransmitHandle *th;
114 * GNUNES_YES iff the join message has been sent to the service.
119 * Closure for the insert done callback.
124 * Called when the conclude operation finishes or fails.
126 GNUNET_CONSENSUS_ConcludeCallback conclude_cb;
129 * Closure for the conclude callback.
134 * Deadline for the conclude operation.
136 struct GNUNET_TIME_Absolute conclude_deadline;
138 unsigned int conclude_min_size;
140 struct QueuedMessage *messages_head;
142 struct QueuedMessage *messages_tail;
149 * Schedule transmitting the next message.
151 * @param consensus consensus handle
154 send_next (struct GNUNET_CONSENSUS_Handle *consensus);
158 * Function called to notify a client about the connection
159 * begin ready to queue more data. "buf" will be
160 * NULL and "size" zero if the connection was closed for
161 * writing in the meantime.
164 * @param size number of bytes available in buf
165 * @param buf where the callee should write the message
166 * @return number of bytes written to buf
169 transmit_queued (void *cls, size_t size,
172 struct GNUNET_CONSENSUS_Handle *consensus;
173 struct QueuedMessage *qmsg;
176 consensus = (struct GNUNET_CONSENSUS_Handle *) cls;
177 consensus->th = NULL;
179 qmsg = consensus->messages_head;
180 GNUNET_CONTAINER_DLL_remove (consensus->messages_head, consensus->messages_tail, qmsg);
184 if (NULL != qmsg->idc)
186 qmsg->idc (qmsg->idc_cls, GNUNET_YES);
191 msg_size = ntohs (qmsg->msg->size);
193 GNUNET_assert (size >= msg_size);
195 memcpy (buf, qmsg->msg, msg_size);
196 if (NULL != qmsg->idc)
198 qmsg->idc (qmsg->idc_cls, GNUNET_YES);
200 GNUNET_free (qmsg->msg);
202 /* FIXME: free the messages */
204 send_next (consensus);
211 * Schedule transmitting the next message.
213 * @param consensus consensus handle
216 send_next (struct GNUNET_CONSENSUS_Handle *consensus)
218 if (NULL != consensus->th)
221 if (NULL != consensus->messages_head)
224 GNUNET_CLIENT_notify_transmit_ready (consensus->client, ntohs (consensus->messages_head->msg->size),
225 GNUNET_TIME_UNIT_FOREVER_REL,
226 GNUNET_NO, &transmit_queued, consensus);
232 * Called when the server has sent is a new element
234 * @param consensus consensus handle
235 * @param msg element message
238 handle_new_element (struct GNUNET_CONSENSUS_Handle *consensus,
239 struct GNUNET_CONSENSUS_ElementMessage *msg)
241 struct GNUNET_CONSENSUS_Element element;
243 LOG (GNUNET_ERROR_TYPE_DEBUG, "received new element\n");
245 element.type = msg->element_type;
246 element.size = ntohs (msg->header.size) - sizeof (struct GNUNET_CONSENSUS_ElementMessage);
247 element.data = &msg[1];
249 consensus->new_element_cb (consensus->new_element_cls, &element);
251 send_next (consensus);
256 * Called when the server has announced
257 * that the conclusion is over.
259 * @param consensus consensus handle
260 * @param msg conclude done message
263 handle_conclude_done (struct GNUNET_CONSENSUS_Handle *consensus,
264 const struct GNUNET_MessageHeader *msg)
266 GNUNET_CONSENSUS_ConcludeCallback cc;
268 GNUNET_assert (NULL != (cc = consensus->conclude_cb));
269 consensus->conclude_cb = NULL;
270 cc (consensus->conclude_cls);
275 * Type of a function to call when we receive a message
279 * @param msg message received, NULL on timeout or fatal error
282 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
284 struct GNUNET_CONSENSUS_Handle *consensus = cls;
286 LOG (GNUNET_ERROR_TYPE_DEBUG, "received message from consensus service\n");
290 /* Error, timeout, death */
291 LOG (GNUNET_ERROR_TYPE_ERROR, "error receiving\n");
292 GNUNET_CLIENT_disconnect (consensus->client);
293 consensus->client = NULL;
294 consensus->new_element_cb (consensus->new_element_cls, NULL);
297 GNUNET_CLIENT_receive (consensus->client, &message_handler, consensus,
298 GNUNET_TIME_UNIT_FOREVER_REL);
299 switch (ntohs (msg->type))
301 case GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT:
302 handle_new_element (consensus, (struct GNUNET_CONSENSUS_ElementMessage *) msg);
304 case GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE:
305 handle_conclude_done (consensus, msg);
313 * Function called to notify a client about the connection
314 * begin ready to queue more data. "buf" will be
315 * NULL and "size" zero if the connection was closed for
316 * writing in the meantime.
319 * @param size number of bytes available in buf
320 * @param buf where the callee should write the message
321 * @return number of bytes written to buf
324 transmit_join (void *cls, size_t size, void *buf)
326 struct GNUNET_CONSENSUS_JoinMessage *msg;
327 struct GNUNET_CONSENSUS_Handle *consensus;
330 GNUNET_assert (NULL != buf);
332 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmitting join message\n");
335 consensus->th = NULL;
336 consensus->joined = 1;
340 msize = sizeof (struct GNUNET_CONSENSUS_JoinMessage) +
341 consensus->num_peers * sizeof (struct GNUNET_PeerIdentity);
343 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN);
344 msg->header.size = htons (msize);
345 msg->session_id = consensus->session_id;
346 msg->num_peers = htonl (consensus->num_peers);
349 consensus->num_peers * sizeof (struct GNUNET_PeerIdentity));
350 send_next (consensus);
351 GNUNET_CLIENT_receive (consensus->client, &message_handler, consensus,
352 GNUNET_TIME_UNIT_FOREVER_REL);
358 * Create a consensus session.
360 * @param cfg configuration to use for connecting to the consensus service
361 * @param num_peers number of peers in the peers array
362 * @param peers array of peers participating in this consensus session
363 * Inclusion of the local peer is optional.
364 * @param session_id session identifier
365 * Allows a group of peers to have more than consensus session.
366 * @param new_element_cb callback, called when a new element is added to the set by
368 * @param new_element_cls closure for new_element
369 * @return handle to use, NULL on error
371 struct GNUNET_CONSENSUS_Handle *
372 GNUNET_CONSENSUS_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
373 unsigned int num_peers,
374 const struct GNUNET_PeerIdentity *peers,
375 const struct GNUNET_HashCode *session_id,
376 GNUNET_CONSENSUS_ElementCallback new_element_cb,
377 void *new_element_cls)
379 struct GNUNET_CONSENSUS_Handle *consensus;
380 size_t join_message_size;
382 consensus = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_Handle));
383 consensus->cfg = cfg;
384 consensus->new_element_cb = new_element_cb;
385 consensus->new_element_cls = new_element_cls;
386 consensus->num_peers = num_peers;
387 consensus->session_id = *session_id;
390 consensus->peers = NULL;
391 else if (num_peers > 0)
393 GNUNET_memdup (peers, num_peers * sizeof (struct GNUNET_PeerIdentity));
395 consensus->client = GNUNET_CLIENT_connect ("consensus", cfg);
397 GNUNET_assert (consensus->client != NULL);
399 join_message_size = (sizeof (struct GNUNET_CONSENSUS_JoinMessage)) +
400 (num_peers * sizeof (struct GNUNET_PeerIdentity));
403 GNUNET_CLIENT_notify_transmit_ready (consensus->client,
405 GNUNET_TIME_UNIT_FOREVER_REL,
406 GNUNET_NO, &transmit_join, consensus);
409 GNUNET_assert (consensus->th != NULL);
416 * Insert an element in the set being reconsiled. Must not be called after
417 * "GNUNET_CONSENSUS_conclude".
419 * @param consensus handle for the consensus session
420 * @param element the element to be inserted
421 * @param idc function called when we are done with this element and it
422 * is thus allowed to call GNUNET_CONSENSUS_insert again
423 * @param idc_cls closure for 'idc'
426 GNUNET_CONSENSUS_insert (struct GNUNET_CONSENSUS_Handle *consensus,
427 const struct GNUNET_CONSENSUS_Element *element,
428 GNUNET_CONSENSUS_InsertDoneCallback idc,
431 struct QueuedMessage *qmsg;
432 struct GNUNET_CONSENSUS_ElementMessage *element_msg;
433 size_t element_msg_size;
435 LOG (GNUNET_ERROR_TYPE_DEBUG, "inserting, size=%llu\n", element->size);
437 element_msg_size = (sizeof (struct GNUNET_CONSENSUS_ElementMessage) +
440 element_msg = GNUNET_malloc (element_msg_size);
441 element_msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT);
442 element_msg->header.size = htons (element_msg_size);
443 memcpy (&element_msg[1], element->data, element->size);
445 qmsg = GNUNET_malloc (sizeof (struct QueuedMessage));
446 qmsg->msg = (struct GNUNET_MessageHeader *) element_msg;
448 qmsg->idc_cls = idc_cls;
450 GNUNET_CONTAINER_DLL_insert_tail (consensus->messages_head, consensus->messages_tail, qmsg);
452 send_next (consensus);
457 * We are done with inserting new elements into the consensus;
458 * try to conclude the consensus within a given time window.
459 * After conclude has been called, no further elements may be
460 * inserted by the client.
462 * @param consensus consensus session
463 * @param timeout timeout after which the conculde callback
465 * @param conclude called when the conclusion was successful
466 * @param conclude_cls closure for the conclude callback
469 GNUNET_CONSENSUS_conclude (struct GNUNET_CONSENSUS_Handle *consensus,
470 struct GNUNET_TIME_Relative timeout,
471 GNUNET_CONSENSUS_ConcludeCallback conclude,
474 struct QueuedMessage *qmsg;
475 struct GNUNET_CONSENSUS_ConcludeMessage *conclude_msg;
477 GNUNET_assert (NULL != conclude);
478 GNUNET_assert (NULL == consensus->conclude_cb);
480 consensus->conclude_cls = conclude_cls;
481 consensus->conclude_cb = conclude;
483 conclude_msg = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_ConcludeMessage));
484 conclude_msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE);
485 conclude_msg->header.size = htons (sizeof (struct GNUNET_CONSENSUS_ConcludeMessage));
486 conclude_msg->timeout = GNUNET_TIME_relative_hton (timeout);
488 qmsg = GNUNET_malloc (sizeof (struct QueuedMessage));
489 qmsg->msg = (struct GNUNET_MessageHeader *) conclude_msg;
491 GNUNET_CONTAINER_DLL_insert_tail (consensus->messages_head, consensus->messages_tail, qmsg);
493 send_next (consensus);
498 * Destroy a consensus handle (free all state associated with
499 * it, no longer call any of the callbacks).
501 * @param consensus handle to destroy
504 GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus)
506 if (consensus->client != NULL)
508 GNUNET_CLIENT_disconnect (consensus->client);
509 consensus->client = NULL;
511 if (NULL != consensus->peers)
512 GNUNET_free (consensus->peers);
513 GNUNET_free (consensus);