aboutsummaryrefslogtreecommitdiff
path: root/src/lockmanager/lockmanager_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lockmanager/lockmanager_api.c')
-rw-r--r--src/lockmanager/lockmanager_api.c176
1 files changed, 131 insertions, 45 deletions
diff --git a/src/lockmanager/lockmanager_api.c b/src/lockmanager/lockmanager_api.c
index 7e20e27de..6524c7134 100644
--- a/src/lockmanager/lockmanager_api.c
+++ b/src/lockmanager/lockmanager_api.c
@@ -107,6 +107,63 @@ struct GNUNET_LOCKMANAGER_LockingRequest
107 107
108 108
109/** 109/**
110 * Generate hash with domain name and the lock
111 *
112 * @param domain NULL terminated domain name
113 *
114 * @param domain_length the length of the domain name including the terminating
115 * NULL if already known; 0 to calculate
116 *
117 * @param lock the lock number
118 * @param where to write the generated hash
119 */
120static void
121hash_domain_and_lock (const char *domain,
122 uint16_t domain_length,
123 uint32_t lock,
124 GNUNET_HashCode *ret)
125{
126 unsigned int str_len;
127 uint32_t *block;
128 size_t block_size;
129
130 str_len = (0 == domain_length) ? strlen (domain) : domain_length - 1;
131 block_size = sizeof (uint32_t) + str_len;
132 block = GNUNET_malloc (block_size);
133 /* Copy data */
134 *block = lock;
135 memcpy (&block[1], domain, str_len);
136
137 GNUNET_CRYPTO_hash (block, block_size, ret);
138 GNUNET_free (block);
139}
140
141
142/**
143 * Task for calling status change callback for a lock
144 *
145 * @param cls the LockingRequest associated with this lock
146 * @param tc the TaskScheduler context
147 */
148static void
149call_status_cb_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
150{
151 const struct GNUNET_LOCKMANAGER_LockingRequest *r = cls;
152
153 if (NULL != r->status_cb)
154 {
155 LOG (GNUNET_ERROR_TYPE_DEBUG,
156 "Calling status change callback for lock: %d in domain: %s\n",
157 r->lock, r->domain);
158 r->status_cb (r->status_cb_cls,
159 r->domain,
160 r->lock,
161 r->status);
162 }
163}
164
165
166/**
110 * Handler for server replies 167 * Handler for server replies
111 * 168 *
112 * @param cls the LOCKMANAGER_Handle 169 * @param cls the LOCKMANAGER_Handle
@@ -116,13 +173,68 @@ static void
116handle_replies (void *cls, 173handle_replies (void *cls,
117 const struct GNUNET_MessageHeader *msg) 174 const struct GNUNET_MessageHeader *msg)
118{ 175{
176 struct GNUNET_LOCKMANAGER_Handle *handle = cls;
177
119 if (NULL == msg) 178 if (NULL == msg)
120 LOG (GNUNET_ERROR_TYPE_DEBUG, 179 {
121 "Lockmanager service not available or went down\n"); 180 LOG (GNUNET_ERROR_TYPE_DEBUG,
122 return; 181 "Lockmanager service not available or went down\n");
182 return;
183 }
123 184
124 LOG (GNUNET_ERROR_TYPE_DEBUG, 185 switch (ntohs (msg->type))
125 "Received SUCCESS message\n"); 186 {
187 case GNUNET_MESSAGE_TYPE_LOCKMANAGER_SUCCESS:
188 LOG (GNUNET_ERROR_TYPE_DEBUG,
189 "Received SUCCESS message\n");
190
191 const struct GNUNET_LOCKMANAGER_Message *m;
192 const char *domain;
193 struct GNUNET_HashCode hash;
194 uint32_t lock;
195 int match_found;
196
197 m = (struct GNUNET_LOCKMANAGER_Message *) msg;
198 domain = (char *) &m[1];
199 lock = ntohl (m->lock);
200 hash_domain_and_lock (domain, 0, lock, &hash);
201
202 LOG (GNUNET_ERROR_TYPE_DEBUG,
203 "\t on lock: %d in domain %s\n",
204 lock, domain);
205
206 int match_iterator(void *cls, const GNUNET_HashCode *key, void *value)
207 {
208 struct GNUNET_LOCKMANAGER_LockingRequest *r = value;
209
210 if ( !((0 != strcmp (domain, r->domain))
211 && (lock == r->lock)))
212 return GNUNET_YES;
213
214 match_found = GNUNET_YES;
215 if (GNUNET_LOCKMANAGER_SUCCESS != r->status)
216 {
217 LOG (GNUNET_ERROR_TYPE_DEBUG,
218 "Changing status for lock: %d in domain: %s to SUCCESS\n",
219 r->lock, r->domain);
220 r->status = GNUNET_LOCKMANAGER_SUCCESS;
221 GNUNET_SCHEDULER_add_continuation (&call_status_cb_task,
222 r,
223 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
224 }
225 return GNUNET_NO;
226 }
227
228 match_found = GNUNET_NO;
229 GNUNET_CONTAINER_multihashmap_get_multiple (handle->hashmap,
230 &hash,
231 &match_iterator,
232 NULL);
233 GNUNET_assert (GNUNET_YES == match_found);
234 break;
235 default:
236 GNUNET_break(0);
237 }
126} 238}
127 239
128 240
@@ -160,7 +272,7 @@ transmit_notify (void *cls, size_t size, void *buf)
160 * 272 *
161 * @param cls NULL 273 * @param cls NULL
162 * @param key current key code 274 * @param key current key code
163 * @param value value in the hash map 275 * @param value the Locking request
164 * @return GNUNET_YES if we should continue to 276 * @return GNUNET_YES if we should continue to
165 * iterate, 277 * iterate,
166 * GNUNET_NO if not. 278 * GNUNET_NO if not.
@@ -170,49 +282,21 @@ free_iterator(void *cls,
170 const GNUNET_HashCode * key, 282 const GNUNET_HashCode * key,
171 void *value) 283 void *value)
172{ 284{
285 struct GNUNET_LOCKMANAGER_LockingRequest *r = value;
286
173 LOG (GNUNET_ERROR_TYPE_DEBUG, 287 LOG (GNUNET_ERROR_TYPE_DEBUG,
174 "Clearing locking request\n"); 288 "Clearing locking request\n");
175 GNUNET_free (value); 289 GNUNET_free (r->domain);
290 GNUNET_free (r);
176 return GNUNET_YES; 291 return GNUNET_YES;
177} 292}
178 293
179 294
180/**
181 * Generate hash with domain name and the lock
182 *
183 * @param domain NULL terminated domain name
184 *
185 * @param domain_length the length of the domain name including the terminating
186 * NULL if already known; 0 to calculate
187 *
188 * @param lock the lock number
189 * @param where to write the generated hash
190 */
191static void
192hash_domain_and_lock (const char *domain,
193 uint16_t domain_length,
194 uint32_t lock,
195 GNUNET_HashCode *ret)
196{
197 unsigned int str_len;
198 uint32_t *block;
199 size_t block_size;
200
201 str_len = (0 == domain_length) ? strlen (domain) : domain_length - 1;
202 block_size = sizeof (uint32_t) + str_len;
203 block = GNUNET_malloc (block_size);
204 /* Copy data */
205 *block = lock;
206 memcpy (&block[1], domain, str_len);
207
208 GNUNET_CRYPTO_hash (block, block_size, ret);
209 GNUNET_free (block);
210}
211
212/*******************/ 295/*******************/
213/* API Definitions */ 296/* API Definitions */
214/*******************/ 297/*******************/
215 298
299
216/** 300/**
217 * Connect to the lockmanager service 301 * Connect to the lockmanager service
218 * 302 *
@@ -240,7 +324,7 @@ GNUNET_LOCKMANAGER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
240 324
241 GNUNET_CLIENT_receive (h->conn, 325 GNUNET_CLIENT_receive (h->conn,
242 &handle_replies, 326 &handle_replies,
243 NULL, 327 h,
244 GNUNET_TIME_UNIT_FOREVER_REL); 328 GNUNET_TIME_UNIT_FOREVER_REL);
245 329
246 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__); 330 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
@@ -318,6 +402,7 @@ GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle,
318 r->handle = handle; 402 r->handle = handle;
319 r->lock = lock; 403 r->lock = lock;
320 r->domain = GNUNET_malloc (r->domain_name_length); 404 r->domain = GNUNET_malloc (r->domain_name_length);
405 r->status = GNUNET_LOCKMANAGER_RELEASE;
321 memcpy (r->domain, domain_name, r->domain_name_length); 406 memcpy (r->domain, domain_name, r->domain_name_length);
322 407
323 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message) + r->domain_name_length; 408 msg_size = sizeof (struct GNUNET_LOCKMANAGER_Message) + r->domain_name_length;
@@ -332,8 +417,8 @@ GNUNET_LOCKMANAGER_acquire_lock (struct GNUNET_LOCKMANAGER_Handle *handle,
332 GNUNET_CLIENT_notify_transmit_ready (r->handle->conn, 417 GNUNET_CLIENT_notify_transmit_ready (r->handle->conn,
333 msg_size, 418 msg_size,
334 TIMEOUT, 419 TIMEOUT,
335 GNUNET_NO, 420 GNUNET_YES,
336 *transmit_notify, 421 &transmit_notify,
337 msg); 422 msg);
338 hash_domain_and_lock (r->domain, 423 hash_domain_and_lock (r->domain,
339 r->domain_name_length, 424 r->domain_name_length,
@@ -386,8 +471,7 @@ GNUNET_LOCKMANAGER_cancel_request (struct GNUNET_LOCKMANAGER_LockingRequest
386 &transmit_notify, 471 &transmit_notify,
387 msg); 472 msg);
388 } 473 }
389 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__); 474
390
391 hash_domain_and_lock (request->domain, 475 hash_domain_and_lock (request->domain,
392 request->domain_name_length, 476 request->domain_name_length,
393 request->lock, 477 request->lock,
@@ -396,6 +480,8 @@ GNUNET_LOCKMANAGER_cancel_request (struct GNUNET_LOCKMANAGER_LockingRequest
396 GNUNET_assert (GNUNET_YES == 480 GNUNET_assert (GNUNET_YES ==
397 GNUNET_CONTAINER_multihashmap_remove 481 GNUNET_CONTAINER_multihashmap_remove
398 (request->handle->hashmap, &hash, request)); 482 (request->handle->hashmap, &hash, request));
399 483
484 GNUNET_free (request->domain);
400 GNUNET_free (request); 485 GNUNET_free (request);
486 LOG (GNUNET_ERROR_TYPE_DEBUG, "%s() END\n", __func__);
401} 487}