2 This file is part of GNUnet.
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 peerstore/peerstore_api.c
23 * @brief API for peerstore
24 * @author Omar Tarabai
27 #include "gnunet_util_lib.h"
28 #include "peerstore.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "peerstore-api",__VA_ARGS__)
32 /******************************************************************************/
33 /************************ DATA STRUCTURES ****************************/
34 /******************************************************************************/
37 * Handle to the peerstore service.
39 struct GNUNET_PEERSTORE_Handle
45 const struct GNUNET_CONFIGURATION_Handle *cfg;
48 * Connection to the service.
50 struct GNUNET_CLIENT_Connection *client;
53 * Head of transmission queue.
55 struct GNUNET_PEERSTORE_AddContext *rc_head;
58 * Tail of transmission queue.
60 struct GNUNET_PEERSTORE_AddContext *rc_tail;
63 * Handle for the current transmission request, or NULL if none is pending.
65 struct GNUNET_CLIENT_TransmitHandle *th;
68 * ID for a reconnect task.
70 GNUNET_SCHEDULER_TaskIdentifier r_task;
73 * Are we now receiving?
80 * Entry in the transmission queue to PEERSTORE service.
83 struct GNUNET_PEERSTORE_AddContext
86 * This is a linked list.
88 struct GNUNET_PEERSTORE_AddContext *next;
91 * This is a linked list.
93 struct GNUNET_PEERSTORE_AddContext *prev;
96 * Handle to the PEERSTORE service.
98 struct GNUNET_PEERSTORE_Handle *h;
101 * Function to call after request has been transmitted, or NULL.
103 GNUNET_PEERSTORE_Continuation cont;
106 * Closure for 'cont'.
111 * Number of bytes of the request message (follows after this struct).
117 /******************************************************************************/
118 /*********************** DECLARATIONS *************************/
119 /******************************************************************************/
122 * Close the existing connection to PEERSTORE and reconnect.
124 * @param h handle to the service
127 reconnect (struct GNUNET_PEERSTORE_Handle *h);
130 * Check if we have a request pending in the transmission queue and are
131 * able to transmit it right now. If so, schedule transmission.
133 * @param h handle to the service
136 trigger_transmit (struct GNUNET_PEERSTORE_Handle *h);
138 /******************************************************************************/
139 /******************* CONNECTION FUNCTIONS *********************/
140 /******************************************************************************/
143 * Connect to the PEERSTORE service.
145 * @return NULL on error
147 struct GNUNET_PEERSTORE_Handle *
148 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
150 struct GNUNET_CLIENT_Connection *client;
151 struct GNUNET_PEERSTORE_Handle *h;
153 client = GNUNET_CLIENT_connect ("peerstore", cfg);
156 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
163 * Disconnect from the PEERSTORE service
165 * @param h handle to disconnect
168 GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
170 if (NULL != h->client)
172 GNUNET_CLIENT_disconnect (h->client);
179 * Task scheduled to re-try connecting to the PEERSTORE service.
181 * @param cls the 'struct GNUNET_PEERSTORE_Handle'
182 * @param tc scheduler context
185 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
187 struct GNUNET_PEERSTORE_Handle *h = cls;
189 h->r_task = GNUNET_SCHEDULER_NO_TASK;
194 * Close the existing connection to PEERSTORE and reconnect.
196 * @param h handle to the service
199 reconnect (struct GNUNET_PEERSTORE_Handle *h)
201 if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
203 GNUNET_SCHEDULER_cancel (h->r_task);
204 h->r_task = GNUNET_SCHEDULER_NO_TASK;
208 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
211 if (NULL != h->client)
213 GNUNET_CLIENT_disconnect (h->client);
216 h->in_receive = GNUNET_NO;
217 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
218 if (NULL == h->client)
221 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
225 trigger_transmit (h);
229 * Transmit the request at the head of the transmission queue
230 * and trigger continuation (if any).
232 * @param cls the 'struct GNUNET_PEERSTORE_Handle' (with the queue)
233 * @param size size of the buffer (0 on error)
234 * @param buf where to copy the message
235 * @return number of bytes copied to buf
238 do_transmit (void *cls, size_t size, void *buf)
240 struct GNUNET_PEERSTORE_Handle *h = cls;
241 struct GNUNET_PEERSTORE_AddContext *rc = h->rc_head;
246 return 0; /* request was cancelled in the meantime */
249 /* PEERSTORE service died */
250 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
251 "Failed to transmit message to `%s' service.\n", "PEERSTORE");
252 GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, rc);
254 if (NULL != rc->cont)
255 rc->cont (rc->cont_cls, _("failed to transmit request (service down?)"));
262 /* change in head of queue (i.e. cancel + add), try again */
263 trigger_transmit (h);
266 LOG (GNUNET_ERROR_TYPE_DEBUG,
267 "Transmitting request of size %u to `%s' service.\n", ret, "PEERSTORE");
268 memcpy (buf, &rc[1], ret);
269 GNUNET_CONTAINER_DLL_remove (h->rc_head, h->rc_tail, rc);
270 trigger_transmit (h);
271 if (NULL != rc->cont)
272 rc->cont (rc->cont_cls, NULL);
278 * Check if we have a request pending in the transmission queue and are
279 * able to transmit it right now. If so, schedule transmission.
281 * @param h handle to the service
284 trigger_transmit (struct GNUNET_PEERSTORE_Handle *h)
286 struct GNUNET_PEERSTORE_AddContext *rc;
288 if (NULL == (rc = h->rc_head))
289 return; /* no requests queued */
291 return; /* request already pending */
292 if (NULL == h->client)
294 /* disconnected, try to reconnect */
299 GNUNET_CLIENT_notify_transmit_ready (h->client, rc->size,
300 GNUNET_TIME_UNIT_FOREVER_REL,
305 /******************************************************************************/
306 /******************* STORE FUNCTIONS *********************/
307 /******************************************************************************/
311 /* end of peerstore_api.c */