-
Notifications
You must be signed in to change notification settings - Fork 469
[WIP] Extend DynamicSubscription to support serialized messages #2195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
emersonknapp
wants to merge
4
commits into
ros2:dynamic_subscription
Choose a base branch
from
emersonknapp:emersonknapp/dynamic-serialized-sub
base: dynamic_subscription
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// Copyright 2022 Open Source Robotics Foundation, Inc. | ||
// | ||
// 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. | ||
|
||
#ifndef RCLCPP__DYNAMIC_SUBSCRIPTION_HPP_ | ||
#define RCLCPP__DYNAMIC_SUBSCRIPTION_HPP_ | ||
|
||
#include <rosidl_dynamic_typesupport/identifier.h> | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <string> | ||
|
||
#include "rcpputils/shared_library.hpp" | ||
|
||
#include "rclcpp/callback_group.hpp" | ||
#include "rclcpp/dynamic_typesupport/dynamic_message_type_support.hpp" | ||
#include "rclcpp/macros.hpp" | ||
#include "rclcpp/node_interfaces/node_base_interface.hpp" | ||
#include "rclcpp/node_interfaces/node_topics_interface.hpp" | ||
#include "rclcpp/qos.hpp" | ||
#include "rclcpp/serialized_message.hpp" | ||
#include "rclcpp/subscription_base.hpp" | ||
#include "rclcpp/typesupport_helpers.hpp" | ||
#include "rclcpp/visibility_control.hpp" | ||
|
||
namespace rclcpp | ||
{ | ||
|
||
typedef std::variant< | ||
std::function<void (dynamic_typesupport::DynamicMessage::SharedPtr)>, | ||
std::function<void (dynamic_typesupport::DynamicMessage::SharedPtr, const MessageInfo &)>, | ||
std::function<void (std::shared_ptr<SerializedMessage>)>, | ||
std::function<void (std::shared_ptr<SerializedMessage>, const MessageInfo &)> | ||
> AnyDynamicSubscriptionCallback; | ||
|
||
/// %Subscription for messages whose type descriptions are obtained at runtime. | ||
/** | ||
* Since the type is not known at compile time, this is not a template, and the dynamic library | ||
* containing type support information has to be identified and loaded based on the type name. | ||
* | ||
* NOTE(methylDragon): No considerations for intra-process handling are made. | ||
*/ | ||
class DynamicSubscription : public rclcpp::SubscriptionBase | ||
{ | ||
public: | ||
// cppcheck-suppress unknownMacro | ||
RCLCPP_SMART_PTR_DEFINITIONS(DynamicSubscription) | ||
|
||
template<typename AllocatorT = std::allocator<void>> | ||
DynamicSubscription( | ||
rclcpp::node_interfaces::NodeBaseInterface * node_base, | ||
rclcpp::dynamic_typesupport::DynamicMessageTypeSupport::SharedPtr type_support, | ||
const std::string & topic_name, | ||
const rclcpp::QoS & qos, | ||
AnyDynamicSubscriptionCallback callback, | ||
const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options) | ||
: SubscriptionBase( | ||
node_base, | ||
type_support->get_const_rosidl_message_type_support(), | ||
topic_name, | ||
options.to_rcl_subscription_options( | ||
qos), | ||
options.event_callbacks, | ||
options.use_default_callbacks, | ||
is_callback_serialized(callback) ? | ||
DeliveredMessageKind::SERIALIZED_MESSAGE : | ||
DeliveredMessageKind::DYNAMIC_MESSAGE), | ||
ts_(type_support), | ||
callback_(callback), | ||
serialization_support_(nullptr), | ||
dynamic_message_(nullptr), | ||
dynamic_message_type_(nullptr) | ||
{ | ||
if (!type_support) { | ||
throw std::runtime_error("DynamicMessageTypeSupport cannot be nullptr!"); | ||
} | ||
|
||
if (type_support->get_const_rosidl_message_type_support().typesupport_identifier != | ||
rosidl_get_dynamic_typesupport_identifier()) | ||
{ | ||
throw std::runtime_error( | ||
"DynamicSubscription must use dynamic type introspection type support!"); | ||
} | ||
|
||
serialization_support_ = type_support->get_shared_dynamic_serialization_support(); | ||
dynamic_message_type_ = type_support->get_shared_dynamic_message_type()->clone_shared(); | ||
dynamic_message_ = type_support->get_shared_dynamic_message()->clone_shared(); | ||
} | ||
|
||
// TODO(methylDragon): | ||
/// Deferred type description constructor, only usable if the middleware implementation supports | ||
/// type discovery | ||
|
||
RCLCPP_PUBLIC | ||
virtual ~DynamicSubscription() = default; | ||
|
||
// Same as create_serialized_message() as the subscription is to serialized_messages only | ||
RCLCPP_PUBLIC | ||
std::shared_ptr<void> create_message() override; | ||
|
||
RCLCPP_PUBLIC | ||
std::shared_ptr<rclcpp::SerializedMessage> create_serialized_message() override; | ||
|
||
/// Cast the message to a rclcpp::SerializedMessage and call the callback. | ||
RCLCPP_PUBLIC | ||
void handle_message( | ||
std::shared_ptr<void> & message, const rclcpp::MessageInfo & message_info) override; | ||
|
||
/// Handle dispatching rclcpp::SerializedMessage to user callback. | ||
RCLCPP_PUBLIC | ||
void | ||
handle_serialized_message( | ||
const std::shared_ptr<rclcpp::SerializedMessage> & serialized_message, | ||
const rclcpp::MessageInfo & message_info) override; | ||
|
||
/// This function is currently not implemented. | ||
RCLCPP_PUBLIC | ||
void handle_loaned_message( | ||
void * loaned_message, const rclcpp::MessageInfo & message_info) override; | ||
|
||
// Same as return_serialized_message() as the subscription is to serialized_messages only | ||
RCLCPP_PUBLIC | ||
void return_message(std::shared_ptr<void> & message) override; | ||
|
||
RCLCPP_PUBLIC | ||
void return_serialized_message(std::shared_ptr<rclcpp::SerializedMessage> & message) override; | ||
|
||
|
||
// DYNAMIC TYPE ================================================================================== | ||
// TODO(methylDragon): Reorder later | ||
RCLCPP_PUBLIC | ||
rclcpp::dynamic_typesupport::DynamicMessageType::SharedPtr | ||
get_shared_dynamic_message_type() override; | ||
|
||
RCLCPP_PUBLIC | ||
rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr get_shared_dynamic_message() override; | ||
|
||
RCLCPP_PUBLIC | ||
rclcpp::dynamic_typesupport::DynamicSerializationSupport::SharedPtr | ||
get_shared_dynamic_serialization_support() override; | ||
|
||
RCLCPP_PUBLIC | ||
rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr create_dynamic_message() override; | ||
|
||
RCLCPP_PUBLIC | ||
void return_dynamic_message( | ||
rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr & message) override; | ||
|
||
RCLCPP_PUBLIC | ||
void handle_dynamic_message( | ||
const rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr & message, | ||
const rclcpp::MessageInfo & message_info) override; | ||
|
||
private: | ||
RCLCPP_DISABLE_COPY(DynamicSubscription) | ||
|
||
static bool is_callback_serialized(const rclcpp::AnyDynamicSubscriptionCallback &); | ||
|
||
rclcpp::dynamic_typesupport::DynamicMessageTypeSupport::SharedPtr ts_; | ||
rclcpp::AnyDynamicSubscriptionCallback callback_; | ||
|
||
rclcpp::dynamic_typesupport::DynamicSerializationSupport::SharedPtr serialization_support_; | ||
rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr dynamic_message_; | ||
rclcpp::dynamic_typesupport::DynamicMessageType::SharedPtr dynamic_message_type_; | ||
}; | ||
|
||
} // namespace rclcpp | ||
|
||
#endif // RCLCPP__DYNAMIC_SUBSCRIPTION_HPP_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.