Common
common packages for kyubic
 
Loading...
Searching...
No Matches
topic_pub_sub_check_base.hpp
Go to the documentation of this file.
1
10#ifndef _TOPIC_PUB_SUB_CHECK_BASE_HPP
11#define _TOPIC_PUB_SUB_CHECK_BASE_HPP
12
13#include <rclcpp/rclcpp.hpp>
14#include <string>
16#include <thread>
17
19{
20
21// Define comparison modes
23{
24 GREATER_OR_EQUAL, // count >= expected
25 EQUAL // count == expected
26};
27
32{
33protected:
34 std::string topic_name_;
35 uint32_t timeout_ms_ = 1000;
36 size_t expected_count_ = 1;
38
47 const std::string topic_name, const uint32_t timeout_ms, const size_t expected_count = 1,
49 {
50 topic_name_ = topic_name;
51 timeout_ms_ = timeout_ms;
52 expected_count_ = expected_count;
53 mode_ = mode;
54 };
55
56 virtual void prepare_check(rclcpp::Node::SharedPtr node) override = 0;
57 virtual size_t get_count(rclcpp::Node::SharedPtr node, const std::string & topic) = 0;
58 virtual std::string get_target_type() const = 0;
59
60private:
61 std::string status_msg_;
62
63 bool check_impl(rclcpp::Node::SharedPtr node) override
64 {
65 if (topic_name_.empty()) {
66 status_msg_ = "Topic name not set. Please invoke `set_config()` function.";
67 return false;
68 }
69
70 auto start_time = std::chrono::steady_clock::now();
71 auto timeout_duration = std::chrono::milliseconds(timeout_ms_);
72
73 while (true) {
74 // Inherited count method
75 size_t current_count = get_count(node, topic_name_);
76
77 // Comparison
78 bool passed = false;
80 passed = (current_count >= expected_count_);
81 } else {
82 passed = (current_count == expected_count_);
83 }
84
85 if (passed) {
86 status_msg_ = "Found " + std::to_string(current_count) + " " + get_target_type() + "(s).";
87 return true; // PASS
88 }
89
90 // Check timeout
91 auto elapsed = std::chrono::steady_clock::now() - start_time;
92 if (elapsed > timeout_duration) {
93 std::string mode_str = (mode_ == ComparisonMode::GREATER_OR_EQUAL) ? ">=" : "==";
94 status_msg_ = "Timeout. Found " + std::to_string(current_count) + " " + get_target_type() +
95 "(s) (Expected " + mode_str + " " + std::to_string(expected_count_) + ").";
96 return false; // FAIL
97 }
98
99 std::this_thread::sleep_for(std::chrono::milliseconds(100));
100 }
101 }
102
103 std::string report_impl([[maybe_unused]] rclcpp::Node::SharedPtr node) override
104 {
105 return "Topic " + get_target_type() + ": " + topic_name_ + " | " + status_msg_;
106 }
107};
108
113{
114private:
115 std::string get_unique_id() const override
116 {
117 return "TopicPublisherCheckBase::" + topic_name_ + "::" + std::to_string(expected_count_) +
118 "::" + std::to_string(static_cast<int>(mode_));
119 }
120
121 size_t get_count(rclcpp::Node::SharedPtr node, const std::string & topic) override
122 {
123 return node->count_publishers(topic);
124 }
125
126 std::string get_target_type() const override { return "Publisher"; }
127};
128
133{
134private:
135 std::string get_unique_id() const override
136 {
137 return "TopicSubscriberCheckBase::" + topic_name_ + "::" + std::to_string(expected_count_) +
138 "::" + std::to_string(static_cast<int>(mode_));
139 }
140
141 size_t get_count(rclcpp::Node::SharedPtr node, const std::string & topic) override
142 {
143 return node->count_subscribers(topic);
144 }
145
146 std::string get_target_type() const override { return "Subscriber"; }
147};
148
149} // namespace system_health_check::base
150
151#endif
Abstract base class for all system check plugins.
Definition: system_check_base.hpp:24
Abstract base class for checking Topic Publisher/Subscriber counts.
Definition: topic_pub_sub_check_base.hpp:32
ComparisonMode mode_
Definition: topic_pub_sub_check_base.hpp:37
uint32_t timeout_ms_
Definition: topic_pub_sub_check_base.hpp:35
size_t expected_count_
Definition: topic_pub_sub_check_base.hpp:36
std::string topic_name_
Definition: topic_pub_sub_check_base.hpp:34
virtual size_t get_count(rclcpp::Node::SharedPtr node, const std::string &topic)=0
void set_config(const std::string topic_name, const uint32_t timeout_ms, const size_t expected_count=1, const ComparisonMode mode=ComparisonMode::EQUAL)
Configure the check settings.
Definition: topic_pub_sub_check_base.hpp:46
virtual std::string get_target_type() const =0
virtual void prepare_check(rclcpp::Node::SharedPtr node) override=0
Prepares the check before execution.
Checks if there are any PUBLISHERS for the specified topic.
Definition: topic_pub_sub_check_base.hpp:113
Checks if there are any SUBSCRIBERS for the specified topic.
Definition: topic_pub_sub_check_base.hpp:133
Definition: action_server_check_base.hpp:25
ComparisonMode
Definition: topic_pub_sub_check_base.hpp:23
Abstruct Class of check process.