GNUnet  0.10.x
testbed_api_barriers.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2008--2013, 2016 GNUnet e.V.
4 
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.
9 
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.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
26 #include "platform.h"
27 #include "gnunet_testbed_service.h"
28 #include "testbed_api.h"
29 
33 #define LOG(type, ...) \
34  GNUNET_log_from (type, "testbed-api-barriers", __VA_ARGS__);
35 
39 #define LOG_DEBUG(...) \
40  LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
41 
42 
47 {
51  char *name;
52 
57 
62 
67 
71  void *cb_cls;
72 };
73 
74 
75 
83 static int
84 check_status (void *cls,
86 {
87  /* FIXME: this fails to actually check that the message
88  follows the protocol spec (0-terminations!). However,
89  not critical as #handle_status() doesn't interpret the
90  variable-size part anyway right now. */
91  return GNUNET_OK;
92 }
93 
94 
102 static void
103 handle_status (void *cls,
104  const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
105 {
106  struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
107 
109  "Got barrier status %d\n",
110  (int) ntohs (msg->status));
111  switch (ntohs (msg->status))
112  {
114  h->cb (h->cb_cls,
115  h->name,
116  GNUNET_SYSERR);
117  break;
119  h->cb (h->cb_cls,
120  h->name,
121  GNUNET_SYSERR);
122  GNUNET_break (0);
123  break;
125  h->cb (h->cb_cls,
126  h->name,
127  GNUNET_OK);
128  break;
129  default:
130  GNUNET_break_op (0);
131  h->cb (h->cb_cls,
132  h->name,
133  GNUNET_SYSERR);
134  break;
135  }
137 }
138 
139 
148 static void
149 mq_error_handler (void *cls,
150  enum GNUNET_MQ_Error error)
151 {
152  struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
153 
154  h->cb (h->cb_cls,
155  h->name,
156  GNUNET_SYSERR);
158 }
159 
160 
175  void *cb_cls)
176 {
179  struct GNUNET_MQ_MessageHandler handlers[] = {
183  h),
185  };
186  struct GNUNET_MQ_Envelope *env;
188  const char *cfg_filename;
189  size_t name_len;
190 
191  GNUNET_assert (NULL != cb);
192  cfg_filename = getenv (ENV_TESTBED_CONFIG);
193  if (NULL == cfg_filename)
194  {
196  "Are you running under testbed?\n");
197  GNUNET_free (h);
198  return NULL;
199  }
201  if (GNUNET_OK !=
203  cfg_filename))
204  {
206  "Unable to load configuration from file `%s'\n",
207  cfg_filename);
209  GNUNET_free (h);
210  return NULL;
211  }
213  "Waiting on barrier `%s'\n",
214  name);
215  h->name = GNUNET_strdup (name);
216  h->cb = cb;
217  h->cb_cls = cb_cls;
218  h->mq = GNUNET_CLIENT_connect (h->cfg,
219  "testbed-barrier",
220  handlers,
222  h);
223  if (NULL == h->mq)
224  {
226  "Unable to connect to local testbed-barrier service\n");
228  return NULL;
229  }
230  name_len = strlen (name); /* NOTE: unusual to not have 0-termination, change? */
231  env = GNUNET_MQ_msg_extra (msg,
232  name_len,
234  GNUNET_memcpy (msg->name,
235  name,
236  name_len);
237  GNUNET_MQ_send (h->mq,
238  env);
239  return h;
240 }
241 
242 
248 void
250 {
251  if (NULL != h->mq)
252  {
253  GNUNET_MQ_destroy (h->mq);
254  h->mq = NULL;
255  }
256  GNUNET_free (h->name);
258  GNUNET_free (h);
259 }
260 
261 /* end of testbed_api_barriers.c */
#define GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS
Message for signalling status of a barrier.
static char * cfg_filename
Name of the configuration file.
void GNUNET_TESTBED_barrier_wait_cancel(struct GNUNET_TESTBED_BarrierWaitHandle *h)
Cancel a barrier wait handle.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
#define LOG(type,...)
Logging shorthand.
char name[0]
The name of the barrier they have reached.
Definition: testbed.h:901
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:901
GNUNET_MQ_Error
Error codes for the queue.
struct GNUNET_CONFIGURATION_Handle * cfg
Then configuration used for the client connection.
#define GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT
Message sent by a peer when it has reached a barrier and is waiting for it to be crossed.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_MQ_Handle * mq
The testbed-barrier service message queue.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
Message sent from peers to the testbed-barrier service to indicate that they have reached a barrier a...
Definition: testbed.h:891
int GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
uint16_t status
status.
Definition: testbed.h:872
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
#define GNUNET_memcpy(dst, src, n)
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
uint16_t status
See PRISM_STATUS_*-constants.
GNUNET_TESTBED_barrier_wait_cb cb
The barrier wait callback.
Message handler for a specific message type.
static void handle_status(void *cls, const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
Type of a function to call when we receive a message from the service.
void * cb_cls
The closure for cb.
struct GNUNET_TESTBED_BarrierWaitHandle * GNUNET_TESTBED_barrier_wait(const char *name, GNUNET_TESTBED_barrier_wait_cb cb, void *cb_cls)
Wait for a barrier to be crossed.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
char * getenv()
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
Interface for functions internally exported from testbed_api.c.
Handle to a message queue.
Definition: mq.c:85
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
configuration data
Definition: configuration.c:85
#define GNUNET_log(kind,...)
void(* GNUNET_TESTBED_barrier_wait_cb)(void *cls, const char *name, int status)
Functions of this type are to be given as acallback argumetn to GNUNET_TESTBED_barrier_wait().
Barrier initialised successfully.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
Message for signalling status changes of a barrier.
Definition: testbed.h:862
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:353
char * name
The name of the barrier.
static int check_status(void *cls, const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
Check if barrier status message is well-formed.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define ENV_TESTBED_CONFIG
The environmental variable which when available refers to the configuration file the local testbed co...
Definition: testbed.h:817
#define GNUNET_free(ptr)
Wrapper around free.