2 This file is part of GNUnet.
3 (C) 2012 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 License as published
7 by the Free Software Foundation; either version 2, 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 lockmanager/lockmanager_api.c
23 * @brief API implementation of gnunet_lockmanager_service.h
24 * @author Sree Harsha Totakura
28 #include "gnunet_common.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_client_lib.h"
31 #include "gnunet_lockmanager_service.h"
33 #include "lockmanager.h"
35 #define LOG(kind,...) \
36 GNUNET_log_from (kind, "lockmanager-api",__VA_ARGS__)
38 #define TIME_REL_MINS(min) \
39 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, min)
41 #define TIMEOUT TIME_REL_MINS(3)
44 * Handler for the lockmanager service
46 struct GNUNET_LOCKMANAGER_Handle
49 * The client connection to the service
51 struct GNUNET_CLIENT_Connection *conn;
56 * Structure for Locking Request
58 struct GNUNET_LOCKMANAGER_LockingRequest
61 * The handle associated with this request
63 struct GNUNET_LOCKMANAGER_Handle *handle;
68 GNUNET_LOCKMANAGER_StatusCallback status_cb;
71 * Closure for the status callback
76 * The pending transmit handle for the ACQUIRE message
78 struct GNUNET_CLIENT_TransmitHandle *transmit_handle;
81 * The locking domain of this request
91 * The status of the lock
93 enum GNUNET_LOCKMANAGER_Status status;
96 * The length of the locking domain string including the trailing NULL
98 uint16_t domain_name_length;
103 * Message handler for SUCCESS messages
105 * @param cls the LOCKMANAGER_Handle
106 * @param msg message received, NULL on timeout or fatal error
109 handle_success (void *cls,
110 const struct GNUNET_MessageHeader *msg)
115 LOG (GNUNET_ERROR_TYPE_DEBUG,
116 "Received SUCCESS message\n");
121 * We wait for DUMMY message which will never be sent by the server. However,
122 * in case the server shuts-down/crashes/restarts we are notified by this call
123 * back with a NULL for msg.
126 * @param msg message received, NULL on timeout or fatal error
129 handle_server_crash (void *cls,
130 const struct GNUNET_MessageHeader *msg)
132 LOG (GNUNET_ERROR_TYPE_DEBUG,
133 "Lockmanager service not available or went down\n");
139 * Transmit notify for sending message to server
141 * @param cls the message to send
142 * @param size number of bytes available in buf
143 * @param buf where the callee should write the message
144 * @return number of bytes written to buf
147 transmit_notify (void *cls, size_t size, void *buf)
149 struct GNUNET_LOCKMANAGER_Message *msg = cls;
152 if ((0 == size) || (NULL == buf))
154 /* FIXME: Timed out -- requeue? */
157 msg_size = ntohs (msg->header.size);
158 GNUNET_assert (size >= msg_size);
159 memcpy (buf, msg, msg_size);
166 /*******************/
167 /* API Definitions */
168 /*******************/
171 * Connect to the lockmanager service
173 * @param cfg the configuration to use
175 * @return upon success the handle to the service; NULL upon error
177 struct GNUNET_LOCKMANAGER_Handle *
178 GNUNET_LOCKMANAGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
180 struct GNUNET_LOCKMANAGER_Handle *h;
182 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
183 h = GNUNET_malloc (sizeof (struct GNUNET_LOCKMANAGER_Handle));
184 h->conn = GNUNET_CLIENT_connect ("lockmanager", cfg);
188 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
192 GNUNET_CLIENT_receive (h->conn,
193 &handle_server_crash,
195 GNUNET_TIME_UNIT_FOREVER_REL);
197 /* FIXME: Assertions fail in client.c if trying to receive multiple messages */
198 /* GNUNET_CLIENT_receive (h->conn, */
199 /* &handle_success, */
201 /* GNUNET_TIME_UNIT_FOREVER_REL); */
203 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
209 * Disconnect from the lockmanager service
211 * @param handle the handle to the lockmanager service
214 GNUNET_LOCKMANAGER_disconnect (struct GNUNET_LOCKMANAGER_Handle *handle)
216 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
217 GNUNET_CLIENT_disconnect (handle->conn);
218 GNUNET_free (handle);
219 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
224 * Tries to acquire the given lock(even if the lock has been lost) until the
225 * request is called. If the lock is available the status_cb will be
226 * called. If the lock is busy then the request is queued and status_cb
227 * will be called when the lock has been made available and acquired by us.
229 * @param handle the handle to the lockmanager service
231 * @param domain_name name of the locking domain. Clients who want to share
232 * locks must use the same name for the locking domain. Also the
233 * domain_name should be selected with the prefix
234 * "GNUNET_<PROGRAM_NAME>_" to avoid domain name collisions.
237 * @param lock which lock to lock
239 * @param status_cb the callback for signalling when the lock is acquired and
242 * @param status_cb_cls the closure to the above callback
244 * @return the locking request handle for this request. It will be invalidated
245 * when status_cb is called.
247 struct GNUNET_LOCKMANAGER_LockingRequest *
248 GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle,
249 const char *domain_name,
251 GNUNET_LOCKMANAGER_StatusCallback
255 struct GNUNET_LOCKMANAGER_LockingRequest *r;
256 struct GNUNET_LOCKMANAGER_Message *msg;
259 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
260 r = GNUNET_malloc (sizeof (struct GNUNET_LOCKMANAGER_LockingRequest));
261 r->domain_name_length = strlen (domain_name) + 1;
264 r->domain = GNUNET_malloc (r->domain_name_length);
265 memcpy (r->domain, domain_name, r->domain_name_length);
267 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message) + r->domain_name_length;
268 msg = GNUNET_malloc (msg_size);
269 msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE);
270 msg->header.size = htons (msg_size);
271 msg->lock = htonl (lock);
272 memcpy (&msg[1], r->domain, r->domain_name_length);
275 GNUNET_CLIENT_notify_transmit_ready (r->handle->conn,
281 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
288 * Function to cancel the locking request generated by
289 * GNUNET_LOCKMANAGER_acquire_lock. If the lock is acquired us then the lock is
290 * released. GNUNET_LOCKMANAGER_StatusCallback will not be called upon any
291 * status changes resulting due to this call.
293 * @param request the LockingRequest to cancel
296 GNUNET_LOCKMANAGER_cancel_request (struct GNUNET_LOCKMANAGER_LockingRequest
299 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
300 /* FIXME: Stop ACQUIRE retransmissions */
301 if (GNUNET_LOCKMANAGER_SUCCESS == request->status)
303 struct GNUNET_LOCKMANAGER_Message *msg;
306 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message)
307 + request->domain_name_length;
308 msg = GNUNET_malloc (msg_size);
309 msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE);
310 msg->header.size = htons (msg_size);
311 msg->lock = htonl (request->lock);
312 memcpy (&msg[1], request->domain, request->domain_name_length);
314 GNUNET_CLIENT_notify_transmit_ready (request->handle->conn,
316 TIMEOUT, /* What if this fails */
321 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
322 GNUNET_free (request);