GNUnet  0.11.x
testbed_api_cmd_peer.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 
21 
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_ng_lib.h"
31 #include "gnunet-service-testbed.h"
32 #include "testbed_api_peers.h"
33 
34 
38 #define LOG(kind, ...) \
39  GNUNET_log (kind, __VA_ARGS__)
40 
41 
51 static int
53  const void **ret,
54  const char *trait,
55  unsigned int index)
56 {
57  struct PeerCmdState *ps = cls;
58 
59  struct GNUNET_TESTING_Trait traits[] = {
60  {
61  .index = 0,
62  .trait_name = "peer",
63  .ptr = (const void *) ps->peer,
64  },
66  };
67 
68  return GNUNET_TESTING_get_trait (traits,
69  ret,
70  trait,
71  index);
72 
73  return GNUNET_OK;
74 }
75 
83 int
85  struct GNUNET_TESTBED_Peer **
86  peer)
87 {
88  return cmd->traits (cmd->cls,
89  (const void **) peer,
90  "peer",
91  (unsigned int) 0);
92 }
93 
94 
101 static void
102 peer_cleanup (void *cls,
103  const struct GNUNET_TESTING_Command *cmd)
104 {
105  (void) cls;
106 }
107 
108 
115 static void
116 do_abort (void *cls)
117 {
118  struct PeerCmdState *ps = cls;
119 
120  if (GNUNET_NO == ps->peer_ready)
121  {
122  LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
123  ps->abort_task = NULL;
125  }
126 }
127 
128 
136 static void
137 peer_started_cb (void *cls,
138  const char *emsg)
139 {
140  struct PeerCmdState *ps = cls;
141 
143  if (NULL == emsg)
144  {
145  ps->peer_ready = GNUNET_YES;
146  }
147  else
148  {
149  LOG (GNUNET_ERROR_TYPE_ERROR, "There was an error starting a peer: %s\n",
150  emsg);
151  }
152 
153 }
154 
155 
165 static void
166 peer_create_cb (void *cls,
167  struct GNUNET_TESTBED_Peer *peer,
168  const char *emsg)
169 {
170  struct PeerCmdState *ps = cls;
171 
172  ps->peer = peer;
175  peer,
177  ps);
178 }
179 
180 
181 static void
182 peer_run (void *cls,
183  const struct GNUNET_TESTING_Command *cmd,
185 {
186  struct PeerCmdState *ps = cls;
187  const struct GNUNET_TESTING_Command *controller_cmd;
188  struct GNUNET_TESTBED_Controller *controller;
189 
190  ps->is = is;
192  ps->controller_label);
193  GNUNET_TESTBED_get_trait_controller (controller_cmd,
194  &controller);
195  ps->host = GNUNET_TESTBED_host_create (ps->hostname, ps->username, ps->cfg,
196  ps->port);
197  ps->operation =
198  GNUNET_TESTBED_peer_create (controller,
199  ps->host,
200  ps->cfg,
202  ps);
203 
204  ps->abort_task =
207  &do_abort,
208  ps);
209 }
210 
211 
212 void
213 peer_stopped_cb (void *cls,
214  const char *emsg)
215 {
216  struct PeerCmdState *ps = cls;
217 
218  if (NULL != emsg)
219  {
220  LOG (GNUNET_ERROR_TYPE_ERROR, "There was an error stopping a peer: %s\n",
221  emsg);
222  }
225 }
226 
227 
233 void
235 {
237  "Shutting down...\n");
238 
240 
241  if (NULL != ps->abort_task)
243  if (NULL != ps->cfg)
245  if (NULL != ps->host)
247 
250  ps);
251 
252 }
253 
254 
257  const char *controller_label,
258  const char *hostname,
259  const char *username,
260  uint16_t port,
262 {
263  struct PeerCmdState *ps;
264 
265  ps = GNUNET_new (struct PeerCmdState);
266  ps->hostname = hostname;
267  ps->username = username;
268  ps->port = port;
269  ps->cfg = cfg;
271 
272  struct GNUNET_TESTING_Command cmd = {
273  .cls = ps,
274  .label = label,
275  .run = &peer_run,
276  .cleanup = &peer_cleanup,
277  .traits = &peer_traits
278  };
279 
280  return cmd;
281 }
struct GNUNET_TESTBED_Peer * peer
Peer to start.
struct GNUNET_TESTING_Interpreter * is
Interpreter state.
struct GNUNET_TESTBED_Operation * GNUNET_TESTBED_peer_stop(void *op_cls, struct GNUNET_TESTBED_Peer *peer, GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
Stop the given peer.
static void peer_started_cb(void *cls, const char *emsg)
Functions of this signature are called when a peer has been successfully created. ...
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 int peer_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 peer_going_down
Flag indicating controller is going down.
static void do_abort(void *cls)
abort task to run on test timed out
#define GNUNET_TIME_UNIT_MINUTES
One minute.
const char * label
Label for the command.
struct GNUNET_SCHEDULER_Task * abort_task
Abort task identifier.
struct GNUNET_TESTBED_Operation * GNUNET_TESTBED_peer_start(void *op_cls, struct GNUNET_TESTBED_Peer *peer, GNUNET_TESTBED_PeerChurnCallback pcc, void *pcc_cls)
Start the given peer.
A command to be run by the interpreter.
const char * username
Username to use for the login; may be NULL.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
uint16_t port
Port number to use for ssh; use 0 to let ssh decide.
struct GNUNET_TESTBED_Operation * operation
Handle to operation.
void GNUNET_TESTBED_shutdown_peer(struct PeerCmdState *ps)
Shutdown nicely.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static void peer_create_cb(void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
Functions of this signature are called when a peer has been successfully created. ...
static void peer_cleanup(void *cls, const struct GNUNET_TESTING_Command *cmd)
void peer_stopped_cb(void *cls, const char *emsg)
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
Global state of the interpreter, used by a command to access information about other commands...
Definition: testing.h:34
const char * controller_label
The label of a controller command.
const struct GNUNET_TESTING_Command * GNUNET_TESTING_interpreter_lookup_command(const char *label)
Lookup command by label.
unsigned int index
Index number associated with the trait.
struct GNUNET_TESTING_Interpreter * is
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
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
struct GNUNET_TESTBED_Host * host
The host to run peers and controllers on.
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.
int peer_ready
Flag indicating if peer is ready.
static void peer_run(void *cls, const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTING_Interpreter *is)
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.
void GNUNET_TESTBED_host_destroy(struct GNUNET_TESTBED_Host *host)
Destroy a host handle.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
int GNUNET_TESTING_get_trait(const struct GNUNET_TESTING_Trait *traits, const void **ret, const char *trait, unsigned int index)
Extract a trait.
int GNUNET_TESTBED_get_trait_peer(const struct GNUNET_TESTING_Command *cmd, struct GNUNET_TESTBED_Peer **peer)
Offer data from trait.
struct GNUNET_CONFIGURATION_Handle * cfg
The configuration to use as a template while starting a controller on this host.
struct GNUNET_TESTBED_Operation * GNUNET_TESTBED_peer_destroy(struct GNUNET_TESTBED_Peer *peer)
Destroy the given peer; the peer should have been stopped first (if it was started).
internal API to access the &#39;peers&#39; subsystem
#define LOG(kind,...)
Generic logging shortcut.
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
void * cls
Closure for all commands with command-specific context information.
struct GNUNET_TESTING_Command GNUNET_TESTBED_cmd_peer(const char *label, const char *controller_label, const char *hostname, const char *username, uint16_t port, struct GNUNET_CONFIGURATION_Handle *cfg)
A peer controlled by the testing framework.
struct GNUNET_TESTBED_Operation * GNUNET_TESTBED_peer_create(struct GNUNET_TESTBED_Controller *controller, struct GNUNET_TESTBED_Host *host, const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_TESTBED_PeerCreateCallback cb, void *cls)
Create the given peer at the specified host using the given controller.
struct GNUNET_TESTING_Trait GNUNET_TESTING_trait_end(void)
"end" trait.
const char * hostname
Name of the host, use "NULL" for localhost.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972