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);
235 queue_message (struct GNUNET_CONSENSUS_Handle *consensus, struct GNUNET_MessageHeader *msg)
237 struct QueuedMessage *qm;
238 qm = GNUNET_malloc (sizeof *qm);
240 GNUNET_CONTAINER_DLL_insert_tail (consensus->messages_head, consensus->messages_tail, qm);
245 * Called when the server has sent is a new element
247 * @param consensus consensus handle
248 * @param msg element message
251 handle_new_element (struct GNUNET_CONSENSUS_Handle *consensus,
252 struct GNUNET_CONSENSUS_ElementMessage *msg)
254 struct GNUNET_CONSENSUS_Element element;
255 struct GNUNET_CONSENSUS_AckMessage *ack_msg;
258 LOG (GNUNET_ERROR_TYPE_DEBUG, "received new element\n");
260 element.type = msg->element_type;
261 element.size = ntohs (msg->header.size) - sizeof (struct GNUNET_CONSENSUS_ElementMessage);
262 element.data = &msg[1];
264 ret = consensus->new_element_cb (consensus->new_element_cls, &element);
266 ack_msg = GNUNET_new (struct GNUNET_CONSENSUS_AckMessage);
267 ack_msg->header.size = htons (sizeof *ack_msg);
268 ack_msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_ACK);
271 queue_message (consensus, &ack_msg->header);
273 send_next (consensus);
278 * Called when the server has announced
279 * that the conclusion is over.
281 * @param consensus consensus handle
282 * @param msg conclude done message
285 handle_conclude_done (struct GNUNET_CONSENSUS_Handle *consensus,
286 const struct GNUNET_MessageHeader *msg)
288 GNUNET_assert (NULL != consensus->conclude_cb);
289 consensus->may_not_destroy = GNUNET_YES;
290 consensus->conclude_cb (consensus->conclude_cls);
291 consensus->may_not_destroy = GNUNET_NO;
292 consensus->conclude_cb = NULL;
298 * Type of a function to call when we receive a message
302 * @param msg message received, NULL on timeout or fatal error
305 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
307 struct GNUNET_CONSENSUS_Handle *consensus = cls;
309 LOG (GNUNET_ERROR_TYPE_DEBUG, "received message from consensus service\n");
313 /* Error, timeout, death */
314 LOG (GNUNET_ERROR_TYPE_ERROR, "error receiving\n");
315 GNUNET_CLIENT_disconnect (consensus->client);
316 consensus->client = NULL;
317 consensus->new_element_cb (consensus->new_element_cls, NULL);
321 switch (ntohs (msg->type))
323 case GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT:
324 handle_new_element (consensus, (struct GNUNET_CONSENSUS_ElementMessage *) msg);
326 case GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE:
327 handle_conclude_done (consensus, msg);
332 GNUNET_CLIENT_receive (consensus->client, &message_handler, consensus,
333 GNUNET_TIME_UNIT_FOREVER_REL);
337 * Function called to notify a client about the connection
338 * begin ready to queue more data. "buf" will be
339 * NULL and "size" zero if the connection was closed for
340 * writing in the meantime.
343 * @param size number of bytes available in buf
344 * @param buf where the callee should write the message
345 * @return number of bytes written to buf
348 transmit_join (void *cls, size_t size, void *buf)
350 struct GNUNET_CONSENSUS_JoinMessage *msg;
351 struct GNUNET_CONSENSUS_Handle *consensus;
354 GNUNET_assert (NULL != buf);
356 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmitting join message\n");
359 consensus->th = NULL;
360 consensus->joined = 1;
364 msize = sizeof (struct GNUNET_CONSENSUS_JoinMessage) +
365 consensus->num_peers * sizeof (struct GNUNET_PeerIdentity);
367 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN);
368 msg->header.size = htons (msize);
369 msg->session_id = consensus->session_id;
370 msg->num_peers = htonl (consensus->num_peers);
373 consensus->num_peers * sizeof (struct GNUNET_PeerIdentity));
374 send_next (consensus);
375 GNUNET_CLIENT_receive (consensus->client, &message_handler, consensus,
376 GNUNET_TIME_UNIT_FOREVER_REL);
382 * Create a consensus session.
384 * @param cfg configuration to use for connecting to the consensus service
385 * @param num_peers number of peers in the peers array
386 * @param peers array of peers participating in this consensus session
387 * Inclusion of the local peer is optional.
388 * @param session_id session identifier
389 * Allows a group of peers to have more than consensus session.
390 * @param new_element_cb callback, called when a new element is added to the set by
392 * @param new_element_cls closure for new_element
393 * @return handle to use, NULL on error
395 struct GNUNET_CONSENSUS_Handle *
396 GNUNET_CONSENSUS_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
397 unsigned int num_peers,
398 const struct GNUNET_PeerIdentity *peers,
399 const struct GNUNET_HashCode *session_id,
400 GNUNET_CONSENSUS_ElementCallback new_element_cb,
401 void *new_element_cls)
403 struct GNUNET_CONSENSUS_Handle *consensus;
404 size_t join_message_size;
406 consensus = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_Handle));
407 consensus->cfg = cfg;
408 consensus->new_element_cb = new_element_cb;
409 consensus->new_element_cls = new_element_cls;
410 consensus->num_peers = num_peers;
411 consensus->session_id = *session_id;
414 consensus->peers = NULL;
415 else if (num_peers > 0)
417 GNUNET_memdup (peers, num_peers * sizeof (struct GNUNET_PeerIdentity));
419 consensus->client = GNUNET_CLIENT_connect ("consensus", cfg);
421 GNUNET_assert (consensus->client != NULL);
423 join_message_size = (sizeof (struct GNUNET_CONSENSUS_JoinMessage)) +
424 (num_peers * sizeof (struct GNUNET_PeerIdentity));
427 GNUNET_CLIENT_notify_transmit_ready (consensus->client,
429 GNUNET_TIME_UNIT_FOREVER_REL,
430 GNUNET_NO, &transmit_join, consensus);
433 GNUNET_assert (consensus->th != NULL);
440 * Insert an element in the set being reconsiled. Must not be called after
441 * "GNUNET_CONSENSUS_conclude".
443 * @param consensus handle for the consensus session
444 * @param element the element to be inserted
445 * @param idc function called when we are done with this element and it
446 * is thus allowed to call GNUNET_CONSENSUS_insert again
447 * @param idc_cls closure for 'idc'
450 GNUNET_CONSENSUS_insert (struct GNUNET_CONSENSUS_Handle *consensus,
451 const struct GNUNET_CONSENSUS_Element *element,
452 GNUNET_CONSENSUS_InsertDoneCallback idc,
455 struct QueuedMessage *qmsg;
456 struct GNUNET_CONSENSUS_ElementMessage *element_msg;
457 size_t element_msg_size;
459 LOG (GNUNET_ERROR_TYPE_DEBUG, "inserting, size=%llu\n", element->size);
461 element_msg_size = (sizeof (struct GNUNET_CONSENSUS_ElementMessage) +
464 element_msg = GNUNET_malloc (element_msg_size);
465 element_msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT);
466 element_msg->header.size = htons (element_msg_size);
467 memcpy (&element_msg[1], element->data, element->size);
469 qmsg = GNUNET_malloc (sizeof (struct QueuedMessage));
470 qmsg->msg = (struct GNUNET_MessageHeader *) element_msg;
472 qmsg->idc_cls = idc_cls;
474 GNUNET_CONTAINER_DLL_insert_tail (consensus->messages_head, consensus->messages_tail, qmsg);
476 send_next (consensus);
481 * We are done with inserting new elements into the consensus;
482 * try to conclude the consensus within a given time window.
483 * After conclude has been called, no further elements may be
484 * inserted by the client.
486 * @param consensus consensus session
487 * @param timeout timeout after which the conculde callback
489 * @param conclude called when the conclusion was successful
490 * @param conclude_cls closure for the conclude callback
493 GNUNET_CONSENSUS_conclude (struct GNUNET_CONSENSUS_Handle *consensus,
494 struct GNUNET_TIME_Relative timeout,
495 GNUNET_CONSENSUS_ConcludeCallback conclude,
498 struct QueuedMessage *qmsg;
499 struct GNUNET_CONSENSUS_ConcludeMessage *conclude_msg;
501 GNUNET_assert (NULL != conclude);
502 GNUNET_assert (NULL == consensus->conclude_cb);
504 consensus->conclude_cls = conclude_cls;
505 consensus->conclude_cb = conclude;
507 conclude_msg = GNUNET_malloc (sizeof (struct GNUNET_CONSENSUS_ConcludeMessage));
508 conclude_msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE);
509 conclude_msg->header.size = htons (sizeof (struct GNUNET_CONSENSUS_ConcludeMessage));
510 conclude_msg->timeout = GNUNET_TIME_relative_hton (timeout);
512 qmsg = GNUNET_malloc (sizeof (struct QueuedMessage));
513 qmsg->msg = (struct GNUNET_MessageHeader *) conclude_msg;
515 GNUNET_CONTAINER_DLL_insert_tail (consensus->messages_head, consensus->messages_tail, qmsg);
517 send_next (consensus);
522 * Destroy a consensus handle (free all state associated with
523 * it, no longer call any of the callbacks).
525 * @param consensus handle to destroy
528 GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus)
530 if (GNUNET_YES == consensus->may_not_destroy)
532 LOG (GNUNET_ERROR_TYPE_ERROR, "destroy may not be called right now\n");
535 if (consensus->client != NULL)
537 GNUNET_CLIENT_disconnect (consensus->client);
538 consensus->client = NULL;
540 if (NULL != consensus->peers)
541 GNUNET_free (consensus->peers);
542 GNUNET_free (consensus);