diff --git a/rclcpp/CMakeLists.txt b/rclcpp/CMakeLists.txt index 1389ad8..781a06c 100644 --- a/rclcpp/CMakeLists.txt +++ b/rclcpp/CMakeLists.txt @@ -535,6 +535,12 @@ if(BUILD_TESTING) target_link_libraries(test_wait_set ${PROJECT_NAME}) endif() + ament_add_gtest(test_subscription_options test/test_subscription_options.cpp) + if(TARGET test_subscription_options) + ament_target_dependencies(test_subscription_options "rcl") + target_link_libraries(test_subscription_options ${PROJECT_NAME}) + endif() + # Install test resources install( DIRECTORY test/resources diff --git a/rclcpp/include/rclcpp/subscription_options.hpp b/rclcpp/include/rclcpp/subscription_options.hpp index 2e27de6..72e1f4b 100644 --- a/rclcpp/include/rclcpp/subscription_options.hpp +++ b/rclcpp/include/rclcpp/subscription_options.hpp @@ -15,6 +15,7 @@ #ifndef RCLCPP__SUBSCRIPTION_OPTIONS_HPP_ #define RCLCPP__SUBSCRIPTION_OPTIONS_HPP_ +#include #include #include #include @@ -54,6 +55,24 @@ struct SubscriptionOptionsBase /// Optional RMW implementation specific payload to be used during creation of the subscription. std::shared_ptr rmw_implementation_payload = nullptr; + + // Options to configure topic statistics collector in the subscription. + struct TopicStatisticsOptions + { + // Represent the state of topic statistics collector. + enum class TopicStatisticsState {ENABLED, DISABLED}; + + // Enable and disable topic statistics calculation and publication. Defaults to disabled. + TopicStatisticsState state = TopicStatisticsState::DISABLED; + + // Topic to which topic statistics get published when enabled. Defaults to /statistics. + std::string publish_topic = "/statistics"; + + // Topic statistics publication period in ms. Defaults to one minute. + std::chrono::milliseconds publish_period{std::chrono::seconds(1)}; + }; + + TopicStatisticsOptions topic_stats_options; }; /// Structure containing optional configuration for Subscriptions. @@ -104,6 +123,7 @@ struct SubscriptionOptionsWithAllocator : public SubscriptionOptionsBase }; using SubscriptionOptions = SubscriptionOptionsWithAllocator>; +using TopicStatisticsState = SubscriptionOptionsBase::TopicStatisticsOptions::TopicStatisticsState; } // namespace rclcpp diff --git a/rclcpp/test/test_subscription_options.cpp b/rclcpp/test/test_subscription_options.cpp new file mode 100644 index 0000000..e884b03 --- /dev/null +++ b/rclcpp/test/test_subscription_options.cpp @@ -0,0 +1,44 @@ +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include + +#include "rclcpp/subscription_options.hpp" + +using namespace std::chrono_literals; + +namespace +{ +constexpr const char defaultPublishTopic[] = "/statistics"; +} + +TEST(TestSubscriptionOptions, topic_statistics_options) { + auto options = rclcpp::SubscriptionOptions(); + + EXPECT_EQ(options.topic_stats_options.state, rclcpp::TopicStatisticsState::DISABLED); + EXPECT_EQ(options.topic_stats_options.publish_topic, defaultPublishTopic); + EXPECT_EQ(options.topic_stats_options.publish_period, 1s); + + options.topic_stats_options.state = rclcpp::TopicStatisticsState::ENABLED; + options.topic_stats_options.publish_topic = "topic_statistics"; + options.topic_stats_options.publish_period = 5min; + + EXPECT_EQ(options.topic_stats_options.state, rclcpp::TopicStatisticsState::ENABLED); + EXPECT_EQ(options.topic_stats_options.publish_topic, "topic_statistics"); + EXPECT_EQ(options.topic_stats_options.publish_period, 5min); +}