Skip to content
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

initial import for lifecycle subscriber #292

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rclcpp/include/rclcpp/create_subscription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace rclcpp
{

template<typename MessageT, typename CallbackT, typename AllocatorT, typename SubscriptionT>
typename rclcpp::subscription::Subscription<MessageT, AllocatorT>::SharedPtr
typename std::shared_ptr<SubscriptionT>
create_subscription(
rclcpp::node_interfaces::NodeTopicsInterface * node_topics,
const std::string & topic_name,
Expand Down
17 changes: 12 additions & 5 deletions rclcpp/include/rclcpp/subscription.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class SubscriptionBase
// \return Shared pointer to the fresh message.
virtual std::shared_ptr<void>
create_message() = 0;

/// Check if we need to handle the message, and execute the callback if we do.
/**
* \param[in] message Shared pointer to the message to handle.
Expand Down Expand Up @@ -164,13 +165,16 @@ class Subscription : public SubscriptionBase
* Behavior may be undefined if called while the subscription could be executing.
* \param[in] message_memory_strategy Shared pointer to the memory strategy to set.
*/
void set_message_memory_strategy(
virtual void
set_message_memory_strategy(
typename message_memory_strategy::MessageMemoryStrategy<MessageT,
Alloc>::SharedPtr message_memory_strategy)
{
message_memory_strategy_ = message_memory_strategy;
}
std::shared_ptr<void> create_message()

virtual std::shared_ptr<void>
create_message()
{
/* The default message memory strategy provides a dynamically allocated message on each call to
* create_message, though alternative memory strategies that re-use a preallocated message may be
Expand All @@ -179,7 +183,8 @@ class Subscription : public SubscriptionBase
return message_memory_strategy_->borrow_message();
}

void handle_message(std::shared_ptr<void> & message, const rmw_message_info_t & message_info)
virtual void
handle_message(std::shared_ptr<void> & message, const rmw_message_info_t & message_info)
{
if (matches_any_intra_process_publishers_) {
if (matches_any_intra_process_publishers_(&message_info.publisher_gid)) {
Expand All @@ -192,13 +197,15 @@ class Subscription : public SubscriptionBase
any_callback_.dispatch(typed_message, message_info);
}

void return_message(std::shared_ptr<void> & message)
virtual void
return_message(std::shared_ptr<void> & message)
{
auto typed_message = std::static_pointer_cast<MessageT>(message);
message_memory_strategy_->return_message(typed_message);
}

void handle_intra_process_message(
virtual void
handle_intra_process_message(
rcl_interfaces::msg::IntraProcessMessage & ipm,
const rmw_message_info_t & message_info)
{
Expand Down
9 changes: 4 additions & 5 deletions rclcpp/include/rclcpp/subscription_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ create_subscription_factory(
AnySubscriptionCallback<MessageT, Alloc> any_subscription_callback(allocator);
any_subscription_callback.set(std::forward<CallbackT>(callback));

auto message_alloc =
std::make_shared<typename subscription::Subscription<MessageT, Alloc>::MessageAlloc>();
auto message_alloc = std::make_shared<typename subscription::Subscription<MessageT, Alloc>::MessageAlloc>();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we have to create a SubscriptionT allocator here


// factory function that creates a MessageT specific SubscriptionT
factory.create_typed_subscription =
Expand All @@ -91,10 +90,10 @@ create_subscription_factory(
subscription_options.allocator =
rclcpp::allocator::get_rcl_allocator<MessageT>(*message_alloc.get());

using rclcpp::subscription::Subscription;
//using rclcpp::subscription::Subscription;
using rclcpp::subscription::SubscriptionBase;

auto sub = Subscription<MessageT, Alloc>::make_shared(
std::shared_ptr<SubscriptionT> sub = std::make_shared<SubscriptionT>(
node_base->get_shared_rcl_node_handle(),
topic_name,
subscription_options,
Expand Down Expand Up @@ -126,7 +125,7 @@ create_subscription_factory(
uint64_t publisher_id,
uint64_t message_sequence,
uint64_t subscription_id,
typename rclcpp::subscription::Subscription<MessageT, Alloc>::MessageUniquePtr & message)
typename SubscriptionT::MessageUniquePtr & message)
{
auto ipm = weak_ipm.lock();
if (!ipm) {
Expand Down
11 changes: 5 additions & 6 deletions rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
#include "rclcpp_lifecycle/lifecycle_publisher.hpp"
#include "rclcpp_lifecycle/lifecycle_subscription.hpp"
#include "rclcpp_lifecycle/state.hpp"
#include "rclcpp_lifecycle/transition.hpp"
#include "rclcpp_lifecycle/visibility_control.h"
Expand Down Expand Up @@ -147,9 +148,8 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface,
template<
typename MessageT,
typename CallbackT,
typename Alloc = std::allocator<void>,
typename SubscriptionT = rclcpp::subscription::Subscription<MessageT, Alloc>>
std::shared_ptr<SubscriptionT>
typename Alloc = std::allocator<void>>
std::shared_ptr<rclcpp_lifecycle::LifecycleSubscription<MessageT, Alloc>>
create_subscription(
const std::string & topic_name,
CallbackT && callback,
Expand Down Expand Up @@ -177,9 +177,8 @@ class LifecycleNode : public node_interfaces::LifecycleNodeInterface,
template<
typename MessageT,
typename CallbackT,
typename Alloc = std::allocator<void>,
typename SubscriptionT = rclcpp::subscription::Subscription<MessageT, Alloc>>
std::shared_ptr<SubscriptionT>
typename Alloc = std::allocator<void>>
std::shared_ptr<rclcpp_lifecycle::LifecycleSubscription<MessageT, Alloc>>
create_subscription(
const std::string & topic_name,
size_t qos_history_depth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ LifecycleNode::create_publisher(
}

// TODO(karsten1987): Create LifecycleSubscriber
template<typename MessageT, typename CallbackT, typename Alloc, typename SubscriptionT>
std::shared_ptr<SubscriptionT>
template<typename MessageT, typename CallbackT, typename Alloc>
std::shared_ptr<rclcpp_lifecycle::LifecycleSubscription<MessageT, Alloc>>
LifecycleNode::create_subscription(
const std::string & topic_name,
CallbackT && callback,
Expand All @@ -79,6 +79,8 @@ LifecycleNode::create_subscription(
msg_mem_strat,
std::shared_ptr<Alloc> allocator)
{
using SubscriptionT = rclcpp_lifecycle::LifecycleSubscription<MessageT, Alloc>;

if (!allocator) {
allocator = std::make_shared<Alloc>();
}
Expand All @@ -90,7 +92,7 @@ LifecycleNode::create_subscription(

return rclcpp::create_subscription<
MessageT, CallbackT, Alloc,
rclcpp::subscription::Subscription<MessageT, Alloc>>(
SubscriptionT>(
this->node_topics_.get(),
topic_name,
std::forward<CallbackT>(callback),
Expand All @@ -105,9 +107,8 @@ LifecycleNode::create_subscription(
template<
typename MessageT,
typename CallbackT,
typename Alloc,
typename SubscriptionT>
std::shared_ptr<SubscriptionT>
typename Alloc>
std::shared_ptr<rclcpp_lifecycle::LifecycleSubscription<MessageT, Alloc>>
LifecycleNode::create_subscription(
const std::string & topic_name,
size_t qos_history_depth,
Expand Down
114 changes: 114 additions & 0 deletions rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_subscription.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright 2014 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_LIFECYCLE__LIFECYCLE_SUBSCRIPTION_HPP_
#define RCLCPP_LIFECYCLE__LIFECYCLE_SUBSCRIPTION_HPP_

#include <functional>
#include <memory>

#include "rclcpp/subscription.hpp"

namespace rclcpp_lifecycle
{

class LifecycleSubscriptionInterface
{
public:
virtual void on_activate() = 0;
virtual void on_deactivate() = 0;
virtual bool is_activated() = 0;
};

/// Subscription implementation, templated on the type of message this subscription receives.
template<typename MessageT, typename Alloc = std::allocator<void>>
class LifecycleSubscription : public LifecycleSubscriptionInterface,
public rclcpp::subscription::Subscription<MessageT, Alloc>
{
public:
//using MessageAllocTraits = rclcpp::allocator::AllocRebind<MessageT, Alloc>;
//using MessageAlloc = typename MessageAllocTraits::allocator_type;
//using MessageDeleter = rclcpp::allocator::Deleter<MessageAlloc, MessageT>;
//using MessageUniquePtr = std::unique_ptr<MessageT, MessageDeleter>;

//RCLCPP_SMART_PTR_DEFINITIONS(LifecycleSubscription)

/// Default constructor.
/**
* The constructor for a subscription is almost never called directly. Instead, subscriptions
* should be instantiated through Node::create_subscription.
* \param[in] node_handle rcl representation of the node that owns this subscription.
* \param[in] topic_name Name of the topic to subscribe to.
* \param[in] subscription_options options for the subscription.
* \param[in] callback User defined callback to call when a message is received.
* \param[in] memory_strategy The memory strategy to be used for managing message memory.
*/
LifecycleSubscription(
std::shared_ptr<rcl_node_t> node_handle,
const std::string & topic_name,
const rcl_subscription_options_t & subscription_options,
rclcpp::any_subscription_callback::AnySubscriptionCallback<MessageT, Alloc> callback,
typename rclcpp::message_memory_strategy::MessageMemoryStrategy<MessageT, Alloc>::SharedPtr
memory_strategy = rclcpp::message_memory_strategy::MessageMemoryStrategy<MessageT,
Alloc>::create_default())
: rclcpp::subscription::Subscription<MessageT, Alloc>(
node_handle,
topic_name,
subscription_options,
callback,
memory_strategy),
enabled_(false)
{ }

/// Check if we need to handle the message, and execute the callback if we do.
/**
* \param[in] message Shared pointer to the message to handle.
* \param[in] message_info Metadata associated with this message.
*/
virtual void
handle_message(std::shared_ptr<void> & message, const rmw_message_info_t & message_info)
{
fprintf(stderr, "Handle Message callled.\n");
if (!enabled_) {
return;
}

rclcpp::subscription::Subscription<MessageT, Alloc>::handle_message(message, message_info);
}

void
on_activate()
{
enabled_ = true;
}

void
on_deactivate()
{
enabled_ = false;
}

bool
is_activated()
{
return enabled_;
}

private:
bool enabled_ = false;
};

} // namespace rclcpp_lifecycle

#endif // RCLCPP_LIFECYCLE__LIFECYCLE_SUBSCRIPTION_HPP_