GNUnet  0.10.x
consensus_api.c
Go to the documentation of this file.
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 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_client_lib.h"
31 #include "consensus.h"
32 
33 
34 #define LOG(kind,...) GNUNET_log_from (kind, "consensus-api",__VA_ARGS__)
35 
36 
41 {
46 
51 
56 
61 
65  int joined;
66 
71 
75  void *conclude_cls;
76 
81 
86 };
87 
88 
94 {
96  void *cls;
97 };
98 
99 
106 static int
107 check_new_element (void *cls,
108  const struct GNUNET_CONSENSUS_ElementMessage *msg)
109 {
110  /* any size is fine, elements are variable-size */
111  return GNUNET_OK;
112 }
113 
114 
121 static void
123  const struct GNUNET_CONSENSUS_ElementMessage *msg)
124 {
125  struct GNUNET_CONSENSUS_Handle *consensus = cls;
126  struct GNUNET_SET_Element element;
127 
129  "received new element\n");
130  element.element_type = msg->element_type;
131  element.size = ntohs (msg->header.size) - sizeof (struct GNUNET_CONSENSUS_ElementMessage);
132  element.data = &msg[1];
133  consensus->new_element_cb (consensus->new_element_cls,
134  &element);
135 }
136 
137 
145 static void
147  const struct GNUNET_MessageHeader *msg)
148 {
149  struct GNUNET_CONSENSUS_Handle *consensus = cls;
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 
169 static void
170 mq_error_handler (void *cls,
171  enum GNUNET_MQ_Error error)
172 {
174  "consensus service disconnected us\n");
175 }
176 
177 
197  unsigned int num_peers,
198  const struct GNUNET_PeerIdentity *peers,
199  const struct GNUNET_HashCode *session_id,
203  void *new_element_cls)
204 {
205  struct GNUNET_CONSENSUS_Handle *consensus
207  struct GNUNET_MQ_MessageHandler mq_handlers[] = {
208  GNUNET_MQ_hd_var_size (new_element,
211  consensus),
212  GNUNET_MQ_hd_fixed_size (conclude_done,
214  struct GNUNET_MessageHeader,
215  consensus),
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,
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)),
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 
252 static void
253 idc_adapter (void *cls)
254 {
255  struct InsertDoneInfo *i = cls;
256  i->idc (i->cls, GNUNET_OK);
257  GNUNET_free (i);
258 }
259 
270 void
272  const struct GNUNET_SET_Element *element,
274  void *idc_cls)
275 {
276  struct GNUNET_CONSENSUS_ElementMessage *element_msg;
277  struct GNUNET_MQ_Envelope *ev;
278  struct InsertDoneInfo *i;
279 
280  LOG (GNUNET_ERROR_TYPE_DEBUG, "inserting, size=%llu\n", element->size);
281 
282  ev = GNUNET_MQ_msg_extra (element_msg, element->size,
284 
285  GNUNET_memcpy (&element_msg[1], element->data, element->size);
286 
287  if (NULL != idc)
288  {
289  i = GNUNET_new (struct InsertDoneInfo);
290  i->idc = idc;
291  i->cls = idc_cls;
293  }
294  GNUNET_MQ_send (consensus->mq, ev);
295 }
296 
297 
310 void
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 
324  GNUNET_MQ_send (consensus->mq, ev);
325 }
326 
327 
334 void
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 /* end of consensus_api.c */
GNUNET_CONSENSUS_ConcludeCallback conclude_cb
Called when the conclude operation finishes or fails.
Definition: consensus_api.c:70
void * new_element_cls
Closure for new_element_cb.
Definition: consensus_api.c:55
void GNUNET_CONSENSUS_destroy(struct GNUNET_CONSENSUS_Handle *consensus)
Destroy a consensus handle (free all state associated with it, no longer call any of the callbacks)...
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
void(* GNUNET_CONSENSUS_ElementCallback)(void *cls, const struct GNUNET_SET_Element *element)
Called when a new element was received from another peer, or an error occurred.
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:901
GNUNET_MQ_Error
Error codes for the queue.
Element stored in a set.
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define LOG(kind,...)
Definition: consensus_api.c:34
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void GNUNET_CONSENSUS_insert(struct GNUNET_CONSENSUS_Handle *consensus, const struct GNUNET_SET_Element *element, GNUNET_CONSENSUS_InsertDoneCallback idc, void *idc_cls)
Insert an element in the set being reconsiled.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
static void handle_new_element(void *cls, const struct GNUNET_CONSENSUS_ElementMessage *msg)
Called when the server has sent is a new element.
struct GNUNET_TIME_AbsoluteNBO start
Start time for the consensus.
Definition: consensus.h:58
struct GNUNET_HashCode session_id
The (local) session identifier for the consensus session.
Definition: consensus_api.c:60
FIXME: this should not bee necessary when the API issue has been fixed.
Definition: consensus_api.c:93
struct GNUNET_HashCode session_id
Session id of the consensus.
Definition: consensus.h:53
const void * data
Actual data of the element.
uint32_t num_peers
Number of peers (at the end of this message) that want to participate in the consensus.
Definition: consensus.h:48
GNUNET_CONSENSUS_ElementCallback new_element_cb
Callback for new elements.
Definition: consensus_api.c:50
static void idc_adapter(void *cls)
GNUNET_CONSENSUS_InsertDoneCallback idc
Definition: consensus_api.c:95
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
void(* GNUNET_CONSENSUS_ConcludeCallback)(void *cls)
Called when a conclusion was successful.
#define GNUNET_memcpy(dst, src, n)
void GNUNET_MQ_notify_sent(struct GNUNET_MQ_Envelope *ev, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
Call a callback once the envelope has been sent, that is, sending it can not be canceled anymore...
Definition: mq.c:774
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
Message with an element.
Definition: consensus.h:72
struct GNUNET_MessageHeader header
Type: Either GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT or GNUNET_MESSAGE_TYPE_CONSENSUS_C...
Definition: consensus.h:80
A 512-bit hashcode.
Message handler for a specific message type.
void(* GNUNET_CONSENSUS_InsertDoneCallback)(void *cls, int success)
Called when an insertion (transmission to consensus service, which does not imply fully consensus on ...
static int check_new_element(void *cls, const struct GNUNET_CONSENSUS_ElementMessage *msg)
Called when the server has sent is a new element.
#define GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT
Sent by service when a new element is added.
#define GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE
Sent by client to service in order to start the consensus conclusion.
struct GNUNET_TIME_Absolute conclude_deadline
Deadline for the conclude operation.
Definition: consensus_api.c:80
Sent by the client to the service, when the client wants the service to join a consensus session...
Definition: consensus.h:37
static unsigned int num_peers
#define GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT
Insert an element.
int joined
GNUNET_YES iff the join message has been sent to the service.
Definition: consensus_api.c:65
Handle to a message queue.
Definition: mq.c:85
#define GNUNET_MQ_msg_header(type)
Allocate a GNUNET_MQ_Envelope, where the message only consists of a header.
Definition: gnunet_mq_lib.h:76
The identity of the host (wraps the signing key of the peer).
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: consensus_api.c:45
configuration data
Definition: configuration.c:85
static struct CadetPeer * peers
Operation to get peer ids.
uint16_t element_type
Type: GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_NEW_ELEMENT.
Definition: consensus.h:85
#define GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE
Sent by service to client in order to signal a completed consensus conclusion.
uint16_t size
Number of bytes in the buffer pointed to by data.
void GNUNET_CONSENSUS_conclude(struct GNUNET_CONSENSUS_Handle *consensus, GNUNET_CONSENSUS_ConcludeCallback conclude, void *conclude_cls)
We are done with inserting new elements into the consensus; try to conclude the consensus within a gi...
#define GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN
Join a consensus session.
Handle for the service.
Definition: consensus_api.c:40
static struct GNUNET_TIME_Absolute deadline
Deadline for all consensuses.
Header for all communications.
void * conclude_cls
Closure for the conclude_cb callback.
Definition: consensus_api.c:75
Time for absolute times used by GNUnet, in microseconds.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:353
struct GNUNET_CONSENSUS_Handle * GNUNET_CONSENSUS_create(const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int num_peers, const struct GNUNET_PeerIdentity *peers, const struct GNUNET_HashCode *session_id, struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Absolute deadline, GNUNET_CONSENSUS_ElementCallback new_element_cb, void *new_element_cls)
Create a consensus session.
struct GNUNET_MQ_Handle * mq
Message queue for the client.
Definition: consensus_api.c:85
static void handle_conclude_done(void *cls, const struct GNUNET_MessageHeader *msg)
Called when the server has announced that the conclusion is over.
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:654
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
struct GNUNET_TIME_AbsoluteNBO deadline
Deadline for conclude.
Definition: consensus.h:63
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t element_type
Application-specific element type.