Common
common packages for kyubic
 
Loading...
Searching...
No Matches
action_server_check_base.hpp
Go to the documentation of this file.
1
10#ifndef _ACTION_SERVER_CHECK_BASE_HPP
11#define _ACTION_SERVER_CHECK_BASE_HPP
12
13#include <rclcpp/rclcpp.hpp>
15
16// RCL Action Graph headers
17#include <rcl/allocator.h>
18#include <rcl/types.h>
19#include <rcl_action/graph.h>
20
21#include <string>
22#include <thread>
23
25{
26
32{
33protected:
34 std::string action_name_;
35 uint32_t timeout_ms_ = 1000;
36 size_t expected_count_ = 1;
37
39 const std::string action_name, const uint32_t timeout_ms, size_t expected_count = 1)
40 {
41 action_name_ = action_name;
42 timeout_ms_ = timeout_ms;
43 expected_count_ = expected_count;
44 };
45
46 virtual void prepare_check(rclcpp::Node::SharedPtr node) override = 0;
47
49 rcl_node_t * rcl_node, rcl_allocator_t * allocator, const std::string & node_name,
50 const std::string & node_ns) = 0;
51
52 virtual std::string get_target_type() const = 0;
53
54private:
55 std::string status_msg_;
56
57 bool check_impl(rclcpp::Node::SharedPtr node) override
58 {
59 if (action_name_.empty()) {
60 status_msg_ = "Action name not set. Please invoke `set_config()` function.";
61 return false;
62 }
63
64 auto start_time = std::chrono::steady_clock::now();
65 auto timeout_duration = std::chrono::milliseconds(timeout_ms_);
66
67 // RCLノードハンドルとアロケータの取得
68 rcl_node_t * rcl_node = node->get_node_base_interface()->get_rcl_node_handle();
69 rcl_allocator_t allocator = rcl_get_default_allocator();
70
71 // 自身の完全修飾名を取得 (除外用)
72 std::string current_node_name = node->get_fully_qualified_name();
73
74 while (true) {
75 size_t count = 0;
76
77 // 1. 全ノード名を取得
78 auto node_names = node->get_node_names();
79
80 // 2. 各ノードを巡回
81 for (const auto & full_node_name : node_names) {
82 // 自身の除外
83 if (full_node_name == current_node_name) {
84 continue;
85 }
86
87 // --- 名前空間とノード名の分離処理 ---
88 std::string target_node_name;
89 std::string target_node_ns;
90
91 size_t last_slash = full_node_name.find_last_of('/');
92 if (last_slash == std::string::npos || last_slash == 0) {
93 target_node_ns = "/";
94 target_node_name = (last_slash == 0) ? full_node_name.substr(1) : full_node_name;
95 } else {
96 target_node_ns = full_node_name.substr(0, last_slash);
97 target_node_name = full_node_name.substr(last_slash + 1);
98 }
99
100 // --- 派生クラスごとのチェック実行 ---
101 if (check_entity_in_node(rcl_node, &allocator, target_node_name, target_node_ns)) {
102 count++;
103 }
104 }
105
106 // 判定
107 if (count >= expected_count_) {
108 status_msg_ = "Found " + std::to_string(count) + " Action " + get_target_type() + "(s).";
109 return true; // PASS
110 }
111
112 // タイムアウト判定
113 auto elapsed = std::chrono::steady_clock::now() - start_time;
114 if (elapsed > timeout_duration) {
115 status_msg_ = "Timeout. Found " + std::to_string(count) + " Action " + get_target_type() +
116 "(s) (Expected >= " + std::to_string(expected_count_) + ").";
117 return false; // FAIL
118 }
119
120 std::this_thread::sleep_for(std::chrono::milliseconds(100));
121 }
122 }
123
124 std::string report_impl([[maybe_unused]] rclcpp::Node::SharedPtr node) override
125 {
126 return "Action " + get_target_type() + ": " + action_name_ + " | " + status_msg_;
127 }
128};
129
134{
135private:
136 std::string get_unique_id() const override
137 {
138 return "ActionServerCheckBase::" + action_name_ + "::" + std::to_string(expected_count_);
139 }
140
141 std::string get_target_type() const override { return "Server"; }
142
143 bool check_entity_in_node(
144 rcl_node_t * rcl_node, rcl_allocator_t * allocator, const std::string & node_name,
145 const std::string & node_ns) override
146 {
147 rcl_names_and_types_t names_and_types = rcl_get_zero_initialized_names_and_types();
148 bool found = false;
149
150 // Server用のAPIを使用
151 rcl_ret_t ret = rcl_action_get_server_names_and_types_by_node(
152 rcl_node, allocator, node_name.c_str(), node_ns.c_str(), &names_and_types);
153
154 if (ret == RCL_RET_OK) {
155 for (size_t i = 0; i < names_and_types.names.size; ++i) {
156 if (std::string(names_and_types.names.data[i]) == action_name_) {
157 found = true;
158 break;
159 }
160 }
161 }
162
163 rcl_ret_t fini_ret = rcl_names_and_types_fini(&names_and_types);
164 (void)fini_ret;
165
166 return found;
167 }
168};
169
174{
175private:
176 std::string get_unique_id() const override
177 {
178 return "ActionClientCheckBase::" + action_name_ + "::" + std::to_string(expected_count_);
179 }
180
181 std::string get_target_type() const override { return "Client"; }
182
183 bool check_entity_in_node(
184 rcl_node_t * rcl_node, rcl_allocator_t * allocator, const std::string & node_name,
185 const std::string & node_ns) override
186 {
187 rcl_names_and_types_t names_and_types = rcl_get_zero_initialized_names_and_types();
188 bool found = false;
189
190 // Client用のAPIを使用
191 rcl_ret_t ret = rcl_action_get_client_names_and_types_by_node(
192 rcl_node, allocator, node_name.c_str(), node_ns.c_str(), &names_and_types);
193
194 if (ret == RCL_RET_OK) {
195 for (size_t i = 0; i < names_and_types.names.size; ++i) {
196 if (std::string(names_and_types.names.data[i]) == action_name_) {
197 found = true;
198 break;
199 }
200 }
201 }
202
203 rcl_ret_t fini_ret = rcl_names_and_types_fini(&names_and_types);
204 (void)fini_ret;
205
206 return found;
207 }
208};
209
210} // namespace system_health_check::base
211
212#endif
Checks specifically for Action CLIENTS.
Definition: action_server_check_base.hpp:174
Abstract base class for checking Action Server/Client existence. Handles node iteration,...
Definition: action_server_check_base.hpp:32
virtual void prepare_check(rclcpp::Node::SharedPtr node) override=0
Prepares the check before execution.
uint32_t timeout_ms_
Definition: action_server_check_base.hpp:35
void set_config(const std::string action_name, const uint32_t timeout_ms, size_t expected_count=1)
Definition: action_server_check_base.hpp:38
std::string action_name_
Definition: action_server_check_base.hpp:34
virtual bool check_entity_in_node(rcl_node_t *rcl_node, rcl_allocator_t *allocator, const std::string &node_name, const std::string &node_ns)=0
size_t expected_count_
Definition: action_server_check_base.hpp:36
virtual std::string get_target_type() const =0
Checks specifically for Action SERVERS.
Definition: action_server_check_base.hpp:134
Abstract base class for all system check plugins.
Definition: system_check_base.hpp:24
Definition: action_server_check_base.hpp:25
Abstruct Class of check process.