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/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"
34 struct GNUNET_TESTBED_Barrier
37 * hashcode identifying this barrier in the hashmap
39 struct GNUNET_HashCode key;
42 * The controller handle given while initiliasing this barrier
44 struct GNUNET_TESTBED_Controller *c;
47 * The name of the barrier
52 * The continuation callback to call when we have a status update on this
54 GNUNET_TESTBED_barrier_status_cb cb;
57 * the closure for the above callback
65 * handle for hashtable of barrier handles
67 static struct GNUNET_CONTAINER_MultiHashMap *barrier_map;
71 * Remove a barrier and it was the last one in the barrier hash map, destroy the
74 * @param barrier the barrier to remove
77 barrier_remove (struct GNUNET_TESTBED_Barrier *barrier)
79 GNUNET_assert (NULL != barrier_map); /* No barriers present */
80 GNUNET_assert (GNUNET_OK ==
81 GNUNET_CONTAINER_multihashmap_remove (barrier_map,
84 GNUNET_free (barrier->name);
85 GNUNET_free (barrier);
86 if (0 == GNUNET_CONTAINER_multihashmap_size (barrier_map))
88 GNUNET_CONTAINER_multihashmap_destroy (barrier_map);
95 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS messages
97 * @param c the controller handle to determine the connection this message
99 * @param msg the barrier status message
100 * @return GNUNET_OK to keep the connection active; GNUNET_SYSERR to tear it
101 * down signalling an error
104 GNUNET_TESTBED_handle_barrier_status_ (struct GNUNET_TESTBED_Controller *c,
105 const struct GNUNET_TESTBED_BarrierStatusMsg
108 struct GNUNET_TESTBED_Barrier *barrier;
111 struct GNUNET_HashCode key;
119 msize = ntohs (msg->header.size);
121 name_len = ntohs (msg->name_len);
122 if ( (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len + 1 > msize)
123 || ('\0' != name[name_len]) )
126 return GNUNET_SYSERR;
128 status = ntohs (msg->status);
129 if (BARRIER_STATUS_ERROR == status)
132 emsg_len = msize - (sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len
137 return GNUNET_SYSERR;
140 emsg = GNUNET_malloc (emsg_len);
142 emsg[emsg_len] = '\0';
143 (void) memcpy (emsg, msg->data + name_len + 1, emsg_len);
145 if (NULL == barrier_map)
147 GNUNET_CRYPTO_hash (name, name_len, &key);
148 barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key);
151 GNUNET_assert (NULL != barrier->cb);
152 barrier->cb (barrier->cls, name, barrier, status, emsg);
153 if (BARRIER_STATUS_INITIALISED == status)
154 return GNUNET_OK; /* just initialised; skip cleanup */
157 GNUNET_free_non_null (emsg);
159 barrier_remove (barrier);
165 * Initialise a barrier and call the given callback when the required percentage
166 * of peers (quorum) reach the barrier OR upon error.
168 * @param controller the handle to the controller
169 * @param name identification name of the barrier
170 * @param quorum the percentage of peers that is required to reach the barrier.
171 * Peers signal reaching a barrier by calling
172 * GNUNET_TESTBED_barrier_reached().
173 * @param cb the callback to call when the barrier is reached or upon error.
175 * @param cls closure for the above callback
176 * @return barrier handle; NULL upon error
178 struct GNUNET_TESTBED_Barrier *
179 GNUNET_TESTBED_barrier_init (struct GNUNET_TESTBED_Controller *controller,
182 GNUNET_TESTBED_barrier_status_cb cb, void *cls)
184 struct GNUNET_TESTBED_BarrierInit *msg;
185 struct GNUNET_TESTBED_Barrier *barrier;
186 struct GNUNET_HashCode key;
190 GNUNET_assert (quorum <= 100);
191 GNUNET_assert (NULL != cb);
192 name_len = strlen (name);
193 GNUNET_assert (0 < name_len);
194 GNUNET_CRYPTO_hash (name, name_len, &key);
195 if (NULL == barrier_map)
196 barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES);
198 GNUNET_CONTAINER_multihashmap_contains (barrier_map, &key))
203 barrier = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Barrier));
204 barrier->c = controller;
205 barrier->name = GNUNET_strdup (name);
208 (void) memcpy (&barrier->key, &key, sizeof (struct GNUNET_HashCode));
209 GNUNET_assert (GNUNET_OK ==
210 GNUNET_CONTAINER_multihashmap_put (barrier_map, &barrier->key,
212 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
213 msize = name_len + sizeof (struct GNUNET_TESTBED_BarrierInit);
214 msg = GNUNET_malloc (msize);
215 msg->header.size = htons (msize);
216 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT);
217 msg->quorum = (uint8_t) quorum;
218 (void) memcpy (msg->name, barrier->name, name_len);
219 GNUNET_TESTBED_queue_message_ (barrier->c, &msg->header);
227 * @param barrier the barrier handle
230 GNUNET_TESTBED_barrier_cancel (struct GNUNET_TESTBED_Barrier *barrier)
232 struct GNUNET_TESTBED_BarrierCancel *msg;
235 msize = sizeof (struct GNUNET_TESTBED_BarrierCancel) + strlen (barrier->name);
236 msg = GNUNET_malloc (msize);
237 msg->header.size = htons (msize);
238 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL);
239 (void) memcpy (msg->name, barrier->name, strlen (barrier->name));
240 GNUNET_TESTBED_queue_message_ (barrier->c, &msg->header);
241 barrier_remove (barrier);
246 * Barrier wait handle
248 struct GNUNET_TESTBED_BarrierWaitHandle
251 * The name of the barrier
256 * Then configuration used for the client connection
258 struct GNUNET_CONFIGURATION_Handle *cfg;
261 * The client connection
263 struct GNUNET_CLIENT_Connection *conn;
268 struct GNUNET_CLIENT_TransmitHandle *tx;
271 * The message to transmit with tx
273 struct GNUNET_MessageHeader *msg;
276 * The barrier wait callback
278 GNUNET_TESTBED_barrier_wait_cb cb;
281 * The closure for the above callback
288 * Function to destroy barrier wait handle
290 * @param h the handle to destroy
293 destroy_handle (struct GNUNET_TESTBED_BarrierWaitHandle *h)
295 GNUNET_free (h->name);
297 GNUNET_CLIENT_notify_transmit_ready_cancel (h->tx);
299 GNUNET_CLIENT_disconnect (h->conn);
301 GNUNET_free (h->msg);
302 GNUNET_CONFIGURATION_destroy (h->cfg);
308 * Type of a function to call when we receive a message
312 * @param message received message; NULL on timeout or fatal error
315 receive_handler (void *cls, const struct GNUNET_MessageHeader *message)
317 struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
318 const struct GNUNET_TESTBED_BarrierStatusMsg *msg;
321 if (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS != ntohs (message->type))
326 msize = ntohs (message->size);
327 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierStatusMsg))
332 msg = (const struct GNUNET_TESTBED_BarrierStatusMsg *) message;
333 switch (ntohs (msg->status))
335 case BARRIER_STATUS_ERROR:
337 case BARRIER_STATUS_INITIALISED:
338 GNUNET_break (0); /* FIXME */
340 case BARRIER_STATUS_CROSSED:
341 h->cb (h->cls, h->name, GNUNET_OK);
346 h->cb (h->cls, h->name, GNUNET_SYSERR);
354 * Function called to notify a client about the connection
355 * begin ready to queue more data. "buf" will be
356 * NULL and "size" zero if the connection was closed for
357 * writing in the meantime.
360 * @param size number of bytes available in buf
361 * @param buf where the callee should write the message
362 * @return number of bytes written to buf
365 transmit_notify (void *cls, size_t size, void *buf)
367 struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
371 if ((0 == size) || (NULL == buf))
376 msize = htons (h->msg->size);
377 GNUNET_assert (msize <= size);
378 (void) memcpy (buf, h->msg, msize);
379 GNUNET_free (h->msg);
381 GNUNET_CLIENT_receive (h->conn, &receive_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
387 * Wait for a barrier to be crossed. This function should be called by the
388 * peers which have been started by the testbed. If the peer is not started by
389 * testbed this function may return error
391 * @param name the name of the barrier
392 * @param cb the barrier wait callback
393 * @param cls the closure for the above callback
394 * @return barrier wait handle which can be used to cancel the waiting at
395 * anytime before the callback is called. NULL upon error.
397 struct GNUNET_TESTBED_BarrierWaitHandle *
398 GNUNET_TESTBED_barrier_wait (const char *name,
399 GNUNET_TESTBED_barrier_wait_cb cb,
402 struct GNUNET_TESTBED_BarrierWait *msg;
403 struct GNUNET_CONFIGURATION_Handle *cfg;
404 struct GNUNET_TESTBED_BarrierWaitHandle *h;
409 GNUNET_assert (NULL != cb);
410 GNUNET_assert (NULL != name);
411 cfg_filename = getenv (ENV_TESTBED_CONFIG);
412 if (NULL == cfg_filename)
414 cfg = GNUNET_CONFIGURATION_create ();
415 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_filename));
417 GNUNET_CONFIGURATION_destroy (cfg);
420 h = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_BarrierWaitHandle));
421 h->name = GNUNET_strdup (name);
423 h->conn = GNUNET_CLIENT_connect ("testbed-barrier", h->cfg);
431 name_len = strlen (name);
432 msize = sizeof (struct GNUNET_TESTBED_BarrierWait) + name_len;
433 msg = GNUNET_malloc (msize);
434 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT);
435 msg->header.size = htons (msize);
436 (void) memcpy (msg->name, name, name_len);
437 h->msg = &msg->header;
439 GNUNET_CLIENT_notify_transmit_ready (h->conn, msize,
440 GNUNET_TIME_UNIT_FOREVER_REL,
449 * Cancel a barrier wait handle
451 * @param h the barrier wait handle
454 GNUNET_TESTBED_barrier_wait_cancel (struct GNUNET_TESTBED_BarrierWaitHandle *h)
459 /* end of testbed_api_barriers.c */