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