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"
30 * timeout for outgoing message transmissions in seconds
32 #define MESSAGE_SEND_TIMEOUT(s) \
33 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, s)
37 * Test to see if local peers have reached the required quorum of a barrier
39 #define LOCAL_QUORUM_REACHED(barrier) \
40 ((barrier->quorum * GST_num_local_peers) <= (barrier->nreached * 100))
50 * Message queue for transmitting messages
55 * next pointer for DLL
57 struct MessageQueue *next;
60 * prev pointer for DLL
62 struct MessageQueue *prev;
65 * The message to be sent
67 struct GNUNET_MessageHeader *msg;
72 * Context to be associated with each client
77 * The barrier this client is waiting for
79 struct Barrier *barrier;
84 struct ClientCtx *next;
89 struct ClientCtx *prev;
94 struct GNUNET_SERVER_Client *client;
97 * the transmission handle
99 struct GNUNET_SERVER_TransmitHandle *tx;
104 struct MessageQueue *mq_head;
109 struct MessageQueue *mq_tail;
114 * Wrapper around Barrier handle
121 struct WBarrier *next;
126 struct WBarrier *prev;
129 * The local barrier associated with the creation of this wrapper
131 struct Barrier *barrier;
134 * The barrier handle from API
136 struct GNUNET_TESTBED_Barrier *hbarrier;
139 * Has this barrier been crossed?
151 * The hashcode of the barrier name
153 struct GNUNET_HashCode hash;
156 * The name of the barrier
161 * DLL head for the list of clients waiting for this barrier
163 struct ClientCtx *head;
166 * DLL tail for the list of clients waiting for this barrier
168 struct ClientCtx *tail;
171 * DLL head for the list of barrier handles
173 struct WBarrier *whead;
176 * DLL tail for the list of barrier handles
178 struct WBarrier *wtail;
181 * Number of barriers wrapped in the above DLL
183 unsigned int num_wbarriers;
186 * Number of wrapped barriers reached so far
188 unsigned int num_wbarriers_reached;
191 * Number of peers which have reached this barrier
193 unsigned int nreached;
196 * Number of slaves we have initialised this barrier
198 unsigned int nslaves;
201 * Quorum percentage to be reached
206 * Was there a timeout while propagating initialisation
213 * Hashtable handle for storing initialised barriers
215 static struct GNUNET_CONTAINER_MultiHashMap *barrier_map;
220 static struct GNUNET_SERVICE_Context *ctx;
224 * Function called to notify a client about the connection
225 * begin ready to queue more data. "buf" will be
226 * NULL and "size" zero if the connection was closed for
227 * writing in the meantime.
229 * @param cls client context
230 * @param size number of bytes available in buf
231 * @param buf where the callee should write the message
232 * @return number of bytes written to buf
235 transmit_ready_cb (void *cls, size_t size, void *buf)
237 struct ClientCtx *ctx = cls;
238 struct GNUNET_SERVER_Client *client = ctx->client;
239 struct MessageQueue *mq;
240 struct GNUNET_MessageHeader *msg;
245 if ((0 == size) || (NULL == buf))
247 GNUNET_assert (NULL != ctx->client);
248 GNUNET_SERVER_client_drop (ctx->client);
254 wrote = ntohs (msg->size);
255 GNUNET_assert (size >= wrote);
256 (void) memcpy (buf, msg, wrote);
257 GNUNET_CONTAINER_DLL_remove (ctx->mq_head, ctx->mq_tail, mq);
258 GNUNET_free (mq->msg);
260 if (NULL != (mq = ctx->mq_head))
261 ctx->tx = GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
262 MESSAGE_SEND_TIMEOUT (30),
263 &transmit_ready_cb, ctx);
269 * Queue a message into a clients message queue
271 * @param ctx the context associated with the client
272 * @param msg the message to queue. Will be consumed
275 queue_message (struct ClientCtx *ctx, struct GNUNET_MessageHeader *msg)
277 struct MessageQueue *mq;
278 struct GNUNET_SERVER_Client *client = ctx->client;
280 mq = GNUNET_malloc (sizeof (struct MessageQueue));
282 GNUNET_CONTAINER_DLL_insert_tail (ctx->mq_head, ctx->mq_tail, mq);
284 ctx->tx = GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
285 MESSAGE_SEND_TIMEOUT (30),
286 &transmit_ready_cb, ctx);
292 * Function to remove a barrier from the barrier map and cleanup resources
293 * occupied by a barrier
295 * @param barrier the barrier handle
298 remove_barrier (struct Barrier *barrier)
300 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (barrier_map,
303 GNUNET_free (barrier->name);
304 GNUNET_free (barrier);
309 * Function called upon timeout while waiting for a response from the
310 * subcontrollers to barrier init message
316 fwd_tout_barrier_init (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
318 struct ForwardedOperationContext *foctx = cls;
319 struct Barrier *barrier = foctx->cls;
322 barrier->timedout = GNUNET_YES;
323 if (0 == barrier->nslaves)
325 GST_send_operation_fail_msg (foctx->client, foctx->operation_id,
326 "Timeout while contacting a slave controller");
327 remove_barrier (barrier);
333 * Task for sending barrier crossed notifications to waiting client
335 * @param cls the barrier which is crossed
336 * @param tc scheduler task context
339 notify_task_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
341 struct Barrier *barrier = cls;
342 struct ClientCtx *client_ctx;
343 struct GNUNET_TESTBED_BarrierStatus *msg;
344 struct GNUNET_MessageHeader *dup_msg;
348 name_len = strlen (barrier->name) + 1;
349 msize = sizeof (struct GNUNET_TESTBED_BarrierStatus) + name_len;
350 msg = GNUNET_malloc (msize);
351 msg->header.size = htons (msize);
352 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS);
354 msg->name_len = htons (name_len);
355 (void) memcpy (msg->data, barrier->name, name_len);
356 msg->data[name_len] = '\0';
357 while (NULL != (client_ctx = barrier->head))
359 dup_msg = GNUNET_copy_message (&msg->header);
360 queue_message (client_ctx, dup_msg);
361 GNUNET_CONTAINER_DLL_remove (barrier->head, barrier->tail, client_ctx);
362 GNUNET_SERVER_client_set_user_context_ (client_ctx->client, NULL, 0);
363 GNUNET_free (client_ctx);
369 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT messages. This
370 * message should come from peers or a shared helper service using the
371 * testbed-barrier client API (@see gnunet_testbed_barrier_service.h)
373 * This handler is queued in the main service and will handle the messages sent
374 * either from the testbed driver or from a high level controller
377 * @param client identification of the client
378 * @param message the actual message
381 handle_barrier_wait (void *cls, struct GNUNET_SERVER_Client *client,
382 const struct GNUNET_MessageHeader *message)
384 const struct GNUNET_TESTBED_BarrierWait *msg;
385 struct Barrier *barrier;
387 struct ClientCtx *client_ctx;
388 struct GNUNET_HashCode key;
392 msize = ntohs (message->size);
393 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierWait))
396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
399 if (NULL == barrier_map)
402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
405 msg = (const struct GNUNET_TESTBED_BarrierWait *) message;
406 name_len = msize - sizeof (struct GNUNET_TESTBED_BarrierWait);
407 name = GNUNET_malloc (name_len + 1);
408 name[name_len] = '\0';
409 (void) memcpy (name, msg->name, name_len);
410 GNUNET_CRYPTO_hash (name, name_len - 1, &key);
411 if (NULL == (barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key)))
414 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
418 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
419 if (NULL == client_ctx)
421 client_ctx = GNUNET_malloc (sizeof (struct ClientCtx));
422 client_ctx->client = client;
423 GNUNET_SERVER_client_keep (client);
424 client_ctx->barrier = barrier;
425 GNUNET_CONTAINER_DLL_insert_tail (barrier->head, barrier->tail, client_ctx);
427 if (LOCAL_QUORUM_REACHED (barrier))
428 notify_task_cb (barrier, NULL);
430 GNUNET_SERVER_receive_done (client, GNUNET_OK);
435 * Functions with this signature are called whenever a client
436 * is disconnected on the network level.
439 * @param client identification of the client; NULL
440 * for the last call when the server is destroyed
443 disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
445 struct ClientCtx *client_ctx;
446 struct Barrier *barrier;
448 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
449 if (NULL == client_ctx)
451 barrier = client_ctx->barrier;
452 GNUNET_CONTAINER_DLL_remove (barrier->head, barrier->tail, client_ctx);
453 if (NULL != client_ctx->tx)
454 GNUNET_SERVER_notify_transmit_ready_cancel (client_ctx->tx);
460 * Function to initialise barrriers component
463 GST_barriers_init (struct GNUNET_CONFIGURATION_Handle *cfg)
465 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
466 {&handle_barrier_wait, NULL, GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT, 0},
469 struct GNUNET_SERVER_Handle *srv;
471 barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES);
472 ctx = GNUNET_SERVICE_start ("testbed-barrier", cfg,
473 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN);
474 srv = GNUNET_SERVICE_get_server (ctx);
475 GNUNET_SERVER_add_handlers (srv, message_handlers);
476 GNUNET_SERVER_disconnect_notify (srv, &disconnect_cb, NULL);
481 * Function to stop the barrier service
486 GNUNET_assert (NULL != barrier_map);
487 GNUNET_CONTAINER_multihashmap_destroy (barrier_map);
488 GNUNET_assert (NULL != ctx);
489 GNUNET_SERVICE_stop (ctx);
494 * Functions of this type are to be given as callback argument to
495 * GNUNET_TESTBED_barrier_init(). The callback will be called when status
496 * information is available for the barrier.
498 * @param cls the closure given to GNUNET_TESTBED_barrier_init()
499 * @param name the name of the barrier
500 * @param barrier the barrier handle
501 * @param status status of the barrier; GNUNET_OK if the barrier is crossed;
502 * GNUNET_SYSERR upon error
503 * @param emsg if the status were to be GNUNET_SYSERR, this parameter has the
507 wbarrier_status_cb (void *cls, const char *name,
508 struct GNUNET_TESTBED_Barrier *b_,
509 int status, const char *emsg)
511 struct WBarrier *wrapper = cls;
512 struct Barrier *barrier = wrapper->barrier;
514 GNUNET_assert (b_ == wrapper->hbarrier);
515 wrapper->hbarrier = NULL;
516 GNUNET_CONTAINER_DLL_remove (barrier->whead, barrier->wtail, wrapper);
517 GNUNET_free (wrapper);
518 if (GNUNET_SYSERR == status)
520 LOG (GNUNET_ERROR_TYPE_ERROR,
521 "Initialising barrier (%s) failed at a sub-controller: %s\n",
522 barrier->name, (NULL != emsg) ? emsg : "NULL");
523 while (NULL != (wrapper = barrier->whead))
525 GNUNET_TESTBED_barrier_cancel (wrapper->hbarrier);
526 GNUNET_CONTAINER_DLL_remove (barrier->whead, barrier->wtail, wrapper);
527 GNUNET_free (wrapper);
529 /* Send parent controller failure message */
532 barrier->num_wbarriers_reached++;
533 if ((barrier->num_wbarriers_reached == barrier->num_wbarriers)
534 && (LOCAL_QUORUM_REACHED (barrier)))
536 /* Send parent controller success status message */
544 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT messages. This
545 * message should always come from a parent controller or the testbed API if we
546 * are the root controller.
548 * This handler is queued in the main service and will handle the messages sent
549 * either from the testbed driver or from a high level controller
552 * @param client identification of the client
553 * @param message the actual message
556 GST_handle_barrier_init (void *cls, struct GNUNET_SERVER_Client *client,
557 const struct GNUNET_MessageHeader *message)
559 const struct GNUNET_TESTBED_BarrierInit *msg;
561 struct Barrier *barrier;
563 struct WBarrier *wrapper;
564 struct GNUNET_HashCode hash;
570 if (NULL == GST_context)
573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
576 if (client != GST_context->client)
579 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
582 msize = ntohs (message->size);
583 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierInit))
586 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
589 msg = (const struct GNUNET_TESTBED_BarrierInit *) message;
590 op_id = GNUNET_ntohll (msg->op_id);
592 name_len = (size_t) msize - sizeof (struct GNUNET_TESTBED_BarrierInit);
593 GNUNET_CRYPTO_hash (name, name_len, &hash);
594 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (barrier_map, &hash))
596 GST_send_operation_fail_msg (client, op_id, "Barrier already initialised");
597 GNUNET_SERVER_receive_done (client, GNUNET_OK);
600 barrier = GNUNET_malloc (sizeof (struct Barrier));
601 (void) memcpy (&barrier->hash, &hash, sizeof (struct GNUNET_HashCode));
602 barrier->quorum = msg->quorum;
603 barrier->name = GNUNET_malloc (name_len + 1);
604 barrier->name[name_len] = '\0';
605 (void) memcpy (barrier->name, name, name_len);
606 GNUNET_assert (GNUNET_OK ==
607 GNUNET_CONTAINER_multihashmap_put (barrier_map,
610 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
611 GNUNET_SERVER_receive_done (client, GNUNET_OK);
612 /* Propagate barrier init to subcontrollers */
613 for (cnt = 0; cnt < GST_slave_list_size; cnt++)
615 if (NULL == (slave = GST_slave_list[cnt]))
617 if (NULL == slave->controller)
619 GNUNET_break (0);/* May happen when we are connecting to the controller */
622 wrapper = GNUNET_malloc (sizeof (struct WBarrier));
623 wrapper->barrier = barrier;
624 wrapper->hbarrier = GNUNET_TESTBED_barrier_init (slave->controller,