|
| 1 | +// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <chrono> |
| 16 | +#include <memory> |
| 17 | +#include <string> |
| 18 | + |
| 19 | +#include "rcutils/cmdline_parser.h" |
| 20 | + |
| 21 | +#include "rclcpp/rclcpp.hpp" |
| 22 | +#include "rclcpp/executors/single_threaded_executor.hpp" |
| 23 | + |
| 24 | +#include "std_msgs/msg/string.hpp" |
| 25 | +#include "std_msgs/msg/bool.hpp" |
| 26 | + |
| 27 | +#include "quality_of_service_demo/common_nodes.hpp" |
| 28 | + |
| 29 | +void print_usage() |
| 30 | +{ |
| 31 | + std::cout << "Usage:\n"; |
| 32 | + std::cout << "incompatible_qos [-h] <incompatible_qos_policy_name>\n\n"; |
| 33 | + std::cout << "required arguments:\n"; |
| 34 | + std::cout << "incompatible_qos_policy_name: The QoS Policy that should be incompatible between\n" |
| 35 | + " the publisher and subscription (durability, deadline,\n" |
| 36 | + " liveliness_policy, liveliness_lease_duration,\n" |
| 37 | + " or reliability).\n\n"; |
| 38 | + std::cout << "optional arguments:\n"; |
| 39 | + std::cout << "-h: Print this help message.\n"; |
| 40 | +} |
| 41 | + |
| 42 | +int main(int argc, char * argv[]) |
| 43 | +{ |
| 44 | + // Force flush of the stdout buffer. |
| 45 | + setvbuf(stdout, NULL, _IONBF, BUFSIZ); |
| 46 | + |
| 47 | + // Argument count and usage |
| 48 | + if (argc < 2 || rcutils_cli_option_exist(argv, argv + argc, "-h")) { |
| 49 | + print_usage(); |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + // Configuration variables |
| 54 | + std::string qos_policy_name(argv[1]); |
| 55 | + rclcpp::QoS qos_profile_publisher(10); |
| 56 | + rclcpp::QoS qos_profile_subscription(10); |
| 57 | + |
| 58 | + if (qos_policy_name == "durability") { |
| 59 | + std::cout << "Durability incompatibility selected.\n" |
| 60 | + "Incompatibility condition: publisher durability kind < " |
| 61 | + "subscripition durability kind.\n" |
| 62 | + "Setting publisher durability to: VOLATILE\n" |
| 63 | + "Setting subscription durability to: TRANSIENT_LOCAL\n"; |
| 64 | + qos_profile_publisher.durability(RMW_QOS_POLICY_DURABILITY_VOLATILE); |
| 65 | + qos_profile_subscription.durability(RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL); |
| 66 | + } else if (qos_policy_name == "deadline") { |
| 67 | + std::cout << "Deadline incompatibility selected.\n" |
| 68 | + "Incompatibility condition: publisher deadline > subscription deadline\n" |
| 69 | + "Setting publisher deadline to: 2 seconds\n" |
| 70 | + "Setting subscription deadline to: 1 second\n"; |
| 71 | + qos_profile_publisher.deadline(std::chrono::milliseconds(2000)); |
| 72 | + qos_profile_subscription.deadline(std::chrono::milliseconds(1000)); |
| 73 | + } else if (qos_policy_name == "liveliness_policy") { |
| 74 | + std::cout << "Liveliness Policy incompatibility selected.\n" |
| 75 | + "Incompatibility condition: publisher liveliness policy < " |
| 76 | + "subscripition liveliness policy.\n" |
| 77 | + "Setting publisher liveliness policy to: MANUAL_BY_NODE\n" |
| 78 | + "Setting subscription liveliness policy to: MANUAL_BY_TOPIC\n"; |
| 79 | + qos_profile_publisher.liveliness(RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_NODE); |
| 80 | + qos_profile_subscription.liveliness(RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC); |
| 81 | + } else if (qos_policy_name == "liveliness_lease_duration") { |
| 82 | + std::cout << "Liveliness lease duration incompatibility selected.\n" |
| 83 | + "Incompatibility condition: publisher liveliness lease duration > " |
| 84 | + "subscription liveliness lease duration\n" |
| 85 | + "Setting publisher liveliness lease duration to: 2 seconds\n" |
| 86 | + "Setting subscription liveliness lease duration to: 1 seconds\n"; |
| 87 | + qos_profile_publisher.liveliness_lease_duration(std::chrono::milliseconds(2000)); |
| 88 | + qos_profile_subscription.liveliness_lease_duration(std::chrono::milliseconds(1000)); |
| 89 | + } else if (qos_policy_name == "reliability") { |
| 90 | + std::cout << "Reliability incompatibility selected.\n" |
| 91 | + "Incompatibility condition: publisher reliability < subscripition reliability.\n" |
| 92 | + "Setting publisher reliability to: BEST_EFFORT\n" |
| 93 | + "Setting subscription reliability to: RELIABLE\n"; |
| 94 | + qos_profile_publisher.reliability(RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT); |
| 95 | + qos_profile_subscription.reliability(RMW_QOS_POLICY_RELIABILITY_RELIABLE); |
| 96 | + } else { |
| 97 | + std::cout << qos_policy_name << " is not a valid qos policy name\n"; |
| 98 | + print_usage(); |
| 99 | + return 0; |
| 100 | + } |
| 101 | + std::cout << "\n"; |
| 102 | + |
| 103 | + // Initialization and configuration |
| 104 | + rclcpp::init(argc, argv); |
| 105 | + const std::string topic("qos_incompatible_qos_chatter"); |
| 106 | + constexpr size_t num_msgs = 5; |
| 107 | + |
| 108 | + auto talker = std::make_shared<Talker>(qos_profile_publisher, topic, num_msgs); |
| 109 | + talker->get_options().event_callbacks.incompatible_qos_callback = |
| 110 | + [node = talker.get()](rclcpp::QOSOfferedIncompatibleQoSInfo & event) -> void |
| 111 | + { |
| 112 | + RCLCPP_INFO( |
| 113 | + node->get_logger(), |
| 114 | + "Offered incompatible qos - total %d delta %d last_policy_kind: %d", |
| 115 | + event.total_count, event.total_count_change, event.last_policy_kind); |
| 116 | + }; |
| 117 | + |
| 118 | + auto listener = std::make_shared<Listener>(qos_profile_subscription, topic); |
| 119 | + listener->get_options().event_callbacks.incompatible_qos_callback = |
| 120 | + [node = listener.get()](rclcpp::QOSRequestedIncompatibleQoSInfo & event) -> void |
| 121 | + { |
| 122 | + RCLCPP_INFO( |
| 123 | + node->get_logger(), |
| 124 | + "Requested incompatible qos - total %d delta %d last_policy_kind: %d", |
| 125 | + event.total_count, event.total_count_change, event.last_policy_kind); |
| 126 | + }; |
| 127 | + |
| 128 | + try { |
| 129 | + talker->initialize(); |
| 130 | + listener->initialize(); |
| 131 | + } catch (const rclcpp::UnsupportedEventTypeException & exc) { |
| 132 | + std::cout << '\n' << exc.what() << "\n\n" |
| 133 | + "Please try this demo using a different RMW implementation\n"; |
| 134 | + return -1; |
| 135 | + } |
| 136 | + |
| 137 | + // Execution |
| 138 | + rclcpp::executors::SingleThreadedExecutor executor; |
| 139 | + executor.add_node(talker); |
| 140 | + executor.add_node(listener); |
| 141 | + |
| 142 | + while (talker->get_published_count() < num_msgs) { |
| 143 | + executor.spin_once(); |
| 144 | + } |
| 145 | + |
| 146 | + // Cleanup |
| 147 | + rclcpp::shutdown(); |
| 148 | + |
| 149 | + return 0; |
| 150 | +} |
0 commit comments