mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
service_server.impl.hpp
1#ifndef MRS_LIB_SERVICE_SERVER_IMPL_HPP_
2#define MRS_LIB_SERVICE_SERVER_IMPL_HPP_
3
4#include "mrs_lib/service_server.hpp"
5
6#include <cassert>
7#include <functional>
8#include <memory>
9#include <string>
10#include <utility>
11
12#include <rclcpp/create_service.hpp>
13#include <rclcpp/service.hpp>
14
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"
20
21namespace mrs_lib
22{
23
25 template <class ServiceType>
26 class ServiceServer<ServiceType>::Impl
27 {
28 using RosServiceServerPimpl = Pimpl<rclcpp::Service<ServiceType>, std::shared_ptr<rclcpp::Service<ServiceType>>>;
29
30 public:
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)),
37 resolved_service_name_(service_server_->get_service_name())
38 {
39 logger_.info("Created service server '{}' -> '{}'", get_unresolved_service_name(), get_service_name());
40 }
41
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;
51 }))),
52 resolved_service_name_(service_server_->get_service_name())
53 {
54 logger_.info("Created service server '{}' -> '{}'", get_unresolved_service_name(), get_service_name());
55 }
56
57 ~Impl() = default;
58 Impl(const Impl&) = delete;
59 Impl& operator=(const Impl&) = delete;
60 Impl(Impl&&) = delete;
61 Impl& operator=(Impl&&) = delete;
62
63 [[nodiscard]] std::string get_service_name() const
64 {
65 return resolved_service_name_;
66 }
67
68 [[nodiscard]] std::string get_unresolved_service_name() const
69 {
70 return unresolved_service_name_;
71 }
72
73 private:
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)>
76 create_coro_callback(ServiceCoroCallback callback)
77 {
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);
87 });
88 };
89 }
90
91 ServiceServerNodeInterfaces node_interfaces_;
92 mrs_lib::Logger logger_;
93
94 std::string unresolved_service_name_;
95
96 RosServiceServerPimpl service_server_;
97
98 // rclcpp does not currently expose get_service_name as const.
99 // Fix in: ros2/rclcpp#3253
100 // We cache the value to be usable in const methods.
101 std::string resolved_service_name_;
102 };
103
104 template <class ServiceType>
105 ServiceServer<ServiceType>::ServiceServer(const ServiceServerOptions& options, std::string_view service_name, ServiceCallback callback)
106 : impl_(std::in_place_type_t<Impl>{}, options, service_name, callback)
107 {
108 }
109
110 template <class ServiceType>
111 ServiceServer<ServiceType>::ServiceServer(const ServiceServerOptions& options, std::string_view service_name, ServiceCoroCallback callback)
112 : impl_(std::in_place_type_t<Impl>{}, options, service_name, callback)
113 {
114 }
115
116 template <class ServiceType>
118 {
119 return impl_->get_service_name();
120 }
121
122 template <class ServiceType>
124 {
125 return impl_->get_unresolved_service_name();
126 }
127
128} // namespace mrs_lib
129
130
131#endif // MRS_LIB_SERVICE_SERVER_IMPL_HPP_
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