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"
29 #include "peerstore_common.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "peerstore-api",__VA_ARGS__)
33 /******************************************************************************/
34 /************************ DATA STRUCTURES ****************************/
35 /******************************************************************************/
38 * Handle to the PEERSTORE service.
40 struct GNUNET_PEERSTORE_Handle
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Connection to the service.
51 struct GNUNET_CLIENT_Connection *client;
56 * Context for a store request
58 struct GNUNET_PEERSTORE_StoreContext
62 * Handle to the PEERSTORE service.
64 struct GNUNET_PEERSTORE_Handle *h;
67 * Continuation called with service response
69 GNUNET_PEERSTORE_Continuation cont;
78 /******************************************************************************/
79 /******************* CONNECTION FUNCTIONS *********************/
80 /******************************************************************************/
83 * Close the existing connection to PEERSTORE and reconnect.
85 * @param h handle to the service
88 reconnect (struct GNUNET_PEERSTORE_Handle *h)
91 LOG(GNUNET_ERROR_TYPE_DEBUG, "Reconnecting...\n");
92 if (NULL != h->client)
94 GNUNET_CLIENT_disconnect (h->client);
97 h->client = GNUNET_CLIENT_connect ("peerstore", h->cfg);
102 * Connect to the PEERSTORE service.
104 * @return NULL on error
106 struct GNUNET_PEERSTORE_Handle *
107 GNUNET_PEERSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
109 struct GNUNET_CLIENT_Connection *client;
110 struct GNUNET_PEERSTORE_Handle *h;
112 client = GNUNET_CLIENT_connect ("peerstore", cfg);
115 h = GNUNET_new (struct GNUNET_PEERSTORE_Handle);
118 LOG(GNUNET_ERROR_TYPE_DEBUG, "New connection created\n");
123 * Disconnect from the PEERSTORE service
124 * Do not call in case of pending requests
126 * @param h handle to disconnect
129 GNUNET_PEERSTORE_disconnect(struct GNUNET_PEERSTORE_Handle *h)
131 if (NULL != h->client)
133 GNUNET_CLIENT_disconnect (h->client);
137 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnected, BYE!\n");
141 /******************************************************************************/
142 /******************* ADD FUNCTIONS *********************/
143 /******************************************************************************/
146 * When a response for store request is received
148 * @param cls a 'struct GNUNET_PEERSTORE_StoreContext *'
149 * @param msg message received, NULL on timeout or fatal error
151 void store_response_receiver (void *cls, const struct GNUNET_MessageHeader *msg)
153 struct GNUNET_PEERSTORE_StoreContext *sc = cls;
160 sc->cont(sc->cont_cls, GNUNET_SYSERR);
164 msg_type = ntohs(msg->type);
165 if(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_OK == msg_type)
166 sc->cont(sc->cont_cls, GNUNET_OK);
167 else if(GNUNET_MESSAGE_TYPE_PEERSTORE_STORE_RESULT_FAIL == msg_type)
168 sc->cont(sc->cont_cls, GNUNET_SYSERR);
171 LOG(GNUNET_ERROR_TYPE_ERROR, "Invalid response from `PEERSTORE' service.\n");
172 sc->cont(sc->cont_cls, GNUNET_SYSERR);
178 * Cancel a store request
180 * @param sc Store request context
183 GNUNET_PEERSTORE_store_cancel (struct GNUNET_PEERSTORE_StoreContext *sc)
189 * Store a new entry in the PEERSTORE
191 * @param h Handle to the PEERSTORE service
192 * @param sub_system name of the sub system
193 * @param peer Peer Identity
194 * @param key entry key
195 * @param value entry value BLOB
196 * @param size size of 'value'
197 * @param lifetime relative time after which the entry is (possibly) deleted
198 * @param cont Continuation function after the store request is processed
199 * @param cont_cls Closure for 'cont'
201 struct GNUNET_PEERSTORE_StoreContext *
202 GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
203 const char *sub_system,
204 const struct GNUNET_PeerIdentity *peer,
208 struct GNUNET_TIME_Relative lifetime,
209 GNUNET_PEERSTORE_Continuation cont,
212 struct GNUNET_PEERSTORE_StoreContext *sc;
213 struct StoreRecordMessage *srm;
215 LOG (GNUNET_ERROR_TYPE_DEBUG,
216 "Storing value (size: %lu) for subsytem `%s', peer `%s', key `%s'\n",
217 size, sub_system, GNUNET_i2s (peer), key);
218 sc = GNUNET_new(struct GNUNET_PEERSTORE_StoreContext);
220 sc->cont_cls = cont_cls;
222 srm = PEERSTORE_create_record_message(sub_system,
228 GNUNET_CLIENT_transmit_and_get_response(h->client,
229 (const struct GNUNET_MessageHeader *)srm,
230 GNUNET_TIME_UNIT_FOREVER_REL,
232 &store_response_receiver,
239 /* end of peerstore_api.c */