GNUnet  0.19.4
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 
82 static int
83 check_status (void *cls,
85 {
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. */
90  return GNUNET_OK;
91 }
92 
93 
101 static void
102 handle_status (void *cls,
103  const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
104 {
105  struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
106 
108  "Got barrier status %d\n",
109  (int) ntohs (msg->status));
110  switch (ntohs (msg->status))
111  {
113  h->cb (h->cb_cls,
114  h->name,
115  GNUNET_SYSERR);
116  break;
117 
119  h->cb (h->cb_cls,
120  h->name,
121  GNUNET_SYSERR);
122  GNUNET_break (0);
123  break;
124 
126  h->cb (h->cb_cls,
127  h->name,
128  GNUNET_OK);
129  break;
130 
131  default:
132  GNUNET_break_op (0);
133  h->cb (h->cb_cls,
134  h->name,
135  GNUNET_SYSERR);
136  break;
137  }
139 }
140 
141 
150 static void
151 mq_error_handler (void *cls,
152  enum GNUNET_MQ_Error error)
153 {
154  struct GNUNET_TESTBED_BarrierWaitHandle *h = cls;
155 
156  h->cb (h->cb_cls,
157  h->name,
158  GNUNET_SYSERR);
160 }
161 
162 
166  void *cb_cls)
167 {
174  h),
176  };
177  struct GNUNET_MQ_Envelope *env;
179  const char *cfg_filename;
180  size_t name_len;
181 
182  GNUNET_assert (NULL != cb);
184  if (NULL == cfg_filename)
185  {
187  "Are you running under testbed?\n");
188  GNUNET_free (h);
189  return NULL;
190  }
192  if (GNUNET_OK !=
194  cfg_filename))
195  {
197  "Unable to load configuration from file `%s'\n",
198  cfg_filename);
200  GNUNET_free (h);
201  return NULL;
202  }
204  "Waiting on barrier `%s'\n",
205  name);
206  h->name = GNUNET_strdup (name);
207  h->cb = cb;
208  h->cb_cls = cb_cls;
210  "testbed-barrier",
211  handlers,
213  h);
214  if (NULL == h->mq)
215  {
217  "Unable to connect to local testbed-barrier service\n");
219  return NULL;
220  }
221  name_len = strlen (name); /* NOTE: unusual to not have 0-termination, change? */
223  name_len,
225  GNUNET_memcpy (msg->name,
226  name,
227  name_len);
228  GNUNET_MQ_send (h->mq,
229  env);
230  return h;
231 }
232 
233 
239 void
241 {
242  if (NULL != h->mq)
243  {
245  h->mq = NULL;
246  }
247  GNUNET_free (h->name);
249  GNUNET_free (h);
250 }
251 
252 
253 /* end of testbed_api_barriers.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
char * getenv()
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static char * cfg_filename
Name of the configuration file.
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
uint16_t status
See PRISM_STATUS_*-constants.
API for writing tests and creating large-scale emulation testbeds for GNUnet.
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:1057
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
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:304
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#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:62
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:683
#define GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS
Message for signalling status of a barrier.
#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.
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.
void(* GNUNET_TESTBED_barrier_wait_cb)(void *cls, const char *name, int status)
Functions of this type are to be given as acallback argument to GNUNET_TESTBED_barrier_wait().
void GNUNET_TESTBED_barrier_wait_cancel(struct GNUNET_TESTBED_BarrierWaitHandle *h)
Cancel a barrier wait handle.
@ GNUNET_TESTBED_BARRIERSTATUS_CROSSED
Barrier is crossed.
@ GNUNET_TESTBED_BARRIERSTATUS_ERROR
Error status.
@ GNUNET_TESTBED_BARRIERSTATUS_INITIALISED
Barrier initialised successfully.
const char * name
struct GNUNET_MQ_Handle * mq
Our connection to the ARM service.
Definition: arm_api.c:107
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration that we are using.
Definition: arm_api.c:112
Handle to a message queue.
Definition: mq.c:87
Message handler for a specific message type.
Message for signalling status changes of a barrier.
Definition: testbed.h:822
struct GNUNET_MQ_Handle * mq
The testbed-barrier service message queue.
void * cb_cls
The closure for cb.
struct GNUNET_CONFIGURATION_Handle * cfg
Then configuration used for the client connection.
GNUNET_TESTBED_barrier_wait_cb cb
The barrier wait callback.
char * name
The name of the barrier.
Message sent from peers to the testbed-barrier service to indicate that they have reached a barrier a...
Definition: testbed.h:851
#define ENV_TESTBED_CONFIG
The environmental variable which when available refers to the configuration file the local testbed co...
Definition: testbed.h:776
Interface for functions internally exported from testbed_api.c.
#define LOG(type,...)
Logging shorthand.
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...
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.
static int check_status(void *cls, const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
Check if barrier status message is well-formed.