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;
141 struct QueuedMessage *messages_tail;
144 * GNUNET_YES when currently in a section where destroy may not be
153 * Schedule transmitting the next message.
155 * @param consensus consensus handle
158 send_next (struct GNUNET_CONSENSUS_Handle *consensus);
162 * Function called to notify a client about the connection
163 * begin ready to queue more data. "buf" will be
164 * NULL and "size" zero if the connection was closed for
165 * writing in the meantime.
168 * @param size number of bytes available in buf
169 * @param buf where the callee should write the message
170 * @return number of bytes written to buf
173 transmit_queued (void *cls, size_t size,
176 struct GNUNET_CONSENSUS_Handle *consensus;
177 struct QueuedMessage *qmsg;
180 consensus = (struct GNUNET_CONSENSUS_Handle *) cls;
181 consensus->th = NULL;
183 qmsg = consensus->messages_head;
184 GNUNET_CONTAINER_DLL_remove (consensus->messages_head, consensus->messages_tail, qmsg);
188 if (NULL != qmsg->idc)
190 qmsg->idc (qmsg->idc_cls, GNUNET_YES);
195 msg_size = ntohs (qmsg->msg->size);
197 GNUNET_assert (size >= msg_size);
199 memcpy (buf, qmsg->msg, msg_size);
200 if (NULL != qmsg->idc)
202 qmsg->idc (qmsg->idc_cls, GNUNET_YES);
204 GNUNET_free (qmsg->msg);
206 /* FIXME: free the messages */
208 send_next (consensus);
215 * Schedule transmitting the next message.
217 * @param consensus consensus handle
220 send_next (struct GNUNET_CONSENSUS_Handle *consensus)
222 if (NULL != consensus->th)
225 if (NULL != consensus->messages_head)
228 GNUNET_CLIENT_notify_transmit_ready (consensus->client, ntohs (consensus->messages_head->msg->size),
229 GNUNET_TIME_UNIT_FOREVER_REL,
230 GNUNET_NO, &transmit_queued, consensus);
236 * Called when the server has sent is a new element
238 * @param consensus consensus handle
239 * @param msg element message
242 handle_new_element (struct GNUNET_CONSENSUS_Handle *consensus,
243 struct GNUNET_CONSENSUS_ElementMessage *msg)
245 struct GNUNET_CONSENSUS_Element element;
247 LOG (GNUNET_ERROR_TYPE_DEBUG, "received new element\n");
249 element.type = msg->element_type;
250 element.size = ntohs (msg->header.size) - sizeof (struct GNUNET_CONSENSUS_ElementMessage);
251 element.data = &msg[1];
253 consensus->new_element_cb (consensus->new_element_cls, &element);
255 send_next (consensus);
260 * Called when the server has announced
261 * that the conclusion is over.
263 * @param consensus consensus handle
264 * @param msg conclude done message
267 handle_conclude_done (struct GNUNET_CONSENSUS_Handle *consensus,
268 const struct GNUNET_MessageHeader *msg)
270 GNUNET_assert (NULL != consensus->conclude_cb);
271 consensus->may_not_destroy = GNUNET_YES;
272 consensus->conclude_cb (consensus->conclude_cls);
273 consensus->may_not_destroy = GNUNET_NO;
274 consensus->conclude_cb = NULL;
280 * Type of a function to call when we receive a message
284 * @param msg message received, NULL on timeout or fatal error
287 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
289 struct GNUNET_CONSENSUS_Handle *consensus = cls;
291 LOG (GNUNET_ERROR_TYPE_DEBUG, "received message from consensus service\n");
295 /* Error, timeout, death */
296 LOG (GNUNET_ERROR_TYPE_ERROR, "error receiving\n");
297 GNUNET_CLIENT_disconnect (consensus->client);
298 consensus->client = NULL;
299 consensus->new_element_cb (consensus->new_element_cls, NULL);
303 switch (ntohs (msg->type))
305 case GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT:
306 handle_new_element (consensus, (struct GNUNET_CONSENSUS_ElementMessage *) msg);
308 case GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE:
309 handle_conclude_done (consensus, msg);
314 GNUNET_CLIENT_receive (consensus->client, &message_handler, consensus,
315 GNUNET_TIME_UNIT_FOREVER_REL);
319 * Function called to notify a client about the connection
320 * begin ready to queue more data. "buf" will be
321 * NULL and "size" zero if the connection was closed for
322 * writing in the meantime.
325 * @param size number of bytes available in buf
326 * @param buf where the callee should write the message
327 * @return number of bytes written to buf
330 transmit_join (void *cls, size_t size, void *buf)
332 struct GNUNET_CONSENSUS_JoinMessage *msg;
333 struct GNUNET_CONSENSUS_Handle *consensus;
336 GNUNET_assert (NULL != buf);
338 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmitting join message\n");
341 consensus->th = NULL;
342 consensus->joined = 1;
346 msize = sizeof (struct GNUNET_CONSENSUS_JoinMessage) +
347 consensus->num_peers * sizeof (struct GNUNET_PeerIdentity);
349 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN);
350 msg->header.size = htons (msize);
351 msg->session_id = consensus->session_id;
352 msg->num_peers = htonl (consensus->num_peers);
355 consensus->num_peers * sizeof (struct GNUNET_PeerIdentity));
356 send_next (consensus);
357 GNUNET_CLIENT_receive (consensus->client, &message_handler, consensus,
358 GNUNET_TIME_UNIT_FOREVER_REL);
364 * Create a consensus session.
366 * @param cfg configuration to use for connecting to the consensus service
367 * @param num_peers number of peers in the peers array
368 * @param peers array of peers participating in this consensus session
369 * Inclusion of the local peer is optional.
370 * @param session_id session identifier
371 * Allows a group of peers to have more than consensus session.
372 * @param new_element_cb callback, called when a new element is added to the set by
374 * @param new_element_cls closure for new_element
375 * @return handle to use, NULL on error
377 struct GNUNET_CONSENSUS_Handle *
378 GNUNET_CONSENSUS_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
379 unsigned int num_peers,
380 const struct GNUNET_PeerIdentity *peers,
381 const struct GNUNET_HashCode *session_id,
382 GNUNET_CONSENSUS_ElementCallback new_element_cb,
383 void *new_element_cls)
385 struct GNUNET_CONSENSUS_Handle *consensus;
386 size_t join_message_size;
388 consensus = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_Handle));
389 consensus->cfg = cfg;
390 consensus->new_element_cb = new_element_cb;
391 consensus->new_element_cls = new_element_cls;
392 consensus->num_peers = num_peers;
393 consensus->session_id = *session_id;
396 consensus->peers = NULL;
397 else if (num_peers > 0)
399 GNUNET_memdup (peers, num_peers * sizeof (struct GNUNET_PeerIdentity));
401 consensus->client = GNUNET_CLIENT_connect ("consensus", cfg);
403 GNUNET_assert (consensus->client != NULL);
405 join_message_size = (sizeof (struct GNUNET_CONSENSUS_JoinMessage)) +
406 (num_peers * sizeof (struct GNUNET_PeerIdentity));
409 GNUNET_CLIENT_notify_transmit_ready (consensus->client,
411 GNUNET_TIME_UNIT_FOREVER_REL,
412 GNUNET_NO, &transmit_join, consensus);
415 GNUNET_assert (consensus->th != NULL);
422 * Insert an element in the set being reconsiled. Must not be called after
423 * "GNUNET_CONSENSUS_conclude".
425 * @param consensus handle for the consensus session
426 * @param element the element to be inserted
427 * @param idc function called when we are done with this element and it
428 * is thus allowed to call GNUNET_CONSENSUS_insert again
429 * @param idc_cls closure for 'idc'
432 GNUNET_CONSENSUS_insert (struct GNUNET_CONSENSUS_Handle *consensus,
433 const struct GNUNET_CONSENSUS_Element *element,
434 GNUNET_CONSENSUS_InsertDoneCallback idc,
437 struct QueuedMessage *qmsg;
438 struct GNUNET_CONSENSUS_ElementMessage *element_msg;
439 size_t element_msg_size;
441 LOG (GNUNET_ERROR_TYPE_DEBUG, "inserting, size=%llu\n", element->size);
443 element_msg_size = (sizeof (struct GNUNET_CONSENSUS_ElementMessage) +
446 element_msg = GNUNET_malloc (element_msg_size);
447 element_msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT);
448 element_msg->header.size = htons (element_msg_size);
449 memcpy (&element_msg[1], element->data, element->size);
451 qmsg = GNUNET_malloc (sizeof (struct QueuedMessage));
452 qmsg->msg = (struct GNUNET_MessageHeader *) element_msg;
454 qmsg->idc_cls = idc_cls;
456 GNUNET_CONTAINER_DLL_insert_tail (consensus->messages_head, consensus->messages_tail, qmsg);
458 send_next (consensus);
463 * We are done with inserting new elements into the consensus;
464 * try to conclude the consensus within a given time window.
465 * After conclude has been called, no further elements may be
466 * inserted by the client.
468 * @param consensus consensus session
469 * @param timeout timeout after which the conculde callback
471 * @param conclude called when the conclusion was successful
472 * @param conclude_cls closure for the conclude callback
475 GNUNET_CONSENSUS_conclude (struct GNUNET_CONSENSUS_Handle *consensus,
476 struct GNUNET_TIME_Relative timeout,
477 GNUNET_CONSENSUS_ConcludeCallback conclude,
480 struct QueuedMessage *qmsg;
481 struct GNUNET_CONSENSUS_ConcludeMessage *conclude_msg;
483 GNUNET_assert (NULL != conclude);
484 GNUNET_assert (NULL == consensus->conclude_cb);
486 consensus->conclude_cls = conclude_cls;
487 consensus->conclude_cb = conclude;
489 conclude_msg = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_ConcludeMessage));
490 conclude_msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE);
491 conclude_msg->header.size = htons (sizeof (struct GNUNET_CONSENSUS_ConcludeMessage));
492 conclude_msg->timeout = GNUNET_TIME_relative_hton (timeout);
494 qmsg = GNUNET_malloc (sizeof (struct QueuedMessage));
495 qmsg->msg = (struct GNUNET_MessageHeader *) conclude_msg;
497 GNUNET_CONTAINER_DLL_insert_tail (consensus->messages_head, consensus->messages_tail, qmsg);
499 send_next (consensus);
504 * Destroy a consensus handle (free all state associated with
505 * it, no longer call any of the callbacks).
507 * @param consensus handle to destroy
510 GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus)
512 if (GNUNET_YES == consensus->may_not_destroy)
514 LOG (GNUNET_ERROR_TYPE_ERROR, "destroy may not be called right now\n");
517 if (consensus->client != NULL)
519 GNUNET_CLIENT_disconnect (consensus->client);
520 consensus->client = NULL;
522 if (NULL != consensus->peers)
523 GNUNET_free (consensus->peers);
524 GNUNET_free (consensus);