diff options
Diffstat (limited to 'src/testbed/testbed_api_cmd_netjail_start_testbed.c')
-rw-r--r-- | src/testbed/testbed_api_cmd_netjail_start_testbed.c | 327 |
1 files changed, 231 insertions, 96 deletions
diff --git a/src/testbed/testbed_api_cmd_netjail_start_testbed.c b/src/testbed/testbed_api_cmd_netjail_start_testbed.c index da51350a1..786084917 100644 --- a/src/testbed/testbed_api_cmd_netjail_start_testbed.c +++ b/src/testbed/testbed_api_cmd_netjail_start_testbed.c | |||
@@ -27,20 +27,20 @@ | |||
27 | #include "gnunet_testing_ng_lib.h" | 27 | #include "gnunet_testing_ng_lib.h" |
28 | #include "gnunet_testbed_ng_service.h" | 28 | #include "gnunet_testbed_ng_service.h" |
29 | #include "testbed_api.h" | 29 | #include "testbed_api.h" |
30 | #include "testbed_api_hosts.h" | ||
30 | 31 | ||
31 | #define NETJAIL_EXEC_SCRIPT "./netjail_exec.sh" | 32 | #define NETJAIL_EXEC_SCRIPT "./netjail_exec.sh" |
32 | 33 | ||
34 | |||
33 | struct NetJailState | 35 | struct NetJailState |
34 | { | 36 | { |
35 | 37 | ||
36 | /** | 38 | /** |
37 | * The process handle | 39 | * The process handle |
38 | */ | 40 | */ |
39 | struct GNUNET_HELPER_Handle *helper; | 41 | struct GNUNET_HELPER_Handle **helper; |
40 | |||
41 | GNUNET_MessageTokenizerCallback cb; | ||
42 | 42 | ||
43 | GNUNET_HELPER_ExceptionCallback exp_cb; | 43 | unsigned int n_helper; |
44 | 44 | ||
45 | char *binary_name; | 45 | char *binary_name; |
46 | 46 | ||
@@ -48,19 +48,36 @@ struct NetJailState | |||
48 | 48 | ||
49 | char *global_n; | 49 | char *global_n; |
50 | 50 | ||
51 | char **binary_argv; | ||
52 | |||
53 | /** | 51 | /** |
54 | * The send handle for the helper | 52 | * The send handle for the helper |
55 | */ | 53 | */ |
56 | struct GNUNET_HELPER_SendHandle *shandle; | 54 | struct GNUNET_HELPER_SendHandle **shandle; |
55 | |||
56 | unsigned int n_shandle; | ||
57 | 57 | ||
58 | /** | 58 | /** |
59 | * The message corresponding to send handle | 59 | * The message corresponding to send handle |
60 | */ | 60 | */ |
61 | struct GNUNET_MessageHeader *msg; | 61 | struct GNUNET_MessageHeader **msg; |
62 | |||
63 | unsigned int n_msg; | ||
64 | |||
65 | unsigned int number_of_testbeds_started; | ||
66 | |||
67 | /** | ||
68 | * The host where the controller is running | ||
69 | */ | ||
70 | struct GNUNET_TESTBED_Host **host; | ||
71 | |||
72 | unsigned int n_host; | ||
62 | }; | 73 | }; |
63 | 74 | ||
75 | struct TestbedCount | ||
76 | { | ||
77 | unsigned int count; | ||
78 | |||
79 | struct NetJailState *ns; | ||
80 | }; | ||
64 | 81 | ||
65 | /** | 82 | /** |
66 | * | 83 | * |
@@ -93,40 +110,66 @@ netjail_exec_traits (void *cls, | |||
93 | const char *trait, | 110 | const char *trait, |
94 | unsigned int index) | 111 | unsigned int index) |
95 | { | 112 | { |
96 | return GNUNET_OK; | 113 | struct NetJailState *ns = cls; |
114 | struct GNUNET_HELPER_Handle **helper = ns->helper; | ||
115 | struct GNUNET_TESTBED_Host **hosts = ns->host; | ||
116 | |||
117 | |||
118 | struct GNUNET_TESTING_Trait traits[] = { | ||
119 | { | ||
120 | .index = 0, | ||
121 | .trait_name = "helper_handles", | ||
122 | .ptr = (const void *) helper, | ||
123 | }, | ||
124 | { | ||
125 | .index = 1, | ||
126 | .trait_name = "hosts", | ||
127 | .ptr = (const void *) hosts, | ||
128 | }, | ||
129 | GNUNET_TESTING_trait_end () | ||
130 | }; | ||
131 | |||
132 | return GNUNET_TESTING_get_trait (traits, | ||
133 | ret, | ||
134 | trait, | ||
135 | index); | ||
97 | } | 136 | } |
98 | 137 | ||
99 | 138 | ||
100 | // TODO This would be a useful macro. | ||
101 | /** | 139 | /** |
102 | * Function to join NULL terminated list of arguments | 140 | * Offer handles to testbed helper from trait |
103 | * | 141 | * |
104 | * @param argv1 the NULL terminated list of arguments. Cannot be NULL. | 142 | * @param cmd command to extract the message from. |
105 | * @param argv2 the NULL terminated list of arguments. Cannot be NULL. | 143 | * @param pt pointer to message. |
106 | * @return the joined NULL terminated arguments | 144 | * @return #GNUNET_OK on success. |
107 | */ | 145 | */ |
108 | static char ** | 146 | int |
109 | join_argv (const char *const *argv1, const char *const *argv2) | 147 | GNUNET_TESTBED_get_trait_helper_handles (const struct |
148 | GNUNET_TESTING_Command *cmd, | ||
149 | struct GNUNET_HELPER_Handle ***helper) | ||
110 | { | 150 | { |
111 | char **argvj; | 151 | return cmd->traits (cmd->cls, |
112 | char *argv; | 152 | (const void **) helper, |
113 | unsigned int carg = 0; | 153 | "helper_handles", |
114 | unsigned int cnt; | 154 | (unsigned int) 0); |
115 | 155 | } | |
116 | carg = 0; | 156 | |
117 | argvj = NULL; | 157 | /** |
118 | for (cnt = 0; NULL != argv1[cnt]; cnt++) | 158 | * Offer handles to testbed helper from trait |
119 | { | 159 | * |
120 | argv = GNUNET_strdup (argv1[cnt]); | 160 | * @param cmd command to extract the message from. |
121 | GNUNET_array_append (argvj, carg, argv); | 161 | * @param pt pointer to message. |
122 | } | 162 | * @return #GNUNET_OK on success. |
123 | for (cnt = 0; NULL != argv2[cnt]; cnt++) | 163 | */ |
124 | { | 164 | int |
125 | argv = GNUNET_strdup (argv2[cnt]); | 165 | GNUNET_TESTBED_get_trait_hosts (const struct |
126 | GNUNET_array_append (argvj, carg, argv); | 166 | GNUNET_TESTING_Command *cmd, |
127 | } | 167 | struct GNUNET_TESTBED_Host ***hosts) |
128 | GNUNET_array_append (argvj, carg, NULL); | 168 | { |
129 | return argvj; | 169 | return cmd->traits (cmd->cls, |
170 | (const void **) hosts, | ||
171 | "hosts", | ||
172 | (unsigned int) 1); | ||
130 | } | 173 | } |
131 | 174 | ||
132 | 175 | ||
@@ -141,41 +184,95 @@ join_argv (const char *const *argv1, const char *const *argv2) | |||
141 | static void | 184 | static void |
142 | clear_msg (void *cls, int result) | 185 | clear_msg (void *cls, int result) |
143 | { | 186 | { |
144 | struct NetJailState *ns = cls; | 187 | struct TestbedCount *tbc = cls; |
145 | 188 | struct NetJailState *ns = tbc->ns; | |
146 | GNUNET_assert (NULL != ns->shandle); | 189 | |
147 | ns->shandle = NULL; | 190 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
148 | GNUNET_free (ns->msg); | 191 | "clear_msg tbc->count: %d\n", |
149 | ns->msg = NULL; | 192 | tbc->count); |
193 | GNUNET_assert (NULL != ns->shandle[tbc->count - 1]); | ||
194 | ns->shandle[tbc->count - 1] = NULL; | ||
195 | GNUNET_free (ns->msg[tbc->count - 1]); | ||
196 | ns->msg[tbc->count - 1] = NULL; | ||
150 | } | 197 | } |
151 | 198 | ||
152 | 199 | ||
153 | /** | 200 | /** |
154 | * Run the "hello world" CMD. | 201 | * Functions with this signature are called whenever a |
155 | * | 202 | * complete message is received by the tokenizer. |
156 | * @param cls closure. | 203 | * |
157 | * @param cmd CMD being run. | 204 | * Do not call GNUNET_SERVER_mst_destroy in callback |
158 | * @param is interpreter state. | 205 | * |
159 | */ | 206 | * @param cls closure |
207 | * @param client identification of the client | ||
208 | * @param message the actual message | ||
209 | * | ||
210 | * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing | ||
211 | */ | ||
212 | static int | ||
213 | helper_mst (void *cls, const struct GNUNET_MessageHeader *message) | ||
214 | { | ||
215 | struct TestbedCount *tbc = cls; | ||
216 | struct NetJailState *ns = tbc->ns; | ||
217 | struct GNUNET_TESTBED_Host *host = ns->host[tbc->count - 1]; | ||
218 | |||
219 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
220 | "helper_mst tbc->count: %d\n", | ||
221 | tbc->count); | ||
222 | GNUNET_TESTBED_extract_cfg (host, message); | ||
223 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
224 | "Received message from helper.\n"); | ||
225 | ns->number_of_testbeds_started++; | ||
226 | return GNUNET_OK; | ||
227 | } | ||
228 | |||
229 | |||
160 | static void | 230 | static void |
161 | netjail_exec_run (void *cls, | 231 | exp_cb (void *cls) |
162 | const struct GNUNET_TESTING_Command *cmd, | ||
163 | struct GNUNET_TESTING_Interpreter *is) | ||
164 | { | 232 | { |
165 | struct NetJailState *ns = cls; | 233 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called exp_cb.\n"); |
166 | char **helper_argv; | 234 | GNUNET_TESTING_interpreter_fail (); |
235 | } | ||
236 | |||
237 | |||
238 | static void | ||
239 | start_testbed (struct NetJailState *ns, struct | ||
240 | GNUNET_CONFIGURATION_Handle *config, | ||
241 | char *n_char, | ||
242 | char *m_char) | ||
243 | { | ||
244 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
167 | struct GNUNET_TESTBED_HelperInit *msg; | 245 | struct GNUNET_TESTBED_HelperInit *msg; |
168 | struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create (); | 246 | struct TestbedCount *tbc; |
169 | char *const script_argv[] = {NETJAIL_EXEC_SCRIPT, | 247 | char *const script_argv[] = {NETJAIL_EXEC_SCRIPT, |
170 | ns->local_m, | 248 | m_char, |
171 | "1", | 249 | n_char, |
172 | "1", | 250 | GNUNET_OS_get_libexec_binary_path ( |
251 | HELPER_TESTBED_BINARY), | ||
173 | NULL}; | 252 | NULL}; |
174 | GNUNET_MessageTokenizerCallback cb = ns->cb; | 253 | unsigned int m = atoi (m_char); |
175 | GNUNET_HELPER_ExceptionCallback exp_cb = ns->exp_cb; | 254 | unsigned int n = atoi (n_char); |
255 | |||
256 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
257 | "m: %d n: %d\n", | ||
258 | m, | ||
259 | n); | ||
260 | |||
261 | tbc = GNUNET_new (struct TestbedCount); | ||
262 | tbc->ns = ns; | ||
263 | tbc->count = (n - 1) * atoi (ns->local_m) + m; | ||
264 | |||
265 | cfg = GNUNET_CONFIGURATION_dup (config); | ||
266 | |||
267 | GNUNET_array_append (ns->host, ns->n_host, | ||
268 | GNUNET_TESTBED_host_create_with_id (tbc->count - 1, | ||
269 | NULL, | ||
270 | NULL, | ||
271 | cfg, | ||
272 | 0)); | ||
176 | 273 | ||
177 | if ((GNUNET_YES != GNUNET_DISK_file_test ("test_testbed_api.conf")) || | 274 | if ((GNUNET_YES != GNUNET_DISK_file_test ("test_testbed_api.conf")) || |
178 | (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, | 275 | (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (config, |
179 | "test_testbed_api.conf"))) | 276 | "test_testbed_api.conf"))) |
180 | { | 277 | { |
181 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 278 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
@@ -184,30 +281,88 @@ netjail_exec_run (void *cls, | |||
184 | "test_testbed_api.conf"); | 281 | "test_testbed_api.conf"); |
185 | } | 282 | } |
186 | 283 | ||
187 | helper_argv = join_argv ((const char **) script_argv, | 284 | GNUNET_array_append (ns->helper, ns->n_helper, GNUNET_HELPER_start ( |
188 | (const char **) ns->binary_argv); | 285 | GNUNET_YES, |
189 | 286 | NETJAIL_EXEC_SCRIPT, | |
190 | ns->helper = GNUNET_HELPER_start (GNUNET_YES, | 287 | script_argv, |
191 | NETJAIL_EXEC_SCRIPT, | 288 | & |
192 | helper_argv, | 289 | helper_mst, |
193 | cb, | 290 | exp_cb, |
194 | exp_cb, | 291 | tbc)); |
195 | ns); | 292 | |
196 | 293 | struct GNUNET_HELPER_Handle *helper = ns->helper[tbc->count - 1]; | |
197 | msg = GNUNET_TESTBED_create_helper_init_msg_ ("127.0.0.1", NULL, cfg); | 294 | |
198 | ns->msg = &msg->header; | 295 | msg = GNUNET_TESTBED_create_helper_init_msg_ ("127.0.0.1", NULL, config); |
199 | ns->shandle = GNUNET_HELPER_send (ns->helper, &msg->header, GNUNET_NO, | 296 | GNUNET_array_append (ns->msg, ns->n_msg, &msg->header); |
200 | &clear_msg, ns); | 297 | |
201 | if (NULL == ns->shandle) | 298 | GNUNET_array_append (ns->shandle, ns->n_shandle, GNUNET_HELPER_send ( |
299 | helper, | ||
300 | &msg->header, | ||
301 | GNUNET_NO, | ||
302 | &clear_msg, | ||
303 | tbc)); | ||
304 | if (NULL == ns->shandle[tbc->count - 1]) | ||
202 | { | 305 | { |
203 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 306 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
204 | "Send handle is NULL!\n"); | 307 | "Send handle is NULL!\n"); |
205 | GNUNET_free (msg); | 308 | GNUNET_free (msg); |
309 | GNUNET_TESTING_interpreter_fail (); | ||
206 | } | 310 | } |
207 | } | 311 | } |
208 | 312 | ||
209 | 313 | ||
210 | /** | 314 | /** |
315 | * Run the "hello world" CMD. | ||
316 | * | ||
317 | * @param cls closure. | ||
318 | * @param cmd CMD being run. | ||
319 | * @param is interpreter state. | ||
320 | */ | ||
321 | static void | ||
322 | netjail_exec_run (void *cls, | ||
323 | const struct GNUNET_TESTING_Command *cmd, | ||
324 | struct GNUNET_TESTING_Interpreter *is) | ||
325 | { | ||
326 | char str_m[12]; | ||
327 | char str_n[12]; | ||
328 | struct NetJailState *ns = cls; | ||
329 | struct GNUNET_CONFIGURATION_Handle *config = | ||
330 | GNUNET_CONFIGURATION_create (); | ||
331 | |||
332 | for (int i = 1; i <= atoi (ns->global_n); i++) { | ||
333 | for (int j = 1; j <= atoi (ns->local_m); j++) | ||
334 | { | ||
335 | sprintf (str_n, "%d", i); | ||
336 | sprintf (str_m, "%d", j); | ||
337 | start_testbed (ns, config, | ||
338 | str_n, | ||
339 | str_m); | ||
340 | } | ||
341 | } | ||
342 | } | ||
343 | |||
344 | |||
345 | static int | ||
346 | netjail_start_finish (void *cls, | ||
347 | GNUNET_SCHEDULER_TaskCallback cont, | ||
348 | void *cont_cls) | ||
349 | { | ||
350 | unsigned int ret = GNUNET_NO; | ||
351 | struct NetJailState *ns = cls; | ||
352 | |||
353 | if (ns->number_of_testbeds_started == atoi (ns->local_m) * atoi ( | ||
354 | ns->global_n)) | ||
355 | { | ||
356 | ret = GNUNET_YES; | ||
357 | cont (cont_cls); | ||
358 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
359 | "All helper started!\n"); | ||
360 | } | ||
361 | return ret; | ||
362 | } | ||
363 | |||
364 | |||
365 | /** | ||
211 | * Create command. | 366 | * Create command. |
212 | * | 367 | * |
213 | * @param label name for command. | 368 | * @param label name for command. |
@@ -216,40 +371,20 @@ netjail_exec_run (void *cls, | |||
216 | */ | 371 | */ |
217 | struct GNUNET_TESTING_Command | 372 | struct GNUNET_TESTING_Command |
218 | GNUNET_TESTBED_cmd_netjail_start_testbed (const char *label, | 373 | GNUNET_TESTBED_cmd_netjail_start_testbed (const char *label, |
219 | char *const binary_argv[], | ||
220 | char *local_m, | 374 | char *local_m, |
221 | char *global_n, | 375 | char *global_n) |
222 | GNUNET_MessageTokenizerCallback cb, | ||
223 | GNUNET_HELPER_ExceptionCallback exp_cb) | ||
224 | { | 376 | { |
225 | struct NetJailState *ns; | 377 | struct NetJailState *ns; |
226 | unsigned int append_cnt; | ||
227 | char **argvj; | ||
228 | char *argv; | ||
229 | unsigned int carg = 0; | ||
230 | 378 | ||
231 | ns = GNUNET_new (struct NetJailState); | 379 | ns = GNUNET_new (struct NetJailState); |
232 | argvj = NULL; | ||
233 | for (append_cnt = 0; NULL != binary_argv[append_cnt]; append_cnt++) | ||
234 | { | ||
235 | argv = GNUNET_strdup (binary_argv[append_cnt]); | ||
236 | GNUNET_array_append (argvj, | ||
237 | carg, | ||
238 | argv); | ||
239 | } | ||
240 | GNUNET_array_append (argvj, carg, NULL); | ||
241 | |||
242 | ns->binary_argv = argvj; | ||
243 | |||
244 | ns->local_m = local_m; | 380 | ns->local_m = local_m; |
245 | ns->global_n = global_n; | 381 | ns->global_n = global_n; |
246 | ns->cb = cb; | ||
247 | ns->exp_cb = exp_cb; | ||
248 | 382 | ||
249 | struct GNUNET_TESTING_Command cmd = { | 383 | struct GNUNET_TESTING_Command cmd = { |
250 | .cls = ns, | 384 | .cls = ns, |
251 | .label = label, | 385 | .label = label, |
252 | .run = &netjail_exec_run, | 386 | .run = &netjail_exec_run, |
387 | .finish = &netjail_start_finish, | ||
253 | .cleanup = &netjail_exec_cleanup, | 388 | .cleanup = &netjail_exec_cleanup, |
254 | .traits = &netjail_exec_traits | 389 | .traits = &netjail_exec_traits |
255 | }; | 390 | }; |