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)
43 * Message queue for transmitting messages
48 * next pointer for DLL
50 struct MessageQueue *next;
53 * prev pointer for DLL
55 struct MessageQueue *prev;
58 * The message to be sent
60 struct GNUNET_MessageHeader *msg;
64 * Context to be associated with each client
69 * The barrier this client is waiting for
71 struct Barrier *barrier;
76 struct ClientCtx *next;
81 struct ClientCtx *prev;
86 struct GNUNET_SERVER_Client *client;
89 * the transmission handle
91 struct GNUNET_SERVER_TransmitHandle *tx;
96 struct MessageQueue *mq_head;
101 struct MessageQueue *mq_tail;
111 * The hashcode of the barrier name
113 struct GNUNET_HashCode hash;
116 * The name of the barrier
121 * DLL head for the list of clients waiting for this barrier
123 struct ClientCtx *head;
126 * DLL tail for the list of clients waiting for this barrier
128 struct ClientCtx *tail;
131 * Number of peers which have reached this barrier
133 unsigned int nreached;
136 * Number of slaves we have initialised this barrier
138 unsigned int nslaves;
141 * Quorum percentage to be reached
146 * Was there a timeout while propagating initialisation
153 * Hashtable handle for storing initialised barriers
155 static struct GNUNET_CONTAINER_MultiHashMap *barrier_map;
160 static struct GNUNET_SERVICE_Context *ctx;
164 * Function called to notify a client about the connection
165 * begin ready to queue more data. "buf" will be
166 * NULL and "size" zero if the connection was closed for
167 * writing in the meantime.
169 * @param cls client context
170 * @param size number of bytes available in buf
171 * @param buf where the callee should write the message
172 * @return number of bytes written to buf
175 transmit_ready_cb (void *cls, size_t size, void *buf)
177 struct ClientCtx *ctx = cls;
178 struct GNUNET_SERVER_Client *client = ctx->client;
179 struct MessageQueue *mq;
180 struct GNUNET_MessageHeader *msg;
185 if ((0 == size) || (NULL == buf))
187 GNUNET_assert (NULL != ctx->client);
188 GNUNET_SERVER_client_drop (ctx->client);
194 wrote = ntohs (msg->size);
195 GNUNET_assert (size >= wrote);
196 (void) memcpy (buf, msg, wrote);
197 GNUNET_CONTAINER_DLL_remove (ctx->mq_head, ctx->mq_tail, mq);
198 GNUNET_free (mq->msg);
200 if (NULL != (mq = ctx->mq_head))
201 ctx->tx = GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
202 MESSAGE_SEND_TIMEOUT (30),
203 &transmit_ready_cb, ctx);
209 * Queue a message into a clients message queue
211 * @param ctx the context associated with the client
212 * @param msg the message to queue. Will be consumed
213 * @param suspended is the client suspended at the time of calling queue_message
216 queue_message (struct ClientCtx *ctx, struct GNUNET_MessageHeader *msg)
218 struct MessageQueue *mq;
219 struct GNUNET_SERVER_Client *client = ctx->client;
221 mq = GNUNET_malloc (sizeof (struct MessageQueue));
223 GNUNET_CONTAINER_DLL_insert_tail (ctx->mq_head, ctx->mq_tail, mq);
225 ctx->tx = GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
226 MESSAGE_SEND_TIMEOUT (30),
227 &transmit_ready_cb, ctx);
233 * Function to remove a barrier from the barrier map and cleanup resources
234 * occupied by a barrier
236 * @param barrier the barrier handle
239 remove_barrier (struct Barrier *barrier)
241 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (barrier_map,
244 GNUNET_free (barrier->name);
245 GNUNET_free (barrier);
250 * Function called upon timeout while waiting for a response from the
251 * subcontrollers to barrier init message
257 fwd_tout_barrier_init (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
259 struct ForwardedOperationContext *foctx = cls;
260 struct Barrier *barrier = foctx->cls;
263 barrier->timedout = GNUNET_YES;
264 if (0 == barrier->nslaves)
266 GST_send_operation_fail_msg (foctx->client, foctx->operation_id,
267 "Timeout while contacting a slave controller");
268 remove_barrier (barrier);
274 * Task for sending barrier crossed notifications to waiting client
276 * @param cls the barrier which is crossed
277 * @param tc scheduler task context
280 notify_task_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
282 struct Barrier *barrier = cls;
283 struct ClientCtx *client_ctx;
284 struct GNUNET_TESTBED_BarrierStatus *msg;
285 struct GNUNET_MessageHeader *dup_msg;
289 name_len = strlen (barrier->name) + 1;
290 msize = sizeof (struct GNUNET_TESTBED_BarrierStatus) + name_len;
291 msg = GNUNET_malloc (msize);
292 msg->header.size = htons (msize);
293 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS);
295 msg->name_len = htons (name_len);
296 (void) memcpy (msg->data, barrier->name, name_len);
297 msg->data[name_len] = '\0';
298 while (NULL != (client_ctx = barrier->head))
300 dup_msg = GNUNET_copy_message (&msg->header);
301 queue_message (client_ctx, dup_msg);
302 GNUNET_CONTAINER_DLL_remove (barrier->head, barrier->tail, client_ctx);
303 GNUNET_SERVER_client_set_user_context_ (client_ctx->client, NULL, 0);
304 GNUNET_free (client_ctx);
310 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT messages. This
311 * message should come from peers or a shared helper service using the
312 * testbed-barrier client API (@see gnunet_testbed_barrier_service.h)
314 * This handler is queued in the main service and will handle the messages sent
315 * either from the testbed driver or from a high level controller
318 * @param client identification of the client
319 * @param message the actual message
322 handle_barrier_wait (void *cls, struct GNUNET_SERVER_Client *client,
323 const struct GNUNET_MessageHeader *message)
325 const struct GNUNET_TESTBED_BarrierWait *msg;
326 struct Barrier *barrier;
328 struct ClientCtx *client_ctx;
329 struct GNUNET_HashCode key;
333 msize = ntohs (message->size);
334 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierWait))
337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
340 if (NULL == barrier_map)
343 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
346 msg = (const struct GNUNET_TESTBED_BarrierWait *) message;
347 name_len = msize - sizeof (struct GNUNET_TESTBED_BarrierWait);
348 name = GNUNET_malloc (name_len + 1);
349 name[name_len] = '\0';
350 (void) memcpy (name, msg->name, name_len);
351 GNUNET_CRYPTO_hash (name, name_len - 1, &key);
352 if (NULL == (barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key)))
355 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
359 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
360 if (NULL == client_ctx)
362 client_ctx = GNUNET_malloc (sizeof (struct ClientCtx));
363 client_ctx->client = client;
364 GNUNET_SERVER_client_keep (client);
365 client_ctx->barrier = barrier;
366 GNUNET_CONTAINER_DLL_insert_tail (barrier->head, barrier->tail, client_ctx);
368 if ((barrier->quorum * GST_num_local_peers) <= (barrier->nreached * 100))
369 notify_task_cb (barrier, NULL);
371 GNUNET_SERVER_receive_done (client, GNUNET_OK);
376 * Functions with this signature are called whenever a client
377 * is disconnected on the network level.
380 * @param client identification of the client; NULL
381 * for the last call when the server is destroyed
384 disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
386 struct ClientCtx *client_ctx;
387 struct Barrier *barrier;
389 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
390 if (NULL == client_ctx)
392 barrier = client_ctx->barrier;
393 GNUNET_CONTAINER_DLL_remove (barrier->head, barrier->tail, client_ctx);
394 if (NULL != client_ctx->tx)
395 GNUNET_SERVER_notify_transmit_ready_cancel (client_ctx->tx);
401 * Function to initialise barrriers component
404 GST_barriers_init (struct GNUNET_CONFIGURATION_Handle *cfg)
406 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
407 {&handle_barrier_wait, NULL, GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT, 0},
410 struct GNUNET_SERVER_Handle *srv;
412 barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES);
413 ctx = GNUNET_SERVICE_start ("testbed-barrier", cfg,
414 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN);
415 srv = GNUNET_SERVICE_get_server (ctx);
416 GNUNET_SERVER_add_handlers (srv, message_handlers);
417 GNUNET_SERVER_disconnect_notify (srv, &disconnect_cb, NULL);
422 * Function to stop the barrier service
427 GNUNET_assert (NULL != ctx);
428 GNUNET_SERVICE_stop (ctx);
433 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT messages. This
434 * message should always come from a parent controller or the testbed API if we
435 * are the root controller.
437 * This handler is queued in the main service and will handle the messages sent
438 * either from the testbed driver or from a high level controller
441 * @param client identification of the client
442 * @param message the actual message
445 GST_handle_barrier_init (void *cls, struct GNUNET_SERVER_Client *client,
446 const struct GNUNET_MessageHeader *message)
448 const struct GNUNET_TESTBED_BarrierInit *msg;
450 struct Barrier *barrier;
452 struct GNUNET_HashCode hash;
458 if (NULL == GST_context)
461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
464 if (client != GST_context->client)
467 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
470 msize = ntohs (message->size);
471 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierInit))
474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
477 msg = (const struct GNUNET_TESTBED_BarrierInit *) message;
478 op_id = GNUNET_ntohll (msg->op_id);
480 name_len = (size_t) msize - sizeof (struct GNUNET_TESTBED_BarrierInit);
481 GNUNET_CRYPTO_hash (name, name_len, &hash);
482 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (barrier_map, &hash))
484 GST_send_operation_fail_msg (client, op_id, "Barrier already initialised");
485 GNUNET_SERVER_receive_done (client, GNUNET_OK);
488 barrier = GNUNET_malloc (sizeof (struct Barrier));
489 (void) memcpy (&barrier->hash, &hash, sizeof (struct GNUNET_HashCode));
490 barrier->quorum = msg->quorum;
491 barrier->name = GNUNET_malloc (name_len + 1);
492 barrier->name[name_len] = '\0';
493 (void) memcpy (barrier->name, name, name_len);
494 GNUNET_assert (GNUNET_OK ==
495 GNUNET_CONTAINER_multihashmap_put (barrier_map,
498 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
499 GNUNET_SERVER_receive_done (client, GNUNET_OK);
500 /* Propagate barrier init to subcontrollers */
501 for (cnt = 0; cnt < GST_slave_list_size; cnt++)
503 if (NULL == (slave = GST_slave_list[cnt]))
505 if (NULL == slave->controller)
507 GNUNET_break (0);/* May happen when we are connecting to the controller */
510 GNUNET_break (0); /* FIXME */