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
29 #include "gnunet_common.h"
30 #include "gnunet_container_lib.h"
31 #include "gnunet_client_lib.h"
32 #include "gnunet_crypto_lib.h"
33 #include "gnunet_lockmanager_service.h"
34 #include "gnunet_protocols.h"
36 #include "lockmanager.h"
38 #define LOG(kind,...) \
39 GNUNET_log_from (kind, "lockmanager-api",__VA_ARGS__)
41 #define TIME_REL_MINS(min) \
42 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, min)
44 #define TIMEOUT TIME_REL_MINS(3)
53 * The next pointer for doubly linked list
55 struct MessageQueue *next;
58 * The prev pointer for doubly linked list
60 struct MessageQueue *prev;
63 * The LOCKMANAGER Message
65 struct GNUNET_LOCKMANAGER_Message *msg;
68 * If this is a AQUIRE_LOCK message, this is the
69 * affiliated locking request.
71 struct GNUNET_LOCKMANAGER_LockingRequest *lr;
76 * Handler for the lockmanager service
78 struct GNUNET_LOCKMANAGER_Handle
81 * The client connection to the service
83 struct GNUNET_CLIENT_Connection *conn;
86 * The transmit handle for transmissions using conn
88 struct GNUNET_CLIENT_TransmitHandle *transmit_handle;
93 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
96 * Double linked list head for message queue
98 struct MessageQueue *mq_head;
101 * Double linked list tail for message queue
103 struct MessageQueue *mq_tail;
106 * Are we currently handling replies?
113 * Structure for Locking Request
115 struct GNUNET_LOCKMANAGER_LockingRequest
118 * The handle associated with this request
120 struct GNUNET_LOCKMANAGER_Handle *handle;
123 * The status callback
125 GNUNET_LOCKMANAGER_StatusCallback status_cb;
128 * Entry in the request message queue for aquiring this
129 * lock; NULL after request has been sent.
131 struct MessageQueue *mqe;
134 * Closure for the status callback
139 * The locking domain of this request
149 * The status of the lock
151 enum GNUNET_LOCKMANAGER_Status status;
156 * Structure for matching a lock
158 struct LockingRequestMatch
161 * The matched LockingRequest entry; Should be NULL if no entry is found
163 struct GNUNET_LOCKMANAGER_LockingRequest *matched_entry;
166 * The locking domain name of the lock
178 * Handler for server replies
180 * @param cls the LOCKMANAGER_Handle
181 * @param msg received message, NULL on timeout or fatal error
184 handle_replies (void *cls,
185 const struct GNUNET_MessageHeader *msg);
189 * Transmit notify for sending message to server
191 * @param cls the lockmanager handle
192 * @param size number of bytes available in buf
193 * @param buf where the callee should write the message
194 * @return number of bytes written to buf
197 transmit_notify (void *cls, size_t size, void *buf)
199 struct GNUNET_LOCKMANAGER_Handle *handle = cls;
200 struct MessageQueue *queue_entity;
203 handle->transmit_handle = NULL;
204 queue_entity = handle->mq_head;
205 GNUNET_assert (NULL != queue_entity);
206 if ((0 == size) || (NULL == buf))
208 handle->transmit_handle =
209 GNUNET_CLIENT_notify_transmit_ready (handle->conn,
211 (queue_entity->msg->header.size),
212 GNUNET_TIME_UNIT_FOREVER_REL,
218 msg_size = ntohs (queue_entity->msg->header.size);
219 GNUNET_assert (size >= msg_size);
220 memcpy (buf, queue_entity->msg, msg_size);
221 LOG (GNUNET_ERROR_TYPE_DEBUG,
222 "Message of size %u sent\n", msg_size);
223 GNUNET_free (queue_entity->msg);
224 GNUNET_CONTAINER_DLL_remove (handle->mq_head,
227 GNUNET_free (queue_entity);
228 queue_entity = handle->mq_head;
229 if (NULL != queue_entity)
231 handle->transmit_handle =
232 GNUNET_CLIENT_notify_transmit_ready (handle->conn,
234 (queue_entity->msg->header.size),
240 if (GNUNET_NO == handle->in_replies)
242 handle->in_replies = GNUNET_YES;
243 GNUNET_CLIENT_receive (handle->conn,
246 GNUNET_TIME_UNIT_FOREVER_REL);
253 * Queues a message into handle's send message queue
255 * @param handle the lockmanager handle whose queue will be used
256 * @param msg the message to be queued
259 queue_message (struct GNUNET_LOCKMANAGER_Handle *handle,
260 struct GNUNET_LOCKMANAGER_Message *msg)
262 struct MessageQueue *queue_entity;
264 GNUNET_assert (NULL != msg);
265 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
266 queue_entity->msg = msg;
267 GNUNET_CONTAINER_DLL_insert_tail (handle->mq_head,
270 if (NULL == handle->transmit_handle)
272 handle->transmit_handle =
273 GNUNET_CLIENT_notify_transmit_ready (handle->conn,
274 ntohs (msg->header.size),
284 * Get the key for the given lock in the 'lock_map'.
288 * @param key set to the key
291 get_key (const char *domain_name,
292 uint32_t lock_number,
293 struct GNUNET_HashCode *key)
297 GNUNET_CRYPTO_hash (domain_name,
298 strlen (domain_name),
300 last_32 = (uint32_t *) key;
301 *last_32 ^= lock_number;
306 * Hashmap iterator for matching a LockingRequest
308 * @param cls the LockingRequestMatch structure
309 * @param key current key code
310 * @param value value in the hash map (struct GNUNET_LOCKMANAGER_LockingRequest)
311 * @return GNUNET_YES if we should continue to
316 match_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
318 struct LockingRequestMatch *match = cls;
319 struct GNUNET_LOCKMANAGER_LockingRequest *lr = value;
321 if ( (match->lock == lr->lock) && (0 == strcmp (match->domain, lr->domain)) )
323 match->matched_entry = lr;
331 * Function to find a LockingRequest associated with the given domain and lock
332 * attributes in the map
334 * @param map the map where the LockingRequests are stored
335 * @param domain the locking domain name
336 * @param lock the lock number
337 * @return the found LockingRequest; NULL if a matching LockingRequest wasn't
340 static struct GNUNET_LOCKMANAGER_LockingRequest *
341 hashmap_find_lockingrequest (const struct GNUNET_CONTAINER_MultiHashMap *map,
345 struct GNUNET_HashCode hash;
346 struct LockingRequestMatch lock_match;
348 lock_match.matched_entry = NULL;
349 lock_match.domain = domain;
350 lock_match.lock = lock;
351 get_key (domain, lock, &hash);
352 GNUNET_CONTAINER_multihashmap_get_multiple (map,
356 return lock_match.matched_entry;
361 * Task for calling status change callback for a lock
363 * @param cls the LockingRequest associated with this lock
364 * @param tc the TaskScheduler context
367 call_status_cb_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
369 const struct GNUNET_LOCKMANAGER_LockingRequest *r = cls;
371 if (NULL != r->status_cb)
373 LOG (GNUNET_ERROR_TYPE_DEBUG,
374 "Calling status change for SUCCESS on lock num: %d, domain: %s\n",
376 r->status_cb (r->status_cb_cls,
385 * Function to generate acquire message for a lock
387 * @param domain_name the domain name of the lock
388 * @param lock the lock number
389 * @return the generated GNUNET_LOCKMANAGER_Message
391 static struct GNUNET_LOCKMANAGER_Message *
392 generate_acquire_msg (const char *domain_name, uint32_t lock)
394 struct GNUNET_LOCKMANAGER_Message *msg;
395 size_t domain_name_len;
398 domain_name_len = strlen (domain_name) + 1;
399 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message) + domain_name_len;
400 msg = GNUNET_malloc (msg_size);
401 msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE);
402 msg->header.size = htons (msg_size);
403 msg->lock = htonl (lock);
404 memcpy (&msg[1], domain_name, domain_name_len);
410 * Iterator to call relase on locks
412 * @param cls the lockmanager handle
413 * @param key current key code
414 * @param value the Locking request
415 * @return GNUNET_YES if we should continue to
420 release_n_retry_iterator (void *cls,
421 const struct GNUNET_HashCode * key,
424 struct GNUNET_LOCKMANAGER_LockingRequest *r = value;
425 struct GNUNET_LOCKMANAGER_Handle *h = cls;
426 struct GNUNET_LOCKMANAGER_Message *msg;
428 msg = generate_acquire_msg (r->domain, r->lock);
429 queue_message (h, msg);
430 if (GNUNET_LOCKMANAGER_RELEASE == r->status)
432 if (NULL != r->status_cb)
434 LOG (GNUNET_ERROR_TYPE_DEBUG,
435 "Calling status change for RELEASE on lock num: %d, domain: %s\n",
437 r->status = GNUNET_LOCKMANAGER_RELEASE;
438 r->status_cb (r->status_cb_cls,
441 GNUNET_LOCKMANAGER_RELEASE);
448 * Handler for server replies
450 * @param cls the LOCKMANAGER_Handle
451 * @param msg received message, NULL on timeout or fatal error
454 handle_replies (void *cls,
455 const struct GNUNET_MessageHeader *msg)
457 struct GNUNET_LOCKMANAGER_Handle *handle = cls;
458 const struct GNUNET_LOCKMANAGER_Message *m;
459 struct GNUNET_LOCKMANAGER_LockingRequest *lr;
461 struct GNUNET_HashCode hash;
465 handle->in_replies = GNUNET_NO;
468 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 "Lockmanager service not available or went down\n");
470 /* Should release all locks and retry to acquire them */
471 GNUNET_CONTAINER_multihashmap_iterate (handle->hashmap,
472 &release_n_retry_iterator,
476 handle->in_replies = GNUNET_YES;
477 GNUNET_CLIENT_receive (handle->conn,
480 GNUNET_TIME_UNIT_FOREVER_REL);
481 if (GNUNET_MESSAGE_TYPE_LOCKMANAGER_SUCCESS != ntohs(msg->type))
486 msize = ntohs (msg->size);
487 if (msize <= sizeof (struct GNUNET_LOCKMANAGER_Message))
492 m = (const struct GNUNET_LOCKMANAGER_Message *) msg;
493 domain = (const char *) &m[1];
494 msize -= sizeof (struct GNUNET_LOCKMANAGER_Message);
495 if ('\0' != domain[msize-1])
501 lock = ntohl (m->lock);
502 get_key (domain, lock, &hash);
503 LOG (GNUNET_ERROR_TYPE_DEBUG,
504 "Received SUCCESS message for lock: %d, domain %s\n",
506 if (NULL == (lr = hashmap_find_lockingrequest (handle->hashmap,
513 if (GNUNET_LOCKMANAGER_SUCCESS == lr->status)
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
519 "Changing status for lock: %d in domain: %s to SUCCESS\n",
520 lr->lock, lr->domain);
521 lr->status = GNUNET_LOCKMANAGER_SUCCESS;
522 GNUNET_SCHEDULER_add_continuation (&call_status_cb_task,
524 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
529 * Iterator to free hash map entries.
531 * @param cls the lockmanger handle
532 * @param key current key code
533 * @param value the Locking request
534 * @return GNUNET_YES if we should continue to
539 free_iterator(void *cls,
540 const struct GNUNET_HashCode * key,
543 struct GNUNET_LOCKMANAGER_Handle *h = cls;
544 struct GNUNET_LOCKMANAGER_LockingRequest *r = value;
546 LOG (GNUNET_ERROR_TYPE_DEBUG,
547 "Clearing locking request\n");
548 GNUNET_assert (GNUNET_YES ==
549 GNUNET_CONTAINER_multihashmap_remove (h->hashmap,
552 GNUNET_free (r->domain);
558 /*******************/
559 /* API Definitions */
560 /*******************/
564 * Connect to the lockmanager service
566 * @param cfg the configuration to use
568 * @return upon success the handle to the service; NULL upon error
570 struct GNUNET_LOCKMANAGER_Handle *
571 GNUNET_LOCKMANAGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
573 struct GNUNET_LOCKMANAGER_Handle *h;
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
576 h = GNUNET_malloc (sizeof (struct GNUNET_LOCKMANAGER_Handle));
577 h->conn = GNUNET_CLIENT_connect ("lockmanager", cfg);
581 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
584 h->hashmap = GNUNET_CONTAINER_multihashmap_create (15);
585 GNUNET_assert (NULL != h->hashmap);
586 h->in_replies = GNUNET_YES;
587 GNUNET_CLIENT_receive (h->conn,
590 GNUNET_TIME_UNIT_FOREVER_REL);
591 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
597 * Disconnect from the lockmanager service
599 * @param handle the handle to the lockmanager service
602 GNUNET_LOCKMANAGER_disconnect (struct GNUNET_LOCKMANAGER_Handle *handle)
604 struct MessageQueue *head;
606 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
607 if (0 != GNUNET_CONTAINER_multihashmap_size (handle->hashmap))
609 LOG (GNUNET_ERROR_TYPE_WARNING,
610 "Some locking requests are still present. Cancel them before "
611 "calling %s\n", __func__);
612 GNUNET_CONTAINER_multihashmap_iterate (handle->hashmap,
616 GNUNET_CONTAINER_multihashmap_destroy (handle->hashmap);
617 /* Clear the message queue */
618 if (NULL != handle->transmit_handle)
620 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->transmit_handle);
622 head = handle->mq_head;
625 GNUNET_CONTAINER_DLL_remove (handle->mq_head,
628 GNUNET_free (head->msg);
630 head = handle->mq_head;
632 GNUNET_CLIENT_disconnect (handle->conn);
633 GNUNET_free (handle);
634 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
639 * Tries to acquire the given lock(even if the lock has been lost) until the
640 * request is called. If the lock is available the status_cb will be
641 * called. If the lock is busy then the request is queued and status_cb
642 * will be called when the lock has been made available and acquired by us.
644 * @param handle the handle to the lockmanager service
646 * @param domain_name name of the locking domain. Clients who want to share
647 * locks must use the same name for the locking domain. Also the
648 * domain_name should be selected with the prefix
649 * "GNUNET_<PROGRAM_NAME>_" to avoid domain name collisions.
652 * @param lock which lock to lock
654 * @param status_cb the callback for signalling when the lock is acquired and
657 * @param status_cb_cls the closure to the above callback
659 * @return the locking request handle for this request
661 struct GNUNET_LOCKMANAGER_LockingRequest *
662 GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle,
663 const char *domain_name,
665 GNUNET_LOCKMANAGER_StatusCallback
669 struct GNUNET_LOCKMANAGER_LockingRequest *r;
670 struct GNUNET_LOCKMANAGER_Message *msg;
671 struct GNUNET_HashCode hash;
672 size_t domain_name_length;
674 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
675 r = GNUNET_malloc (sizeof (struct GNUNET_LOCKMANAGER_LockingRequest));
676 domain_name_length = strlen (domain_name) + 1;
679 r->domain = GNUNET_malloc (domain_name_length);
680 r->status = GNUNET_LOCKMANAGER_RELEASE;
681 r->status_cb = status_cb;
682 r->status_cb_cls = status_cb_cls;
683 memcpy (r->domain, domain_name, domain_name_length);
684 msg = generate_acquire_msg (r->domain, r->lock);
685 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queueing ACQUIRE message\n");
686 queue_message (handle, msg);
687 get_key (r->domain, r->lock, &hash);
688 GNUNET_assert (GNUNET_OK ==
689 GNUNET_CONTAINER_multihashmap_put (r->handle->hashmap,
692 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
693 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
699 * Function to cancel the locking request generated by
700 * GNUNET_LOCKMANAGER_acquire_lock. If the lock is acquired by us then the lock
701 * is released. GNUNET_LOCKMANAGER_StatusCallback will not be called upon any
702 * status changes resulting due to this call.
704 * @param request the LockingRequest to cancel
707 GNUNET_LOCKMANAGER_cancel_request (struct GNUNET_LOCKMANAGER_LockingRequest
710 struct GNUNET_LOCKMANAGER_Message *msg;
711 struct GNUNET_HashCode hash;
713 size_t domain_name_length;
715 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
716 /* FIXME: Stop ACQUIRE retransmissions */
717 if (GNUNET_LOCKMANAGER_SUCCESS == request->status)
719 domain_name_length = strlen (request->domain) + 1;
720 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message)
721 + domain_name_length;
722 msg = GNUNET_malloc (msg_size);
723 msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE);
724 msg->header.size = htons (msg_size);
725 msg->lock = htonl (request->lock);
726 memcpy (&msg[1], request->domain, domain_name_length);
727 queue_message (request->handle, msg);
729 get_key (request->domain, request->lock, &hash);
730 GNUNET_assert (GNUNET_YES ==
731 GNUNET_CONTAINER_multihashmap_remove
732 (request->handle->hashmap, &hash, request));
733 GNUNET_free (request->domain);
734 GNUNET_free (request);
735 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);