12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- #ifndef RCLCPP__TOPIC_STATISTICS__SUBSCRIBER_TOPIC_STATISTICS_HPP_
16
- #define RCLCPP__TOPIC_STATISTICS__SUBSCRIBER_TOPIC_STATISTICS_HPP_
15
+ #ifndef RCLCPP__TOPIC_STATISTICS__SUBSCRIPTION_TOPIC_STATISTICS_HPP_
16
+ #define RCLCPP__TOPIC_STATISTICS__SUBSCRIPTION_TOPIC_STATISTICS_HPP_
17
17
18
18
#include < memory>
19
19
#include < string>
@@ -37,8 +37,8 @@ namespace rclcpp
37
37
namespace topic_statistics
38
38
{
39
39
40
- constexpr const char kDefaultPublishTopicName []{" topic_statistics " };
41
- constexpr const std::chrono::milliseconds kDefaultPublishingPeriod {std::chrono::minutes (1 )};
40
+ constexpr const char kDefaultPublishTopicName []{" /statistics " };
41
+ constexpr const std::chrono::milliseconds kDefaultPublishingPeriod {std::chrono::seconds (1 )};
42
42
43
43
using libstatistics_collector::collector::GenerateStatisticMessage;
44
44
using statistics_msgs::msg::MetricsMessage;
@@ -51,7 +51,7 @@ using libstatistics_collector::moving_average_statistics::StatisticData;
51
51
* \tparam CallbackMessageT the subscribed message type
52
52
*/
53
53
template <typename CallbackMessageT>
54
- class SubscriberTopicStatistics
54
+ class SubscriptionTopicStatistics
55
55
{
56
56
using TopicStatsCollector =
57
57
libstatistics_collector::topic_statistics_collector::TopicStatisticsCollector<
@@ -61,28 +61,33 @@ class SubscriberTopicStatistics
61
61
CallbackMessageT>;
62
62
63
63
public:
64
- // / Construct a SubscriberTopicStatistics object.
64
+ // / Construct a SubscriptionTopicStatistics object.
65
65
/* *
66
66
* This object wraps utilities, defined in libstatistics_collector, to collect,
67
- * measure, and publish topic statistics data.
67
+ * measure, and publish topic statistics data. This throws an invalid_argument
68
+ * if the input publisher is null.
68
69
*
69
- * @ param node_name the name of the node, which created this instance, in order to denote
70
+ * \ param node_name the name of the node, which created this instance, in order to denote
70
71
* topic source
71
- * @ param publisher instance constructed by the node in order to publish statistics data.
72
- * This class owns the publisher/
72
+ * \ param publisher instance constructed by the node in order to publish statistics data.
73
+ * This class owns the publisher.
73
74
*/
74
- SubscriberTopicStatistics (
75
+ SubscriptionTopicStatistics (
75
76
const std::string & node_name,
76
77
rclcpp::Publisher<statistics_msgs::msg::MetricsMessage>::SharedPtr publisher)
77
78
: node_name_(node_name),
78
79
publisher_ (std::move(publisher))
79
80
{
80
81
// TODO(dbbonnie): ros-tooling/aws-roadmap/issues/226, received message age
81
- // TODO(dbbonnie) throw if publisher is null
82
+
83
+ if (nullptr == publisher_) {
84
+ throw std::invalid_argument (" publisher pointer is nullptr" );
85
+ }
86
+
82
87
bring_up ();
83
88
}
84
89
85
- virtual ~SubscriberTopicStatistics ()
90
+ virtual ~SubscriptionTopicStatistics ()
86
91
{
87
92
tear_down ();
88
93
}
@@ -101,29 +106,16 @@ class SubscriberTopicStatistics
101
106
}
102
107
}
103
108
104
- // / Return a vector of all the currently collected data
105
- /* *
106
- * \return a vector of all the collected data
107
- */
108
- std::vector<StatisticData> get_current_collector_data () const
109
- {
110
- std::vector<StatisticData> data;
111
- for (const auto & collector : subscriber_statistics_collectors_) {
112
- data.push_back (collector->GetStatisticsResults ());
113
- }
114
- return data;
115
- }
116
-
117
109
// / Set the timer used to publish statistics messages.
118
110
/* *
119
- * @ param measurement_timer the timer to fire the publisher, created by the node
111
+ * \ param measurement_timer the timer to fire the publisher, created by the node
120
112
*/
121
- void set_publisher_timer (const rclcpp::TimerBase::SharedPtr & publisher_timer)
113
+ void set_publisher_timer (rclcpp::TimerBase::SharedPtr publisher_timer)
122
114
{
123
115
publisher_timer_ = publisher_timer;
124
116
}
125
117
126
- // / Publish a populated MetricsStatisticsMessage
118
+ // / Publish a populated MetricsStatisticsMessage.
127
119
virtual void publish_message ()
128
120
{
129
121
rclcpp::Time window_end{get_current_nanoseconds_since_epoch ()};
@@ -143,6 +135,20 @@ class SubscriberTopicStatistics
143
135
window_start_ = window_end;
144
136
}
145
137
138
+ protected:
139
+ // / Return a vector of all the currently collected data.
140
+ /* *
141
+ * \return a vector of all the collected data
142
+ */
143
+ std::vector<StatisticData> get_current_collector_data () const
144
+ {
145
+ std::vector<StatisticData> data;
146
+ for (const auto & collector : subscriber_statistics_collectors_) {
147
+ data.push_back (collector->GetStatisticsResults ());
148
+ }
149
+ return data;
150
+ }
151
+
146
152
private:
147
153
// / Construct and start all collectors and set window_start_.
148
154
void bring_up ()
@@ -175,7 +181,7 @@ class SubscriberTopicStatistics
175
181
/* *
176
182
* \return the current nanoseconds (count) since epoch
177
183
*/
178
- int64_t get_current_nanoseconds_since_epoch ()
184
+ int64_t get_current_nanoseconds_since_epoch () const
179
185
{
180
186
const auto now = std::chrono::system_clock::now ();
181
187
return std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch ()).count ();
@@ -184,7 +190,7 @@ class SubscriberTopicStatistics
184
190
// / Collection of statistics collectors
185
191
std::vector<std::unique_ptr<TopicStatsCollector>> subscriber_statistics_collectors_{};
186
192
// / Node name used to generate topic statistics messages to be published
187
- std::string node_name_;
193
+ const std::string node_name_;
188
194
// / Publisher, created by the node, used to publish topic statistics messages
189
195
rclcpp::Publisher<statistics_msgs::msg::MetricsMessage>::SharedPtr publisher_;
190
196
// / Timer which fires the publisher
@@ -195,4 +201,4 @@ class SubscriberTopicStatistics
195
201
} // namespace topic_statistics
196
202
} // namespace rclcpp
197
203
198
- #endif // RCLCPP__TOPIC_STATISTICS__SUBSCRIBER_TOPIC_STATISTICS_HPP_
204
+ #endif // RCLCPP__TOPIC_STATISTICS__SUBSCRIPTION_TOPIC_STATISTICS_HPP_
0 commit comments