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 * Should the handle be freed when the connection to service is lost?
30 * Should cancel_request have a call back (else simultaneous calls break)
34 #include "gnunet_common.h"
35 #include "gnunet_container_lib.h"
36 #include "gnunet_client_lib.h"
37 #include "gnunet_crypto_lib.h"
38 #include "gnunet_lockmanager_service.h"
39 #include "gnunet_protocols.h"
41 #include "lockmanager.h"
43 #define LOG(kind,...) \
44 GNUNET_log_from (kind, "lockmanager-api",__VA_ARGS__)
46 #define TIME_REL_MINS(min) \
47 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, min)
49 #define TIMEOUT TIME_REL_MINS(3)
58 * The next pointer for doubly linked list
60 struct MessageQueue *next;
63 * The prev pointer for doubly linked list
65 struct MessageQueue *prev;
68 * The LOCKMANAGER Message
70 struct GNUNET_LOCKMANAGER_Message *msg;
75 * Handler for the lockmanager service
77 struct GNUNET_LOCKMANAGER_Handle
80 * The client connection to the service
82 struct GNUNET_CLIENT_Connection *conn;
85 * The transmit handle for transmissions using conn
87 struct GNUNET_CLIENT_TransmitHandle *transmit_handle;
92 struct GNUNET_CONTAINER_MultiHashMap *hashmap;
95 * Double linked list head for message queue
97 struct MessageQueue *mq_head;
100 * Double linked list tail for message queue
102 struct MessageQueue *mq_tail;
105 * Are we currently handling replies?
112 * Structure for Locking Request
114 struct GNUNET_LOCKMANAGER_LockingRequest
117 * The handle associated with this request
119 struct GNUNET_LOCKMANAGER_Handle *handle;
122 * The status callback
124 GNUNET_LOCKMANAGER_StatusCallback status_cb;
127 * Closure for the status callback
132 * The locking domain of this request
142 * The status of the lock
144 enum GNUNET_LOCKMANAGER_Status status;
149 * Structure for matching a lock
151 struct LockingRequestMatch
154 * The matched LockingRequest entry; Should be NULL if no entry is found
156 struct GNUNET_LOCKMANAGER_LockingRequest *matched_entry;
159 * The locking domain name of the lock
171 * Handler for server replies
173 * @param cls the LOCKMANAGER_Handle
174 * @param msg received message, NULL on timeout or fatal error
177 handle_replies (void *cls,
178 const struct GNUNET_MessageHeader *msg);
182 * Transmit notify for sending message to server
184 * @param cls the lockmanager handle
185 * @param size number of bytes available in buf
186 * @param buf where the callee should write the message
187 * @return number of bytes written to buf
190 transmit_notify (void *cls, size_t size, void *buf)
192 struct GNUNET_LOCKMANAGER_Handle *handle = cls;
193 struct MessageQueue *queue_entity;
196 handle->transmit_handle = NULL;
197 queue_entity = handle->mq_head;
198 GNUNET_assert (NULL != queue_entity);
199 if ((0 == size) || (NULL == buf))
201 handle->transmit_handle =
202 GNUNET_CLIENT_notify_transmit_ready (handle->conn,
204 (queue_entity->msg->header.size),
205 GNUNET_TIME_UNIT_FOREVER_REL,
211 msg_size = ntohs (queue_entity->msg->header.size);
212 GNUNET_assert (size >= msg_size);
213 memcpy (buf, queue_entity->msg, msg_size);
214 LOG (GNUNET_ERROR_TYPE_DEBUG,
215 "Message of size %u sent\n", msg_size);
216 GNUNET_free (queue_entity->msg);
217 GNUNET_CONTAINER_DLL_remove (handle->mq_head,
220 GNUNET_free (queue_entity);
221 queue_entity = handle->mq_head;
222 if (NULL != queue_entity)
224 handle->transmit_handle =
225 GNUNET_CLIENT_notify_transmit_ready (handle->conn,
227 (queue_entity->msg->header.size),
233 if (GNUNET_NO == handle->in_replies)
235 GNUNET_CLIENT_receive (handle->conn,
238 GNUNET_TIME_UNIT_FOREVER_REL);
239 handle->in_replies = GNUNET_YES;
246 * Queues a message into handle's send message queue
248 * @param handle the lockmanager handle whose queue will be used
249 * @param msg the message to be queued
252 queue_message (struct GNUNET_LOCKMANAGER_Handle *handle,
253 struct GNUNET_LOCKMANAGER_Message *msg)
255 struct MessageQueue *queue_entity;
257 GNUNET_assert (NULL != msg);
258 queue_entity = GNUNET_malloc (sizeof (struct MessageQueue));
259 queue_entity->msg = msg;
260 GNUNET_CONTAINER_DLL_insert_tail (handle->mq_head,
263 if (NULL == handle->transmit_handle)
265 handle->transmit_handle =
266 GNUNET_CLIENT_notify_transmit_ready (handle->conn,
267 ntohs (msg->header.size),
277 * Get the key for the given lock in the 'lock_map'.
281 * @param key set to the key
284 get_key (const char *domain_name,
285 uint32_t lock_number,
286 struct GNUNET_HashCode *key)
290 GNUNET_CRYPTO_hash (domain_name,
291 strlen (domain_name),
293 last_32 = (uint32_t *) key;
294 *last_32 ^= lock_number;
299 * Hashmap iterator for matching a LockingRequest
301 * @param cls the LockingRequestMatch structure
302 * @param key current key code
303 * @param value value in the hash map (struct GNUNET_LOCKMANAGER_LockingRequest)
304 * @return GNUNET_YES if we should continue to
309 match_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
311 struct LockingRequestMatch *match = cls;
312 struct GNUNET_LOCKMANAGER_LockingRequest *lr = value;
314 if ( (match->lock == lr->lock) && (0 == strcmp (match->domain, lr->domain)) )
316 match->matched_entry = lr;
324 * Function to find a LockingRequest associated with the given domain and lock
325 * attributes in the map
327 * @param map the map where the LockingRequests are stored
328 * @param domain the locking domain name
329 * @param lock the lock number
330 * @return the found LockingRequest; NULL if a matching LockingRequest wasn't
333 static struct GNUNET_LOCKMANAGER_LockingRequest *
334 hashmap_find_lockingrequest (const struct GNUNET_CONTAINER_MultiHashMap *map,
338 struct GNUNET_HashCode hash;
339 struct LockingRequestMatch lock_match;
341 lock_match.matched_entry = NULL;
342 lock_match.domain = domain;
343 lock_match.lock = lock;
344 get_key (domain, lock, &hash);
345 GNUNET_CONTAINER_multihashmap_get_multiple (map,
349 return lock_match.matched_entry;
354 * Task for calling status change callback for a lock
356 * @param cls the LockingRequest associated with this lock
357 * @param tc the TaskScheduler context
360 call_status_cb_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
362 const struct GNUNET_LOCKMANAGER_LockingRequest *r = cls;
364 if (NULL != r->status_cb)
366 LOG (GNUNET_ERROR_TYPE_DEBUG,
367 "Calling status change for SUCCESS on lock num: %d, domain: %s\n",
369 r->status_cb (r->status_cb_cls,
378 * Function to generate acquire message for a lock
380 * @param domain_name the domain name of the lock
381 * @param lock the lock number
382 * @return the generated GNUNET_LOCKMANAGER_Message
384 static struct GNUNET_LOCKMANAGER_Message *
385 generate_acquire_msg (const char *domain_name, uint32_t lock)
387 struct GNUNET_LOCKMANAGER_Message *msg;
388 size_t domain_name_len;
391 domain_name_len = strlen (domain_name) + 1;
392 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message) + domain_name_len;
393 msg = GNUNET_malloc (msg_size);
394 msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE);
395 msg->header.size = htons (msg_size);
396 msg->lock = htonl (lock);
397 memcpy (&msg[1], domain_name, domain_name_len);
403 * Iterator to call relase on locks
405 * @param cls the lockmanager handle
406 * @param key current key code
407 * @param value the Locking request
408 * @return GNUNET_YES if we should continue to
413 release_n_retry_iterator (void *cls,
414 const struct GNUNET_HashCode * key,
417 struct GNUNET_LOCKMANAGER_LockingRequest *r = value;
418 struct GNUNET_LOCKMANAGER_Handle *h = cls;
419 struct GNUNET_LOCKMANAGER_Message *msg;
421 msg = generate_acquire_msg (r->domain, r->lock);
422 queue_message (h, msg);
423 if (GNUNET_LOCKMANAGER_RELEASE == r->status)
425 if (NULL != r->status_cb)
427 LOG (GNUNET_ERROR_TYPE_DEBUG,
428 "Calling status change for RELEASE on lock num: %d, domain: %s\n",
430 r->status = GNUNET_LOCKMANAGER_RELEASE;
431 r->status_cb (r->status_cb_cls,
434 GNUNET_LOCKMANAGER_RELEASE);
441 * Handler for server replies
443 * @param cls the LOCKMANAGER_Handle
444 * @param msg received message, NULL on timeout or fatal error
447 handle_replies (void *cls,
448 const struct GNUNET_MessageHeader *msg)
450 struct GNUNET_LOCKMANAGER_Handle *handle = cls;
451 const struct GNUNET_LOCKMANAGER_Message *m;
452 struct GNUNET_LOCKMANAGER_LockingRequest *lr;
454 struct GNUNET_HashCode hash;
458 handle->in_replies = GNUNET_NO;
461 LOG (GNUNET_ERROR_TYPE_DEBUG,
462 "Lockmanager service not available or went down\n");
463 /* Should release all locks and retry to acquire them */
464 GNUNET_CONTAINER_multihashmap_iterate (handle->hashmap,
465 &release_n_retry_iterator,
469 GNUNET_CLIENT_receive (handle->conn,
472 GNUNET_TIME_UNIT_FOREVER_REL);
473 handle->in_replies = GNUNET_YES;
474 if (GNUNET_MESSAGE_TYPE_LOCKMANAGER_SUCCESS != ntohs(msg->type))
479 msize = ntohs (msg->size);
480 if (msize <= sizeof (struct GNUNET_LOCKMANAGER_Message))
485 m = (const struct GNUNET_LOCKMANAGER_Message *) msg;
486 domain = (const char *) &m[1];
487 msize -= sizeof (struct GNUNET_LOCKMANAGER_Message);
488 if ('\0' != domain[msize-1])
494 lock = ntohl (m->lock);
495 get_key (domain, lock, &hash);
496 LOG (GNUNET_ERROR_TYPE_DEBUG,
497 "Received SUCCESS message for lock: %d, domain %s\n",
499 if (NULL == (lr = hashmap_find_lockingrequest (handle->hashmap,
506 if (GNUNET_LOCKMANAGER_SUCCESS == lr->status)
511 LOG (GNUNET_ERROR_TYPE_DEBUG,
512 "Changing status for lock: %d in domain: %s to SUCCESS\n",
513 lr->lock, lr->domain);
514 lr->status = GNUNET_LOCKMANAGER_SUCCESS;
515 GNUNET_SCHEDULER_add_continuation (&call_status_cb_task,
517 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
522 * Iterator to free hash map entries.
524 * @param cls the lockmanger handle
525 * @param key current key code
526 * @param value the Locking request
527 * @return GNUNET_YES if we should continue to
532 free_iterator(void *cls,
533 const struct GNUNET_HashCode * key,
536 struct GNUNET_LOCKMANAGER_Handle *h = cls;
537 struct GNUNET_LOCKMANAGER_LockingRequest *r = value;
539 LOG (GNUNET_ERROR_TYPE_DEBUG,
540 "Clearing locking request\n");
541 GNUNET_assert (GNUNET_YES ==
542 GNUNET_CONTAINER_multihashmap_remove (h->hashmap,
545 GNUNET_free (r->domain);
551 /*******************/
552 /* API Definitions */
553 /*******************/
557 * Connect to the lockmanager service
559 * @param cfg the configuration to use
561 * @return upon success the handle to the service; NULL upon error
563 struct GNUNET_LOCKMANAGER_Handle *
564 GNUNET_LOCKMANAGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
566 struct GNUNET_LOCKMANAGER_Handle *h;
568 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
569 h = GNUNET_malloc (sizeof (struct GNUNET_LOCKMANAGER_Handle));
570 h->conn = GNUNET_CLIENT_connect ("lockmanager", cfg);
574 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
577 h->hashmap = GNUNET_CONTAINER_multihashmap_create (15);
578 GNUNET_assert (NULL != h->hashmap);
579 GNUNET_CLIENT_receive (h->conn,
582 GNUNET_TIME_UNIT_FOREVER_REL);
583 h->in_replies = GNUNET_YES;
584 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
590 * Disconnect from the lockmanager service
592 * @param handle the handle to the lockmanager service
595 GNUNET_LOCKMANAGER_disconnect (struct GNUNET_LOCKMANAGER_Handle *handle)
597 struct MessageQueue *head;
599 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
600 if (0 != GNUNET_CONTAINER_multihashmap_size (handle->hashmap))
602 LOG (GNUNET_ERROR_TYPE_WARNING,
603 "Some locking requests are still present. Cancel them before "
604 "calling %s\n", __func__);
605 GNUNET_CONTAINER_multihashmap_iterate (handle->hashmap,
609 GNUNET_CONTAINER_multihashmap_destroy (handle->hashmap);
610 /* Clear the message queue */
611 if (NULL != handle->transmit_handle)
613 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->transmit_handle);
615 head = handle->mq_head;
618 GNUNET_CONTAINER_DLL_remove (handle->mq_head,
621 GNUNET_free (head->msg);
623 head = handle->mq_head;
625 GNUNET_CLIENT_disconnect (handle->conn);
626 GNUNET_free (handle);
627 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
632 * Tries to acquire the given lock(even if the lock has been lost) until the
633 * request is called. If the lock is available the status_cb will be
634 * called. If the lock is busy then the request is queued and status_cb
635 * will be called when the lock has been made available and acquired by us.
637 * @param handle the handle to the lockmanager service
639 * @param domain_name name of the locking domain. Clients who want to share
640 * locks must use the same name for the locking domain. Also the
641 * domain_name should be selected with the prefix
642 * "GNUNET_<PROGRAM_NAME>_" to avoid domain name collisions.
645 * @param lock which lock to lock
647 * @param status_cb the callback for signalling when the lock is acquired and
650 * @param status_cb_cls the closure to the above callback
652 * @return the locking request handle for this request
654 struct GNUNET_LOCKMANAGER_LockingRequest *
655 GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle,
656 const char *domain_name,
658 GNUNET_LOCKMANAGER_StatusCallback
662 struct GNUNET_LOCKMANAGER_LockingRequest *r;
663 struct GNUNET_LOCKMANAGER_Message *msg;
664 struct GNUNET_HashCode hash;
665 size_t domain_name_length;
667 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
668 r = GNUNET_malloc (sizeof (struct GNUNET_LOCKMANAGER_LockingRequest));
669 domain_name_length = strlen (domain_name) + 1;
672 r->domain = GNUNET_malloc (domain_name_length);
673 r->status = GNUNET_LOCKMANAGER_RELEASE;
674 r->status_cb = status_cb;
675 r->status_cb_cls = status_cb_cls;
676 memcpy (r->domain, domain_name, domain_name_length);
677 msg = generate_acquire_msg (r->domain, r->lock);
678 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queueing ACQUIRE message\n");
679 queue_message (handle, msg);
680 get_key (r->domain, r->lock, &hash);
681 GNUNET_CONTAINER_multihashmap_put (r->handle->hashmap,
684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
685 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
692 * Function to cancel the locking request generated by
693 * GNUNET_LOCKMANAGER_acquire_lock. If the lock is acquired by us then the lock
694 * is released. GNUNET_LOCKMANAGER_StatusCallback will not be called upon any
695 * status changes resulting due to this call.
697 * @param request the LockingRequest to cancel
700 GNUNET_LOCKMANAGER_cancel_request (struct GNUNET_LOCKMANAGER_LockingRequest
703 struct GNUNET_LOCKMANAGER_Message *msg;
704 struct GNUNET_HashCode hash;
706 size_t domain_name_length;
708 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
709 /* FIXME: Stop ACQUIRE retransmissions */
710 if (GNUNET_LOCKMANAGER_SUCCESS == request->status)
712 domain_name_length = strlen (request->domain) + 1;
713 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message)
714 + domain_name_length;
715 msg = GNUNET_malloc (msg_size);
716 msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE);
717 msg->header.size = htons (msg_size);
718 msg->lock = htonl (request->lock);
719 memcpy (&msg[1], request->domain, domain_name_length);
720 queue_message (request->handle, msg);
722 get_key (request->domain, request->lock, &hash);
723 GNUNET_assert (GNUNET_YES ==
724 GNUNET_CONTAINER_multihashmap_remove
725 (request->handle->hashmap, &hash, request));
726 GNUNET_free (request->domain);
727 GNUNET_free (request);
728 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);