Skip to content

Commit

Permalink
Merge pull request #22 from ros2/request-header
Browse files Browse the repository at this point in the history
Pass request header to callbacks
  • Loading branch information
esteve committed Apr 22, 2015
2 parents 0c0bb8a + c3791fc commit 1bf595d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 19 deletions.
20 changes: 15 additions & 5 deletions rclcpp/include/rclcpp/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,23 @@ class Node
template<typename ServiceT>
typename rclcpp::client::Client<ServiceT>::SharedPtr
create_client(
std::string service_name,
const std::string & service_name,
rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr);

/* Create and return a Service. */
template<typename ServiceT>
typename rclcpp::service::Service<ServiceT>::SharedPtr
create_service(
std::string service_name,
std::function<void(
const std::shared_ptr<typename ServiceT::Request> &,
std::shared_ptr<typename ServiceT::Response> &)> callback,
const std::string & service_name,
typename rclcpp::service::Service<ServiceT>::CallbackType callback,
rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr);

/* Create and return a Service. */
template<typename ServiceT>
typename rclcpp::service::Service<ServiceT>::SharedPtr
create_service(
const std::string & service_name,
typename rclcpp::service::Service<ServiceT>::CallbackWithHeaderType callback_with_header,
rclcpp::callback_group::CallbackGroup::SharedPtr group = nullptr);

private:
Expand All @@ -138,6 +144,10 @@ class Node
size_t number_of_services_;
size_t number_of_clients_;

void register_service(
const std::string & service_name,
std::shared_ptr<rclcpp::service::ServiceBase> serv_base_ptr,
rclcpp::callback_group::CallbackGroup::SharedPtr group);
};

} /* namespace node */
Expand Down
47 changes: 37 additions & 10 deletions rclcpp/include/rclcpp/node_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Node::create_wall_timer(
template<typename ServiceT>
typename client::Client<ServiceT>::SharedPtr
Node::create_client(
std::string service_name,
const std::string & service_name,
rclcpp::callback_group::CallbackGroup::SharedPtr group)
{
using rosidl_generator_cpp::get_service_type_support_handle;
Expand Down Expand Up @@ -185,9 +185,8 @@ Node::create_client(
template<typename ServiceT>
typename service::Service<ServiceT>::SharedPtr
Node::create_service(
std::string service_name,
std::function<void(const std::shared_ptr<typename ServiceT::Request> &,
std::shared_ptr<typename ServiceT::Response> &)> callback,
const std::string & service_name,
typename rclcpp::service::Service<ServiceT>::CallbackType callback,
rclcpp::callback_group::CallbackGroup::SharedPtr group)
{
using rosidl_generator_cpp::get_service_type_support_handle;
Expand All @@ -197,14 +196,44 @@ Node::create_service(
rmw_service_t * service_handle = rmw_create_service(
this->node_handle_, service_type_support_handle, service_name.c_str());

using namespace rclcpp::service;

auto serv = Service<ServiceT>::make_shared(
auto serv = service::Service<ServiceT>::make_shared(
service_handle,
service_name,
callback);
auto serv_base_ptr = std::dynamic_pointer_cast<ServiceBase>(serv);
auto serv_base_ptr = std::dynamic_pointer_cast<service::ServiceBase>(serv);
register_service(service_name, serv_base_ptr, group);
return serv;
}

template<typename ServiceT>
typename service::Service<ServiceT>::SharedPtr
Node::create_service(
const std::string & service_name,
typename rclcpp::service::Service<ServiceT>::CallbackWithHeaderType callback_with_header,
rclcpp::callback_group::CallbackGroup::SharedPtr group)
{
using rosidl_generator_cpp::get_service_type_support_handle;
auto service_type_support_handle =
get_service_type_support_handle<ServiceT>();

rmw_service_t * service_handle = rmw_create_service(
this->node_handle_, service_type_support_handle, service_name.c_str());

auto serv = service::Service<ServiceT>::make_shared(
service_handle,
service_name,
callback_with_header);
auto serv_base_ptr = std::dynamic_pointer_cast<service::ServiceBase>(serv);
register_service(service_name, serv_base_ptr, group);
return serv;
}

void
Node::register_service(
const std::string & service_name,
std::shared_ptr<rclcpp::service::ServiceBase> serv_base_ptr,
rclcpp::callback_group::CallbackGroup::SharedPtr group)
{
if (group) {
if (!group_in_node(group)) {
// TODO: use custom exception
Expand All @@ -215,8 +244,6 @@ Node::create_service(
default_callback_group_->add_service(serv_base_ptr);
}
number_of_services_++;

return serv;
}

#endif /* RCLCPP_RCLCPP_NODE_IMPL_HPP_ */
28 changes: 24 additions & 4 deletions rclcpp/include/rclcpp/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,30 @@ class Service : public ServiceBase
{
public:
typedef std::function<
void (const std::shared_ptr<typename ServiceT::Request> &,
std::shared_ptr<typename ServiceT::Response> &)> CallbackType;
void (
const std::shared_ptr<typename ServiceT::Request> &,
std::shared_ptr<typename ServiceT::Response> &)> CallbackType;

typedef std::function<
void (
const std::shared_ptr<rmw_request_id_t> &,
const std::shared_ptr<typename ServiceT::Request> &,
std::shared_ptr<typename ServiceT::Response> &)> CallbackWithHeaderType;
RCLCPP_MAKE_SHARED_DEFINITIONS(Service);

Service(
rmw_service_t * service_handle,
const std::string & service_name,
CallbackType callback)
: ServiceBase(service_handle, service_name), callback_(callback)
: ServiceBase(service_handle, service_name), callback_(callback), callback_with_header_(nullptr)
{}

Service(
rmw_service_t * service_handle,
const std::string & service_name,
CallbackWithHeaderType callback_with_header)
: ServiceBase(service_handle, service_name), callback_(nullptr),
callback_with_header_(callback_with_header)
{}

std::shared_ptr<void> create_request()
Expand All @@ -114,7 +129,11 @@ class Service : public ServiceBase
auto typed_request = std::static_pointer_cast<typename ServiceT::Request>(request);
auto typed_request_header = std::static_pointer_cast<rmw_request_id_t>(request_header);
auto response = std::shared_ptr<typename ServiceT::Response>(new typename ServiceT::Response);
callback_(typed_request, response);
if (callback_with_header_ != nullptr) {
callback_with_header_(typed_request_header, typed_request, response);
} else {
callback_(typed_request, response);
}
send_response(typed_request_header, response);
}

Expand All @@ -129,6 +148,7 @@ class Service : public ServiceBase
RCLCPP_DISABLE_COPY(Service);

CallbackType callback_;
CallbackWithHeaderType callback_with_header_;
};

} /* namespace service */
Expand Down

0 comments on commit 1bf595d

Please sign in to comment.