diff options
Diffstat (limited to 'src/testbed/testbed_api_cmd_peer.c')
-rw-r--r-- | src/testbed/testbed_api_cmd_peer.c | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/src/testbed/testbed_api_cmd_peer.c b/src/testbed/testbed_api_cmd_peer.c new file mode 100644 index 000000000..4a727bc94 --- /dev/null +++ b/src/testbed/testbed_api_cmd_peer.c | |||
@@ -0,0 +1,249 @@ | |||
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 | /** | ||
23 | * @file testbed/testbed_api_cmd_controller.c | ||
24 | * @brief Command to create a controller. | ||
25 | * @author t3sserakt | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_testing_ng_lib.h" | ||
30 | #include "gnunet_testbed_ng_service.h" | ||
31 | #include "gnunet-service-testbed.h" | ||
32 | #include "testbed_api_peers.h" | ||
33 | |||
34 | |||
35 | /** | ||
36 | * Generic logging shortcut | ||
37 | */ | ||
38 | #define LOG(kind, ...) \ | ||
39 | GNUNET_log (kind, __VA_ARGS__) | ||
40 | |||
41 | |||
42 | /** | ||
43 | * | ||
44 | * | ||
45 | * @param cls closure. | ||
46 | * @param[out] ret result | ||
47 | * @param trait name of the trait. | ||
48 | * @param index index number of the object to offer. | ||
49 | * @return #GNUNET_OK on success. | ||
50 | */ | ||
51 | static int | ||
52 | peer_traits (void *cls, | ||
53 | const void **ret, | ||
54 | const char *trait, | ||
55 | unsigned int index) | ||
56 | { | ||
57 | (void) cls; | ||
58 | return GNUNET_OK; | ||
59 | } | ||
60 | |||
61 | |||
62 | /** | ||
63 | * | ||
64 | * | ||
65 | * @param cls closure | ||
66 | * @param cmd current CMD being cleaned up. | ||
67 | */ | ||
68 | static void | ||
69 | peer_cleanup (void *cls, | ||
70 | const struct GNUNET_TESTING_Command *cmd) | ||
71 | { | ||
72 | (void) cls; | ||
73 | } | ||
74 | |||
75 | |||
76 | /** | ||
77 | * abort task to run on test timed out | ||
78 | * | ||
79 | * @param cls NULL | ||
80 | * @param tc the task context | ||
81 | */ | ||
82 | static void | ||
83 | do_abort (void *cls) | ||
84 | { | ||
85 | struct PeerCmdState *ps = cls; | ||
86 | |||
87 | if (GNUNET_NO == ps->peer_ready) | ||
88 | { | ||
89 | LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n"); | ||
90 | ps->abort_task = NULL; | ||
91 | GNUNET_TESTBED_shutdown_peer (ps); | ||
92 | } | ||
93 | } | ||
94 | |||
95 | |||
96 | /** | ||
97 | * Functions of this signature are called when a peer has been successfully | ||
98 | * created | ||
99 | * | ||
100 | * @param cls the closure from GNUNET_TESTBED_peer_create() | ||
101 | * @param emsg MAY contain an error description, if starting peer failed. | ||
102 | */ | ||
103 | static void | ||
104 | peer_started_cb (void *cls, | ||
105 | const char *emsg) | ||
106 | { | ||
107 | struct PeerCmdState *ps = cls; | ||
108 | |||
109 | GNUNET_TESTBED_operation_done (ps->operation); | ||
110 | if (NULL == emsg) | ||
111 | { | ||
112 | ps->peer_ready = GNUNET_YES; | ||
113 | GNUNET_TESTING_interpreter_next (ps->is); | ||
114 | } | ||
115 | else | ||
116 | { | ||
117 | LOG (GNUNET_ERROR_TYPE_ERROR, "There was an error starting a peer: %s\n", | ||
118 | emsg); | ||
119 | } | ||
120 | |||
121 | } | ||
122 | |||
123 | |||
124 | /** | ||
125 | * Functions of this signature are called when a peer has been successfully | ||
126 | * created | ||
127 | * | ||
128 | * @param cls the closure from GNUNET_TESTBED_peer_create() | ||
129 | * @param peer the handle for the created peer; NULL on any error during | ||
130 | * creation | ||
131 | * @param emsg NULL if peer is not NULL; else MAY contain the error description | ||
132 | */ | ||
133 | static void | ||
134 | peer_create_cb (void *cls, | ||
135 | struct GNUNET_TESTBED_Peer *peer, | ||
136 | const char *emsg) | ||
137 | { | ||
138 | struct PeerCmdState *ps = cls; | ||
139 | |||
140 | ps->peer = peer; | ||
141 | GNUNET_TESTBED_operation_done (ps->operation); | ||
142 | ps->operation = GNUNET_TESTBED_peer_start (NULL, | ||
143 | peer, | ||
144 | &peer_started_cb, | ||
145 | ps); | ||
146 | } | ||
147 | |||
148 | |||
149 | static void | ||
150 | peer_run (void *cls, | ||
151 | const struct GNUNET_TESTING_Command *cmd, | ||
152 | struct GNUNET_TESTING_Interpreter *is) | ||
153 | { | ||
154 | struct PeerCmdState *ps = cls; | ||
155 | const struct GNUNET_TESTING_Command *controller_cmd; | ||
156 | struct GNUNET_TESTBED_Controller *controller; | ||
157 | |||
158 | ps->is = is; | ||
159 | controller_cmd = GNUNET_TESTING_interpreter_lookup_command ( | ||
160 | ps->controller_label); | ||
161 | GNUNET_TESTBED_get_trait_controller (controller_cmd, | ||
162 | &controller); | ||
163 | ps->host = GNUNET_TESTBED_host_create (ps->hostname, ps->username, ps->cfg, | ||
164 | ps->port); | ||
165 | ps->operation = | ||
166 | GNUNET_TESTBED_peer_create (controller, | ||
167 | ps->host, | ||
168 | ps->cfg, | ||
169 | &peer_create_cb, | ||
170 | ps); | ||
171 | |||
172 | ps->abort_task = | ||
173 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | ||
174 | (GNUNET_TIME_UNIT_MINUTES, 5), | ||
175 | &do_abort, | ||
176 | ps); | ||
177 | } | ||
178 | |||
179 | |||
180 | void | ||
181 | peer_stopped_cb (void *cls, | ||
182 | const char *emsg) | ||
183 | { | ||
184 | struct PeerCmdState *ps = cls; | ||
185 | |||
186 | if (NULL != emsg) | ||
187 | { | ||
188 | LOG (GNUNET_ERROR_TYPE_ERROR, "There was an error stopping a peer: %s\n", | ||
189 | emsg); | ||
190 | } | ||
191 | GNUNET_TESTBED_operation_done (ps->operation); | ||
192 | GNUNET_TESTBED_peer_destroy (ps->peer); | ||
193 | } | ||
194 | |||
195 | |||
196 | /** | ||
197 | * Shutdown nicely | ||
198 | * | ||
199 | * @param cs controller state. | ||
200 | */ | ||
201 | void | ||
202 | GNUNET_TESTBED_shutdown_peer (struct PeerCmdState *ps) | ||
203 | { | ||
204 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
205 | "Shutting down...\n"); | ||
206 | |||
207 | ps->peer_going_down = GNUNET_YES; | ||
208 | |||
209 | if (NULL != ps->abort_task) | ||
210 | GNUNET_SCHEDULER_cancel (ps->abort_task); | ||
211 | if (NULL != ps->cfg) | ||
212 | GNUNET_CONFIGURATION_destroy (ps->cfg); | ||
213 | if (NULL != ps->host) | ||
214 | GNUNET_TESTBED_host_destroy (ps->host); | ||
215 | |||
216 | GNUNET_TESTBED_operation_done (ps->operation); | ||
217 | ps->operation = GNUNET_TESTBED_peer_stop (NULL, ps->peer, peer_stopped_cb, | ||
218 | ps); | ||
219 | |||
220 | } | ||
221 | |||
222 | |||
223 | struct GNUNET_TESTING_Command | ||
224 | GNUNET_TESTBED_cmd_peer (const char *label, | ||
225 | const char *controller_label, | ||
226 | const char *hostname, | ||
227 | const char *username, | ||
228 | uint16_t port, | ||
229 | struct GNUNET_CONFIGURATION_Handle *cfg) | ||
230 | { | ||
231 | struct PeerCmdState *ps; | ||
232 | |||
233 | ps = GNUNET_new (struct PeerCmdState); | ||
234 | ps->hostname = hostname; | ||
235 | ps->username = username; | ||
236 | ps->port = port; | ||
237 | ps->cfg = cfg; | ||
238 | ps->controller_label = controller_label; | ||
239 | |||
240 | struct GNUNET_TESTING_Command cmd = { | ||
241 | .cls = ps, | ||
242 | .label = label, | ||
243 | .run = &peer_run, | ||
244 | .cleanup = &peer_cleanup, | ||
245 | .traits = &peer_traits | ||
246 | }; | ||
247 | |||
248 | return cmd; | ||
249 | } | ||