2 This file is part of GNUnet.
3 (C) 2013 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 Liceidentity 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 Liceidentity for more details.
15 You should have received a copy of the GNU General Public Liceidentity
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 psycstore/psycstore_api.c
23 * @brief API to interact with the PSYCstore service
24 * @author Gabor X Toth
25 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_psycstore_service.h"
33 #include "psycstore.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "psycstore-api",__VA_ARGS__)
39 * Handle for an operation with the PSYCstore service.
41 struct GNUNET_PSYCSTORE_OperationHandle
45 * Main PSYCstore handle.
47 struct GNUNET_PSYCSTORE_Handle *h;
50 * We keep operations in a DLL.
52 struct GNUNET_PSYCSTORE_OperationHandle *next;
55 * We keep operations in a DLL.
57 struct GNUNET_PSYCSTORE_OperationHandle *prev;
60 * Message to send to the PSYCstore service.
61 * Allocated at the end of this struct.
63 const struct GNUNET_MessageHeader *msg;
66 * Continuation to invoke with the result of an operation.
68 GNUNET_PSYCSTORE_ResultCallback res_cb;
71 * Continuation to invoke with the result of an operation returning a fragment.
73 GNUNET_PSYCSTORE_FragmentCallback frag_cb;
76 * Continuation to invoke with the result of an operation returning a state variable.
78 GNUNET_PSYCSTORE_StateCallback state_cb;
81 * Closure for the callbacks.
89 * Handle for the service.
91 struct GNUNET_PSYCSTORE_Handle
94 * Configuration to use.
96 const struct GNUNET_CONFIGURATION_Handle *cfg;
99 * Socket (if available).
101 struct GNUNET_CLIENT_Connection *client;
104 * Head of active operations.
106 struct GNUNET_PSYCSTORE_OperationHandle *op_head;
109 * Tail of active operations.
111 struct GNUNET_PSYCSTORE_OperationHandle *op_tail;
114 * Currently pending transmission request, or NULL for none.
116 struct GNUNET_CLIENT_TransmitHandle *th;
119 * Task doing exponential back-off trying to reconnect.
121 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
124 * Time for next connect retry.
126 struct GNUNET_TIME_Relative reconnect_delay;
129 * Are we polling for incoming messages right now?
137 * Try again to connect to the PSYCstore service.
139 * @param cls handle to the PSYCstore service.
140 * @param tc scheduler context
143 reconnect (void *cls,
144 const struct GNUNET_SCHEDULER_TaskContext *tc);
148 * Reschedule a connect attempt to the service.
150 * @param h transport service to reconnect
153 reschedule_connect (struct GNUNET_PSYCSTORE_Handle *h)
155 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
159 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
162 if (NULL != h->client)
164 GNUNET_CLIENT_disconnect (h->client);
167 h->in_receive = GNUNET_NO;
168 LOG (GNUNET_ERROR_TYPE_DEBUG,
169 "Scheduling task to reconnect to PSYCstore service in %s.\n",
170 GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, GNUNET_YES));
172 GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
173 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
178 * Type of a function to call when we receive a message
182 * @param msg message received, NULL on timeout or fatal error
185 message_handler (void *cls,
186 const struct GNUNET_MessageHeader *msg)
188 struct GNUNET_PSYCSTORE_Handle *h = cls;
189 struct GNUNET_PSYCSTORE_OperationHandle *op;
190 const struct ResultCodeMessage *rcm;
196 reschedule_connect (h);
199 LOG (GNUNET_ERROR_TYPE_DEBUG,
200 "Received message of type %d from PSYCstore service\n",
202 size = ntohs (msg->size);
203 switch (ntohs (msg->type))
205 case GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE:
206 if (size < sizeof (struct ResultCodeMessage))
209 reschedule_connect (h);
212 rcm = (const struct ResultCodeMessage *) msg;
213 str = (const char *) &rcm[1];
214 if ( (size > sizeof (struct ResultCodeMessage)) &&
215 ('\0' != str[size - sizeof (struct ResultCodeMessage) - 1]) )
218 reschedule_connect (h);
221 if (size == sizeof (struct ResultCodeMessage))
225 GNUNET_CONTAINER_DLL_remove (h->op_head,
228 GNUNET_CLIENT_receive (h->client, &message_handler, h,
229 GNUNET_TIME_UNIT_FOREVER_REL);
230 if (NULL != op->res_cb)
231 op->res_cb (op->cls, rcm->result_code , str);
236 reschedule_connect (h);
243 * Schedule transmission of the next message from our queue.
245 * @param h PSYCstore handle
248 transmit_next (struct GNUNET_PSYCSTORE_Handle *h);
252 * Transmit next message to service.
254 * @param cls the 'struct GNUNET_PSYCSTORE_Handle'.
255 * @param size number of bytes available in buf
256 * @param buf where to copy the message
257 * @return number of bytes copied to buf
260 send_next_message (void *cls,
264 struct GNUNET_PSYCSTORE_Handle *h = cls;
265 struct GNUNET_PSYCSTORE_OperationHandle *op = h->op_head;
271 ret = ntohs (op->msg->size);
274 reschedule_connect (h);
277 LOG (GNUNET_ERROR_TYPE_DEBUG,
278 "Sending message of type %d to PSYCstore service\n",
279 ntohs (op->msg->type));
280 memcpy (buf, op->msg, ret);
281 if ( (NULL == op->res_cb) &&
282 (NULL == op->frag_cb) &&
283 (NULL == op->state_cb))
285 GNUNET_CONTAINER_DLL_remove (h->op_head,
291 if (GNUNET_NO == h->in_receive)
293 h->in_receive = GNUNET_YES;
294 GNUNET_CLIENT_receive (h->client,
296 GNUNET_TIME_UNIT_FOREVER_REL);
303 * Schedule transmission of the next message from our queue.
305 * @param h PSYCstore handle
308 transmit_next (struct GNUNET_PSYCSTORE_Handle *h)
310 struct GNUNET_PSYCSTORE_OperationHandle *op = h->op_head;
312 GNUNET_assert (NULL == h->th);
315 if (NULL == h->client)
317 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
318 ntohs (op->msg->size),
319 GNUNET_TIME_UNIT_FOREVER_REL,
327 * Try again to connect to the PSYCstore service.
329 * @param cls the handle to the PSYCstore service
330 * @param tc scheduler context
333 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
335 struct GNUNET_PSYCSTORE_Handle *h = cls;
337 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
338 LOG (GNUNET_ERROR_TYPE_DEBUG,
339 "Connecting to PSYCstore service.\n");
340 GNUNET_assert (NULL == h->client);
341 h->client = GNUNET_CLIENT_connect ("psycstore", h->cfg);
342 GNUNET_assert (NULL != h->client);
348 * Connect to the PSYCstore service.
350 * @param cfg the configuration to use
351 * @return handle to use
353 struct GNUNET_PSYCSTORE_Handle *
354 GNUNET_PSYCSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
356 struct GNUNET_PSYCSTORE_Handle *h;
358 h = GNUNET_new (struct GNUNET_PSYCSTORE_Handle);
360 h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
361 h->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, h);
367 * Disconnect from PSYCstore service
369 * @param h handle to destroy
372 GNUNET_PSYCSTORE_disconnect (struct GNUNET_PSYCSTORE_Handle *h)
374 GNUNET_assert (NULL != h);
375 GNUNET_assert (h->op_head == h->op_tail);
376 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
378 GNUNET_SCHEDULER_cancel (h->reconnect_task);
379 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
383 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
386 if (NULL != h->client)
388 GNUNET_CLIENT_disconnect (h->client);
396 * Cancel a PSYCstore operation. Note that the operation MAY still
397 * be executed; this merely cancels the continuation; if the request
398 * was already transmitted, the service may still choose to complete
401 * @param op Operation to cancel.
404 GNUNET_PSYCSTORE_operation_cancel (struct GNUNET_PSYCSTORE_OperationHandle *op)
406 struct GNUNET_PSYCSTORE_Handle *h = op->h;
408 if ( (h->op_head != op) ||
409 (NULL == h->client) )
411 /* request not active, can simply remove */
412 GNUNET_CONTAINER_DLL_remove (h->op_head,
420 /* request active but not yet with service, can still abort */
421 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
423 GNUNET_CONTAINER_DLL_remove (h->op_head,
430 /* request active with service, simply ensure continuations are not called */
437 struct GNUNET_PSYCSTORE_OperationHandle *
438 GNUNET_PSYCSTORE_membership_store (
439 struct GNUNET_PSYCSTORE_Handle *h,
440 const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
441 const struct GNUNET_CRYPTO_EccPublicKey *slave_key,
443 uint64_t announced_at,
444 uint64_t effective_since,
445 uint64_t group_generation,
446 GNUNET_PSYCSTORE_ResultCallback rcb,
453 struct GNUNET_PSYCSTORE_OperationHandle *
454 GNUNET_PSYCSTORE_membership_test (
455 struct GNUNET_PSYCSTORE_Handle *h,
456 const struct GNUNET_CRYPTO_EccPublicKey *channel_key,
457 const struct GNUNET_CRYPTO_EccPublicKey *slave_key,
459 uint64_t group_generation,
460 GNUNET_PSYCSTORE_ResultCallback rcb,
466 /* end of psycstore_api.c */