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 
50  char *name;
51 
56 
61 
66 
70  void *cb_cls;
71 };
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
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 
177  void *cb_cls)
178 {
181  struct GNUNET_MQ_MessageHandler handlers[] = {
185  h),
187  };
188  struct GNUNET_MQ_Envelope *env;
190  const char *cfg_filename;
191  size_t name_len;
192 
193  GNUNET_assert(NULL != cb);
194  cfg_filename = getenv(ENV_TESTBED_CONFIG);
195  if (NULL == cfg_filename)
196  {
198  "Are you running under testbed?\n");
199  GNUNET_free(h);
200  return NULL;
201  }
203  if (GNUNET_OK !=
205  cfg_filename))
206  {
208  "Unable to load configuration from file `%s'\n",
209  cfg_filename);
211  GNUNET_free(h);
212  return NULL;
213  }
215  "Waiting on barrier `%s'\n",
216  name);
217  h->name = GNUNET_strdup(name);
218  h->cb = cb;
219  h->cb_cls = cb_cls;
220  h->mq = GNUNET_CLIENT_connect(h->cfg,
221  "testbed-barrier",
222  handlers,
224  h);
225  if (NULL == h->mq)
226  {
228  "Unable to connect to local testbed-barrier service\n");
230  return NULL;
231  }
232  name_len = strlen(name); /* NOTE: unusual to not have 0-termination, change? */
233  env = GNUNET_MQ_msg_extra(msg,
234  name_len,
236  GNUNET_memcpy(msg->name,
237  name,
238  name_len);
239  GNUNET_MQ_send(h->mq,
240  env);
241  return h;
242 }
243 
244 
250 void
252 {
253  if (NULL != h->mq)
254  {
255  GNUNET_MQ_destroy(h->mq);
256  h->mq = NULL;
257  }
258  GNUNET_free(h->name);
260  GNUNET_free(h);
261 }
262 
263 /* 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:831
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:900
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_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#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:822
int GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
uint16_t status
status.
Definition: testbed.h:803
#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_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:76
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:84
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:83
#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:821
Message for signalling status changes of a barrier.
Definition: testbed.h:794
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:351
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:751
#define GNUNET_free(ptr)
Wrapper around free.