aboutsummaryrefslogtreecommitdiff
path: root/src/lockmanager/gnunet-service-lockmanager.c
blob: ed85d3e3d9d6f7336f8d53f3cb9c845b8c324aa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
     This file is part of GNUnet.
     (C) 2012 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file lockmanager/gnunet-service-lockmanager.c
 * @brief implementation of the LOCKMANAGER service
 * @author Sree Harsha Totakura
 */

#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_container_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_service_lib.h"
#include "gnunet_server_lib.h"

#include "lockmanager.h"

#define VERBOSE GNUNET_YES

#define LOG(kind,...) \
  GNUNET_log (kind, __VA_ARGS__)

#define TIME_REL_MINS(min) \
  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, min)

#define TIMEOUT TIME_REL_MINS(3)


/**
 * Doubly linked list of clients waiting for a lock
 */
struct WaitList
{
  /**
   * The next client structure
   */
  struct WaitList *next;
  
  /**
   * The prev client structure
   */
  struct WaitList *prev;

  /**
   * Pointer to the client
   */
  struct GNUNET_SERVER_Client *client;
};


/**
 * A Lock element for a doubly linked list
 */
struct LockList
{
  /**
   * The next element pointer
   */
  struct LockList *next;

  /**
   * Pointer to the previous element
   */
  struct LockList *prev;

  /**
   * The client whizch is currently holding this lock
   */
  struct GNUNET_SERVER_Client *client;

  /**
   * List head of clients waiting for this lock
   */
  struct WaitList *wait_list_head;

  /**
   * List tail of clients waiting for this lock
   */
  struct WaitList *wait_list_tail;

  /**
   * The name of the locking domain this lock belongs to
   */
  char *domain_name;

  /**
   * The number of this lock
   */
  uint32_t lock_num;
};


/**
 * Doubly linked list of clients having connections to us
 */
struct ClientList
{
  /**
   * The next client structure
   */
  struct ClientList *next;

  /**
   * The previous client structure
   */
  struct ClientList *prev;

  /**
   * Pointer to the client
   */
  struct GNUNET_SERVER_Client *client;
};


/**
 * Head of the doubly linked list of the currently held locks
 */
static struct LockList *ll_head;

/**
 * Tail of the doubly linked list of the currently held locks
 */
static struct LockList *ll_tail;

/**
 * Head of the doubly linked list of clients currently connected
 */
static struct ClientList *cl_head;

/**
 * Tail of the doubly linked list of clients currently connected
 */
static struct ClientList *cl_tail;



/**
 * Function to search for a lock in lock_list matching the given domain_name and
 * lock number
 *
 * @param domain_name the name of the locking domain
 * @param lock_num the number of the lock
 * @param ret this will be the pointer to the corresponding Lock if found; else
 *         it will be the last element in the locks list
 * @return GNUNET_YES if a matching lock is present in lock_list; GNUNET_NO if not
 */
static int
ll_find_lock (const char *domain_name,
           const uint32_t lock_num,
           struct LockList **ret)
{
  struct LockList *current_lock;

  current_lock = ll_head;
  
  while (NULL != current_lock)
    {
      if ( (0 == strcmp (domain_name, current_lock->domain_name))
           && (lock_num == current_lock->lock_num))
        {
          *ret = current_lock;
          return GNUNET_YES;
        }

      current_lock = current_lock->next;
    }
  
  *ret = current_lock;
  return GNUNET_NO;
}


/**
 * Function to append a lock to the global lock list
 *
 * @param domain_name the name of the locking domain
 * @param domain_name_len the length of the domain name
 * @param lock_num the number of the lock
 * @param tail the pointer to the tail of the global lock list
 */
static void
ll_add_lock (const char *domain_name,
          size_t domain_name_len,
          const uint32_t lock_num)
{
  struct LockList *lock;

  lock = GNUNET_malloc (sizeof (struct LockList));
  lock->domain_name = GNUNET_malloc (domain_name_len);
  strncpy (lock->domain_name, domain_name, domain_name_len);
  lock->lock_num = lock_num;
  
  GNUNET_CONTAINER_DLL_insert_tail (ll_head,
                                    ll_tail,
                                    lock);
}


/**
 * Function to delete a lock from the lock list
 *
 * @param lock the lock to be deleted
 */
static void
ll_remove_lock (struct LockList *lock)
{
  GNUNET_assert (NULL != ll_head);
  GNUNET_CONTAINER_DLL_remove (ll_head,
                               ll_tail,
                               lock);
  GNUNET_free (lock->domain_name);
  GNUNET_free (lock);
}


/**
 * Find a client in the waiting list of a lock
 *
 * @param lock the LockList entry of a lock
 * @param client the client to look for
 * @param ret where to store the matched wait list entry
 * @return GNUNET_YES if a match is found; GNUNET_NO if not
 */
static int
ll_wl_find_client (struct LockList *lock,
                   const struct GNUNET_SERVER_Client *client,
                   struct WaitList **ret)
{
  struct WaitList *current_wl_entry;

  current_wl_entry = lock->wait_list_head;

  while (NULL != current_wl_entry)
    {
      if (client == current_wl_entry->client)
        {
          *ret = current_wl_entry;
          return GNUNET_YES;
        }
      current_wl_entry = current_wl_entry->next;
    }
  *ret = current_wl_entry;
  return GNUNET_NO;
}


