Skip to content

Commit d8fd4b8

Browse files
methylDragonBarry-Xu-2018
authored andcommitted
Implement deliver message kind (ros2#2168)
* Implement deliver message kind Signed-off-by: methylDragon <[email protected]> * Add examples to docstring Signed-off-by: methylDragon <[email protected]> --------- Signed-off-by: methylDragon <[email protected]>
1 parent 269c258 commit d8fd4b8

File tree

5 files changed

+38
-39
lines changed

5 files changed

+38
-39
lines changed

rclcpp/include/rclcpp/generic_subscription.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class GenericSubscription : public rclcpp::SubscriptionBase
8484
options.to_rcl_subscription_options(qos),
8585
options.event_callbacks,
8686
options.use_default_callbacks,
87-
SubscriptionType::SERIALIZED_MESSAGE),
87+
DeliveredMessageKind::SERIALIZED_MESSAGE),
8888
callback_(callback),
8989
ts_lib_(ts_lib)
9090
{}

rclcpp/include/rclcpp/subscription.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class Subscription : public SubscriptionBase
144144
// NOTE(methylDragon): Passing these args separately is necessary for event binding
145145
options.event_callbacks,
146146
options.use_default_callbacks,
147-
callback.is_serialized_message_callback() ? SubscriptionType::SERIALIZED_MESSAGE : SubscriptionType::ROS_MESSAGE), // NOLINT
147+
callback.is_serialized_message_callback() ? DeliveredMessageKind::SERIALIZED_MESSAGE : DeliveredMessageKind::ROS_MESSAGE), // NOLINT
148148
any_callback_(callback),
149149
options_(options),
150150
message_memory_strategy_(message_memory_strategy)

rclcpp/include/rclcpp/subscription_base.hpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,25 @@ namespace experimental
6363
class IntraProcessManager;
6464
} // namespace experimental
6565

66-
enum class SubscriptionType : uint8_t
66+
/// The kind of message that the subscription delivers in its callback, used by the executor
67+
/**
68+
* This enum needs to exist because the callback handle is not accessible to the executor's scope.
69+
*
70+
* "Kind" is used since what is being delivered is a category of messages, for example, there are
71+
* different ROS message types that can be delivered, but they're all ROS messages.
72+
*
73+
* As a concrete example, all of the following callbacks will be considered ROS_MESSAGE for
74+
* DeliveredMessageKind:
75+
* - void callback(const std_msgs::msg::String &)
76+
* - void callback(const std::string &) // type adaption
77+
* - void callback(std::unique_ptr<std_msgs::msg::String>)
78+
*/
79+
enum class DeliveredMessageKind : uint8_t
6780
{
68-
INVALID = 0, // The subscription type is most likely uninitialized
69-
ROS_MESSAGE = 1, // take message as ROS message and handle as ROS message
70-
SERIALIZED_MESSAGE = 2, // take message as serialized and handle as serialized
71-
DYNAMIC_MESSAGE_DIRECT = 3, // take message as DynamicMessage and handle as DynamicMessage
72-
DYNAMIC_MESSAGE_FROM_SERIALIZED = 4 // take message as serialized and handle as DynamicMessage
81+
INVALID = 0,
82+
ROS_MESSAGE = 1, // The subscription delivers a ROS message to its callback
83+
SERIALIZED_MESSAGE = 2, // The subscription delivers a serialized message to its callback
84+
DYNAMIC_MESSAGE = 3, // The subscription delivers a dynamic message to its callback
7385
};
7486

7587
/// Virtual base class for subscriptions. This pattern allows us to iterate over different template
@@ -88,7 +100,8 @@ class SubscriptionBase : public std::enable_shared_from_this<SubscriptionBase>
88100
* \param[in] type_support_handle rosidl type support struct, for the Message type of the topic.
89101
* \param[in] topic_name Name of the topic to subscribe to.
90102
* \param[in] subscription_options Options for the subscription.
91-
* \param[in] subscription_type Enum flag to change how the message will be received and delivered
103+
* \param[in] delivered_message_kind Enum flag to change how the message will be received and
104+
* delivered
92105
*/
93106
RCLCPP_PUBLIC
94107
SubscriptionBase(
@@ -98,7 +111,7 @@ class SubscriptionBase : public std::enable_shared_from_this<SubscriptionBase>
98111
const rcl_subscription_options_t & subscription_options,
99112
const SubscriptionEventCallbacks & event_callbacks,
100113
bool use_default_callbacks,
101-
SubscriptionType subscription_type = SubscriptionType::ROS_MESSAGE);
114+
DeliveredMessageKind delivered_message_kind = DeliveredMessageKind::ROS_MESSAGE);
102115

103116
/// Destructor.
104117
RCLCPP_PUBLIC
@@ -249,10 +262,10 @@ class SubscriptionBase : public std::enable_shared_from_this<SubscriptionBase>
249262

250263
/// Return the type of the subscription.
251264
/**
252-
* \return `SubscriptionType`, which adjusts how messages are received and delivered.
265+
* \return `DeliveredMessageKind`, which adjusts how messages are received and delivered.
253266
*/
254267
RCLCPP_PUBLIC
255-
SubscriptionType
268+
DeliveredMessageKind
256269
get_subscription_type() const;
257270

