diff options
Diffstat (limited to 'src/service/arm/testing_arm_cmd_stop_peer.c')
-rw-r--r-- | src/service/arm/testing_arm_cmd_stop_peer.c | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/src/service/arm/testing_arm_cmd_stop_peer.c b/src/service/arm/testing_arm_cmd_stop_peer.c new file mode 100644 index 000000000..e83b2bf29 --- /dev/null +++ b/src/service/arm/testing_arm_cmd_stop_peer.c | |||
@@ -0,0 +1,174 @@ | |||
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 testing_api_cmd_stop_peer.c | ||
23 | * @brief cmd to stop a peer. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testing_lib.h" | ||
29 | #include "gnunet_testbed_lib.h" | ||
30 | #include "gnunet_testing_arm_lib.h" | ||
31 | #include "gnunet_arm_service.h" | ||
32 | |||
33 | |||
34 | /** | ||
35 | * Struct to hold information for callbacks. | ||
36 | */ | ||
37 | struct StopPeerState | ||
38 | { | ||
39 | /** | ||
40 | * Label of the cmd to start the peer. | ||
41 | */ | ||
42 | const char *start_label; | ||
43 | |||
44 | /** | ||
45 | * Label of the cmd. | ||
46 | */ | ||
47 | const char *label; | ||
48 | |||
49 | struct GNUNET_ARM_Operation *op; | ||
50 | |||
51 | struct GNUNET_TESTING_Interpreter *is; | ||
52 | |||
53 | struct GNUNET_TESTING_AsyncContext ac; | ||
54 | }; | ||
55 | |||
56 | |||
57 | /** | ||
58 | * Function called in response to a start/stop request. | ||
59 | * Will be called when request was not sent successfully, | ||
60 | * or when a reply comes. If the request was not sent successfully, | ||
61 | * @a rs will indicate that, and @a result will be undefined. | ||
62 | * | ||
63 | * @param cls closure | ||
64 | * @param rs status of the request | ||
65 | * @param result result of the operation | ||
66 | */ | ||
67 | static void | ||
68 | stop_cb ( | ||
69 | void *cls, | ||
70 | enum GNUNET_ARM_RequestStatus rs, | ||
71 | enum GNUNET_ARM_Result result) | ||
72 | { | ||
73 | struct StopPeerState *stop_ps = cls; | ||
74 | |||
75 | stop_ps->op = NULL; | ||
76 | if (GNUNET_ARM_RESULT_STOPPED != result) | ||
77 | { | ||
78 | GNUNET_TESTING_async_fail (&stop_ps->ac); | ||
79 | return; | ||
80 | } | ||
81 | GNUNET_TESTING_async_finish (&stop_ps->ac); | ||
82 | } | ||
83 | |||
84 | |||
85 | /** | ||
86 | * The run method of this cmd will stop all services of a peer which were used to test the transport service. | ||
87 | * | ||
88 | */ | ||
89 | static void | ||
90 | stop_peer_run (void *cls, | ||
91 | struct GNUNET_TESTING_Interpreter *is) | ||
92 | { | ||
93 | struct StopPeerState *stop_ps = cls; | ||
94 | const struct GNUNET_TESTING_Command *start_cmd; | ||
95 | struct GNUNET_ARM_Handle *ah; | ||
96 | |||
97 | stop_ps->is = is; | ||
98 | start_cmd | ||
99 | = GNUNET_TESTING_interpreter_lookup_command (is, | ||
100 | stop_ps->start_label); | ||
101 | if (NULL == start_cmd) | ||
102 | GNUNET_TESTING_FAIL (is); | ||
103 | if (GNUNET_OK != | ||
104 | GNUNET_TESTING_ARM_get_trait_arm_handle (start_cmd, | ||
105 | &ah)) | ||
106 | GNUNET_TESTING_FAIL (is); | ||
107 | stop_ps->op = GNUNET_ARM_request_service_stop (ah, | ||
108 | "arm", | ||
109 | &stop_cb, | ||
110 | stop_ps); | ||
111 | if (NULL == stop_ps->op) | ||
112 | GNUNET_TESTING_FAIL (is); | ||
113 | } | ||
114 | |||
115 | |||
116 | /** | ||
117 | * The cleanup function of this cmd frees resources the cmd allocated. | ||
118 | * | ||
119 | */ | ||
120 | static void | ||
121 | stop_peer_cleanup (void *cls) | ||
122 | { | ||
123 | struct StopPeerState *sps = cls; | ||
124 | |||
125 | if (NULL != sps->op) | ||
126 | { | ||
127 | GNUNET_TESTING_command_incomplete (sps->is, | ||
128 | sps->label); | ||
129 | GNUNET_ARM_operation_cancel (sps->op); | ||
130 | sps->op = NULL; | ||
131 | } | ||
132 | GNUNET_free (sps); | ||
133 | } | ||
134 | |||
135 | |||
136 | /** | ||
137 | * Trait function of this cmd does nothing. | ||
138 | * | ||
139 | */ | ||
140 | static int | ||
141 | stop_peer_traits (void *cls, | ||
142 | const void **ret, | ||
143 | const char *trait, | ||
144 | unsigned int index) | ||
145 | { | ||
146 | struct GNUNET_TESTING_Trait traits[] = { | ||
147 | GNUNET_TESTING_trait_end () | ||
148 | }; | ||
149 | |||
150 | (void) cls; | ||
151 | return GNUNET_TESTING_get_trait (traits, | ||
152 | ret, | ||
153 | trait, | ||
154 | index); | ||
155 | } | ||
156 | |||
157 | |||
158 | struct GNUNET_TESTING_Command | ||
159 | GNUNET_TESTING_cmd_stop_peer (const char *label, | ||
160 | const char *start_label) | ||
161 | { | ||
162 | struct StopPeerState *sps; | ||
163 | |||
164 | sps = GNUNET_new (struct StopPeerState); | ||
165 | sps->start_label = start_label; | ||
166 | sps->label = label; | ||
167 | return GNUNET_TESTING_command_new_ac ( | ||
168 | sps, | ||
169 | label, | ||
170 | &stop_peer_run, | ||
171 | &stop_peer_cleanup, | ||
172 | &stop_peer_traits, | ||
173 | &sps->ac); | ||
174 | } | ||