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/gnunet-service-lockmanager.c
23 * @brief implementation of the LOCKMANAGER service
24 * @author Sree Harsha Totakura
28 #include "gnunet_common.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_service_lib.h"
32 #include "gnunet_server_lib.h"
34 #include "lockmanager.h"
36 #define VERBOSE GNUNET_YES
38 #define LOG(kind,...) \
39 GNUNET_log (kind, __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)
48 * Doubly linked list of clients waiting for a lock
53 * The next client structure
55 struct WaitList *next;
58 * The prev client structure
60 struct WaitList *prev;
63 * Pointer to the client
65 struct GNUNET_SERVER_Client *client;
70 * A Lock element for a doubly linked list
75 * The next element pointer
77 struct LockList *next;
80 * Pointer to the previous element
82 struct LockList *prev;
85 * List head of clients waiting for this lock
87 struct WaitList *wait_list_head;
90 * List tail of clients waiting for this lock
92 struct WaitList *wait_list_tail;
95 * The client which is currently holding this lock
97 struct GNUNET_SERVER_Client *client;
100 * The name of the locking domain this lock belongs to
105 * The number of this lock
112 * Doubly linked list of clients having connections to us
117 * The next client structure
119 struct ClientList *next;
122 * The previous client structure
124 struct ClientList *prev;
127 * Pointer to the client
129 struct GNUNET_SERVER_Client *client;
134 * Head of the doubly linked list of the currently held locks
136 static struct LockList *ll_head;
139 * Tail of the doubly linked list of the currently held locks
141 static struct LockList *ll_tail;
144 * Head of the doubly linked list of clients currently connected
146 static struct ClientList *cl_head;
149 * Tail of the doubly linked list of clients currently connected
151 static struct ClientList *cl_tail;
155 * Function to search for a lock in lock_list matching the given domain_name and
158 * @param domain_name the name of the locking domain
159 * @param lock_num the number of the lock
160 * @param ret this will be the pointer to the corresponding Lock if found; else
161 * it will be the last element in the locks list
162 * @return GNUNET_YES if a matching lock is present in lock_list; GNUNET_NO if not
165 ll_find_lock (const char *domain_name,
166 const uint32_t lock_num,
167 struct LockList **ret)
169 struct LockList *current_lock;
171 current_lock = ll_head;
173 while (NULL != current_lock)
175 if ( (0 == strcmp (domain_name, current_lock->domain_name))
176 && (lock_num == current_lock->lock_num))
183 current_lock = current_lock->next;
192 * Function to search for a lock in lock_list matching the given domain_name and
195 * @param client the client owning this lock currently
196 * @param ret this will be the pointer to the corresponding Lock if found; else
197 * it will be the last element in the locks list
198 * @return GNUNET_YES if a matching lock is present in lock_list; GNUNET_NO if not
201 ll_find_lock_by_owner (const struct GNUNET_SERVER_Client *client,
202 struct LockList **ret)
204 struct LockList *current_lock;
206 current_lock = ll_head;
208 while (NULL != current_lock)
210 if (client == current_lock->client)
217 current_lock = current_lock->next;
226 * Function to append a lock to the global lock list
228 * @param client the client which currently owns this lock
229 * @param domain_name the name of the locking domain
230 * @param lock_num the number of the lock
231 * @param tail the pointer to the tail of the global lock list
234 ll_add_lock (struct GNUNET_SERVER_Client *client,
235 const char *domain_name,
236 const uint32_t lock_num)
238 struct LockList *lock;
239 size_t domain_name_len;
241 LOG (GNUNET_ERROR_TYPE_DEBUG,
242 "Adding a lock with num: %u and domain: %s to lock list\n",
243 lock_num, domain_name);
245 lock = GNUNET_malloc (sizeof (struct LockList));
246 domain_name_len = strlen (domain_name) + 1;
247 lock->domain_name = GNUNET_malloc (domain_name_len);
248 strncpy (lock->domain_name, domain_name, domain_name_len);
249 lock->lock_num = lock_num;
250 lock->client = client;
252 GNUNET_CONTAINER_DLL_insert_tail (ll_head,
259 * Function to delete a lock from the lock list
261 * @param lock the lock to be deleted
264 ll_remove_lock (struct LockList *lock)
266 LOG (GNUNET_ERROR_TYPE_DEBUG,
267 "Removing lock with num: %u, domain: %s\n",
268 lock->lock_num, lock->domain_name);
269 GNUNET_assert (NULL != ll_head);
270 GNUNET_CONTAINER_DLL_remove (ll_head,
273 GNUNET_free (lock->domain_name);
279 * Find a client in the waiting list of a lock
281 * @param lock the LockList entry of a lock
282 * @param client the client to look for
283 * @param ret where to store the matched wait list entry
284 * @return GNUNET_YES if a match is found; GNUNET_NO if not
287 ll_wl_find_client (struct LockList *lock,
288 const struct GNUNET_SERVER_Client *client,
289 struct WaitList **ret)
291 struct WaitList *current_wl_entry;
293 current_wl_entry = lock->wait_list_head;
295 while (NULL != current_wl_entry)
297 if (client == current_wl_entry->client)
300 *ret = current_wl_entry;
303 current_wl_entry = current_wl_entry->next;
306 *ret = current_wl_entry;
312 * Add a client to the wait list of given lock
314 * @param lock the lock list entry of a lock
315 * @param client the client to queue for the lock's wait list
318 ll_wl_add_client (struct LockList *lock,
319 struct GNUNET_SERVER_Client *client)
321 struct WaitList *wl_entry;
323 LOG (GNUNET_ERROR_TYPE_DEBUG,
324 "Adding a client to lock's wait list\n"
325 "\t lock num: %u, domain: %s\n",
329 wl_entry = GNUNET_malloc (sizeof (struct WaitList));
330 wl_entry->client = client;
331 GNUNET_CONTAINER_DLL_insert_tail (lock->wait_list_head,
332 lock->wait_list_tail,
338 * Remove a client from the wait list of the given lock
340 * @param lock the lock list entry of the lock
341 * @param wl_client the wait list entry to be removed
344 ll_wl_remove_client (struct LockList *lock,
345 struct WaitList *wl_client)
347 GNUNET_CONTAINER_DLL_remove (lock->wait_list_head,
348 lock->wait_list_tail,
351 GNUNET_free (wl_client);
356 * Search for a client in the client list
358 * @param client the client to be searched for
359 * @param ret will be pointing to the matched list entry (if there is a match);
360 * else to the tail of the client list
361 * @return GNUNET_YES if the client is present; GNUNET_NO if not
364 cl_find_client (const struct GNUNET_SERVER_Client *client,
365 struct ClientList **ret)
367 struct ClientList *current;
371 while (NULL != current)
373 if (client == current->client)
380 current = current->next;
390 * Append a client to the client list
392 * @param client the client to be appended to the list
395 cl_add_client (struct GNUNET_SERVER_Client *client)
397 struct ClientList *new_client;
399 LOG (GNUNET_ERROR_TYPE_DEBUG,
400 "Adding a client to the client list\n");
402 new_client = GNUNET_malloc (sizeof (struct ClientList));
403 GNUNET_SERVER_client_keep (client);
404 new_client->client = client;
405 GNUNET_CONTAINER_DLL_insert_tail (cl_head,
412 * Delete the given client from the client list
414 * @param client the client list entry to delete
417 cl_remove_client (struct ClientList *client)
419 LOG (GNUNET_ERROR_TYPE_DEBUG,
420 "Removing a client from the client list\n");
421 GNUNET_SERVER_client_drop (client->client);
422 GNUNET_CONTAINER_DLL_remove (cl_head,
425 GNUNET_free (client);
430 * Transmit notify for sending message to client
432 * @param cls the message to send
433 * @param size number of bytes available in buf
434 * @param buf where the callee should write the message
435 * @return number of bytes written to buf
438 transmit_notify (void *cls, size_t size, void *buf)
440 struct GNUNET_LOCKMANAGER_Message *msg = cls;
443 if ((0 == size) || (NULL == buf))
445 /* FIXME: Timed out -- requeue? */
448 msg_size = ntohs (msg->header.size);
449 GNUNET_assert (size >= msg_size);
450 memcpy (buf, msg, msg_size);
452 LOG (GNUNET_ERROR_TYPE_DEBUG,
453 "Message of size %u sent\n", msg_size);
459 * Send SUCCESS message to the client
461 * @param client the client to which the message has to be sent
462 * @param domain_name the locking domain of the successfully acquried lock
463 * @param lock_num the number of the successfully acquired lock
466 send_success_msg (struct GNUNET_SERVER_Client *client,
467 const char *domain_name,
470 struct GNUNET_LOCKMANAGER_Message *reply;
471 size_t domain_name_len;
474 domain_name_len = strlen (domain_name) + 1;
475 reply_size = sizeof (struct GNUNET_LOCKMANAGER_Message) + domain_name_len;
476 reply = GNUNET_malloc (reply_size);
477 reply->header.size = htons (reply_size);
478 reply->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_SUCCESS);
479 reply->lock = htonl (lock_num);
480 strncpy ((char *) &reply[1], domain_name, domain_name_len);
481 GNUNET_SERVER_notify_transmit_ready (client,
490 * Handler for GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE
493 * @param client the client sending this message
494 * @param message GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE message
497 handle_acquire (void *cls,
498 struct GNUNET_SERVER_Client *client,
499 const struct GNUNET_MessageHeader *message)
501 const struct GNUNET_LOCKMANAGER_Message *request;
502 const char *domain_name;
503 struct LockList *ll_entry;
507 LOG (GNUNET_ERROR_TYPE_DEBUG,
508 "Received an ACQUIRE message\n");
511 /* Check if the client is in client list */
512 if (GNUNET_NO == cl_find_client (client, NULL))
514 cl_add_client (client);
517 request = (struct GNUNET_LOCKMANAGER_Message *) message;
518 lock_num = ntohl (request->lock);
519 domain_name = (char *) &request[1];
520 if (GNUNET_YES == ll_find_lock (domain_name,
523 {/* Add client to the lock's wait list */
524 ll_wl_add_client (ll_entry, client);
528 ll_add_lock (client, domain_name, lock_num);
529 send_success_msg (client, domain_name, lock_num);
532 GNUNET_SERVER_receive_done (client, GNUNET_OK);
537 * Handle for GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE
540 * @param client the client sending this message
541 * @param message the LOCKMANAGER_RELEASE message
544 handle_release (void *cls,
545 struct GNUNET_SERVER_Client *client,
546 const struct GNUNET_MessageHeader *message)
548 const struct GNUNET_LOCKMANAGER_Message *request;
549 const char *domain_name;
550 struct LockList *ll_entry;
553 request = (struct GNUNET_LOCKMANAGER_Message *) message;
554 lock_num = ntohl (request->lock);
555 domain_name = (char *) &request[1];
556 LOG (GNUNET_ERROR_TYPE_DEBUG,
557 "Received a RELEASE message on lock with num: %d, domain: %s\n",
558 lock_num, domain_name);
559 if (GNUNET_YES == ll_find_lock (domain_name, lock_num, &ll_entry))
561 if (NULL == ll_entry->wait_list_head)
563 ll_remove_lock (ll_entry);
567 /* Do furthur processing on lock's wait list here */
572 LOG (GNUNET_ERROR_TYPE_DEBUG,
573 "\t give lock doesn't exist\n");
577 GNUNET_SERVER_receive_done (client, GNUNET_OK);
582 * Callback for client disconnect
585 * @param client the client which has disconnected
588 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
590 struct ClientList *cl_entry;
592 LOG (GNUNET_ERROR_TYPE_DEBUG,
593 "A client has been disconnected -- freeing its locks and resources\n");
595 if (GNUNET_YES == cl_find_client (client, &cl_entry))
597 struct LockList *lock;
599 cl_remove_client (cl_entry);
600 while (GNUNET_YES == ll_find_lock_by_owner (client, &lock))
602 if (NULL == lock->wait_list_head)
604 ll_remove_lock (lock);
608 /* Do furthur processing on lock's wait list here */
612 else GNUNET_break (0);
620 * @param server the initialized server
621 * @param cfg configuration to use
624 lockmanager_run (void *cls,
625 struct GNUNET_SERVER_Handle * server,
626 const struct GNUNET_CONFIGURATION_Handle *cfg)
628 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
630 {&handle_acquire, NULL, GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE, 0},
631 {&handle_release, NULL, GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE, 0},
635 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting lockmanager\n");
636 GNUNET_SERVER_add_handlers (server,
638 GNUNET_SERVER_disconnect_notify (server,
639 &client_disconnect_cb,
646 * The starting point of execution
648 int main (int argc, char *const *argv)
652 GNUNET_log_setup ("lockmanager",
660 LOG (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
663 GNUNET_SERVICE_run (argc,
666 GNUNET_SERVICE_OPTION_NONE,
669 LOG (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");