Refactored to split request messages

This commit is contained in:
Esteve Fernandez 2015-01-08 09:53:24 -08:00
parent 180c0f9016
commit 4b290d66e8
4 changed files with 29 additions and 13 deletions

View file

@ -171,12 +171,14 @@ protected:
rclcpp::service::ServiceBase::SharedPtr &service) rclcpp::service::ServiceBase::SharedPtr &service)
{ {
std::shared_ptr<void> request = service->create_request(); std::shared_ptr<void> request = service->create_request();
std::shared_ptr<void> request_header = service->create_request_header();
bool taken = ros_middleware_interface::take_request( bool taken = ros_middleware_interface::take_request(
service->service_handle_, service->service_handle_,
request.get()); request.get(),
request_header.get());
if (taken) if (taken)
{ {
service->handle_request(request); service->handle_request(request, request_header);
} }
else else
{ {

View file

@ -29,6 +29,8 @@
#include <rclcpp/subscription.hpp> #include <rclcpp/subscription.hpp>
#include <rclcpp/timer.hpp> #include <rclcpp/timer.hpp>
#include <userland_msgs/RequestId.h>
namespace rclcpp namespace rclcpp
{ {
@ -101,7 +103,8 @@ public:
typename rclcpp::service::Service<ServiceT>::SharedPtr typename rclcpp::service::Service<ServiceT>::SharedPtr
create_service( create_service(
std::string service_name, std::string service_name,
std::function<void(const std::shared_ptr<typename ServiceT::RequestWithHeader> &, std::function<void(const std::shared_ptr<typename ServiceT::Request> &,
const std::shared_ptr<userland_msgs::RequestId> &,
std::shared_ptr<typename ServiceT::Response>&)> callback, std::shared_ptr<typename ServiceT::Response>&)> callback,
rclcpp::callback_group::CallbackGroup::SharedPtr group=nullptr); rclcpp::callback_group::CallbackGroup::SharedPtr group=nullptr);

View file

@ -187,7 +187,8 @@ template <typename ServiceT>
typename service::Service<ServiceT>::SharedPtr typename service::Service<ServiceT>::SharedPtr
Node::create_service( Node::create_service(
std::string service_name, std::string service_name,
std::function<void(const std::shared_ptr<typename ServiceT::RequestWithHeader> &, std::function<void(const std::shared_ptr<typename ServiceT::Request> &,
const std::shared_ptr<userland_msgs::RequestId> &,
std::shared_ptr<typename ServiceT::Response>&)> callback, std::shared_ptr<typename ServiceT::Response>&)> callback,
rclcpp::callback_group::CallbackGroup::SharedPtr group) rclcpp::callback_group::CallbackGroup::SharedPtr group)
{ {

View file

@ -23,6 +23,8 @@
#include <rclcpp/macros.hpp> #include <rclcpp/macros.hpp>
#include <userland_msgs/RequestId.h>
namespace rclcpp namespace rclcpp
{ {
@ -55,7 +57,8 @@ public:
} }
virtual std::shared_ptr<void> create_request() = 0; virtual std::shared_ptr<void> create_request() = 0;
virtual void handle_request(std::shared_ptr<void> &request) = 0; virtual std::shared_ptr<void> create_request_header() = 0;
virtual void handle_request(std::shared_ptr<void> &request, std::shared_ptr<void> &req_id) = 0;
private: private:
RCLCPP_DISABLE_COPY(ServiceBase); RCLCPP_DISABLE_COPY(ServiceBase);
@ -70,7 +73,8 @@ class Service : public ServiceBase
{ {
public: public:
typedef std::function< typedef std::function<
void(const std::shared_ptr<typename ServiceT::RequestWithHeader> &, void(const std::shared_ptr<typename ServiceT::Request> &,
const std::shared_ptr<userland_msgs::RequestId> &,
std::shared_ptr<typename ServiceT::Response>&)> CallbackType; std::shared_ptr<typename ServiceT::Response>&)> CallbackType;
RCLCPP_MAKE_SHARED_DEFINITIONS(Service); RCLCPP_MAKE_SHARED_DEFINITIONS(Service);
@ -83,22 +87,28 @@ public:
std::shared_ptr<void> create_request() std::shared_ptr<void> create_request()
{ {
return std::shared_ptr<void>(new typename ServiceT::RequestWithHeader()); return std::shared_ptr<void>(new typename ServiceT::Request());
} }
void handle_request(std::shared_ptr<void> &request) std::shared_ptr<void> create_request_header()
{ {
auto typed_request = std::static_pointer_cast<typename ServiceT::RequestWithHeader>(request); return std::shared_ptr<void>(new userland_msgs::RequestId());
}
void handle_request(std::shared_ptr<void> &request, std::shared_ptr<void> &req_id)
{
auto typed_request = std::static_pointer_cast<typename ServiceT::Request>(request);
auto typed_req_id = std::static_pointer_cast<userland_msgs::RequestId>(req_id);
auto response = std::shared_ptr<typename ServiceT::Response>(new typename ServiceT::Response); auto response = std::shared_ptr<typename ServiceT::Response>(new typename ServiceT::Response);
callback_(typed_request, response); callback_(typed_request, typed_req_id, response);
send_response(typed_request, response); send_response(typed_req_id, response);
} }
void send_response( void send_response(
std::shared_ptr<typename ServiceT::RequestWithHeader> &request, std::shared_ptr<userland_msgs::RequestId> &req_id,
std::shared_ptr<typename ServiceT::Response> &response) std::shared_ptr<typename ServiceT::Response> &response)
{ {
::ros_middleware_interface::send_response(get_service_handle(), request.get(), response.get()); ::ros_middleware_interface::send_response(get_service_handle(), req_id.get(), response.get());
} }
private: private: