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 *ac_head;
58 * Tail of transmission queue.
60 struct GNUNET_PEERSTORE_AddContext *ac_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 * Task scheduled to re-try connecting to the peerstore service.
145 * @param cls the 'struct GNUNET_PEERSTORE_Handle'
146 * @param tc scheduler context
149 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
151 struct GNUNET_PEERSTORE_Handle *h = cls;
153 h->r_task = GNUNET_SCHEDULER_NO_TASK;
158 * Connect to the PEERSTORE service.
160 * @return NULL on error
162 struct GNUNET_PEERSTORE_Handle *
163 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
165 struct GNUNET_CLIENT_Connection *client;
166 struct GNUNET_PEERSTORE_Handle *h;
168 client = GNUNET_CLIENT_connect ("peerstore", cfg);
171 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
178 * Disconnect from the PEERSTORE service
180 * @param h handle to disconnect
183 GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
185 if (NULL != h->client)
187 GNUNET_CLIENT_disconnect (h->client);
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 *ac = h->ac_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->ac_head, h->ac_tail, ac);
254 if (NULL != ac->cont)
255 ac->cont (ac->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, &ac[1], ret);
269 GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
270 trigger_transmit (h);
271 if (NULL != ac->cont)
272 ac->cont (ac->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 *ac;
288 if (NULL == (ac = h->ac_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, ac->size,
300 GNUNET_TIME_UNIT_FOREVER_REL,
305 /******************************************************************************/
306 /******************* ADD FUNCTIONS *********************/
307 /******************************************************************************/
309 struct GNUNET_PEERSTORE_AddContext *
310 GNUNET_PEERSTORE_add (struct GNUNET_PEERSTORE_Handle *h,
311 const struct GNUNET_PeerIdentity *peer,
312 const char *sub_system,
315 struct GNUNET_TIME_Relative lifetime,
316 GNUNET_PEERSTORE_Continuation cont,
319 struct GNUNET_PEERSTORE_AddContext *ac;
320 struct AddEntryMessage *entry;
323 size_t sub_system_size;
326 LOG (GNUNET_ERROR_TYPE_DEBUG,
327 "Storing value (size: %lu) for subsytem `%s' and peer `%s'",
328 size, sub_system, GNUNET_i2s (peer));
329 sub_system_size = strlen(sub_system);
330 request_size = sizeof(struct AddEntryMessage) + sub_system_size + size;
331 ac = GNUNET_malloc(sizeof(struct GNUNET_PEERSTORE_AddContext) + request_size);
333 ac->size = request_size;
334 entry = (struct AddEntryMessage *)&ac[1];
335 entry->header.size = htons(request_size);
336 entry->header.type = htons(GNUNET_MESSAGE_TYPE_PEERSTORE_ADD);
338 entry->sub_system_size = sub_system_size;
339 entry->value_size = size;
340 entry->lifetime = lifetime;
341 ss = (char *)&entry[1];
342 memcpy(ss, sub_system, sub_system_size);
343 val = ss + sub_system_size;
344 memcpy(val, value, size);
345 GNUNET_CONTAINER_DLL_insert_tail(h->ac_head, h->ac_tail, ac);
346 trigger_transmit (h);
352 /* end of peerstore_api.c */