- fixed wrong round comparison code
[oweals/gnunet.git] / src / consensus / consensus_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @file consensus/consensus_api.c
23  * @brief
24  * @author Florian Dold
25  */
26 #include "platform.h"
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"
32
33
34 #define LOG(kind,...) GNUNET_log_from (kind, "consensus-api",__VA_ARGS__)
35
36
37 /**
38  * Handle for the service.
39  */
40 struct GNUNET_CONSENSUS_Handle
41 {
42   /**
43    * Configuration to use.
44    */
45   const struct GNUNET_CONFIGURATION_Handle *cfg;
46
47   /**
48    * Client connected to the consensus service, may be NULL if not connected.
49    */
50   struct GNUNET_CLIENT_Connection *client;
51
52   /**
53    * Callback for new elements. Not called for elements added locally.
54    */
55   GNUNET_CONSENSUS_ElementCallback new_element_cb;
56
57   /**
58    * Closure for new_element_cb
59    */
60   void *new_element_cls;
61
62   /**
63    * The (local) session identifier for the consensus session.
64    */
65   struct GNUNET_HashCode session_id;
66
67   /**
68    * GNUNES_YES iff the join message has been sent to the service.
69    */
70   int joined;
71
72   /**
73    * Called when the conclude operation finishes or fails.
74    */
75   GNUNET_CONSENSUS_ConcludeCallback conclude_cb;
76
77   /**
78    * Closure for the conclude callback.
79    */
80   void *conclude_cls;
81
82   /**
83    * Deadline for the conclude operation.
84    */
85   struct GNUNET_TIME_Absolute conclude_deadline;
86
87   /**
88    * Message queue for the client.
89    */
90   struct GNUNET_MQ_Handle *mq;
91 };
92
93 /**
94  * FIXME: this should not bee necessary when the API
95  * issue has been fixed
96  */
97 struct InsertDoneInfo
98 {
99   GNUNET_CONSENSUS_InsertDoneCallback idc;
100   void *cls;
101 };
102
103
104 /**
105  * Called when the server has sent is a new element
106  *
107  * @param cls consensus handle
108  * @param mh element message
109  */
110 static void
111 handle_new_element (void *cls,
112                     const struct GNUNET_MessageHeader *mh)
113 {
114   struct GNUNET_CONSENSUS_Handle *consensus = cls;
115   const struct GNUNET_CONSENSUS_ElementMessage *msg
116       = (const struct GNUNET_CONSENSUS_ElementMessage *) mh;
117   struct GNUNET_SET_Element element;
118
119   LOG (GNUNET_ERROR_TYPE_DEBUG, "received new element\n");
120
121   element.type = msg->element_type;
122   element.size = ntohs (msg->header.size) - sizeof (struct GNUNET_CONSENSUS_ElementMessage);
123   element.data = &msg[1];
124
125   consensus->new_element_cb (consensus->new_element_cls, &element);
126 }
127
128
129 /**
130  * Called when the server has announced
131  * that the conclusion is over.
132  *
133  * @param cls consensus handle
134  * @param msg conclude done message
135  */
136 static void
137 handle_conclude_done (void *cls,
138                       const struct GNUNET_MessageHeader *msg)
139 {
140   struct GNUNET_CONSENSUS_Handle *consensus = cls;
141
142   GNUNET_CONSENSUS_ConcludeCallback cc;
143
144   GNUNET_MQ_destroy (consensus->mq);
145   consensus->mq = NULL;
146
147   GNUNET_CLIENT_disconnect (consensus->client);
148   consensus->client = NULL;
149
150
151   GNUNET_assert (NULL != (cc = consensus->conclude_cb));
152   consensus->conclude_cb = NULL;
153   cc (consensus->conclude_cls);
154 }
155
156
157 /**
158  * Generic error handler, called with the appropriate
159  * error code and the same closure specified at the creation of
160  * the message queue.
161  * Not every message queue implementation supports an error handler.
162  *
163  * @param cls closure, same closure as for the message handlers
164  * @param error error code
165  */
166 static void
167 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
168 {
169   LOG (GNUNET_ERROR_TYPE_WARNING, "consensus service disconnected us\n");
170 }
171
172
173 /**
174  * Create a consensus session.
175  *
176  * @param cfg configuration to use for connecting to the consensus service
177  * @param num_peers number of peers in the peers array
178  * @param peers array of peers participating in this consensus session
179  *              Inclusion of the local peer is optional.
180  * @param session_id session identifier
181  *                   Allows a group of peers to have more than consensus session.
182  * @param start start time of the consensus, conclude should be called before
183  *              the start time.
184  * @param deadline time when the consensus should have concluded
185  * @param new_element_cb callback, called when a new element is added to the set by
186  *                    another peer
187  * @param new_element_cls closure for new_element
188  * @return handle to use, NULL on error
189  */
190 struct GNUNET_CONSENSUS_Handle *
191 GNUNET_CONSENSUS_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
192                          unsigned int num_peers,
193                          const struct GNUNET_PeerIdentity *peers,
194                          const struct GNUNET_HashCode *session_id,
195                          struct GNUNET_TIME_Absolute start,
196                          struct GNUNET_TIME_Absolute deadline,
197                          GNUNET_CONSENSUS_ElementCallback new_element_cb,
198                          void *new_element_cls)
199 {
200   struct GNUNET_CONSENSUS_Handle *consensus;
201   struct GNUNET_CONSENSUS_JoinMessage *join_msg;
202   struct GNUNET_MQ_Envelope *ev;
203   const static struct GNUNET_MQ_MessageHandler mq_handlers[] = {
204     {handle_new_element,
205       GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_RECEIVED_ELEMENT, 0},
206     {handle_conclude_done,
207       GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE_DONE, 0},
208     GNUNET_MQ_HANDLERS_END
209   };
210
211   consensus = GNUNET_new (struct GNUNET_CONSENSUS_Handle);
212   consensus->cfg = cfg;
213   consensus->new_element_cb = new_element_cb;
214   consensus->new_element_cls = new_element_cls;
215   consensus->session_id = *session_id;
216   consensus->client = GNUNET_CLIENT_connect ("consensus", cfg);
217   consensus->mq = GNUNET_MQ_queue_for_connection_client (consensus->client,
218                                                          mq_handlers, mq_error_handler, consensus);
219
220   GNUNET_assert (consensus->client != NULL);
221
222   ev = GNUNET_MQ_msg_extra (join_msg,
223                             (num_peers * sizeof (struct GNUNET_PeerIdentity)),
224                             GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_JOIN);
225
226   join_msg->session_id = consensus->session_id;
227   join_msg->start = GNUNET_TIME_absolute_hton (start);
228   join_msg->deadline = GNUNET_TIME_absolute_hton (deadline);
229   join_msg->num_peers = htonl (num_peers);
230   memcpy(&join_msg[1],
231          peers,
232          num_peers * sizeof (struct GNUNET_PeerIdentity));
233
234   GNUNET_MQ_send (consensus->mq, ev);
235   return consensus;
236 }
237
238
239 static void
240 idc_adapter (void *cls)
241 {
242   struct InsertDoneInfo *i = cls;
243   i->idc (i->cls, GNUNET_OK);
244   GNUNET_free (i);
245 }
246
247 /**
248  * Insert an element in the set being reconsiled.  Must not be called after
249  * "GNUNET_CONSENSUS_conclude".
250  *
251  * @param consensus handle for the consensus session
252  * @param element the element to be inserted
253  * @param idc function called when we are done with this element and it
254  *            is thus allowed to call GNUNET_CONSENSUS_insert again
255  * @param idc_cls closure for 'idc'
256  */
257 void
258 GNUNET_CONSENSUS_insert (struct GNUNET_CONSENSUS_Handle *consensus,
259                          const struct GNUNET_SET_Element *element,
260                          GNUNET_CONSENSUS_InsertDoneCallback idc,
261                          void *idc_cls)
262 {
263   struct GNUNET_CONSENSUS_ElementMessage *element_msg;
264   struct GNUNET_MQ_Envelope *ev;
265   struct InsertDoneInfo *i;
266
267   LOG (GNUNET_ERROR_TYPE_DEBUG, "inserting, size=%llu\n", element->size);
268
269   ev = GNUNET_MQ_msg_extra (element_msg, element->size,
270                             GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_INSERT);
271
272   memcpy (&element_msg[1], element->data, element->size);
273
274   if (NULL != idc)
275   {
276     i = GNUNET_new (struct InsertDoneInfo);
277     i->idc = idc;
278     i->cls = idc_cls;
279     GNUNET_MQ_notify_sent (ev, idc_adapter, i);
280   }
281   GNUNET_MQ_send (consensus->mq, ev);
282 }
283
284
285 /**
286  * We are done with inserting new elements into the consensus;
287  * try to conclude the consensus within a given time window.
288  * After conclude has been called, no further elements may be
289  * inserted by the client.
290  *
291  * @param consensus consensus session
292  * @param deadline deadline after which the conculde callback
293  *                must be called
294  * @param conclude called when the conclusion was successful
295  * @param conclude_cls closure for the conclude callback
296  */
297 void
298 GNUNET_CONSENSUS_conclude (struct GNUNET_CONSENSUS_Handle *consensus,
299                            GNUNET_CONSENSUS_ConcludeCallback conclude,
300                            void *conclude_cls)
301 {
302   struct GNUNET_MQ_Envelope *ev;
303
304   GNUNET_assert (NULL != conclude);
305   GNUNET_assert (NULL == consensus->conclude_cb);
306
307   consensus->conclude_cls = conclude_cls;
308   consensus->conclude_cb = conclude;
309
310   ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_CONSENSUS_CLIENT_CONCLUDE);
311   GNUNET_MQ_send (consensus->mq, ev);
312 }
313
314
315 /**
316  * Destroy a consensus handle (free all state associated with
317  * it, no longer call any of the callbacks).
318  *
319  * @param consensus handle to destroy
320  */
321 void
322 GNUNET_CONSENSUS_destroy (struct GNUNET_CONSENSUS_Handle *consensus)
323 {
324   if (NULL != consensus->mq)
325   {
326     GNUNET_MQ_destroy (consensus->mq);
327     consensus->mq = NULL;
328   }
329   if (NULL != consensus->client)
330   {
331     GNUNET_CLIENT_disconnect (consensus->client);
332     consensus->client = NULL;
333   }
334   GNUNET_free (consensus);
335 }
336