2 This file is part of GNUnet.
3 (C) 2008--2013 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 3, 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 testbed/gnunet-service-testbed_barriers.c
23 * @brief barrier handling at the testbed controller
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
27 #include "gnunet-service-testbed.h"
28 #include "gnunet-service-testbed_barriers.h"
32 * timeout for outgoing message transmissions in seconds
34 #define MESSAGE_SEND_TIMEOUT(s) \
35 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, s)
39 * Test to see if local peers have reached the required quorum of a barrier
41 #define LOCAL_QUORUM_REACHED(barrier) \
42 ((barrier->quorum * GST_num_local_peers) <= (barrier->nreached * 100))
52 * Message queue for transmitting messages
57 * next pointer for DLL
59 struct MessageQueue *next;
62 * prev pointer for DLL
64 struct MessageQueue *prev;
67 * The message to be sent
69 struct GNUNET_MessageHeader *msg;
74 * Context to be associated with each client
79 * The barrier this client is waiting for
81 struct Barrier *barrier;
86 struct ClientCtx *next;
91 struct ClientCtx *prev;
96 struct GNUNET_SERVER_Client *client;
99 * the transmission handle
101 struct GNUNET_SERVER_TransmitHandle *tx;
106 struct MessageQueue *mq_head;
111 struct MessageQueue *mq_tail;
116 * Wrapper around Barrier handle
123 struct WBarrier *next;
128 struct WBarrier *prev;
131 * The local barrier associated with the creation of this wrapper
133 struct Barrier *barrier;
136 * The barrier handle from API
138 struct GNUNET_TESTBED_Barrier *hbarrier;
141 * Has this barrier been crossed?
153 * The hashcode of the barrier name
155 struct GNUNET_HashCode hash;
158 * The client handle to the master controller
160 struct GNUNET_SERVER_Client *client;
163 * The name of the barrier
168 * DLL head for the list of clients waiting for this barrier
170 struct ClientCtx *head;
173 * DLL tail for the list of clients waiting for this barrier
175 struct ClientCtx *tail;
178 * DLL head for the list of barrier handles
180 struct WBarrier *whead;
183 * DLL tail for the list of barrier handles
185 struct WBarrier *wtail;
188 * Identifier for the timeout task
190 GNUNET_SCHEDULER_TaskIdentifier tout_task;
193 * The status of this barrier
195 enum GNUNET_TESTBED_BarrierStatus status;
198 * Number of barriers wrapped in the above DLL
200 unsigned int num_wbarriers;
203 * Number of wrapped barriers reached so far
205 unsigned int num_wbarriers_reached;
208 * Number of wrapped barrier initialised so far
210 unsigned int num_wbarriers_inited;
213 * Number of peers which have reached this barrier
215 unsigned int nreached;
218 * Number of slaves we have initialised this barrier
220 unsigned int nslaves;
223 * Quorum percentage to be reached
231 * Hashtable handle for storing initialised barriers
233 static struct GNUNET_CONTAINER_MultiHashMap *barrier_map;
238 static struct GNUNET_SERVICE_Context *ctx;
242 * Function called to notify a client about the connection
243 * begin ready to queue more data. "buf" will be
244 * NULL and "size" zero if the connection was closed for
245 * writing in the meantime.
247 * @param cls client context
248 * @param size number of bytes available in buf
249 * @param buf where the callee should write the message
250 * @return number of bytes written to buf
253 transmit_ready_cb (void *cls, size_t size, void *buf)
255 struct ClientCtx *ctx = cls;
256 struct GNUNET_SERVER_Client *client = ctx->client;
257 struct MessageQueue *mq;
258 struct GNUNET_MessageHeader *msg;
263 if ((0 == size) || (NULL == buf))
265 GNUNET_assert (NULL != ctx->client);
266 GNUNET_SERVER_client_drop (ctx->client);
272 wrote = ntohs (msg->size);
273 GNUNET_assert (size >= wrote);
274 (void) memcpy (buf, msg, wrote);
275 GNUNET_CONTAINER_DLL_remove (ctx->mq_head, ctx->mq_tail, mq);
276 GNUNET_free (mq->msg);
278 if (NULL != (mq = ctx->mq_head))
279 ctx->tx = GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
280 MESSAGE_SEND_TIMEOUT (30),
281 &transmit_ready_cb, ctx);
287 * Queue a message into a clients message queue
289 * @param ctx the context associated with the client
290 * @param msg the message to queue. Will be consumed
293 queue_message (struct ClientCtx *ctx, struct GNUNET_MessageHeader *msg)
295 struct MessageQueue *mq;
296 struct GNUNET_SERVER_Client *client = ctx->client;
298 mq = GNUNET_malloc (sizeof (struct MessageQueue));
300 GNUNET_CONTAINER_DLL_insert_tail (ctx->mq_head, ctx->mq_tail, mq);
302 ctx->tx = GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
303 MESSAGE_SEND_TIMEOUT (30),
304 &transmit_ready_cb, ctx);
309 * Function to cleanup client context data structure
311 * @param ctx the client context data structure
314 cleanup_clientctx (struct ClientCtx *ctx)
316 struct MessageQueue *mq;
318 GNUNET_SERVER_client_drop (ctx->client);
320 GNUNET_SERVER_notify_transmit_ready_cancel (ctx->tx);
321 if (NULL != (mq = ctx->mq_head))
323 GNUNET_CONTAINER_DLL_remove (ctx->mq_head, ctx->mq_tail, mq);
324 GNUNET_free (mq->msg);
332 * Function to remove a barrier from the barrier map and cleanup resources
333 * occupied by a barrier
335 * @param barrier the barrier handle
338 remove_barrier (struct Barrier *barrier)
340 struct ClientCtx *ctx;
342 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (barrier_map,
345 while (NULL != (ctx = barrier->head))
347 GNUNET_CONTAINER_DLL_remove (barrier->head, barrier->tail, ctx);
348 cleanup_clientctx (ctx);
350 GNUNET_free (barrier->name);
351 GNUNET_SERVER_client_drop (barrier->client);
352 GNUNET_free (barrier);
357 * Cancels all subcontroller barrier handles
359 * @param barrier the local barrier
362 cancel_wrappers (struct Barrier *barrier)
364 struct WBarrier *wrapper;
366 while (NULL != (wrapper = barrier->whead))
368 GNUNET_TESTBED_barrier_cancel (wrapper->hbarrier);
369 GNUNET_CONTAINER_DLL_remove (barrier->whead, barrier->wtail, wrapper);
370 GNUNET_free (wrapper);
376 * Send a status message about a barrier to the given client
378 * @param client the client to send the message to
379 * @param name the barrier name
380 * @param status the status of the barrier
381 * @param emsg the error message; should be non-NULL for
382 * status=BARRIER_STATUS_ERROR
385 send_client_status_msg (struct GNUNET_SERVER_Client *client,
387 enum GNUNET_TESTBED_BarrierStatus status,
390 struct GNUNET_TESTBED_BarrierStatusMsg *msg;
394 GNUNET_assert ((NULL == emsg) || (BARRIER_STATUS_ERROR == status));
395 name_len = strlen (name) + 1;
396 msize = sizeof (struct GNUNET_TESTBED_BarrierStatusMsg)
398 + (NULL == emsg) ? 0 : strlen (emsg) + 1;
399 msg = GNUNET_malloc (msize);
400 msg->status = htons (status);
401 msg->name_len = htons ((uint16_t) name_len);
402 (void) memcpy (msg->data, name, name_len);
404 (void) memcpy (msg->data + name_len, emsg, strlen (emsg) + 1);
405 GST_queue_message (client, &msg->header);
410 * Sends a barrier failed message
412 * @param barrier the corresponding barrier
413 * @param emsg the error message; should be non-NULL for
414 * status=BARRIER_STATUS_ERROR
417 send_barrier_status_msg (struct Barrier *barrier, const char *emsg)
419 GNUNET_assert (0 != barrier->status);
420 send_client_status_msg (barrier->client, barrier->name,
421 barrier->status, emsg);
427 * Task for sending barrier crossed notifications to waiting client
429 * @param cls the barrier which is crossed
430 * @param tc scheduler task context
433 notify_task_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
435 struct Barrier *barrier = cls;
436 struct ClientCtx *client_ctx;
437 struct GNUNET_TESTBED_BarrierStatusMsg *msg;
438 struct GNUNET_MessageHeader *dup_msg;
442 name_len = strlen (barrier->name) + 1;
443 msize = sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len;
444 msg = GNUNET_malloc (msize);
445 msg->header.size = htons (msize);
446 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS);
448 msg->name_len = htons (name_len);
449 (void) memcpy (msg->data, barrier->name, name_len);
450 msg->data[name_len] = '\0';
451 while (NULL != (client_ctx = barrier->head))
453 dup_msg = GNUNET_copy_message (&msg->header);
454 queue_message (client_ctx, dup_msg);
455 GNUNET_CONTAINER_DLL_remove (barrier->head, barrier->tail, client_ctx);
456 GNUNET_SERVER_client_set_user_context_ (client_ctx->client, NULL, 0);
457 GNUNET_free (client_ctx);
463 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT messages. This
464 * message should come from peers or a shared helper service using the
465 * testbed-barrier client API (@see gnunet_testbed_barrier_service.h)
467 * This handler is queued in the main service and will handle the messages sent
468 * either from the testbed driver or from a high level controller
471 * @param client identification of the client
472 * @param message the actual message
475 handle_barrier_wait (void *cls, struct GNUNET_SERVER_Client *client,
476 const struct GNUNET_MessageHeader *message)
478 const struct GNUNET_TESTBED_BarrierWait *msg;
479 struct Barrier *barrier;
481 struct ClientCtx *client_ctx;
482 struct GNUNET_HashCode key;
486 msize = ntohs (message->size);
487 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierWait))
490 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
493 if (NULL == barrier_map)
496 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
499 msg = (const struct GNUNET_TESTBED_BarrierWait *) message;
500 name_len = msize - sizeof (struct GNUNET_TESTBED_BarrierWait);
501 name = GNUNET_malloc (name_len + 1);
502 name[name_len] = '\0';
503 (void) memcpy (name, msg->name, name_len);
504 GNUNET_CRYPTO_hash (name, name_len - 1, &key);
505 if (NULL == (barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key)))
508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
512 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
513 if (NULL == client_ctx)
515 client_ctx = GNUNET_malloc (sizeof (struct ClientCtx));
516 client_ctx->client = client;
517 GNUNET_SERVER_client_keep (client);
518 client_ctx->barrier = barrier;
519 GNUNET_CONTAINER_DLL_insert_tail (barrier->head, barrier->tail, client_ctx);
521 if (LOCAL_QUORUM_REACHED (barrier))
522 notify_task_cb (barrier, NULL);
524 GNUNET_SERVER_receive_done (client, GNUNET_OK);
529 * Functions with this signature are called whenever a client
530 * is disconnected on the network level.
533 * @param client identification of the client; NULL
534 * for the last call when the server is destroyed
537 disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
539 struct ClientCtx *client_ctx;
541 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
542 if (NULL == client_ctx)
543 return; /* We only set user context for locally
545 cleanup_clientctx (client_ctx);
550 * Function to initialise barrriers component
553 GST_barriers_init (struct GNUNET_CONFIGURATION_Handle *cfg)
555 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
556 {&handle_barrier_wait, NULL, GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT, 0},
559 struct GNUNET_SERVER_Handle *srv;
561 barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES);
562 ctx = GNUNET_SERVICE_start ("testbed-barrier", cfg,
563 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN);
564 srv = GNUNET_SERVICE_get_server (ctx);
565 GNUNET_SERVER_add_handlers (srv, message_handlers);
566 GNUNET_SERVER_disconnect_notify (srv, &disconnect_cb, NULL);
571 * Function to stop the barrier service
576 GNUNET_assert (NULL != barrier_map);
577 GNUNET_CONTAINER_multihashmap_destroy (barrier_map);
578 GNUNET_assert (NULL != ctx);
579 GNUNET_SERVICE_stop (ctx);
584 * Functions of this type are to be given as callback argument to
585 * GNUNET_TESTBED_barrier_init(). The callback will be called when status
586 * information is available for the barrier.
588 * @param cls the closure given to GNUNET_TESTBED_barrier_init()
589 * @param name the name of the barrier
590 * @param b_ the barrier handle
591 * @param status status of the barrier; GNUNET_OK if the barrier is crossed;
592 * GNUNET_SYSERR upon error
593 * @param emsg if the status were to be GNUNET_SYSERR, this parameter has the
597 wbarrier_status_cb (void *cls, const char *name,
598 struct GNUNET_TESTBED_Barrier *b_,
599 enum GNUNET_TESTBED_BarrierStatus status,
602 struct WBarrier *wrapper = cls;
603 struct Barrier *barrier = wrapper->barrier;
605 GNUNET_assert (b_ == wrapper->hbarrier);
606 wrapper->hbarrier = NULL;
607 GNUNET_CONTAINER_DLL_remove (barrier->whead, barrier->wtail, wrapper);
608 GNUNET_free (wrapper);
611 case BARRIER_STATUS_ERROR:
612 LOG (GNUNET_ERROR_TYPE_ERROR,
613 "Initialising barrier `%s' failed at a sub-controller: %s\n",
614 barrier->name, (NULL != emsg) ? emsg : "NULL");
615 cancel_wrappers (barrier);
617 emsg = "Initialisation failed at a sub-controller";
618 barrier->status = BARRIER_STATUS_ERROR;
619 send_barrier_status_msg (barrier, emsg);
621 case BARRIER_STATUS_CROSSED:
622 if (BARRIER_STATUS_INITIALISED != barrier->status)
627 barrier->num_wbarriers_reached++;
628 if ((barrier->num_wbarriers_reached == barrier->num_wbarriers)
629 && (LOCAL_QUORUM_REACHED (barrier)))
631 barrier->status = BARRIER_STATUS_CROSSED;
632 send_barrier_status_msg (barrier, NULL);
635 case BARRIER_STATUS_INITIALISED:
636 if (0 != barrier->status)
641 barrier->num_wbarriers_inited++;
642 if (barrier->num_wbarriers_inited == barrier->num_wbarriers)
644 barrier->status = BARRIER_STATUS_INITIALISED;
645 send_barrier_status_msg (barrier, NULL);
653 * Function called upon timeout while waiting for a response from the
654 * subcontrollers to barrier init message
657 * @param tc scheduler task context
660 fwd_tout_barrier_init (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
662 struct Barrier *barrier = cls;
664 cancel_wrappers (barrier);
665 barrier->status = BARRIER_STATUS_ERROR;
666 send_barrier_status_msg (barrier,
667 "Timedout while propagating barrier initialisation\n");
668 remove_barrier (barrier);
673 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT messages. This
674 * message should always come from a parent controller or the testbed API if we
675 * are the root controller.
677 * This handler is queued in the main service and will handle the messages sent
678 * either from the testbed driver or from a high level controller
681 * @param client identification of the client
682 * @param message the actual message
685 GST_handle_barrier_init (void *cls, struct GNUNET_SERVER_Client *client,
686 const struct GNUNET_MessageHeader *message)
688 const struct GNUNET_TESTBED_BarrierInit *msg;
690 struct Barrier *barrier;
692 struct WBarrier *wrapper;
693 struct GNUNET_HashCode hash;
698 if (NULL == GST_context)
701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
704 if (client != GST_context->client)
707 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
710 msize = ntohs (message->size);
711 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierInit))
714 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
717 msg = (const struct GNUNET_TESTBED_BarrierInit *) message;
718 name_len = (size_t) msize - sizeof (struct GNUNET_TESTBED_BarrierInit);
719 name = GNUNET_malloc (name_len + 1);
720 (void) memcpy (name, msg->name, name_len);
721 GNUNET_CRYPTO_hash (name, name_len, &hash);
722 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (barrier_map, &hash))
725 send_client_status_msg (client, name, BARRIER_STATUS_ERROR,
726 "A barrier with the same name already exists");
728 GNUNET_SERVER_receive_done (client, GNUNET_OK);
731 barrier = GNUNET_malloc (sizeof (struct Barrier));
732 (void) memcpy (&barrier->hash, &hash, sizeof (struct GNUNET_HashCode));
733 barrier->quorum = msg->quorum;
734 barrier->name = name;
735 barrier->client = client;
736 GNUNET_SERVER_client_keep (client);
737 GNUNET_assert (GNUNET_OK ==
738 GNUNET_CONTAINER_multihashmap_put (barrier_map,
741 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
742 GNUNET_SERVER_receive_done (client, GNUNET_OK);
743 /* Propagate barrier init to subcontrollers */
744 for (cnt = 0; cnt < GST_slave_list_size; cnt++)
746 if (NULL == (slave = GST_slave_list[cnt]))
748 if (NULL == slave->controller)
750 GNUNET_break (0);/* May happen when we are connecting to the controller */
753 wrapper = GNUNET_malloc (sizeof (struct WBarrier));
754 wrapper->barrier = barrier;
755 GNUNET_CONTAINER_DLL_insert_tail (barrier->whead, barrier->wtail, wrapper);
756 wrapper->hbarrier = GNUNET_TESTBED_barrier_init (slave->controller,
762 if (NULL == barrier->whead) /* No further propagation */
764 barrier->status = BARRIER_STATUS_INITIALISED;
765 send_barrier_status_msg (barrier, NULL);
767 barrier->tout_task = GNUNET_SCHEDULER_add_delayed (MESSAGE_SEND_TIMEOUT (30),
768 &fwd_tout_barrier_init,
774 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL messages. This
775 * message should always come from a parent controller or the testbed API if we
776 * are the root controller.
778 * This handler is queued in the main service and will handle the messages sent
779 * either from the testbed driver or from a high level controller
782 * @param client identification of the client
783 * @param message the actual message
786 GST_handle_barrier_cancel (void *cls, struct GNUNET_SERVER_Client *client,
787 const struct GNUNET_MessageHeader *message)
789 const struct GNUNET_TESTBED_BarrierCancel *msg;
791 struct Barrier *barrier;
792 struct GNUNET_HashCode hash;
796 if (NULL == GST_context)
799 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
802 if (client != GST_context->client)
805 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
808 msize = ntohs (message->size);
809 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierCancel))
812 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
815 msg = (const struct GNUNET_TESTBED_BarrierCancel *) message;
816 name_len = msize - sizeof (struct GNUNET_TESTBED_BarrierCancel);
817 name = GNUNET_malloc (name_len + 1);
818 (void) memcpy (name, msg->name, name_len);
819 GNUNET_CRYPTO_hash (name, name_len, &hash);
820 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (barrier_map, &hash))
823 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
826 barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &hash);
827 GNUNET_assert (NULL != barrier);
828 cancel_wrappers (barrier);
829 remove_barrier (barrier);
830 GNUNET_SERVER_receive_done (client, GNUNET_OK);
833 /* end of gnunet-service-testbed_barriers.c */