GNUnet  0.11.x
testbed_api_cmd_controller.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2021 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_util_lib.h"
28 #include "gnunet_testing_ng_lib.h"
29 #include "gnunet-service-testbed.h"
30 #include "testbed_api_hosts.h"
32 
33 
37 #define LOG(kind, ...) \
38  GNUNET_log (kind, __VA_ARGS__)
39 
40 
47 static void
48 do_abort (void *cls)
49 {
50  struct ControllerState *cs = cls;
51 
52  if (GNUNET_NO == cs->host_ready)
53  {
54  LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
55  cs->abort_task = NULL;
57  }
58 }
59 
60 
67 static void
68 controller_cleanup (void *cls,
69  const struct GNUNET_TESTING_Command *cmd)
70 {
71  (void) cls;
72 }
73 
74 
82 static void
83 controller_cb (void *cls,
84  const struct GNUNET_TESTBED_EventInformation *event)
85 {
86  struct ControllerState *cs = cls;
87 
88  if (NULL != event->details.operation_finished.emsg)
89  {
90  LOG (GNUNET_ERROR_TYPE_ERROR, "There was an operation error: %s\n",
91  event->details.operation_finished.emsg);
93  }
94  else if (NULL == event->details.operation_finished.generic)
95  {
97  }
98 }
99 
100 
107 static void
108 registration_comp (void *cls,
109  const char *emsg)
110 {
111  struct ControllerState *cs = cls;
112 
113  if (NULL != emsg)
114  {
116  "There was an error during host registration: %s\n",
117  emsg);
119  }
120  else
121  {
122  cs->reg_handle = NULL;
123  cs->host_ready = GNUNET_YES;
124  }
125 }
126 
127 
137 static void
139  const struct GNUNET_CONFIGURATION_Handle *cfg_,
140  int status)
141 {
142  struct ControllerState *cs = cls;
143 
144  if (GNUNET_OK != status)
145  {
146  cs->cp = NULL;
147  return;
148  }
149 
150  cs->controller =
152  cs);
153  cs->reg_handle =
155  cs);
156 }
157 
158 
159 static void
160 controller_run (void *cls,
161  const struct GNUNET_TESTING_Command *cmd,
163 {
164  struct ControllerState *cs = cls;
165 
166  cs->is = is;
167  cs->host = GNUNET_TESTBED_host_create (cs->hostname, cs->username, cs->cfg,
168  cs->port);
170  cs->host,
172  cs);
173  cs->abort_task =
176  &do_abort,
177  cs);
178 }
179 
189 static int
190 controller_traits (void *cls,
191  const void **ret,
192  const char *trait,
193  unsigned int index)
194 {
195  (void) cls;
196 
197  struct ControllerState *cs = cls;
198 
199 
200  struct GNUNET_TESTING_Trait traits[] = {
201  {
202  .index = 0,
203  .trait_name = "controller",
204  .ptr = (const void *) cs->controller,
205  },
207  };
208 
209  return GNUNET_TESTING_get_trait (traits,
210  ret,
211  trait,
212  index);
213  return GNUNET_OK;
214 }
215 
216 
224 int
226  struct GNUNET_TESTBED_Controller **
227  controller)
228 {
229  return cmd->traits (cmd->cls,
230  (const void **) controller,
231  "controller",
232  (unsigned int) 0);
233 }
234 
235 
241 void
243 {
245  "Shutting down...\n");
246 
248 
249  if (NULL != cs->abort_task)
251  if (NULL != cs->reg_handle)
253  if (NULL != cs->controller)
255  if (NULL != cs->cfg)
257  if (NULL != cs->cp)
259  if (NULL != cs->host)
261 }
262 
263 
264 
267  const char *trusted_ip,
268  const char *hostname,
269  const char *username,
270  uint16_t port,
272  uint64_t event_mask)
273 {
274  struct ControllerState *cs;
275 
276  cs = GNUNET_new (struct ControllerState);
277  cs->event_mask = event_mask;
278  cs->trusted_ip = trusted_ip;
279  cs->hostname = hostname;
280  cs->username = username;
281  cs->port = port;
282  cs->cfg = cfg;
283 
284  struct GNUNET_TESTING_Command cmd = {
285  .cls = cs,
286  .label = label,
287  .run = &controller_run,
288  .cleanup = &controller_cleanup,
289  .traits = &controller_traits
290  };
291 
292  return cmd;
293 }
void GNUNET_TESTBED_cancel_registration(struct GNUNET_TESTBED_HostRegistrationHandle *handle)
Cancel the pending registration.
void GNUNET_TESTBED_controller_disconnect(struct GNUNET_TESTBED_Controller *c)
Stop the given controller (also will terminate all peers and controllers dependent on this controller...
Definition: testbed_api.c:1721
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
Handle to interact with a GNUnet testbed controller.
Definition: testbed_api.h:193
static void do_abort(void *cls)
abort task to run on test timed out
static int controller_traits(void *cls, const void **ret, const char *trait, unsigned int index)
data structures shared amongst components of TESTBED service
int GNUNET_TESTBED_get_trait_controller(const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTBED_Controller **controller)
Offer data from trait.
int host_ready
Flag indicating if host create with controller is ready.
#define GNUNET_TIME_UNIT_MINUTES
One minute.
union GNUNET_TESTBED_EventInformation::@43 details
Details about the event.
const char * username
Username to use for the login; may be NULL.
const char * label
Label for the command.
void GNUNET_TESTBED_controller_stop(struct GNUNET_TESTBED_ControllerProc *cproc)
Stop the controller process (also will terminate all peers and controllers dependent on this controll...
struct GNUNET_TESTING_Command GNUNET_TESTBED_cmd_controller(const char *label, const char *trusted_ip, const char *hostname, const char *username, uint16_t port, struct GNUNET_CONFIGURATION_Handle *cfg, uint64_t event_mask)
A command to be run by the interpreter.
Argument to GNUNET_TESTBED_ControllerCallback with details about the event.
static void controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
Signature of the event handler function called by the respective event controller.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t port
Port number to use for ssh; use 0 to let ssh decide.
struct GNUNET_TESTING_Interpreter * is
Interpreter state.
static void controller_status_cb(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg_, int status)
Callback to signal successful startup of the controller process.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1269
struct GNUNET_TESTBED_HostRegistrationHandle * reg_handle
Handle for host registration.
struct GNUNET_CONFIGURATION_Handle * cfg
The configuration to use as a template while starting a controller on this host.
int controller_going_down
Flag indicating controller is going down.
Global state of the interpreter, used by a command to access information about other commands...
Definition: testing.h:34
unsigned int index
Index number associated with the trait.
struct GNUNET_TESTING_Interpreter * is
struct GNUNET_TESTBED_Host * host
The host to run peers and controllers on.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
uint16_t status
See PRISM_STATUS_*-constants.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:311
void GNUNET_TESTBED_operation_done(struct GNUNET_TESTBED_Operation *operation)
This function is used to signal that the event information (struct GNUNET_TESTBED_EventInformation) f...
Definition: testbed_api.c:2045
int(* traits)(void *cls, const void **ret, const char *trait, unsigned int index)
Extract information from a command that is useful for other commands.
static void controller_run(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *is)
uint64_t event_mask
Global event mask for all testbed events.
struct GNUNET_TESTBED_Host * GNUNET_TESTBED_host_create(const char *hostname, const char *username, const struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t port)
Create a host to run peers and controllers on.
struct GNUNET_TESTBED_ControllerProc * cp
The controller process.
void GNUNET_TESTBED_host_destroy(struct GNUNET_TESTBED_Host *host)
Destroy a host handle.
struct GNUNET_SCHEDULER_Task * abort_task
Abort task identifier.
int GNUNET_TESTING_get_trait(const struct GNUNET_TESTING_Trait *traits, const void **ret, const char *trait, unsigned int index)
Extract a trait.
struct GNUNET_TESTBED_Controller * GNUNET_TESTBED_controller_connect(struct GNUNET_TESTBED_Host *host, uint64_t event_mask, GNUNET_TESTBED_ControllerCallback cc, void *cc_cls)
Connect to a controller process.
Definition: testbed_api.c:1555
struct GNUNET_TESTBED_EventInformation::@43::@48 operation_finished
Details about an operation finished event.
uint64_t event_mask
A bit mask with set of events to call the controller for.
static char * hostname
Our hostname; we give this to all the peers we start.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:83
configuration data
Definition: configuration.c:84
#define LOG(kind,...)
Generic logging shortcut.
void * cls
Closure for all commands with command-specific context information.
static void controller_cleanup(void *cls, const struct GNUNET_TESTING_Command *cmd)
internal API to access the &#39;hosts&#39; subsystem
void GNUNET_TESTBED_shutdown_controller(struct ControllerState *cs)
Shutdown nicely.
const char * hostname
Name of the host, use "NULL" for localhost.
struct GNUNET_TESTING_Trait GNUNET_TESTING_trait_end(void)
"end" trait.
struct GNUNET_TESTBED_HostRegistrationHandle * GNUNET_TESTBED_register_host(struct GNUNET_TESTBED_Controller *controller, struct GNUNET_TESTBED_Host *host, GNUNET_TESTBED_HostRegistrationCompletion cc, void *cc_cls)
Register a host with the controller.
static void registration_comp(void *cls, const char *emsg)
Callback which will be called to after a host registration succeeded or failed.
struct GNUNET_TESTBED_Controller * controller
The controller handle.
struct GNUNET_TESTBED_ControllerProc * GNUNET_TESTBED_controller_start(const char *trusted_ip, struct GNUNET_TESTBED_Host *host, GNUNET_TESTBED_ControllerStatusCallback cb, void *cls)
Starts a controller process at the given host.
const char * trusted_ip
The ip address of the controller which will be set as TRUSTED HOST(all connections form this ip are p...
struct GNUNET_TESTBED_Operation * op
Handle for the corresponding operation that generated this event.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972