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 #define LOG(kind,...) \
53 GNUNET_log_from (kind, "testbed-barriers", __VA_ARGS__)
63 * Message queue for transmitting messages
68 * next pointer for DLL
70 struct MessageQueue *next;
73 * prev pointer for DLL
75 struct MessageQueue *prev;
78 * The message to be sent
80 struct GNUNET_MessageHeader *msg;
85 * Context to be associated with each client
90 * The barrier this client is waiting for
92 struct Barrier *barrier;
97 struct ClientCtx *next;
102 struct ClientCtx *prev;
107 struct GNUNET_SERVER_Client *client;
110 * the transmission handle
112 struct GNUNET_SERVER_TransmitHandle *tx;
117 struct MessageQueue *mq_head;
122 struct MessageQueue *mq_tail;
127 * Wrapper around Barrier handle
134 struct WBarrier *next;
139 struct WBarrier *prev;
142 * The local barrier associated with the creation of this wrapper
144 struct Barrier *barrier;
147 * The barrier handle from API
149 struct GNUNET_TESTBED_Barrier *hbarrier;
152 * Has this barrier been crossed?
164 * The hashcode of the barrier name
166 struct GNUNET_HashCode hash;
169 * The client handle to the master controller
171 struct GNUNET_SERVER_Client *client;
174 * The name of the barrier
179 * DLL head for the list of clients waiting for this barrier
181 struct ClientCtx *head;
184 * DLL tail for the list of clients waiting for this barrier
186 struct ClientCtx *tail;
189 * DLL head for the list of barrier handles
191 struct WBarrier *whead;
194 * DLL tail for the list of barrier handles
196 struct WBarrier *wtail;
199 * Identifier for the timeout task
201 GNUNET_SCHEDULER_TaskIdentifier tout_task;
204 * The status of this barrier
206 enum GNUNET_TESTBED_BarrierStatus status;
209 * Number of barriers wrapped in the above DLL
211 unsigned int num_wbarriers;
214 * Number of wrapped barriers reached so far
216 unsigned int num_wbarriers_reached;
219 * Number of wrapped barrier initialised so far
221 unsigned int num_wbarriers_inited;
224 * Number of peers which have reached this barrier
226 unsigned int nreached;
229 * Number of slaves we have initialised this barrier
231 unsigned int nslaves;
234 * Quorum percentage to be reached
242 * Hashtable handle for storing initialised barriers
244 static struct GNUNET_CONTAINER_MultiHashMap *barrier_map;
249 static struct GNUNET_SERVICE_Context *ctx;
253 * Function called to notify a client about the connection
254 * begin ready to queue more data. "buf" will be
255 * NULL and "size" zero if the connection was closed for
256 * writing in the meantime.
258 * @param cls client context
259 * @param size number of bytes available in buf
260 * @param buf where the callee should write the message
261 * @return number of bytes written to buf
264 transmit_ready_cb (void *cls, size_t size, void *buf)
266 struct ClientCtx *ctx = cls;
267 struct GNUNET_SERVER_Client *client = ctx->client;
268 struct MessageQueue *mq;
269 struct GNUNET_MessageHeader *msg;
274 if ((0 == size) || (NULL == buf))
276 GNUNET_assert (NULL != ctx->client);
277 GNUNET_SERVER_client_drop (ctx->client);
283 wrote = ntohs (msg->size);
284 GNUNET_assert (size >= wrote);
285 (void) memcpy (buf, msg, wrote);
286 GNUNET_CONTAINER_DLL_remove (ctx->mq_head, ctx->mq_tail, mq);
287 GNUNET_free (mq->msg);
289 if (NULL != (mq = ctx->mq_head))
290 ctx->tx = GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
291 MESSAGE_SEND_TIMEOUT (30),
292 &transmit_ready_cb, ctx);
298 * Queue a message into a clients message queue
300 * @param ctx the context associated with the client
301 * @param msg the message to queue. Will be consumed
304 queue_message (struct ClientCtx *ctx, struct GNUNET_MessageHeader *msg)
306 struct MessageQueue *mq;
307 struct GNUNET_SERVER_Client *client = ctx->client;
309 mq = GNUNET_malloc (sizeof (struct MessageQueue));
311 GNUNET_CONTAINER_DLL_insert_tail (ctx->mq_head, ctx->mq_tail, mq);
313 ctx->tx = GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
314 MESSAGE_SEND_TIMEOUT (30),
315 &transmit_ready_cb, ctx);
320 * Function to cleanup client context data structure
322 * @param ctx the client context data structure
325 cleanup_clientctx (struct ClientCtx *ctx)
327 struct MessageQueue *mq;
329 GNUNET_SERVER_client_drop (ctx->client);
331 GNUNET_SERVER_notify_transmit_ready_cancel (ctx->tx);
332 if (NULL != (mq = ctx->mq_head))
334 GNUNET_CONTAINER_DLL_remove (ctx->mq_head, ctx->mq_tail, mq);
335 GNUNET_free (mq->msg);
343 * Function to remove a barrier from the barrier map and cleanup resources
344 * occupied by a barrier
346 * @param barrier the barrier handle
349 remove_barrier (struct Barrier *barrier)
351 struct ClientCtx *ctx;
353 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (barrier_map,
356 while (NULL != (ctx = barrier->head))
358 GNUNET_CONTAINER_DLL_remove (barrier->head, barrier->tail, ctx);
359 cleanup_clientctx (ctx);
361 GNUNET_free (barrier->name);
362 GNUNET_SERVER_client_drop (barrier->client);
363 GNUNET_free (barrier);
368 * Cancels all subcontroller barrier handles
370 * @param barrier the local barrier
373 cancel_wrappers (struct Barrier *barrier)
375 struct WBarrier *wrapper;
377 while (NULL != (wrapper = barrier->whead))
379 GNUNET_TESTBED_barrier_cancel (wrapper->hbarrier);
380 GNUNET_CONTAINER_DLL_remove (barrier->whead, barrier->wtail, wrapper);
381 GNUNET_free (wrapper);
387 * Send a status message about a barrier to the given client
389 * @param client the client to send the message to
390 * @param name the barrier name
391 * @param status the status of the barrier
392 * @param emsg the error message; should be non-NULL for
393 * status=BARRIER_STATUS_ERROR
396 send_client_status_msg (struct GNUNET_SERVER_Client *client,
398 enum GNUNET_TESTBED_BarrierStatus status,
401 struct GNUNET_TESTBED_BarrierStatusMsg *msg;
405 GNUNET_assert ((NULL == emsg) || (BARRIER_STATUS_ERROR == status));
406 name_len = strlen (name);
407 msize = sizeof (struct GNUNET_TESTBED_BarrierStatusMsg)
409 + ((NULL == emsg) ? 0 : (strlen (emsg) + 1));
410 msg = GNUNET_malloc (msize);
411 msg->header.size = htons (msize);
412 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS);
413 msg->status = htons (status);
414 msg->name_len = htons ((uint16_t) name_len);
415 (void) memcpy (msg->data, name, name_len);
417 (void) memcpy (msg->data + name_len + 1, emsg, strlen (emsg));
418 GST_queue_message (client, &msg->header);
423 * Sends a barrier failed message
425 * @param barrier the corresponding barrier
426 * @param emsg the error message; should be non-NULL for
427 * status=BARRIER_STATUS_ERROR
430 send_barrier_status_msg (struct Barrier *barrier, const char *emsg)
432 GNUNET_assert (0 != barrier->status);
433 send_client_status_msg (barrier->client, barrier->name,
434 barrier->status, emsg);
440 * Task for sending barrier crossed notifications to waiting client
442 * @param cls the barrier which is crossed
443 * @param tc scheduler task context
446 notify_task_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
448 struct Barrier *barrier = cls;
449 struct ClientCtx *client_ctx;
450 struct GNUNET_TESTBED_BarrierStatusMsg *msg;
451 struct GNUNET_MessageHeader *dup_msg;
455 name_len = strlen (barrier->name) + 1;
456 msize = sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len;
457 msg = GNUNET_malloc (msize);
458 msg->header.size = htons (msize);
459 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS);
460 msg->status = htons (BARRIER_STATUS_CROSSED);
461 msg->name_len = htons (name_len);
462 (void) memcpy (msg->data, barrier->name, name_len);
463 msg->data[name_len] = '\0';
464 while (NULL != (client_ctx = barrier->head))
466 dup_msg = GNUNET_copy_message (&msg->header);
467 queue_message (client_ctx, dup_msg);
468 GNUNET_CONTAINER_DLL_remove (barrier->head, barrier->tail, client_ctx);
474 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT messages. This
475 * message should come from peers or a shared helper service using the
476 * testbed-barrier client API (@see gnunet_testbed_barrier_service.h)
478 * This handler is queued in the main service and will handle the messages sent
479 * either from the testbed driver or from a high level controller
482 * @param client identification of the client
483 * @param message the actual message
486 handle_barrier_wait (void *cls, struct GNUNET_SERVER_Client *client,
487 const struct GNUNET_MessageHeader *message)
489 const struct GNUNET_TESTBED_BarrierWait *msg;
490 struct Barrier *barrier;
492 struct ClientCtx *client_ctx;
493 struct GNUNET_HashCode key;
497 msize = ntohs (message->size);
498 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierWait))
501 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
504 if (NULL == barrier_map)
507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
510 msg = (const struct GNUNET_TESTBED_BarrierWait *) message;
511 name_len = msize - sizeof (struct GNUNET_TESTBED_BarrierWait);
512 name = GNUNET_malloc (name_len + 1);
513 name[name_len] = '\0';
514 (void) memcpy (name, msg->name, name_len);
515 LOG_DEBUG ("Received BARRIER_WAIT for barrier `%s'\n", name);
516 GNUNET_CRYPTO_hash (name, name_len, &key);
517 if (NULL == (barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key)))
520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
524 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
525 if (NULL == client_ctx)
527 client_ctx = GNUNET_malloc (sizeof (struct ClientCtx));
528 client_ctx->client = client;
529 GNUNET_SERVER_client_keep (client);
530 client_ctx->barrier = barrier;
531 GNUNET_CONTAINER_DLL_insert_tail (barrier->head, barrier->tail, client_ctx);
534 if ((barrier->num_wbarriers_reached == barrier->num_wbarriers)
535 && (LOCAL_QUORUM_REACHED (barrier)))
537 barrier->status = BARRIER_STATUS_CROSSED;
538 send_barrier_status_msg (barrier, NULL);
539 notify_task_cb (barrier, NULL);
541 GNUNET_SERVER_receive_done (client, GNUNET_OK);
546 * Functions with this signature are called whenever a client
547 * is disconnected on the network level.
550 * @param client identification of the client; NULL
551 * for the last call when the server is destroyed
554 disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
556 struct ClientCtx *client_ctx;
560 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
561 if (NULL == client_ctx)
562 return; /* We only set user context for locally
564 cleanup_clientctx (client_ctx);
569 * Function to initialise barrriers component
571 * @param cfg the configuration to use for initialisation
574 GST_barriers_init (struct GNUNET_CONFIGURATION_Handle *cfg)
576 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
577 {&handle_barrier_wait, NULL, GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT, 0},
580 struct GNUNET_SERVER_Handle *srv;
582 barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES);
583 ctx = GNUNET_SERVICE_start ("testbed-barrier", cfg,
584 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN);
585 srv = GNUNET_SERVICE_get_server (ctx);
586 GNUNET_SERVER_add_handlers (srv, message_handlers);
587 GNUNET_SERVER_disconnect_notify (srv, &disconnect_cb, NULL);
592 * Function to stop the barrier service
595 GST_barriers_destroy ()
597 GNUNET_assert (NULL != barrier_map);
598 GNUNET_CONTAINER_multihashmap_destroy (barrier_map);
599 GNUNET_assert (NULL != ctx);
600 GNUNET_SERVICE_stop (ctx);
605 * Functions of this type are to be given as callback argument to
606 * GNUNET_TESTBED_barrier_init(). The callback will be called when status
607 * information is available for the barrier.
609 * @param cls the closure given to GNUNET_TESTBED_barrier_init()
610 * @param name the name of the barrier
611 * @param b_ the barrier handle
612 * @param status status of the barrier; GNUNET_OK if the barrier is crossed;
613 * GNUNET_SYSERR upon error
614 * @param emsg if the status were to be GNUNET_SYSERR, this parameter has the
618 wbarrier_status_cb (void *cls, const char *name,
619 struct GNUNET_TESTBED_Barrier *b_,
620 enum GNUNET_TESTBED_BarrierStatus status,
623 struct WBarrier *wrapper = cls;
624 struct Barrier *barrier = wrapper->barrier;
626 GNUNET_assert (b_ == wrapper->hbarrier);
627 wrapper->hbarrier = NULL;
628 GNUNET_CONTAINER_DLL_remove (barrier->whead, barrier->wtail, wrapper);
629 GNUNET_free (wrapper);
632 case BARRIER_STATUS_ERROR:
633 LOG (GNUNET_ERROR_TYPE_ERROR,
634 "Initialising barrier `%s' failed at a sub-controller: %s\n",
635 barrier->name, (NULL != emsg) ? emsg : "NULL");
636 cancel_wrappers (barrier);
638 emsg = "Initialisation failed at a sub-controller";
639 barrier->status = BARRIER_STATUS_ERROR;
640 send_barrier_status_msg (barrier, emsg);
642 case BARRIER_STATUS_CROSSED:
643 if (BARRIER_STATUS_INITIALISED != barrier->status)
648 barrier->num_wbarriers_reached++;
649 if ((barrier->num_wbarriers_reached == barrier->num_wbarriers)
650 && (LOCAL_QUORUM_REACHED (barrier)))
652 barrier->status = BARRIER_STATUS_CROSSED;
653 send_barrier_status_msg (barrier, NULL);
656 case BARRIER_STATUS_INITIALISED:
657 if (0 != barrier->status)
662 barrier->num_wbarriers_inited++;
663 if (barrier->num_wbarriers_inited == barrier->num_wbarriers)
665 barrier->status = BARRIER_STATUS_INITIALISED;
666 send_barrier_status_msg (barrier, NULL);
674 * Function called upon timeout while waiting for a response from the
675 * subcontrollers to barrier init message
678 * @param tc scheduler task context
681 fwd_tout_barrier_init (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
683 struct Barrier *barrier = cls;
685 cancel_wrappers (barrier);
686 barrier->status = BARRIER_STATUS_ERROR;
687 send_barrier_status_msg (barrier,
688 "Timedout while propagating barrier initialisation\n");
689 remove_barrier (barrier);
694 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT messages. This
695 * message should always come from a parent controller or the testbed API if we
696 * are the root controller.
698 * This handler is queued in the main service and will handle the messages sent
699 * either from the testbed driver or from a high level controller
702 * @param client identification of the client
703 * @param message the actual message
706 GST_handle_barrier_init (void *cls, struct GNUNET_SERVER_Client *client,
707 const struct GNUNET_MessageHeader *message)
709 const struct GNUNET_TESTBED_BarrierInit *msg;
711 struct Barrier *barrier;
713 struct WBarrier *wrapper;
714 struct GNUNET_HashCode hash;
719 if (NULL == GST_context)
722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
725 if (client != GST_context->client)
728 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
731 msize = ntohs (message->size);
732 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierInit))
735 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
738 msg = (const struct GNUNET_TESTBED_BarrierInit *) message;
739 name_len = (size_t) msize - sizeof (struct GNUNET_TESTBED_BarrierInit);
740 name = GNUNET_malloc (name_len + 1);
741 (void) memcpy (name, msg->name, name_len);
742 GNUNET_CRYPTO_hash (name, name_len, &hash);
743 LOG_DEBUG ("Received BARRIER_INIT for barrier `%s'\n", name);
744 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (barrier_map, &hash))
747 send_client_status_msg (client, name, BARRIER_STATUS_ERROR,
748 "A barrier with the same name already exists");
750 GNUNET_SERVER_receive_done (client, GNUNET_OK);
753 barrier = GNUNET_malloc (sizeof (struct Barrier));
754 (void) memcpy (&barrier->hash, &hash, sizeof (struct GNUNET_HashCode));
755 barrier->quorum = msg->quorum;
756 barrier->name = name;
757 barrier->client = client;
758 GNUNET_SERVER_client_keep (client);
759 GNUNET_assert (GNUNET_OK ==
760 GNUNET_CONTAINER_multihashmap_put (barrier_map,
763 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
764 GNUNET_SERVER_receive_done (client, GNUNET_OK);
765 /* Propagate barrier init to subcontrollers */
766 for (cnt = 0; cnt < GST_slave_list_size; cnt++)
768 if (NULL == (slave = GST_slave_list[cnt]))
770 if (NULL == slave->controller)
772 GNUNET_break (0);/* May happen when we are connecting to the controller */
775 wrapper = GNUNET_malloc (sizeof (struct WBarrier));
776 wrapper->barrier = barrier;
777 GNUNET_CONTAINER_DLL_insert_tail (barrier->whead, barrier->wtail, wrapper);
778 wrapper->hbarrier = GNUNET_TESTBED_barrier_init (slave->controller,
784 if (NULL == barrier->whead) /* No further propagation */
786 barrier->status = BARRIER_STATUS_INITIALISED;
787 LOG_DEBUG ("Sending BARRIER_STATUS_INITIALISED for barrier `%s'\n",
789 send_barrier_status_msg (barrier, NULL);
791 barrier->tout_task = GNUNET_SCHEDULER_add_delayed (MESSAGE_SEND_TIMEOUT (30),
792 &fwd_tout_barrier_init,
798 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL messages. This
799 * message should always come from a parent controller or the testbed API if we
800 * are the root controller.
802 * This handler is queued in the main service and will handle the messages sent
803 * either from the testbed driver or from a high level controller
806 * @param client identification of the client
807 * @param message the actual message
810 GST_handle_barrier_cancel (void *cls, struct GNUNET_SERVER_Client *client,
811 const struct GNUNET_MessageHeader *message)
813 const struct GNUNET_TESTBED_BarrierCancel *msg;
815 struct Barrier *barrier;
816 struct GNUNET_HashCode hash;
820 if (NULL == GST_context)
823 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
826 if (client != GST_context->client)
829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
832 msize = ntohs (message->size);
833 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierCancel))
836 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
839 msg = (const struct GNUNET_TESTBED_BarrierCancel *) message;
840 name_len = msize - sizeof (struct GNUNET_TESTBED_BarrierCancel);
841 name = GNUNET_malloc (name_len + 1);
842 (void) memcpy (name, msg->name, name_len);
843 GNUNET_CRYPTO_hash (name, name_len, &hash);
844 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (barrier_map, &hash))
847 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
850 barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &hash);
851 GNUNET_assert (NULL != barrier);
852 cancel_wrappers (barrier);
853 remove_barrier (barrier);
854 GNUNET_SERVER_receive_done (client, GNUNET_OK);
857 /* end of gnunet-service-testbed_barriers.c */