Apollo 11.0
自动驾驶开放平台
client_wrapper.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
21#pragma once
22
23#include <string>
24#include <memory>
25
27
32namespace apollo {
33namespace common {
34
39template <typename Request, typename Response>
41 public:
42 ClientWrapper(const std::shared_ptr<cyber::Node>& node,
43 const std::string& service_name);
51 std::shared_ptr<Response> SendRequest(
52 std::shared_ptr<Request> request,
53 const std::chrono::seconds& timeout_s = std::chrono::seconds(5));
54
62 std::shared_ptr<Response> SendRequest(
63 const Request& request,
64 const std::chrono::seconds& timeout_s = std::chrono::seconds(5));
65
69 std::shared_future<Response> AsyncSendRequest(
70 std::shared_ptr<Request> request);
71
75 std::shared_future<Response> AsyncSendRequest(const Request& request);
76
86 std::shared_future<Response> AsyncSendRequest(
87 std::shared_ptr<Request> request,
88 std::function<void(std::shared_future<std::shared_ptr<Response>>)>&& cb);
89
90 private:
91 std::shared_ptr<apollo::cyber::Client<Request, Response>> client_;
92 std::shared_ptr<apollo::cyber::Writer<Request>> request_writer_;
93};
94
95template <typename Request, typename Response>
97 const std::shared_ptr<cyber::Node>& node, const std::string& service_name)
98 : client_(node->CreateClient<Request, Response>(service_name)),
99 request_writer_(node->CreateWriter<Request>(service_name)) {}
100
101template <typename Request, typename Response>
103 std::shared_ptr<Request> request, const std::chrono::seconds& timeout_s) {
104 auto response = client_->SendRequest(request);
105 request_writer_->Write(request);
106 return response;
107}
108
109template <typename Request, typename Response>
111 const Request& request, const std::chrono::seconds& timeout_s) {
112 auto response = client_->SendRequest(request);
113 request_writer_->Write(request);
114 return response;
115}
116
117template <typename Request, typename Response>
119 std::shared_ptr<Request> request) {
120 auto response = client_->AsyncSendRequest(request);
121 request_writer_->Write(request);
122 return response;
123}
124
125template <typename Request, typename Response>
127 const Request& request) {
128 auto response = client_->AsyncSendRequest(request);
129 request_writer_->Write(request);
130 return response;
131}
132
133template <typename Request, typename Response>
135 std::shared_ptr<Request> request,
136 std::function<void(std::shared_future<std::shared_ptr<Response>>)>&& cb) {
137 auto response = client_->AsyncSendRequest(request, cb);
138 request_writer_->Write(request);
139 return response;
140}
141
142} // namespace common
143} // namespace apollo
Wrapper of cyber::Client which sends a topic with service name when SendRequst is invoked.
ClientWrapper(const std::shared_ptr< cyber::Node > &node, const std::string &service_name)
std::shared_future< Response > AsyncSendRequest(std::shared_ptr< Request > request)
Send Request shared ptr asynchronously
std::shared_ptr< Response > SendRequest(std::shared_ptr< Request > request, const std::chrono::seconds &timeout_s=std::chrono::seconds(5))
Request the Service with a shared ptr Request type
class register implement
Definition arena_queue.h:37