2 This file is part of GNUnet.
3 Copyright (C) 2008--2013, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file testbed/testbed_api_barriers.c
23 * @brief API implementation for testbed barriers
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
27 #include "gnunet_testbed_service.h"
28 #include "testbed_api.h"
33 #define LOG(type, ...) \
34 GNUNET_log_from (type, "testbed-api-barriers", __VA_ARGS__);
37 * Debug logging shorthand
39 #define LOG_DEBUG(...) \
40 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
46 struct GNUNET_TESTBED_BarrierWaitHandle
49 * The name of the barrier
54 * Then configuration used for the client connection
56 struct GNUNET_CONFIGURATION_Handle *cfg;
59 * The testbed-barrier service message queue.
61 struct GNUNET_MQ_Handle *mq;
64 * The barrier wait callback
66 GNUNET_TESTBED_barrier_wait_cb cb;
69 * The closure for @e cb.
76 * Check if barrier status message is well-formed.
79 * @param msg received message
80 * @return #GNUNET_OK if the message is well-formed.
83 check_status (void *cls,
84 const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
86 /* FIXME: this fails to actually check that the message
87 follows the protocol spec (0-terminations!). However,
88 not critical as #handle_status() doesn't interpret the
89 variable-size part anyway right now. */
95 * Type of a function to call when we receive a message
99 * @param msg received message
102 handle_status (void *cls,
103 const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
105 struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108 "Got barrier status %d\n",
109 (int) ntohs (msg->status));
110 switch (ntohs (msg->status))
112 case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
118 case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
125 case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
138 GNUNET_TESTBED_barrier_wait_cancel (h);
143 * Generic error handler, called with the appropriate error code and
144 * the same closure specified at the creation of the message queue.
145 * Not every message queue implementation supports an error handler.
147 * @param cls closure with the `struct GNUNET_TESTBED_BarrierWaitHandle *`
148 * @param error error code
151 mq_error_handler (void *cls,
152 enum GNUNET_MQ_Error error)
154 struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
159 GNUNET_TESTBED_barrier_wait_cancel (h);
164 * Wait for a barrier to be crossed. This function should be called by the
165 * peers which have been started by the testbed. If the peer is not started by
166 * testbed this function may return error
168 * @param name the name of the barrier
169 * @param cb the barrier wait callback
170 * @param cb_cls the closure for @a cb
171 * @return barrier wait handle which can be used to cancel the waiting at
172 * anytime before the callback is called. NULL upon error.
174 struct GNUNET_TESTBED_BarrierWaitHandle *
175 GNUNET_TESTBED_barrier_wait (const char *name,
176 GNUNET_TESTBED_barrier_wait_cb cb,
179 struct GNUNET_TESTBED_BarrierWaitHandle *h
180 = GNUNET_new (struct GNUNET_TESTBED_BarrierWaitHandle);
181 struct GNUNET_MQ_MessageHandler handlers[] = {
182 GNUNET_MQ_hd_var_size (status,
183 GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS,
184 struct GNUNET_TESTBED_BarrierStatusMsg,
186 GNUNET_MQ_handler_end ()
188 struct GNUNET_MQ_Envelope *env;
189 struct GNUNET_TESTBED_BarrierWait *msg;
190 const char *cfg_filename;
193 GNUNET_assert (NULL != cb);
194 cfg_filename = getenv (ENV_TESTBED_CONFIG);
195 if (NULL == cfg_filename)
197 LOG (GNUNET_ERROR_TYPE_ERROR,
198 "Are you running under testbed?\n");
202 h->cfg = GNUNET_CONFIGURATION_create ();
204 GNUNET_CONFIGURATION_load (h->cfg,
207 LOG (GNUNET_ERROR_TYPE_ERROR,
208 "Unable to load configuration from file `%s'\n",
210 GNUNET_CONFIGURATION_destroy (h->cfg);
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Waiting on barrier `%s'\n",
217 h->name = GNUNET_strdup (name);
220 h->mq = GNUNET_CLIENT_connect (h->cfg,
227 LOG (GNUNET_ERROR_TYPE_ERROR,
228 "Unable to connect to local testbed-barrier service\n");
229 GNUNET_TESTBED_barrier_wait_cancel (h);
232 name_len = strlen (name); /* NOTE: unusual to not have 0-termination, change? */
233 env = GNUNET_MQ_msg_extra (msg,
235 GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT);
236 GNUNET_memcpy (msg->name,
239 GNUNET_MQ_send (h->mq,
246 * Cancel a barrier wait handle
248 * @param h the barrier wait handle
251 GNUNET_TESTBED_barrier_wait_cancel (struct GNUNET_TESTBED_BarrierWaitHandle *h)
255 GNUNET_MQ_destroy (h->mq);
258 GNUNET_free (h->name);
259 GNUNET_CONFIGURATION_destroy (h->cfg);
264 /* end of testbed_api_barriers.c */