2 This file is part of GNUnet.
3 Copyright (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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file testbed/testbed_api_barriers.c
23 * @brief API implementation for testbed barriers
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_testbed_service.h"
29 #include "testbed_api.h"
30 #include "testbed_api_barriers.h"
35 #define LOG(type, ...) \
36 GNUNET_log_from (type, "testbed-api-barriers", __VA_ARGS__);
39 * Debug logging shorthand
41 #define LOG_DEBUG(...) \
42 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
47 struct GNUNET_TESTBED_Barrier
50 * hashcode identifying this barrier in the hashmap
52 struct GNUNET_HashCode key;
55 * The controller handle given while initiliasing this barrier
57 struct GNUNET_TESTBED_Controller *c;
60 * The name of the barrier
65 * The continuation callback to call when we have a status update on this
67 GNUNET_TESTBED_barrier_status_cb cb;
70 * the closure for the above callback
75 * Should the barrier crossed status message be echoed back to the controller?
82 * handle for hashtable of barrier handles
84 static struct GNUNET_CONTAINER_MultiHashMap *barrier_map;
88 * Remove a barrier and it was the last one in the barrier hash map, destroy the
91 * @param barrier the barrier to remove
94 barrier_remove (struct GNUNET_TESTBED_Barrier *barrier)
96 GNUNET_assert (NULL != barrier_map); /* No barriers present */
97 GNUNET_assert (GNUNET_OK ==
98 GNUNET_CONTAINER_multihashmap_remove (barrier_map,
101 GNUNET_free (barrier->name);
102 GNUNET_free (barrier);
103 if (0 == GNUNET_CONTAINER_multihashmap_size (barrier_map))
105 GNUNET_CONTAINER_multihashmap_destroy (barrier_map);
112 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS messages
114 * @param c the controller handle to determine the connection this message
116 * @param msg the barrier status message
117 * @return GNUNET_OK to keep the connection active; GNUNET_SYSERR to tear it
118 * down signalling an error
121 GNUNET_TESTBED_handle_barrier_status_ (struct GNUNET_TESTBED_Controller *c,
122 const struct GNUNET_TESTBED_BarrierStatusMsg
125 struct GNUNET_TESTBED_Barrier *barrier;
128 struct GNUNET_HashCode key;
136 msize = ntohs (msg->header.size);
138 name_len = ntohs (msg->name_len);
139 LOG_DEBUG ("Received BARRIER_STATUS msg\n");
140 if (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len + 1 > msize)
143 return GNUNET_SYSERR;
145 if ('\0' != name[name_len])
148 return GNUNET_SYSERR;
150 status = ntohs (msg->status);
151 if (GNUNET_TESTBED_BARRIERSTATUS_ERROR == status)
154 emsg_len = msize - (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len
159 return GNUNET_SYSERR;
162 emsg = GNUNET_malloc (emsg_len);
164 emsg[emsg_len] = '\0';
165 (void) memcpy (emsg, msg->data + name_len + 1, emsg_len);
167 if (NULL == barrier_map)
172 GNUNET_CRYPTO_hash (name, name_len, &key);
173 barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key);
179 GNUNET_assert (NULL != barrier->cb);
180 if ((GNUNET_YES == barrier->echo) && (GNUNET_TESTBED_BARRIERSTATUS_CROSSED == status))
181 GNUNET_TESTBED_queue_message_ (c, GNUNET_copy_message (&msg->header));
182 barrier->cb (barrier->cls, name, barrier, status, emsg);
183 if (GNUNET_TESTBED_BARRIERSTATUS_INITIALISED == status)
184 return GNUNET_OK; /* just initialised; skip cleanup */
187 GNUNET_free_non_null (emsg);
189 barrier_remove (barrier);
195 * Initialise a barrier and call the given callback when the required percentage
196 * of peers (quorum) reach the barrier OR upon error.
198 * @param controller the handle to the controller
199 * @param name identification name of the barrier
200 * @param quorum the percentage of peers that is required to reach the barrier.
201 * Peers signal reaching a barrier by calling
202 * GNUNET_TESTBED_barrier_reached().
203 * @param cb the callback to call when the barrier is reached or upon error.
205 * @param cls closure for the above callback
206 * @param echo GNUNET_YES to echo the barrier crossed status message back to the
208 * @return barrier handle; NULL upon error
210 struct GNUNET_TESTBED_Barrier *
211 GNUNET_TESTBED_barrier_init_ (struct GNUNET_TESTBED_Controller *controller,
214 GNUNET_TESTBED_barrier_status_cb cb, void *cls,
217 struct GNUNET_TESTBED_BarrierInit *msg;
218 struct GNUNET_TESTBED_Barrier *barrier;
219 struct GNUNET_HashCode key;
223 GNUNET_assert (quorum <= 100);
224 GNUNET_assert (NULL != cb);
225 name_len = strlen (name);
226 GNUNET_assert (0 < name_len);
227 GNUNET_CRYPTO_hash (name, name_len, &key);
228 if (NULL == barrier_map)
229 barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES);
231 GNUNET_CONTAINER_multihashmap_contains (barrier_map, &key))
236 LOG_DEBUG ("Initialising barrier `%s'\n", name);
237 barrier = GNUNET_new (struct GNUNET_TESTBED_Barrier);
238 barrier->c = controller;
239 barrier->name = GNUNET_strdup (name);
242 barrier->echo = echo;
243 (void) memcpy (&barrier->key, &key, sizeof (struct GNUNET_HashCode));
244 GNUNET_assert (GNUNET_OK ==
245 GNUNET_CONTAINER_multihashmap_put (barrier_map, &barrier->key,
247 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
248 msize = name_len + sizeof (struct GNUNET_TESTBED_BarrierInit);
249 msg = GNUNET_malloc (msize);
250 msg->header.size = htons (msize);
251 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT);
252 msg->quorum = (uint8_t) quorum;
253 (void) memcpy (msg->name, barrier->name, name_len);
254 GNUNET_TESTBED_queue_message_ (barrier->c, &msg->header);
260 * Initialise a barrier and call the given callback when the required percentage
261 * of peers (quorum) reach the barrier OR upon error.
263 * @param controller the handle to the controller
264 * @param name identification name of the barrier
265 * @param quorum the percentage of peers that is required to reach the barrier.
266 * Peers signal reaching a barrier by calling
267 * GNUNET_TESTBED_barrier_reached().
268 * @param cb the callback to call when the barrier is reached or upon error.
270 * @param cls closure for the above callback
271 * @return barrier handle; NULL upon error
273 struct GNUNET_TESTBED_Barrier *
274 GNUNET_TESTBED_barrier_init (struct GNUNET_TESTBED_Controller *controller,
277 GNUNET_TESTBED_barrier_status_cb cb, void *cls)
279 return GNUNET_TESTBED_barrier_init_ (controller, name, quorum, cb, cls, GNUNET_YES);
286 * @param barrier the barrier handle
289 GNUNET_TESTBED_barrier_cancel (struct GNUNET_TESTBED_Barrier *barrier)
291 struct GNUNET_TESTBED_BarrierCancel *msg;
294 msize = sizeof (struct GNUNET_TESTBED_BarrierCancel) + strlen (barrier->name);
295 msg = GNUNET_malloc (msize);
296 msg->header.size = htons (msize);
297 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL);
298 (void) memcpy (msg->name, barrier->name, strlen (barrier->name));
299 GNUNET_TESTBED_queue_message_ (barrier->c, &msg->header);
300 barrier_remove (barrier);
305 * Barrier wait handle
307 struct GNUNET_TESTBED_BarrierWaitHandle
310 * The name of the barrier
315 * Then configuration used for the client connection
317 struct GNUNET_CONFIGURATION_Handle *cfg;
320 * The client connection
322 struct GNUNET_CLIENT_Connection *conn;
327 struct GNUNET_CLIENT_TransmitHandle *tx;
330 * The message to transmit with tx
332 struct GNUNET_MessageHeader *msg;
335 * The barrier wait callback
337 GNUNET_TESTBED_barrier_wait_cb cb;
340 * The closure for the above callback
347 * Function to destroy barrier wait handle
349 * @param h the handle to destroy
352 destroy_handle (struct GNUNET_TESTBED_BarrierWaitHandle *h)
354 GNUNET_free (h->name);
356 GNUNET_CLIENT_notify_transmit_ready_cancel (h->tx);
358 GNUNET_CLIENT_disconnect (h->conn);
360 GNUNET_free (h->msg);
361 GNUNET_CONFIGURATION_destroy (h->cfg);
367 * Type of a function to call when we receive a message
371 * @param message received message; NULL on timeout or fatal error
374 receive_handler (void *cls, const struct GNUNET_MessageHeader *message)
376 struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
377 const struct GNUNET_TESTBED_BarrierStatusMsg *msg;
385 if (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS != ntohs (message->type))
390 msize = ntohs (message->size);
391 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierStatusMsg))
396 msg = (const struct GNUNET_TESTBED_BarrierStatusMsg *) message;
397 switch (ntohs (msg->status))
399 case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
401 case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
402 GNUNET_break (0); /* FIXME */
404 case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
405 h->cb (h->cls, h->name, GNUNET_OK);
412 h->cb (h->cls, h->name, GNUNET_SYSERR);
420 * Function called to notify a client about the connection
421 * begin ready to queue more data. "buf" will be
422 * NULL and "size" zero if the connection was closed for
423 * writing in the meantime.
426 * @param size number of bytes available in buf
427 * @param buf where the callee should write the message
428 * @return number of bytes written to buf
431 transmit_notify (void *cls, size_t size, void *buf)
433 struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
437 if ((0 == size) || (NULL == buf))
442 msize = htons (h->msg->size);
443 GNUNET_assert (msize <= size);
444 (void) memcpy (buf, h->msg, msize);
445 GNUNET_free (h->msg);
447 GNUNET_CLIENT_receive (h->conn, &receive_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
453 * Wait for a barrier to be crossed. This function should be called by the
454 * peers which have been started by the testbed. If the peer is not started by
455 * testbed this function may return error
457 * @param name the name of the barrier
458 * @param cb the barrier wait callback
459 * @param cls the closure for the above callback
460 * @return barrier wait handle which can be used to cancel the waiting at
461 * anytime before the callback is called. NULL upon error.
463 struct GNUNET_TESTBED_BarrierWaitHandle *
464 GNUNET_TESTBED_barrier_wait (const char *name,
465 GNUNET_TESTBED_barrier_wait_cb cb,
468 struct GNUNET_TESTBED_BarrierWait *msg;
469 struct GNUNET_CONFIGURATION_Handle *cfg;
470 struct GNUNET_TESTBED_BarrierWaitHandle *h;
475 GNUNET_assert (NULL != cb);
476 GNUNET_assert (NULL != name);
477 cfg_filename = getenv (ENV_TESTBED_CONFIG);
478 if (NULL == cfg_filename)
480 LOG (GNUNET_ERROR_TYPE_ERROR, "Are you running under testbed?\n");
483 cfg = GNUNET_CONFIGURATION_create ();
484 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_filename))
486 LOG (GNUNET_ERROR_TYPE_ERROR, "Unable to load configuration from file `%s'\n",
488 GNUNET_CONFIGURATION_destroy (cfg);
491 h = GNUNET_new (struct GNUNET_TESTBED_BarrierWaitHandle);
492 h->name = GNUNET_strdup (name);
494 h->conn = GNUNET_CLIENT_connect ("testbed-barrier", h->cfg);
499 LOG (GNUNET_ERROR_TYPE_ERROR, "Unable to connect to local testbed-barrier service\n");
503 name_len = strlen (name);
504 msize = sizeof (struct GNUNET_TESTBED_BarrierWait) + name_len;
505 msg = GNUNET_malloc (msize);
506 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT);
507 msg->header.size = htons (msize);
508 (void) memcpy (msg->name, name, name_len);
509 h->msg = &msg->header;
511 GNUNET_CLIENT_notify_transmit_ready (h->conn, msize,
512 GNUNET_TIME_UNIT_FOREVER_REL,
521 * Cancel a barrier wait handle
523 * @param h the barrier wait handle
526 GNUNET_TESTBED_barrier_wait_cancel (struct GNUNET_TESTBED_BarrierWaitHandle *h)
531 /* end of testbed_api_barriers.c */