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_container_lib.h"
30 #include "gnunet_client_lib.h"
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_lockmanager_service.h"
33 #include "gnunet_protocols.h"
35 #include "lockmanager.h"
37 #define LOG(kind,...) \
38 GNUNET_log_from (kind, "lockmanager-api",__VA_ARGS__)
40 #define TIME_REL_MINS(min) \
41 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, min)
43 #define TIMEOUT TIME_REL_MINS(3)
46 * Handler for the lockmanager service
48 struct GNUNET_LOCKMANAGER_Handle
51 * The client connection to the service
53 struct GNUNET_CLIENT_Connection *conn;
58 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
63 * Structure for Locking Request
65 struct GNUNET_LOCKMANAGER_LockingRequest
68 * The handle associated with this request
70 struct GNUNET_LOCKMANAGER_Handle *handle;
75 GNUNET_LOCKMANAGER_StatusCallback status_cb;
78 * Closure for the status callback
83 * The pending transmit handle for the ACQUIRE message
85 struct GNUNET_CLIENT_TransmitHandle *transmit_handle;
88 * The locking domain of this request
98 * The status of the lock
100 enum GNUNET_LOCKMANAGER_Status status;
105 * Get the key for the given lock in the 'lock_map'.
109 * @param key set to the key
112 get_key (const char *domain_name,
113 uint32_t lock_number,
114 struct GNUNET_HashCode *key)
118 GNUNET_CRYPTO_hash (domain_name,
119 strlen (domain_name),
121 last_32 = (uint32_t *) key;
122 *last_32 ^= lock_number;
127 * Function to find a LockingRequest associated with the given domain and lock
128 * attributes in the map
130 * @param map the map where the LockingRequests are stored
131 * @param domain the locking domain name
132 * @param lock the lock number
133 * @return the found LockingRequest; NULL if a matching LockingRequest wasn't
136 static struct GNUNET_LOCKMANAGER_LockingRequest *
137 hashmap_find_lockingrequest (const struct GNUNET_CONTAINER_MultiHashMap *map,
141 struct GNUNET_LOCKMANAGER_LockingRequest *lr;
142 struct GNUNET_HashCode hash;
145 int match_iterator (void *cls, const GNUNET_HashCode *key, void *value)
148 if ( (lock == lr->lock) && (0 == strcmp (domain, lr->domain)) )
150 match_found = GNUNET_YES;
155 get_key (domain, lock, &hash);
156 match_found = GNUNET_NO;
157 GNUNET_CONTAINER_multihashmap_get_multiple (map,
161 return (GNUNET_YES == match_found) ? lr : NULL;
166 * Task for calling status change callback for a lock
168 * @param cls the LockingRequest associated with this lock
169 * @param tc the TaskScheduler context
172 call_status_cb_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
174 const struct GNUNET_LOCKMANAGER_LockingRequest *r = cls;
176 if (NULL != r->status_cb)
178 LOG (GNUNET_ERROR_TYPE_DEBUG,
179 "Calling status change callback for lock: %d in domain: %s\n",
181 r->status_cb (r->status_cb_cls,
190 * Handler for server replies
192 * @param cls the LOCKMANAGER_Handle
193 * @param msg received message, NULL on timeout or fatal error
196 handle_replies (void *cls,
197 const struct GNUNET_MessageHeader *msg)
199 struct GNUNET_LOCKMANAGER_Handle *handle = cls;
200 const struct GNUNET_LOCKMANAGER_Message *m;
201 struct GNUNET_LOCKMANAGER_LockingRequest *lr;
203 struct GNUNET_HashCode hash;
209 LOG (GNUNET_ERROR_TYPE_DEBUG,
210 "Lockmanager service not available or went down\n");
213 if (GNUNET_MESSAGE_TYPE_LOCKMANAGER_SUCCESS != ntohs(msg->type))
218 msize = ntohs (msg->size);
219 if (msize <= sizeof (struct GNUNET_LOCKMANAGER_Message))
224 m = (const struct GNUNET_LOCKMANAGER_Message *) msg;
225 domain = (const char *) &m[1];
226 msize -= sizeof (struct GNUNET_LOCKMANAGER_Message);
227 if ('\0' != domain[msize-1])
233 lock = ntohl (m->lock);
234 get_key (domain, lock, &hash);
235 LOG (GNUNET_ERROR_TYPE_DEBUG,
236 "Received SUCCESS message for lock: %d, domain %s\n",
238 if (NULL == (lr = hashmap_find_lockingrequest (handle->hashmap,
245 if (GNUNET_LOCKMANAGER_SUCCESS == lr->status)
250 LOG (GNUNET_ERROR_TYPE_DEBUG,
251 "Changing status for lock: %d in domain: %s to SUCCESS\n",
252 lr->lock, lr->domain);
253 lr->status = GNUNET_LOCKMANAGER_SUCCESS;
254 GNUNET_SCHEDULER_add_continuation (&call_status_cb_task,
256 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
261 * Transmit notify for sending message to server
263 * @param cls the message to send
264 * @param size number of bytes available in buf
265 * @param buf where the callee should write the message
266 * @return number of bytes written to buf
269 transmit_notify (void *cls, size_t size, void *buf)
271 struct GNUNET_LOCKMANAGER_Message *msg = cls;
274 if ((0 == size) || (NULL == buf))
276 /* FIXME: Timed out -- requeue? */
279 msg_size = ntohs (msg->header.size);
280 GNUNET_assert (size >= msg_size);
281 memcpy (buf, msg, msg_size);
283 LOG (GNUNET_ERROR_TYPE_DEBUG,
284 "Message of size %u sent\n", msg_size);
290 * Iterator to free hash map entries.
293 * @param key current key code
294 * @param value the Locking request
295 * @return GNUNET_YES if we should continue to
300 free_iterator(void *cls,
301 const GNUNET_HashCode * key,
304 struct GNUNET_LOCKMANAGER_LockingRequest *r = value;
306 LOG (GNUNET_ERROR_TYPE_DEBUG,
307 "Clearing locking request\n");
308 GNUNET_free (r->domain);
314 /*******************/
315 /* API Definitions */
316 /*******************/
320 * Connect to the lockmanager service
322 * @param cfg the configuration to use
324 * @return upon success the handle to the service; NULL upon error
326 struct GNUNET_LOCKMANAGER_Handle *
327 GNUNET_LOCKMANAGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
329 struct GNUNET_LOCKMANAGER_Handle *h;
331 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
332 h = GNUNET_malloc (sizeof (struct GNUNET_LOCKMANAGER_Handle));
333 h->conn = GNUNET_CLIENT_connect ("lockmanager", cfg);
337 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
340 h->hashmap = GNUNET_CONTAINER_multihashmap_create (15);
341 GNUNET_assert (NULL != h->hashmap);
342 GNUNET_CLIENT_receive (h->conn,
345 GNUNET_TIME_UNIT_FOREVER_REL);
347 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
353 * Disconnect from the lockmanager service
355 * @param handle the handle to the lockmanager service
358 GNUNET_LOCKMANAGER_disconnect (struct GNUNET_LOCKMANAGER_Handle *handle)
360 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
361 GNUNET_CLIENT_disconnect (handle->conn);
362 if (0 != GNUNET_CONTAINER_multihashmap_size (handle->hashmap))
364 LOG (GNUNET_ERROR_TYPE_WARNING,
365 "Some locking requests are still present. Cancel them before "
366 "calling %s\n", __func__);
367 GNUNET_CONTAINER_multihashmap_iterate (handle->hashmap,
371 GNUNET_CONTAINER_multihashmap_destroy (handle->hashmap);
372 GNUNET_free (handle);
373 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
378 * Tries to acquire the given lock(even if the lock has been lost) until the
379 * request is called. If the lock is available the status_cb will be
380 * called. If the lock is busy then the request is queued and status_cb
381 * will be called when the lock has been made available and acquired by us.
383 * @param handle the handle to the lockmanager service
385 * @param domain_name name of the locking domain. Clients who want to share
386 * locks must use the same name for the locking domain. Also the
387 * domain_name should be selected with the prefix
388 * "GNUNET_<PROGRAM_NAME>_" to avoid domain name collisions.
391 * @param lock which lock to lock
393 * @param status_cb the callback for signalling when the lock is acquired and
396 * @param status_cb_cls the closure to the above callback
398 * @return the locking request handle for this request
400 struct GNUNET_LOCKMANAGER_LockingRequest *
401 GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle,
402 const char *domain_name,
404 GNUNET_LOCKMANAGER_StatusCallback
408 struct GNUNET_LOCKMANAGER_LockingRequest *r;
409 struct GNUNET_LOCKMANAGER_Message *msg;
410 struct GNUNET_HashCode hash;
412 size_t domain_name_length;
414 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
415 r = GNUNET_malloc (sizeof (struct GNUNET_LOCKMANAGER_LockingRequest));
416 domain_name_length = strlen (domain_name) + 1;
419 r->domain = GNUNET_malloc (domain_name_length);
420 r->status = GNUNET_LOCKMANAGER_RELEASE;
421 r->status_cb = status_cb;
422 r->status_cb_cls = status_cb_cls;
423 memcpy (r->domain, domain_name, domain_name_length);
424 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message) + domain_name_length;
425 msg = GNUNET_malloc (msg_size);
426 msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE);
427 msg->header.size = htons (msg_size);
428 msg->lock = htonl (lock);
429 memcpy (&msg[1], r->domain, domain_name_length);
430 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queueing ACQUIRE message\n");
432 GNUNET_CLIENT_notify_transmit_ready (r->handle->conn,
438 get_key (r->domain, r->lock, &hash);
439 GNUNET_CONTAINER_multihashmap_put (r->handle->hashmap,
442 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
443 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
450 * Function to cancel the locking request generated by
451 * GNUNET_LOCKMANAGER_acquire_lock. If the lock is acquired us then the lock is
452 * released. GNUNET_LOCKMANAGER_StatusCallback will not be called upon any
453 * status changes resulting due to this call.
455 * @param request the LockingRequest to cancel
458 GNUNET_LOCKMANAGER_cancel_request (struct GNUNET_LOCKMANAGER_LockingRequest
461 struct GNUNET_LOCKMANAGER_Message *msg;
462 struct GNUNET_HashCode hash;
464 size_t domain_name_length;
466 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
467 /* FIXME: Stop ACQUIRE retransmissions */
468 if (GNUNET_LOCKMANAGER_SUCCESS == request->status)
470 domain_name_length = strlen (request->domain) + 1;
471 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message)
472 + domain_name_length;
473 msg = GNUNET_malloc (msg_size);
474 msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE);
475 msg->header.size = htons (msg_size);
476 msg->lock = htonl (request->lock);
477 memcpy (&msg[1], request->domain, domain_name_length);
478 GNUNET_CLIENT_notify_transmit_ready (request->handle->conn,
480 TIMEOUT, /* What if this fails */
485 get_key (request->domain, request->lock, &hash);
486 GNUNET_assert (GNUNET_YES ==
487 GNUNET_CONTAINER_multihashmap_remove
488 (request->handle->hashmap, &hash, request));
489 GNUNET_free (request->domain);
490 GNUNET_free (request);
491 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);