/**
 * Add a client to the wait list of a lock
 *
 * @param lock the lock list entry of a lock
 * @param client the client to queue for the lock's wait list
 */
static void
ll_wl_add_client (struct LockList *lock,
                  struct GNUNET_SERVER_Client *client)
{
  struct WaitList *wl_entry;

  wl_entry = GNUNET_malloc (sizeof (struct WaitList));
  wl_entry->client = client;
  GNUNET_CONTAINER_DLL_insert_tail (lock->wait_list_head,
                                    lock->wait_list_tail,
                                    wl_entry);
}


static void
ll_wl_remove_client (struct LockList *lock,
                     struct WaitList *wl_client)
{
  GNUNET_CONTAINER_DLL_remove (lock->wait_list_head,
                               lock->wait_list_tail,
                               wl_client);

  GNUNET_free (wl_client);
}


/**
 * Search for a client in the client list
 *
 * @param client the client to be searched for
 * @param ret will be pointing to the matched list entry (if there is a match);
 *          else to the tail of the client list
 * @return GNUNET_YES if the client is present; GNUNET_NO if not
 */
static int
cl_find_client (const struct GNUNET_SERVER_Client *client,
                struct ClientList **ret)
{
  struct ClientList *current;

  current = cl_head;

  while (NULL != current)
    {
      if (client == current->client)
        {
          *ret = current;
          return GNUNET_YES;
        }

      current = current->next;
    }
  
  *ret = current;
  return GNUNET_NO;
}


/**
 * Append a client to the client list
 *
 * @param client the client to be appended to the list
 */
static void
cl_add_client (struct GNUNET_SERVER_Client *client)
{
  struct ClientList *new_client;

  new_client = GNUNET_malloc (sizeof (struct ClientList));
  new_client->client = client;
  GNUNET_CONTAINER_DLL_insert_tail (cl_head,
                                    cl_tail,
                                    new_client);
}


/**
 * Delete the given client from the client list
 *
 * @param client the client list entry to delete
 */
static void
cl_remove_client (struct ClientList *client)
{
  GNUNET_CONTAINER_DLL_remove (cl_head,
                               cl_tail,
                               client);
  GNUNET_free (client);
}


/**
 * Transmit notify for sending message to client
 *
 * @param cls the message to send
 * @param size number of bytes available in buf
 * @param buf where the callee should write the message
 * @return number of bytes written to buf
 */
static size_t 
transmit_notify (void *cls, size_t size, void *buf)
{
  struct GNUNET_LOCKMANAGER_Message *msg = cls;
  uint16_t msg_size;

  if ((0 == size) || (NULL == buf))
    {
      /* FIXME: Timed out -- requeue? */
      return 0;
    }
  msg_size = ntohs (msg->header.size);
  GNUNET_assert (size >= msg_size);
  memcpy (buf, msg, msg_size);
  GNUNET_free (msg);
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Message of size %u sent\n", msg_size);
  return msg_size;
}


/**
 * Handler for GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE
 *
 * @param cls NULL
 * @param client the client sending this message
 * @param message GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE message
 */
static void
handle_acquire (void *cls,
                struct GNUNET_SERVER_Client *client,
                const struct GNUNET_MessageHeader *message)
{
  const struct GNUNET_LOCKMANAGER_Message *request;
  struct GNUNET_LOCKMANAGER_Message *reply;
  int16_t request_size;
  

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Received an ACQUIRE message\n");
  
  request = (struct GNUNET_LOCKMANAGER_Message *) message;

  /* FIXME: Dummy implementation; just echos success for every lock */
  request_size = ntohs (message->size);
  reply = GNUNET_malloc (request_size);
  memcpy (reply, request, request_size);
  reply->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_SUCCESS);
  GNUNET_SERVER_notify_transmit_ready (client,
                                       request_size,
                                       TIMEOUT,
                                       &transmit_notify,
                                       reply);

  GNUNET_SERVER_receive_done (client, GNUNET_OK);
}


/**
 * Handle for GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE
 *
 * @param cls NULL
 * @param client the client sending this message
 * @param message the LOCKMANAGER_RELEASE message
 */
static void
handle_release (void *cls,
                struct GNUNET_SERVER_Client *client,
                const struct GNUNET_MessageHeader *message)
{
  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Received a RELEASE message\n");

  GNUNET_SERVER_receive_done (client, GNUNET_OK);
}


/**
 * Lock manager setup
 *
 * @param cls closure
 * @param server the initialized server
 * @param cfg configuration to use
 */
static void 
lockmanager_run (void *cls,
                 struct GNUNET_SERVER_Handle * server,
                 const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
    {
      {&handle_acquire, NULL, GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE, 0},
      {&handle_release, NULL, GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE, 0},
      {NULL}
    };
  
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting lockmanager\n");
  GNUNET_SERVER_add_handlers (server,
                              message_handlers);
  
}

/**
 * The starting point of execution
 */
int main (int argc, char *const *argv)
{
  int ret;
  
  GNUNET_log_setup ("lockmanager",
#if VERBOSE
                    "DEBUG",
#else
                    "WARNING",
#endif
                    NULL);

  LOG (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
  ret = 
    (GNUNET_OK ==
     GNUNET_SERVICE_run (argc,
                         argv,
                         "lockmanager",
                         GNUNET_SERVICE_OPTION_NONE,
                         &lockmanager_run,
                         NULL)) ? 0 : 1;
  LOG (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
  return ret;
}