1#ifndef MRS_LIB_SERVICE_SERVER_IMPL_HPP_
2#define MRS_LIB_SERVICE_SERVER_IMPL_HPP_
4#include "mrs_lib/service_server.hpp"
12#include <rclcpp/create_service.hpp>
13#include <rclcpp/service.hpp>
15#include "mrs_lib/coro/runners.hpp"
16#include "mrs_lib/coro/task.hpp"
17#include "mrs_lib/internal/coroutine_callback_helpers.hpp"
18#include "mrs_lib/logger.hpp"
25 template <
class ServiceType>
28 using RosServiceServerPimpl = Pimpl<rclcpp::Service<ServiceType>, std::shared_ptr<rclcpp::Service<ServiceType>>>;
31 Impl(
const ServiceServerOptions& options, std::string_view service_name,
ServiceCallback callback)
32 : node_interfaces_(options.node_interfaces),
33 logger_(node_interfaces_),
34 unresolved_service_name_(service_name),
35 service_server_(rclcpp::create_service<ServiceType>(node_interfaces_.get_node_base_interface(), node_interfaces_.get_node_services_interface(),
36 std::string(service_name), callback, options.qos, options.callback_group)),
42 Impl(
const ServiceServerOptions& options, std::string_view service_name,
ServiceCoroCallback callback)
43 : node_interfaces_(options.node_interfaces),
44 logger_(node_interfaces_),
45 unresolved_service_name_(service_name),
46 service_server_(rclcpp::create_service<ServiceType>(node_interfaces_.get_node_base_interface(), node_interfaces_.get_node_services_interface(),
47 std::string(service_name), create_coro_callback(callback), options.qos, std::invoke([&]() {
48 auto callback_group = options.callback_group;
49 internal::require_callback_group_coro_compatible(callback_group);
50 return callback_group;
52 resolved_service_name_(service_server_->get_service_name())
58 Impl(
const Impl&) =
delete;
59 Impl& operator=(
const Impl&) =
delete;
60 Impl(Impl&&) =
delete;
61 Impl& operator=(Impl&&) =
delete;
65 return resolved_service_name_;
70 return unresolved_service_name_;
74 static std::function<void(std::shared_ptr<rclcpp::Service<ServiceType>> service_server, std::shared_ptr<rmw_request_id_t> header,
75 std::shared_ptr<const Request> request)>
78 return [callback](std::shared_ptr<rclcpp::Service<ServiceType>> service_server, std::shared_ptr<rmw_request_id_t> header,
79 std::shared_ptr<const Request> request) {
80 assert(service_server !=
nullptr);
81 assert(header !=
nullptr);
82 assert(request !=
nullptr);
83 coro::internal::start_task([callback, service_server, header, request]() -> mrs_lib::Task<> {
84 auto response = std::make_shared<Response>();
85 co_await callback(request, response);
86 service_server->send_response(*header, *response);
92 mrs_lib::Logger logger_;
94 std::string unresolved_service_name_;
96 RosServiceServerPimpl service_server_;
101 std::string resolved_service_name_;
104 template <
class ServiceType>
106 : impl_(std::in_place_type_t<Impl>{}, options, service_name, callback)
110 template <
class ServiceType>
112 : impl_(std::in_place_type_t<Impl>{}, options, service_name, callback)
116 template <
class ServiceType>
119 return impl_->get_service_name();
122 template <
class ServiceType>
125 return impl_->get_unresolved_service_name();
rclcpp::Service wrapper with coroutines support.
Definition service_server.hpp:50
std::string get_unresolved_service_name() const
Returns the unresolved name of the service handled by this service server.
Definition service_server.impl.hpp:123
ServiceServer(const ServiceServerOptions &options, std::string_view service_name, ServiceCallback callback)
Construct the service server with the specified callback.
Definition service_server.impl.hpp:105
CoroCallback< void(std::shared_ptr< const Request > req, std::shared_ptr< Response >)> ServiceCoroCallback
Type of the accepted coroutine callback.
Definition service_server.hpp:61
std::string get_service_name() const
Returns the resolved (full) name of the service handled by this service server.
Definition service_server.impl.hpp:117
std::function< void(std::shared_ptr< const Request > req, std::shared_ptr< Response >)> ServiceCallback
Type of the accepted callback.
Definition service_server.hpp:58
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
rclcpp::node_interfaces::NodeInterfaces< rclcpp::node_interfaces::NodeServicesInterface, rclcpp::node_interfaces::NodeBaseInterface, rclcpp::node_interfaces::NodeLoggingInterface, rclcpp::node_interfaces::NodeClockInterface > ServiceServerNodeInterfaces
ROS node interfaces required by the ServiceServer.
Definition service_server.hpp:25
Options for configuring the ServiceServer.
Definition service_server.hpp:33