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 * Task for calling status change callback for a lock
129 * @param cls the LockingRequest associated with this lock
130 * @param tc the TaskScheduler context
133 call_status_cb_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 const struct GNUNET_LOCKMANAGER_LockingRequest *r = cls;
137 if (NULL != r->status_cb)
139 LOG (GNUNET_ERROR_TYPE_DEBUG,
140 "Calling status change callback for lock: %d in domain: %s\n",
142 r->status_cb (r->status_cb_cls,
151 * Handler for server replies
153 * @param cls the LOCKMANAGER_Handle
154 * @param msg received message, NULL on timeout or fatal error
157 handle_replies (void *cls,
158 const struct GNUNET_MessageHeader *msg)
160 struct GNUNET_LOCKMANAGER_Handle *handle = cls;
161 const struct GNUNET_LOCKMANAGER_Message *m;
163 struct GNUNET_HashCode hash;
170 LOG (GNUNET_ERROR_TYPE_DEBUG,
171 "Lockmanager service not available or went down\n");
174 if (GNUNET_MESSAGE_TYPE_LOCKMANAGER_SUCCESS != ntohs(msg->type))
179 msize = ntohs (msg->size);
180 if (msize <= sizeof (struct GNUNET_LOCKMANAGER_Message))
185 m = (const struct GNUNET_LOCKMANAGER_Message *) msg;
186 domain = (const char *) &m[1];
187 msize -= sizeof (struct GNUNET_LOCKMANAGER_Message);
188 if ('\0' != domain[msize-1])
194 lock = ntohl (m->lock);
195 get_key (domain, lock, &hash);
196 LOG (GNUNET_ERROR_TYPE_DEBUG,
197 "Received SUCCESS message for lock: %d, domain %s\n",
199 int match_iterator(void *cls, const GNUNET_HashCode *key, void *value)
201 struct GNUNET_LOCKMANAGER_LockingRequest *r = value;
203 if ( !((0 == strcmp (domain, r->domain))
204 && (lock == r->lock)))
206 match_found = GNUNET_YES;
207 if (GNUNET_LOCKMANAGER_SUCCESS != r->status)
209 LOG (GNUNET_ERROR_TYPE_DEBUG,
210 "Changing status for lock: %d in domain: %s to SUCCESS\n",
212 r->status = GNUNET_LOCKMANAGER_SUCCESS;
213 GNUNET_SCHEDULER_add_continuation (&call_status_cb_task,
215 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
219 match_found = GNUNET_NO;
220 GNUNET_CONTAINER_multihashmap_get_multiple (handle->hashmap,
224 GNUNET_break (GNUNET_YES == match_found);
229 * Transmit notify for sending message to server
231 * @param cls the message to send
232 * @param size number of bytes available in buf
233 * @param buf where the callee should write the message
234 * @return number of bytes written to buf
237 transmit_notify (void *cls, size_t size, void *buf)
239 struct GNUNET_LOCKMANAGER_Message *msg = cls;
242 if ((0 == size) || (NULL == buf))
244 /* FIXME: Timed out -- requeue? */
247 msg_size = ntohs (msg->header.size);
248 GNUNET_assert (size >= msg_size);
249 memcpy (buf, msg, msg_size);
251 LOG (GNUNET_ERROR_TYPE_DEBUG,
252 "Message of size %u sent\n", msg_size);
258 * Iterator to free hash map entries.
261 * @param key current key code
262 * @param value the Locking request
263 * @return GNUNET_YES if we should continue to
268 free_iterator(void *cls,
269 const GNUNET_HashCode * key,
272 struct GNUNET_LOCKMANAGER_LockingRequest *r = value;
274 LOG (GNUNET_ERROR_TYPE_DEBUG,
275 "Clearing locking request\n");
276 GNUNET_free (r->domain);
282 /*******************/
283 /* API Definitions */
284 /*******************/
288 * Connect to the lockmanager service
290 * @param cfg the configuration to use
292 * @return upon success the handle to the service; NULL upon error
294 struct GNUNET_LOCKMANAGER_Handle *
295 GNUNET_LOCKMANAGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
297 struct GNUNET_LOCKMANAGER_Handle *h;
299 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
300 h = GNUNET_malloc (sizeof (struct GNUNET_LOCKMANAGER_Handle));
301 h->conn = GNUNET_CLIENT_connect ("lockmanager", cfg);
305 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
308 h->hashmap = GNUNET_CONTAINER_multihashmap_create (15);
309 GNUNET_assert (NULL != h->hashmap);
310 GNUNET_CLIENT_receive (h->conn,
313 GNUNET_TIME_UNIT_FOREVER_REL);
315 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
321 * Disconnect from the lockmanager service
323 * @param handle the handle to the lockmanager service
326 GNUNET_LOCKMANAGER_disconnect (struct GNUNET_LOCKMANAGER_Handle *handle)
328 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
329 GNUNET_CLIENT_disconnect (handle->conn);
330 if (0 != GNUNET_CONTAINER_multihashmap_size (handle->hashmap))
332 LOG (GNUNET_ERROR_TYPE_WARNING,
333 "Some locking requests are still present. Cancel them before "
334 "calling %s\n", __func__);
335 GNUNET_CONTAINER_multihashmap_iterate (handle->hashmap,
339 GNUNET_CONTAINER_multihashmap_destroy (handle->hashmap);
340 GNUNET_free (handle);
341 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
346 * Tries to acquire the given lock(even if the lock has been lost) until the
347 * request is called. If the lock is available the status_cb will be
348 * called. If the lock is busy then the request is queued and status_cb
349 * will be called when the lock has been made available and acquired by us.
351 * @param handle the handle to the lockmanager service
353 * @param domain_name name of the locking domain. Clients who want to share
354 * locks must use the same name for the locking domain. Also the
355 * domain_name should be selected with the prefix
356 * "GNUNET_<PROGRAM_NAME>_" to avoid domain name collisions.
359 * @param lock which lock to lock
361 * @param status_cb the callback for signalling when the lock is acquired and
364 * @param status_cb_cls the closure to the above callback
366 * @return the locking request handle for this request. It will be invalidated
367 * when status_cb is called.
369 struct GNUNET_LOCKMANAGER_LockingRequest *
370 GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle,
371 const char *domain_name,
373 GNUNET_LOCKMANAGER_StatusCallback
377 struct GNUNET_LOCKMANAGER_LockingRequest *r;
378 struct GNUNET_LOCKMANAGER_Message *msg;
379 struct GNUNET_HashCode hash;
381 size_t domain_name_length;
383 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
384 r = GNUNET_malloc (sizeof (struct GNUNET_LOCKMANAGER_LockingRequest));
385 domain_name_length = strlen (domain_name) + 1;
388 r->domain = GNUNET_malloc (domain_name_length);
389 r->status = GNUNET_LOCKMANAGER_RELEASE;
390 r->status_cb = status_cb;
391 r->status_cb_cls = status_cb_cls;
392 memcpy (r->domain, domain_name, domain_name_length);
393 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message) + domain_name_length;
394 msg = GNUNET_malloc (msg_size);
395 msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE);
396 msg->header.size = htons (msg_size);
397 msg->lock = htonl (lock);
398 memcpy (&msg[1], r->domain, domain_name_length);
399 LOG (GNUNET_ERROR_TYPE_DEBUG, "Queueing ACQUIRE message\n");
401 GNUNET_CLIENT_notify_transmit_ready (r->handle->conn,
407 get_key (r->domain, r->lock, &hash);
408 GNUNET_CONTAINER_multihashmap_put (r->handle->hashmap,
411 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
412 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
419 * Function to cancel the locking request generated by
420 * GNUNET_LOCKMANAGER_acquire_lock. If the lock is acquired us then the lock is
421 * released. GNUNET_LOCKMANAGER_StatusCallback will not be called upon any
422 * status changes resulting due to this call.
424 * @param request the LockingRequest to cancel
427 GNUNET_LOCKMANAGER_cancel_request (struct GNUNET_LOCKMANAGER_LockingRequest
430 struct GNUNET_LOCKMANAGER_Message *msg;
431 struct GNUNET_HashCode hash;
433 size_t domain_name_length;
435 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s()\n", __func__);
436 /* FIXME: Stop ACQUIRE retransmissions */
437 if (GNUNET_LOCKMANAGER_SUCCESS == request->status)
439 domain_name_length = strlen (request->domain) + 1;
440 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message)
441 + domain_name_length;
442 msg = GNUNET_malloc (msg_size);
443 msg->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE);
444 msg->header.size = htons (msg_size);
445 msg->lock = htonl (request->lock);
446 memcpy (&msg[1], request->domain, domain_name_length);
447 GNUNET_CLIENT_notify_transmit_ready (request->handle->conn,
449 TIMEOUT, /* What if this fails */
454 get_key (request->domain, request->lock, &hash);
455 GNUNET_assert (GNUNET_YES ==
456 GNUNET_CONTAINER_multihashmap_remove
457 (request->handle->hashmap, &hash, request));
458 GNUNET_free (request->domain);
459 GNUNET_free (request);
460 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);