258271
/// Get matching publisher count.
@@ -650,7 +663,7 @@ class SubscriptionBase : public std::enable_shared_from_this<SubscriptionBase>
650663
RCLCPP_DISABLE_COPY(SubscriptionBase)
651664

652665
rosidl_message_type_support_t type_support_;
653-
SubscriptionType subscription_type_;
666+
DeliveredMessageKind delivered_message_type_;
654667

655668
std::atomic<bool> subscription_in_use_by_wait_set_{false};
656669
std::atomic<bool> intra_process_subscription_waitable_in_use_by_wait_set_{false};

rclcpp/src/rclcpp/executor.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@ Executor::execute_subscription(rclcpp::SubscriptionBase::SharedPtr subscription)
605605
message_info.get_rmw_message_info().from_intra_process = false;
606606

607607
switch (subscription->get_subscription_type()) {
608-
// Take ROS message
609-
case rclcpp::SubscriptionType::ROS_MESSAGE:
608+
// Deliver ROS message
609+
case rclcpp::DeliveredMessageKind::ROS_MESSAGE:
610610
{
611611
if (subscription->can_loan_messages()) {
612612
// This is the case where a loaned message is taken from the middleware via
@@ -659,8 +659,8 @@ Executor::execute_subscription(rclcpp::SubscriptionBase::SharedPtr subscription)
659659
break;
660660
}
661661

662-
// Take serialized message
663-
case rclcpp::SubscriptionType::SERIALIZED_MESSAGE:
662+
// Deliver serialized message
663+
case rclcpp::DeliveredMessageKind::SERIALIZED_MESSAGE:
664664
{
665665
// This is the case where a copy of the serialized message is taken from
666666
// the middleware via inter-process communication.
@@ -679,21 +679,15 @@ Executor::execute_subscription(rclcpp::SubscriptionBase::SharedPtr subscription)
679679
}
680680

681681
// DYNAMIC SUBSCRIPTION ========================================================================
682-
// Take dynamic message directly from the middleware
683-
case rclcpp::SubscriptionType::DYNAMIC_MESSAGE_DIRECT:
684-
{
685-
throw std::runtime_error("Unimplemented");
686-
}
687-
688-
// Take serialized and then convert to dynamic message
689-
case rclcpp::SubscriptionType::DYNAMIC_MESSAGE_FROM_SERIALIZED:
682+
// Deliver dynamic message
683+
case rclcpp::DeliveredMessageKind::DYNAMIC_MESSAGE:
690684
{
691685
throw std::runtime_error("Unimplemented");
692686
}
693687

694688
default:
695689
{
696-
throw std::runtime_error("Subscription type is not supported");
690+
throw std::runtime_error("Delivered message kind is not supported");
697691
}
698692
}
699693
return;

rclcpp/src/rclcpp/subscription_base.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,16 @@ SubscriptionBase::SubscriptionBase(
4444
const rcl_subscription_options_t & subscription_options,
4545
const SubscriptionEventCallbacks & event_callbacks,
4646
bool use_default_callbacks,
47-
SubscriptionType subscription_type)
47+
DeliveredMessageKind delivered_message_kind)
4848
: node_base_(node_base),
4949
node_handle_(node_base_->get_shared_rcl_node_handle()),
5050
node_logger_(rclcpp::get_node_logger(node_handle_.get())),
5151
use_intra_process_(false),
5252
intra_process_subscription_id_(0),
5353
event_callbacks_(event_callbacks),
5454
type_support_(type_support_handle),
55-
subscription_type_(subscription_type)
55+
delivered_message_type_(delivered_message_kind)
5656
{
57-
if (!rmw_feature_supported(RMW_MIDDLEWARE_CAN_TAKE_DYNAMIC_MESSAGE) &&
58-
subscription_type == rclcpp::SubscriptionType::DYNAMIC_MESSAGE_DIRECT)
59-
{
60-
throw std::runtime_error(
61-
"Cannot set subscription to take dynamic message directly, feature not supported in rmw"
62-
);
63-
}
64-
6557
auto custom_deletor = [node_handle = this->node_handle_](rcl_subscription_t * rcl_subs)
6658
{
6759
if (rcl_subscription_fini(rcl_subs, node_handle.get()) != RCL_RET_OK) {
@@ -269,13 +261,13 @@ SubscriptionBase::get_message_type_support_handle() const
269261
bool
270262
SubscriptionBase::is_serialized() const
271263
{
272-
return subscription_type_ == rclcpp::SubscriptionType::SERIALIZED_MESSAGE;
264+
return delivered_message_type_ == rclcpp::DeliveredMessageKind::SERIALIZED_MESSAGE;
273265
}
274266

275-
rclcpp::SubscriptionType
267+
rclcpp::DeliveredMessageKind
276268
SubscriptionBase::get_subscription_type() const
277269
{
278-
return subscription_type_;
270+
return delivered_message_type_;
279271
}
280272

281273
size_t

0 commit comments

Comments
 (0)