aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/testbed_api_cmd_controller.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testbed/testbed_api_cmd_controller.c')
-rw-r--r--src/testbed/testbed_api_cmd_controller.c294
1 files changed, 294 insertions, 0 deletions
diff --git a/src/testbed/testbed_api_cmd_controller.c b/src/testbed/testbed_api_cmd_controller.c
new file mode 100644
index 000000000..d65f41760
--- /dev/null
+++ b/src/testbed/testbed_api_cmd_controller.c
@@ -0,0 +1,294 @@
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/**
22 * @file testbed/testbed_api_cmd_controller.c
23 * @brief Command to create a controller.
24 * @author t3sserakt
25 */
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"
31#include "gnunet_testbed_ng_service.h"
32
33
34/**
35 * Generic logging shortcut
36 */
37#define LOG(kind, ...) \
38 GNUNET_log (kind, __VA_ARGS__)
39
40
41/**
42 * abort task to run on test timed out
43 *
44 * @param cls NULL
45 * @param tc the task context
46 */
47static void
48do_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;
56 GNUNET_TESTBED_shutdown_controller (cs);
57 }
58}
59
60
61/**
62*
63*
64* @param cls closure
65* @param cmd current CMD being cleaned up.
66*/
67static void
68controller_cleanup (void *cls,
69 const struct GNUNET_TESTING_Command *cmd)
70{
71 (void) cls;
72}
73
74
75/**
76 * Signature of the event handler function called by the
77 * respective event controller.
78 *
79 * @param cls closure
80 * @param event information about the event
81 */
82static void
83controller_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);
92 GNUNET_TESTBED_shutdown_controller (cs);
93 }
94 else if (NULL == event->details.operation_finished.generic)
95 {
96 GNUNET_TESTBED_operation_done (event->op);
97 }
98}
99
100
101/**
102 * Callback which will be called to after a host registration succeeded or failed
103 *
104 * @param cls the host which has been registered
105 * @param emsg the error message; NULL if host registration is successful
106 */
107static void
108registration_comp (void *cls,
109 const char *emsg)
110{
111 struct ControllerState *cs = cls;
112
113 if (NULL != emsg)
114 {
115 LOG (GNUNET_ERROR_TYPE_ERROR,
116 "There was an error during host registration: %s\n",
117 emsg);
118 GNUNET_TESTBED_shutdown_controller (cs);
119 }
120 else
121 {
122 cs->reg_handle = NULL;
123 cs->host_ready = GNUNET_YES;
124 GNUNET_TESTING_interpreter_next (cs->is);
125 }
126}
127
128
129/**
130 * Callback to signal successfull startup of the controller process
131 *
132 * @param cls the closure from GNUNET_TESTBED_controller_start()
133 * @param cfg the configuration with which the controller has been started;
134 * NULL if status is not #GNUNET_OK
135 * @param status #GNUNET_OK if the startup is successfull; #GNUNET_SYSERR if not,
136 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
137 */
138static void
139controller_status_cb (void *cls,
140 const struct GNUNET_CONFIGURATION_Handle *cfg_,
141 int status)
142{
143 struct ControllerState *cs = cls;
144
145 if (GNUNET_OK != status)
146 {
147 cs->cp = NULL;
148 return;
149 }
150
151 cs->controller =
152 GNUNET_TESTBED_controller_connect (cs->host, cs->event_mask, &controller_cb,
153 cs);
154 cs->reg_handle =
155 GNUNET_TESTBED_register_host (cs->controller, cs->host, &registration_comp,
156 cs);
157}
158
159
160static void
161controller_run (void *cls,
162 const struct GNUNET_TESTING_Command *cmd,
163 struct GNUNET_TESTING_Interpreter *is)
164{
165 struct ControllerState *cs = cls;
166
167 cs->is = is;
168 cs->host = GNUNET_TESTBED_host_create (cs->hostname, cs->username, cs->cfg,
169 cs->port);
170 cs->cp = GNUNET_TESTBED_controller_start (cs->trusted_ip,
171 cs->host,
172 &controller_status_cb,
173 cs);
174 cs->abort_task =
175 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
176 (GNUNET_TIME_UNIT_MINUTES, 5),
177 &do_abort,
178 cs);
179}
180
181/**
182*
183*
184* @param cls closure.
185* @param[out] ret result
186* @param trait name of the trait.
187* @param index index number of the object to offer.
188* @return #GNUNET_OK on success.
189*/
190static int
191controller_traits (void *cls,
192 const void **ret,
193 const char *trait,
194 unsigned int index)
195{
196 (void) cls;
197
198 struct ControllerState *cs = cls;
199
200
201 struct GNUNET_TESTING_Trait traits[] = {
202 {
203 .index = 0,
204 .trait_name = "controller",
205 .ptr = (const void *) cs->controller,
206 },
207 GNUNET_TESTING_trait_end ()
208 };
209
210 return GNUNET_TESTING_get_trait (traits,
211 ret,
212 trait,
213 index);
214 return GNUNET_OK;
215}
216
217
218/**
219 * Offer data from trait
220 *
221 * @param cmd command to extract the controller from.
222 * @param pt pointer to controller.
223 * @return #GNUNET_OK on success.
224 */
225int
226GNUNET_TESTBED_get_trait_controller (const struct GNUNET_TESTING_Command *cmd,
227 struct GNUNET_TESTBED_Controller **
228 controller)
229{
230 return cmd->traits (cmd->cls,
231 (const void **) controller,
232 "controller",
233 (unsigned int) 0);
234}
235
236
237/**
238 * Shutdown nicely
239 *
240 * @param cs controller state.
241 */
242void
243GNUNET_TESTBED_shutdown_controller (struct ControllerState *cs)
244{
245 LOG (GNUNET_ERROR_TYPE_DEBUG,
246 "Shutting down...\n");
247
248 cs->controller_going_down = GNUNET_YES;
249
250 if (NULL != cs->abort_task)
251 GNUNET_SCHEDULER_cancel (cs->abort_task);
252 if (NULL != cs->reg_handle)
253 GNUNET_TESTBED_cancel_registration (cs->reg_handle);
254 if (NULL != cs->controller)
255 GNUNET_TESTBED_controller_disconnect (cs->controller);
256 if (NULL != cs->cfg)
257 GNUNET_CONFIGURATION_destroy (cs->cfg);
258 if (NULL != cs->cp)
259 GNUNET_TESTBED_controller_stop (cs->cp);
260 if (NULL != cs->host)
261 GNUNET_TESTBED_host_destroy (cs->host);
262}
263
264
265
266struct GNUNET_TESTING_Command
267GNUNET_TESTBED_cmd_controller (const char *label,
268 const char *trusted_ip,
269 const char *hostname,
270 const char *username,
271 uint16_t port,
272 struct GNUNET_CONFIGURATION_Handle *cfg,
273 uint64_t event_mask)
274{
275 struct ControllerState *cs;
276
277 cs = GNUNET_new (struct ControllerState);
278 cs->event_mask = event_mask;
279 cs->trusted_ip = trusted_ip;
280 cs->hostname = hostname;
281 cs->username = username;
282 cs->port = port;
283 cs->cfg = cfg;
284
285 struct GNUNET_TESTING_Command cmd = {
286 .cls = cs,
287 .label = label,
288 .run = &controller_run,
289 .cleanup = &controller_cleanup,
290 .traits = &controller_traits
291 };
292
293 return cmd;
294}