partial docs
[oweals/gnunet.git] / src / set / set_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 set/set_api.c
23  * @brief api for the set service
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_set_service.h"
31 #include "set.h"
32 #include "mq.h"
33
34
35 #define LOG(kind,...) GNUNET_log_from (kind, "set-api",__VA_ARGS__)
36
37 /**
38  * Opaque handle to a set.
39  */
40 struct GNUNET_SET_Handle
41 {
42   struct GNUNET_CLIENT_Connection *client;
43   struct GNUNET_MQ_MessageQueue *mq;
44   unsigned int messages_since_ack;
45 };
46
47 /**
48  * Opaque handle to a set operation request from another peer.
49  */
50 struct GNUNET_SET_Request
51 {
52   uint32_t request_id;
53   int accepted;
54 };
55
56
57 struct GNUNET_SET_OperationHandle
58 {
59   GNUNET_SET_ResultIterator result_cb;
60   void *result_cls;
61   struct GNUNET_SET_Handle *set;
62   uint32_t request_id;
63   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
64 };
65
66
67 /**
68  * Opaque handle to a listen operation.
69  */
70 struct GNUNET_SET_ListenHandle
71 {
72   struct GNUNET_CLIENT_Connection *client;
73   struct GNUNET_MQ_MessageQueue* mq;
74   GNUNET_SET_ListenCallback listen_cb;
75   void *listen_cls;
76 };
77
78
79 /**
80  * Handle result message for a set operation.
81  *
82  * @param cls the set
83  * @param mh the message
84  */
85 void
86 handle_result (void *cls, struct GNUNET_MessageHeader *mh)
87 {
88   struct ResultMessage *msg = (struct ResultMessage *) mh;
89   struct GNUNET_SET_Handle *set = cls;
90   struct GNUNET_SET_OperationHandle *oh;
91   struct GNUNET_SET_Element e;
92
93   if (set->messages_since_ack >= GNUNET_SET_ACK_WINDOW/2)
94   {
95     struct GNUNET_MQ_Message *mqm;
96     mqm = GNUNET_MQ_msg_raw (GNUNET_MESSAGE_TYPE_SET_ACK);
97     GNUNET_MQ_send (set->mq, mqm);
98   }
99
100   oh = GNUNET_MQ_assoc_get (set->mq, ntohl (msg->request_id));
101   GNUNET_break (NULL != oh);
102   if (GNUNET_SCHEDULER_NO_TASK != oh->timeout_task)
103   {
104     GNUNET_SCHEDULER_cancel (oh->timeout_task);
105     oh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
106   }
107   if (htons (msg->result_status) != GNUNET_SET_STATUS_OK)
108   {
109     if (NULL != oh->result_cb)
110       oh->result_cb (oh->result_cls, NULL, htons (msg->result_status));
111     GNUNET_MQ_assoc_remove (set->mq, ntohl (msg->request_id));
112     GNUNET_free (oh);
113     return;
114   }
115   e.data = &msg[1];
116   e.size = ntohs (mh->size) - sizeof (struct ResultMessage);
117   e.type = msg->element_type;
118   if (NULL != oh->result_cb)
119     oh->result_cb (oh->result_cls, &e, htons (msg->result_status));
120 }
121
122 /**
123  * Handle request message for a listen operation
124  *
125  * @param cls the listen handle
126  * @param mh the message
127  */
128 void
129 handle_request (void *cls, struct GNUNET_MessageHeader *mh)
130 {
131   struct RequestMessage *msg = (struct RequestMessage *) mh;
132   struct GNUNET_SET_ListenHandle *lh = cls;
133   struct GNUNET_SET_Request *req;
134
135   req = GNUNET_new (struct GNUNET_SET_Request);
136   req->request_id = ntohl (msg->request_id);
137   lh->listen_cb (lh->listen_cls, &msg->peer_id, &mh[1], req);
138   if (GNUNET_NO == req->accepted)
139     GNUNET_free (req);
140 }
141
142
143 /**
144  * Create an empty set, supporting the specified operation.
145  *
146  * @param cfg configuration to use for connecting to the
147  *        set service
148  * @param op operation supported by the set
149  *        Note that the operation has to be specified
150  *        beforehand, as certain set operations need to maintain
151  *        data structures spefific to the operation
152  * @return a handle to the set
153  */
154 struct GNUNET_SET_Handle *
155 GNUNET_SET_create (struct GNUNET_CONFIGURATION_Handle *cfg,
156                    enum GNUNET_SET_OperationType op)
157 {
158   struct GNUNET_SET_Handle *set;
159   struct GNUNET_MQ_Message *mqm;
160   struct SetCreateMessage *msg;
161   static const struct GNUNET_MQ_Handler mq_handlers[] = {
162     {handle_result, GNUNET_MESSAGE_TYPE_SET_RESULT},
163     GNUNET_MQ_HANDLERS_END
164   };
165
166   set = GNUNET_new (struct GNUNET_SET_Handle);
167   set->client = GNUNET_CLIENT_connect ("set", cfg);
168   GNUNET_assert (NULL != set->client);
169   set->mq = GNUNET_MQ_queue_for_connection_client (set->client, mq_handlers, set);
170   mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_CREATE);
171   msg->operation = htons (op);
172   GNUNET_MQ_send (set->mq, mqm);
173   return set;
174 }
175
176
177 /**
178  * Add an element to the given set.
179  * After the element has been added (in the sense of being
180  * transmitted to the set service), cont will be called.
181  * Calls to add_element can be queued
182  *
183  * @param set set to add element to
184  * @param element element to add to the set
185  * @param cont continuation called after the element has been added
186  * @param cont_cls closure for cont
187  */
188 void
189 GNUNET_SET_add_element (struct GNUNET_SET_Handle *set,
190                         const struct GNUNET_SET_Element *element,
191                         GNUNET_SET_Continuation cont,
192                         void *cont_cls)
193 {
194   struct GNUNET_MQ_Message *mqm;
195   struct ElementMessage *msg;
196
197   mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_ADD);
198   msg->element_type = element->type;
199   memcpy (&msg[1], element->data, element->size);
200   GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
201   GNUNET_MQ_send (set->mq, mqm);
202 }
203
204
205 /**
206  * Remove an element to the given set.
207  * After the element has been removed (in the sense of the
208  * request being transmitted to the set service), cont will be called.
209  * Calls to remove_element can be queued
210  *
211  * @param set set to remove element from
212  * @param element element to remove from the set
213  * @param cont continuation called after the element has been removed
214  * @param cont_cls closure for cont
215  */
216 void
217 GNUNET_SET_remove_element (struct GNUNET_SET_Handle *set,
218                            const struct GNUNET_SET_Element *element,
219                            GNUNET_SET_Continuation cont,
220                            void *cont_cls)
221 {
222   struct GNUNET_MQ_Message *mqm;
223   struct ElementMessage *msg;
224
225   mqm = GNUNET_MQ_msg_extra (msg, element->size, GNUNET_MESSAGE_TYPE_SET_REMOVE);
226   msg->element_type = element->type;
227   memcpy (&msg[1], element->data, element->size);
228   GNUNET_MQ_notify_sent (mqm, cont, cont_cls);
229   GNUNET_MQ_send (set->mq, mqm);
230 }
231
232
233 /**
234  * Destroy the set handle, and free all associated resources.
235  */
236 void
237 GNUNET_SET_destroy (struct GNUNET_SET_Handle *set)
238 {
239   GNUNET_CLIENT_disconnect (set->client);
240   set->client = NULL;
241   GNUNET_MQ_destroy (set->mq);
242   set->mq = NULL;
243 }
244
245
246 /**
247  * Destroy an operation handle
248  *
249  * @cls closure, the operation handle
250  */
251 static void
252 operation_destroy (void *cls)
253 {
254   struct GNUNET_SET_OperationHandle *oh = cls;
255   struct GNUNET_SET_OperationHandle *oh_assoc;
256
257   oh_assoc = GNUNET_MQ_assoc_remove (oh->set->mq, oh->request_id);
258   GNUNET_assert (oh_assoc == oh);
259   oh->set = NULL;
260   GNUNET_free (oh);
261 }
262
263
264 /**
265  * Signature of the main function of a task.
266  *
267  * @param cls closure
268  * @param tc context information (why was this task triggered now)
269  */
270 static void
271 operation_timeout_task (void *cls,
272                         const struct GNUNET_SCHEDULER_TaskContext * tc)
273 {
274   struct GNUNET_SET_OperationHandle *oh = cls;
275   oh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
276   if (NULL != oh->result_cb)
277     oh->result_cb (oh->result_cls, NULL, GNUNET_SET_STATUS_TIMEOUT);
278   oh->result_cb = NULL;
279   oh->result_cls = NULL;
280   GNUNET_SET_operation_cancel (oh);
281 }
282
283
284 /**
285  * Evaluate a set operation with our set and the set of another peer.
286  *
287  * @param set set to use
288  * @param other_peer peer with the other set
289  * @param app_id hash for the application using the set
290  * @param context_msg additional information for the request
291  * @param result_cb called on error or success
292  * @param result_cls closure for result_cb
293  * @return a handle to cancel the operation
294  */
295 struct GNUNET_SET_OperationHandle *
296 GNUNET_SET_evaluate (struct GNUNET_SET_Handle *set,
297                      const struct GNUNET_PeerIdentity *other_peer,
298                      const struct GNUNET_HashCode *app_id,
299                      const struct GNUNET_MessageHeader *context_msg,
300                      struct GNUNET_TIME_Relative timeout,
301                      enum GNUNET_SET_ResultMode result_mode,
302                      GNUNET_SET_ResultIterator result_cb,
303                      void *result_cls)
304 {
305   struct GNUNET_MQ_Message *mqm;
306   struct EvaluateMessage *msg;
307   struct GNUNET_SET_OperationHandle *oh;
308
309   oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
310   oh->result_cb = result_cb;
311   oh->result_cls = result_cls;
312   oh->set = set;
313
314   mqm = GNUNET_MQ_msg_extra (msg, htons(context_msg->size), GNUNET_MESSAGE_TYPE_SET_EVALUATE);
315   msg->request_id = htonl (GNUNET_MQ_assoc_add (set->mq, mqm, oh));
316   msg->other_peer = *other_peer;
317   msg->app_id = *app_id;
318   memcpy (&msg[1], context_msg, htons (context_msg->size));
319   oh->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, operation_timeout_task, oh);
320   /* destroy the operation if the queue gets destroyed */
321   GNUNET_MQ_notify_destroy (mqm, operation_destroy, oh);
322   GNUNET_MQ_send (set->mq, mqm);
323
324   return oh;
325 }
326
327
328 /**
329  * Wait for set operation requests for the given application id
330  * 
331  * @param cfg configuration to use for connecting to
332  *            the set service
333  * @param operation operation we want to listen for
334  * @param app_id id of the application that handles set operation requests
335  * @param listen_cb called for each incoming request matching the operation
336  *                  and application id
337  * @param listen_cls handle for listen_cb
338  * @return a handle that can be used to cancel the listen operation
339  */
340 struct GNUNET_SET_ListenHandle *
341 GNUNET_SET_listen (struct GNUNET_CONFIGURATION_Handle *cfg,
342                    enum GNUNET_SET_OperationType operation,
343                    const struct GNUNET_HashCode *app_id,
344                    GNUNET_SET_ListenCallback listen_cb,
345                    void *listen_cls)
346 {
347   struct GNUNET_SET_ListenHandle *lh;
348   struct GNUNET_MQ_Message *mqm;
349   struct ListenMessage *msg;
350   static const struct GNUNET_MQ_Handler mq_handlers[] = {
351     {handle_request, GNUNET_MESSAGE_TYPE_SET_REQUEST},
352     GNUNET_MQ_HANDLERS_END
353   };
354
355   lh = GNUNET_new (struct GNUNET_SET_ListenHandle);
356   lh->client = GNUNET_CLIENT_connect ("set", cfg);
357   lh->listen_cb = listen_cb;
358   lh->listen_cls = listen_cls;
359   GNUNET_assert (NULL != lh->client);
360   lh->mq = GNUNET_MQ_queue_for_connection_client (lh->client, mq_handlers, lh);
361   mqm = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_SET_LISTEN);
362   msg->operation = htons (operation);
363   msg->app_id = *app_id;
364   GNUNET_MQ_send (lh->mq, mqm);
365
366   return lh;
367 }
368
369
370 /**
371  * Cancel the given listen operation.
372  *
373  * @param lh handle for the listen operation
374  */
375 void
376 GNUNET_SET_listen_cancel (struct GNUNET_SET_ListenHandle *lh)
377 {
378   GNUNET_MQ_destroy (lh->mq);
379   lh->mq = NULL;
380   GNUNET_CLIENT_disconnect (lh->client);
381   lh->client = NULL;
382   lh->listen_cb = NULL;
383   lh->listen_cls = NULL;
384 }
385
386
387 /**
388  * Accept a request we got via GNUNET_SET_listen
389  *
390  * @param request request to accept
391  * @param set set used for the requested operation 
392  * @param timeout timeout for the set operation
393  * @param result_cb callback for the results
394  * @param cls closure for result_cb
395  */
396 struct GNUNET_SET_OperationHandle *
397 GNUNET_SET_accept (struct GNUNET_SET_Request *request,
398                    struct GNUNET_SET_Handle *set,
399                    struct GNUNET_TIME_Relative timeout,
400                    enum GNUNET_SET_ResultMode result_mode,
401                    GNUNET_SET_ResultIterator result_cb,
402                    void *result_cls)
403 {
404   struct GNUNET_MQ_Message *mqm;
405   struct AcceptMessage *msg;
406   struct GNUNET_SET_OperationHandle *oh;
407
408   /* don't accept a request twice! */
409   GNUNET_assert (GNUNET_NO == request->accepted);
410   request->accepted = GNUNET_YES;
411
412   oh = GNUNET_new (struct GNUNET_SET_OperationHandle);
413   oh->result_cb = result_cb;
414   oh->result_cls = result_cls;
415   oh->set = set;
416
417   mqm = GNUNET_MQ_msg (msg , GNUNET_MESSAGE_TYPE_SET_ACCEPT);
418   msg->request_id = htonl (request->request_id);
419   oh->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout, operation_timeout_task, oh);
420   /* destroy the operation if the queue gets destroyed */
421   GNUNET_MQ_notify_destroy (mqm, operation_destroy, oh);
422   GNUNET_MQ_send (set->mq, mqm);
423
424   return oh;
425 }
426
427
428 /**
429  * Cancel the given set operation.
430  *
431  * @param op set operation to cancel
432  */
433 void
434 GNUNET_SET_operation_cancel (struct GNUNET_SET_OperationHandle *h)
435 {
436   struct GNUNET_MQ_Message *mqm;
437   struct GNUNET_SET_OperationHandle *h_assoc;
438
439   h_assoc = GNUNET_MQ_assoc_remove (h->set->mq, h->request_id);
440   GNUNET_assert (h_assoc == h);
441   mqm = GNUNET_MQ_msg_raw (GNUNET_MESSAGE_TYPE_SET_CANCEL);
442   GNUNET_MQ_send (h->set->mq, mqm);
443   GNUNET_free (h);
444 }
445