10#ifndef _SERVICE_SERVER_CHECK_BASE_HPP
11#define _SERVICE_SERVER_CHECK_BASE_HPP
14#include <rclcpp/rclcpp.hpp>
20#include <rcl/allocator.h>
39 const std::string service_name,
const uint32_t timeout_ms,
size_t expected_count = 1)
49 rclcpp::Node::SharedPtr node,
const std::string & node_name,
const std::string & node_ns) = 0;
55 std::string status_msg_;
57 bool check_impl(rclcpp::Node::SharedPtr node)
override
60 status_msg_ =
"Service name not set. Please invoke `set_config()` function.";
64 auto start_time = std::chrono::steady_clock::now();
65 auto timeout_duration = std::chrono::milliseconds(
timeout_ms_);
69 std::string current_node_name = node->get_fully_qualified_name();
75 auto node_names = node->get_node_names();
78 for (
const auto & full_node_name : node_names) {
80 if (full_node_name == current_node_name) {
85 std::string target_node_name;
86 std::string target_node_ns;
88 size_t last_slash = full_node_name.find_last_of(
'/');
89 if (last_slash == std::string::npos || last_slash == 0) {
91 target_node_name = (last_slash == 0) ? full_node_name.substr(1) : full_node_name;
93 target_node_ns = full_node_name.substr(0, last_slash);
94 target_node_name = full_node_name.substr(last_slash + 1);
105 status_msg_ =
"Found " + std::to_string(count) +
" " +
get_target_type() +
"(s).";
110 auto elapsed = std::chrono::steady_clock::now() - start_time;
111 if (elapsed > timeout_duration) {
112 status_msg_ =
"Timeout. Found " + std::to_string(count) +
" " +
get_target_type() +
117 std::this_thread::sleep_for(std::chrono::milliseconds(100));
121 std::string report_impl([[maybe_unused]] rclcpp::Node::SharedPtr node)
override
133 std::string get_unique_id()
const override
138 std::string get_target_type()
const override {
return "Server"; }
140 bool check_entity_in_node(
141 rclcpp::Node::SharedPtr node,
const std::string & node_name,
142 const std::string & node_ns)
override
146 auto services = node->get_service_names_and_types_by_node(node_name, node_ns);
148 }
catch (
const std::exception &) {
160 std::string get_unique_id()
const override
165 std::string get_target_type()
const override {
return "Client"; }
167 bool check_entity_in_node(
168 rclcpp::Node::SharedPtr node,
const std::string & node_name,
169 const std::string & node_ns)
override
171 rcl_node_t * rcl_node = node->get_node_base_interface()->get_rcl_node_handle();
172 rcl_allocator_t allocator = rcl_get_default_allocator();
173 rcl_names_and_types_t client_names_and_types = rcl_get_zero_initialized_names_and_types();
176 rcl_ret_t ret = rcl_get_client_names_and_types_by_node(
177 rcl_node, &allocator, node_name.c_str(), node_ns.c_str(), &client_names_and_types);
179 if (ret == RCL_RET_OK) {
180 for (
size_t i = 0; i < client_names_and_types.names.size; ++i) {
181 if (std::string(client_names_and_types.names.data[i]) ==
service_name_) {
189 rcl_ret_t fini_ret = rcl_names_and_types_fini(&client_names_and_types);
Checks for Service CLIENT existence.
Definition: service_server_check_base.hpp:158
Abstract base class for checking Service Server/Client existence. Handles node iteration,...
Definition: service_server_check_base.hpp:32
std::string service_name_
Definition: service_server_check_base.hpp:34
size_t expected_count_
Definition: service_server_check_base.hpp:36
virtual void prepare_check(rclcpp::Node::SharedPtr node) override=0
Prepares the check before execution.
uint32_t timeout_ms_
Definition: service_server_check_base.hpp:35
void set_config(const std::string service_name, const uint32_t timeout_ms, size_t expected_count=1)
Definition: service_server_check_base.hpp:38
virtual bool check_entity_in_node(rclcpp::Node::SharedPtr node, const std::string &node_name, const std::string &node_ns)=0
virtual std::string get_target_type() const =0
Checks for Service SERVER existence.
Definition: service_server_check_base.hpp:131
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.