|
| 1 | +// Copyright 2023 Sony Group Corporation. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef RCLCPP__GENERIC_CLIENT_HPP_ |
| 16 | +#define RCLCPP__GENERIC_CLIENT_HPP_ |
| 17 | + |
| 18 | +#include <map> |
| 19 | +#include <memory> |
| 20 | +#include <future> |
| 21 | +#include <string> |
| 22 | +#include <vector> |
| 23 | +#include <utility> |
| 24 | + |
| 25 | +#include "rcl/client.h" |
| 26 | + |
| 27 | +#include "rclcpp/client.hpp" |
| 28 | +#include "rclcpp/visibility_control.hpp" |
| 29 | +#include "rcpputils/shared_library.hpp" |
| 30 | + |
| 31 | +#include "rosidl_typesupport_introspection_cpp/message_introspection.hpp" |
| 32 | + |
| 33 | +namespace rclcpp |
| 34 | +{ |
| 35 | +class GenericClient : public ClientBase |
| 36 | +{ |
| 37 | +public: |
| 38 | + using Request = void *; // Serialized data pointer of request message |
| 39 | + using Response = void *; // Serialized data pointer of response message |
| 40 | + |
| 41 | + using SharedResponse = std::shared_ptr<void>; |
| 42 | + |
| 43 | + using Promise = std::promise<SharedResponse>; |
| 44 | + using SharedPromise = std::shared_ptr<Promise>; |
| 45 | + |
| 46 | + using Future = std::future<SharedResponse>; |
| 47 | + using SharedFuture = std::shared_future<SharedResponse>; |
| 48 | + |
| 49 | + RCLCPP_SMART_PTR_DEFINITIONS(GenericClient) |
| 50 | + |
| 51 | + /// A convenient GenericClient::Future and request id pair. |
| 52 | + /** |
| 53 | + * Public members: |
| 54 | + * - future: a std::future<void *>. |
| 55 | + * - request_id: the request id associated with the future. |
| 56 | + * |
| 57 | + * All the other methods are equivalent to the ones std::future provides. |
| 58 | + */ |
| 59 | + struct FutureAndRequestId |
| 60 | + : detail::FutureAndRequestId<Future> |
| 61 | + { |
| 62 | + using detail::FutureAndRequestId<Future>::FutureAndRequestId; |
| 63 | + |
| 64 | + /// See std::future::share(). |
| 65 | + SharedFuture share() noexcept {return this->future.share();} |
| 66 | + |
| 67 | + /// Move constructor. |
| 68 | + FutureAndRequestId(FutureAndRequestId && other) noexcept = default; |
| 69 | + /// Deleted copy constructor, each instance is a unique owner of the future. |
| 70 | + FutureAndRequestId(const FutureAndRequestId & other) = delete; |
| 71 | + /// Move assignment. |
| 72 | + FutureAndRequestId & operator=(FutureAndRequestId && other) noexcept = default; |
| 73 | + /// Deleted copy assignment, each instance is a unique owner of the future. |
| 74 | + FutureAndRequestId & operator=(const FutureAndRequestId & other) = delete; |
| 75 | + /// Destructor. |
| 76 | + ~FutureAndRequestId() = default; |
| 77 | + }; |
| 78 | + |
| 79 | + GenericClient( |
| 80 | + rclcpp::node_interfaces::NodeBaseInterface * node_base, |
| 81 | + rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph, |
| 82 | + const std::string & service_name, |
| 83 | + const std::string & service_type, |
| 84 | + rcl_client_options_t & client_options); |
| 85 | + |
| 86 | + RCLCPP_PUBLIC |
| 87 | + SharedResponse |
| 88 | + create_response() override; |
| 89 | + |
| 90 | + RCLCPP_PUBLIC |
| 91 | + std::shared_ptr<rmw_request_id_t> |
| 92 | + create_request_header() override; |
| 93 | + |
| 94 | + RCLCPP_PUBLIC |
| 95 | + void |
| 96 | + handle_response( |
| 97 | + std::shared_ptr<rmw_request_id_t> request_header, |
| 98 | + std::shared_ptr<void> response) override; |
| 99 | + |
| 100 | + /// Send a request to the service server. |
| 101 | + /** |
| 102 | + * This method returns a `FutureAndRequestId` instance |
| 103 | + * that can be passed to Executor::spin_until_future_complete() to |
| 104 | + * wait until it has been completed. |
| 105 | + * |
| 106 | + * If the future never completes, |
| 107 | + * e.g. the call to Executor::spin_until_future_complete() times out, |
| 108 | + * GenericClient::remove_pending_request() must be called to clean the client internal state. |
| 109 | + * Not doing so will make the `Client` instance to use more memory each time a response is not |
| 110 | + * received from the service server. |
| 111 | + * |
| 112 | + * ```cpp |
| 113 | + * auto future = client->async_send_request(my_request); |
| 114 | + * if ( |
| 115 | + * rclcpp::FutureReturnCode::TIMEOUT == |
| 116 | + * executor->spin_until_future_complete(future, timeout)) |
| 117 | + * { |
| 118 | + * client->remove_pending_request(future); |
| 119 | + * // handle timeout |
| 120 | + * } else { |
| 121 | + * handle_response(future.get()); |
| 122 | + * } |
| 123 | + * ``` |
| 124 | + * |
| 125 | + * \param[in] request request to be send. |
| 126 | + * \return a FutureAndRequestId instance. |
| 127 | + */ |
| 128 | + RCLCPP_PUBLIC |
| 129 | + FutureAndRequestId |
| 130 | + async_send_request(const Request request); |
| 131 | + |
| 132 | + /// Clean all pending requests older than a time_point. |
| 133 | + /** |
| 134 | + * \param[in] time_point Requests that were sent before this point are going to be removed. |
| 135 | + * \param[inout] pruned_requests Removed requests id will be pushed to the vector |
| 136 | + * if a pointer is provided. |
| 137 | + * \return number of pending requests that were removed. |
| 138 | + */ |
| 139 | + template<typename AllocatorT = std::allocator<int64_t>> |
| 140 | + size_t |
| 141 | + prune_requests_older_than( |
| 142 | + std::chrono::time_point<std::chrono::system_clock> time_point, |
| 143 | + std::vector<int64_t, AllocatorT> * pruned_requests = nullptr) |
| 144 | + { |
| 145 | + return detail::prune_requests_older_than_impl( |
| 146 | + pending_requests_, |
| 147 | + pending_requests_mutex_, |
| 148 | + time_point, |
| 149 | + pruned_requests); |
| 150 | + } |
| 151 | + |
| 152 | + RCLCPP_PUBLIC |
| 153 | + size_t |
| 154 | + prune_pending_requests(); |
| 155 | + |
| 156 | + RCLCPP_PUBLIC |
| 157 | + bool |
| 158 | + remove_pending_request( |
| 159 | + int64_t request_id); |
| 160 | + |
| 161 | + /// Take the next response for this client. |
| 162 | + /** |
| 163 | + * \sa ClientBase::take_type_erased_response(). |
| 164 | + * |
| 165 | + * \param[out] response_out The reference to a Service Response into |
| 166 | + * which the middleware will copy the response being taken. |
| 167 | + * \param[out] request_header_out The request header to be filled by the |
| 168 | + * middleware when taking, and which can be used to associate the response |
| 169 | + * to a specific request. |
| 170 | + * \returns true if the response was taken, otherwise false. |
| 171 | + * \throws rclcpp::exceptions::RCLError based exceptions if the underlying |
| 172 | + * rcl function fail. |
| 173 | + */ |
| 174 | + RCLCPP_PUBLIC |
| 175 | + bool |
| 176 | + take_response(Response response_out, rmw_request_id_t & request_header_out) |
| 177 | + { |
| 178 | + return this->take_type_erased_response(response_out, request_header_out); |
| 179 | + } |
| 180 | + |
| 181 | +protected: |
| 182 | + using CallbackInfoVariant = std::variant< |
| 183 | + std::promise<SharedResponse>>; // Use variant for extension |
| 184 | + |
| 185 | + int64_t |
| 186 | + async_send_request_impl( |
| 187 | + const Request request, |
| 188 | + CallbackInfoVariant value); |
| 189 | + |
| 190 | + std::optional<CallbackInfoVariant> |
| 191 | + get_and_erase_pending_request( |
| 192 | + int64_t request_number); |
| 193 | + |
| 194 | + RCLCPP_DISABLE_COPY(GenericClient) |
| 195 | + |
| 196 | + std::map<int64_t, std::pair< |
| 197 | + std::chrono::time_point<std::chrono::system_clock>, |
| 198 | + CallbackInfoVariant>> pending_requests_; |
| 199 | + std::mutex pending_requests_mutex_; |
| 200 | + |
| 201 | +private: |
| 202 | + std::shared_ptr<rcpputils::SharedLibrary> ts_lib_; |
| 203 | + const rosidl_typesupport_introspection_cpp::MessageMembers * response_members_; |
| 204 | +}; |
| 205 | +} // namespace rclcpp |
| 206 | + |
| 207 | +#endif // RCLCPP__GENERIC_CLIENT_HPP_ |
0 commit comments