aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_stop_peer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testing/testing_api_cmd_stop_peer.c')
-rw-r--r--src/testing/testing_api_cmd_stop_peer.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/testing/testing_api_cmd_stop_peer.c b/src/testing/testing_api_cmd_stop_peer.c
new file mode 100644
index 000000000..2cbf978c6
--- /dev/null
+++ b/src/testing/testing_api_cmd_stop_peer.c
@@ -0,0 +1,130 @@
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_ng_lib.h"
29#include "gnunet_testing_netjail_lib.h"
30
31/**
32 * Generic logging shortcut
33 */
34#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
35
36
37/**
38 * Struct to hold information for callbacks.
39 *
40 */
41struct StopPeerState
42{
43 // Label of the cmd to start the peer.
44 const char *start_label;
45};
46
47
48/**
49 * The run method of this cmd will stop all services of a peer which were used to test the transport service.
50 *
51 */
52static void
53stop_peer_run (void *cls,
54 struct GNUNET_TESTING_Interpreter *is)
55{
56 struct StopPeerState *stop_ps = cls;
57 const struct GNUNET_TESTING_StartPeerState *sps;
58 const struct GNUNET_TESTING_Command *start_cmd;
59
60 start_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
61 stop_ps->start_label);
62 GNUNET_TESTING_get_trait_state (start_cmd,
63 &sps);
64
65 if (NULL != sps->peer)
66 {
67 if (GNUNET_OK !=
68 GNUNET_TESTING_peer_stop (sps->peer))
69 {
70 LOG (GNUNET_ERROR_TYPE_ERROR,
71 "Testing lib failed to stop peer %u (`%s')\n",
72 sps->no,
73 GNUNET_i2s (&sps->id));
74 }
75 GNUNET_TESTING_peer_destroy (sps->peer);
76 }
77 if (NULL != sps->rh_task)
78 GNUNET_SCHEDULER_cancel (sps->rh_task);
79}
80
81
82/**
83 * The cleanup function of this cmd frees resources the cmd allocated.
84 *
85 */
86static void
87stop_peer_cleanup (void *cls)
88{
89 struct StopPeerState *sps = cls;
90
91 GNUNET_free (sps);
92}
93
94
95/**
96 * Trait function of this cmd does nothing.
97 *
98 */
99static int
100stop_peer_traits (void *cls,
101 const void **ret,
102 const char *trait,
103 unsigned int index)
104{
105 return GNUNET_OK;
106}
107
108
109/**
110 * Create command.
111 *
112 * @param label name for command.
113 * @param start_label Label of the cmd to start the peer.
114 * @return command.
115 */
116struct GNUNET_TESTING_Command
117GNUNET_TESTING_cmd_stop_peer (const char *label,
118 const char *start_label)
119{
120 struct StopPeerState *sps;
121
122 sps = GNUNET_new (struct StopPeerState);
123 sps->start_label = start_label;
124 return GNUNET_TESTING_command_new (sps,
125 label,
126 &stop_peer_run,
127 &stop_peer_cleanup,
128 &stop_peer_traits,
129 NULL);
130}