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_BarrierStatus
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_BarrierStatus) + name_len + 1 > msize)
123 || ('\0' != name[name_len]) )
126 return GNUNET_SYSERR;
128 status = ntohs (msg->status);
132 emsg_len = msize - (sizeof (struct GNUNET_TESTBED_BarrierStatus) + 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);
155 GNUNET_free_non_null (emsg);
157 barrier_remove (barrier);
163 * Initialise a barrier and call the given callback when the required percentage
164 * of peers (quorum) reach the barrier OR upon error.
166 * @param controller the handle to the controller
167 * @param name identification name of the barrier
168 * @param quorum the percentage of peers that is required to reach the barrier.
169 * Peers signal reaching a barrier by calling
170 * GNUNET_TESTBED_barrier_reached().
171 * @param cb the callback to call when the barrier is reached or upon error.
173 * @param cls closure for the above callback
174 * @return barrier handle; NULL upon error
176 struct GNUNET_TESTBED_Barrier *
177 GNUNET_TESTBED_barrier_init (struct GNUNET_TESTBED_Controller *controller,
180 GNUNET_TESTBED_barrier_status_cb cb, void *cls)
182 struct GNUNET_TESTBED_Barrier *barrier;
183 struct GNUNET_HashCode key;
186 GNUNET_assert (quorum <= 100);
187 GNUNET_assert (NULL != cb);
188 name_len = strlen (name);
189 GNUNET_assert (0 < name_len);
190 GNUNET_CRYPTO_hash (name, name_len, &key);
191 if (NULL == barrier_map)
192 barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES);
194 GNUNET_CONTAINER_multihashmap_contains (barrier_map, &key))
199 barrier = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Barrier));
200 barrier->name = GNUNET_strdup (name);
203 (void) memcpy (&barrier->key, &key, sizeof (struct GNUNET_HashCode));
204 GNUNET_assert (GNUNET_OK ==
205 GNUNET_CONTAINER_multihashmap_put (barrier_map, &barrier->key,
207 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
215 * @param barrier the barrier handle
218 GNUNET_TESTBED_barrier_cancel (struct GNUNET_TESTBED_Barrier *barrier)
220 barrier_remove (barrier);