Apollo 10.0
自动驾驶开放平台
apollo::cyber::Client< Request, Response > 模板类 参考

Client get Response from a responding Service by sending a Request 更多...

#include <client.h>

类 apollo::cyber::Client< Request, Response > 继承关系图:
apollo::cyber::Client< Request, Response > 的协作图:

Public 类型

using SharedRequest = typename std::shared_ptr< Request >
 
using SharedResponse = typename std::shared_ptr< Response >
 
using Promise = std::promise< SharedResponse >
 
using SharedPromise = std::shared_ptr< Promise >
 
using SharedFuture = std::shared_future< SharedResponse >
 
using CallbackType = std::function< void(SharedFuture)>
 

Public 成员函数

 Client (const std::string &node_name, const std::string &service_name)
 Construct a new Client object
 
 Client ()=delete
 forbid Constructing a new Client object with empty params
 
virtual ~Client ()
 
bool Init ()
 Init the Client
 
SharedResponse SendRequest (SharedRequest request, const std::chrono::seconds &timeout_s=std::chrono::seconds(5))
 Request the Service with a shared ptr Request type
 
SharedResponse SendRequest (const Request &request, const std::chrono::seconds &timeout_s=std::chrono::seconds(5))
 Request the Service with a Request object
 
SharedFuture AsyncSendRequest (SharedRequest request)
 Send Request shared ptr asynchronously
 
SharedFuture AsyncSendRequest (const Request &request)
 Send Request object asynchronously
 
SharedFuture AsyncSendRequest (SharedRequest request, CallbackType &&cb)
 Send Request shared ptr asynchronously and invoke cb after we get response
 
bool ServiceIsReady () const
 Is the Service is ready?
 
void Destroy ()
 destroy this Client
 
template<typename RatioT = std::milli>
bool WaitForService (std::chrono::duration< int64_t, RatioT > timeout=std::chrono::duration< int64_t, RatioT >(-1))
 wait for the connection with the Service established
 
- Public 成员函数 继承自 apollo::cyber::ClientBase
 ClientBase (const std::string &service_name)
 Construct a new Client Base object
 
virtual ~ClientBase ()
 
const std::string & ServiceName () const
 Get the service name
 

额外继承的成员函数

- Protected 成员函数 继承自 apollo::cyber::ClientBase
bool WaitForServiceNanoseconds (std::chrono::nanoseconds time_out)
 
- Protected 属性 继承自 apollo::cyber::ClientBase
std::string service_name_
 

详细描述

template<typename Request, typename Response>
class apollo::cyber::Client< Request, Response >

Client get Response from a responding Service by sending a Request

模板参数
Requestthe Service request type
Responsethe Service response type
警告
One Client can only request one Service

在文件 client.h47 行定义.

成员类型定义说明

◆ CallbackType

template<typename Request , typename Response >
using apollo::cyber::Client< Request, Response >::CallbackType = std::function<void(SharedFuture)>

在文件 client.h54 行定义.

◆ Promise

template<typename Request , typename Response >
using apollo::cyber::Client< Request, Response >::Promise = std::promise<SharedResponse>

在文件 client.h51 行定义.

◆ SharedFuture

template<typename Request , typename Response >
using apollo::cyber::Client< Request, Response >::SharedFuture = std::shared_future<SharedResponse>

在文件 client.h53 行定义.

◆ SharedPromise

template<typename Request , typename Response >
using apollo::cyber::Client< Request, Response >::SharedPromise = std::shared_ptr<Promise>

在文件 client.h52 行定义.

◆ SharedRequest

template<typename Request , typename Response >
using apollo::cyber::Client< Request, Response >::SharedRequest = typename std::shared_ptr<Request>

在文件 client.h49 行定义.

◆ SharedResponse

template<typename Request , typename Response >
using apollo::cyber::Client< Request, Response >::SharedResponse = typename std::shared_ptr<Response>

在文件 client.h50 行定义.

构造及析构函数说明

◆ Client() [1/2]

template<typename Request , typename Response >
apollo::cyber::Client< Request, Response >::Client ( const std::string &  node_name,
const std::string &  service_name 
)
inline

Construct a new Client object

参数
node_nameused to fill RoleAttribute
service_nameservice name the Client can request

在文件 client.h62 行定义.

63 : ClientBase(service_name),
64 node_name_(node_name),
65 request_channel_(service_name + SRV_CHANNEL_REQ_SUFFIX),
66 response_channel_(service_name + SRV_CHANNEL_RES_SUFFIX),
67 sequence_number_(0) {}
ClientBase(const std::string &service_name)
Construct a new Client Base object
Definition client_base.h:40

◆ Client() [2/2]

template<typename Request , typename Response >
apollo::cyber::Client< Request, Response >::Client ( )
delete

forbid Constructing a new Client object with empty params

◆ ~Client()

template<typename Request , typename Response >
virtual apollo::cyber::Client< Request, Response >::~Client ( )
inlinevirtual

在文件 client.h74 行定义.

74{}

成员函数说明

◆ AsyncSendRequest() [1/3]

template<typename Request , typename Response >
Client< Request, Response >::SharedFuture Client::AsyncSendRequest ( const Request &  request)

Send Request object asynchronously

在文件 client.h255 行定义.

255 {
256 auto request_ptr = std::make_shared<const Request>(request);
257 return AsyncSendRequest(request_ptr);
258}
SharedFuture AsyncSendRequest(SharedRequest request)
Send Request shared ptr asynchronously
Definition client.h:262

◆ AsyncSendRequest() [2/3]

template<typename Request , typename Response >
Client< Request, Response >::SharedFuture Client::AsyncSendRequest ( SharedRequest  request)

Send Request shared ptr asynchronously

在文件 client.h262 行定义.

262 {
263 return AsyncSendRequest(request, [](SharedFuture) {});
264}
std::shared_future< SharedResponse > SharedFuture
Definition client.h:53

◆ AsyncSendRequest() [3/3]

template<typename Request , typename Response >
Client< Request, Response >::SharedFuture Client::AsyncSendRequest ( SharedRequest  request,
CallbackType &&  cb 
)

Send Request shared ptr asynchronously and invoke cb after we get response

参数
requestRequest shared ptr
cbcallback function after we get response
返回
SharedFuture a std::future shared ptr

在文件 client.h268 行定义.

269 {
270 if (IsInit()) {
271 std::lock_guard<std::mutex> lock(pending_requests_mutex_);
272 sequence_number_++;
273 transport::MessageInfo info(writer_id_, sequence_number_, writer_id_);
274 request_transmitter_->Transmit(request, info);
275 SharedPromise call_promise = std::make_shared<Promise>();
276 SharedFuture f(call_promise->get_future());
277 pending_requests_[info.seq_num()] =
278 std::make_tuple(call_promise, std::forward<CallbackType>(cb), f);
279 return f;
280 } else {
281 return std::shared_future<std::shared_ptr<Response>>();
282 }
283}
double f
std::shared_ptr< Promise > SharedPromise
Definition client.h:52

◆ Destroy()

template<typename Request , typename Response >
void Client::Destroy ( )
virtual

destroy this Client

实现了 apollo::cyber::ClientBase.

在文件 client.h178 行定义.

178{}

◆ Init()

template<typename Request , typename Response >
bool Client::Init ( )

Init the Client

返回
true if init successfully
false if init failed

在文件 client.h181 行定义.

181 {
182 proto::RoleAttributes role;
183 role.set_node_name(node_name_);
184 role.set_channel_name(request_channel_);
185 auto channel_id = common::GlobalData::RegisterChannel(request_channel_);
186 role.set_channel_id(channel_id);
187 role.mutable_qos_profile()->CopyFrom(
189 auto transport = transport::Transport::Instance();
190 request_transmitter_ =
191 transport->CreateTransmitter<Request>(role, proto::OptionalMode::RTPS);
192 if (request_transmitter_ == nullptr) {
193 AERROR << "Create request pub failed.";
194 return false;
195 }
196 writer_id_ = request_transmitter_->id();
197
198 response_callback_ =
200 std::placeholders::_1, std::placeholders::_2);
201
202 role.set_channel_name(response_channel_);
203 channel_id = common::GlobalData::RegisterChannel(response_channel_);
204 role.set_channel_id(channel_id);
205 response_receiver_ = transport->CreateReceiver<Response>(
206 role,
207 [=](const std::shared_ptr<Response>& response,
208 const transport::MessageInfo& message_info,
209 const proto::RoleAttributes& reader_attr) {
210 (void)message_info;
211 (void)reader_attr;
212 response_callback_(response, message_info);
213 },
215 if (response_receiver_ == nullptr) {
216 AERROR << "Create response sub failed.";
217 request_transmitter_.reset();
218 return false;
219 }
220 return true;
221}
static uint64_t RegisterChannel(const std::string &channel)
static const QosProfile QOS_PROFILE_SERVICES_DEFAULT
#define AERROR
Definition log.h:44

◆ SendRequest() [1/2]

template<typename Request , typename Response >
Client< Request, Response >::SharedResponse Client::SendRequest ( const Request &  request,
const std::chrono::seconds &  timeout_s = std::chrono::seconds(5) 
)

Request the Service with a Request object

参数
requestRequest object
timeout_srequest timeout, if timeout, response will be empty
返回
SharedResponse result of this request

在文件 client.h244 行定义.

245 {
246 if (!IsInit()) {
247 return nullptr;
248 }
249 auto request_ptr = std::make_shared<const Request>(request);
250 return SendRequest(request_ptr, timeout_s);
251}
SharedResponse SendRequest(SharedRequest request, const std::chrono::seconds &timeout_s=std::chrono::seconds(5))
Request the Service with a shared ptr Request type
Definition client.h:225

◆ SendRequest() [2/2]

template<typename Request , typename Response >
Client< Request, Response >::SharedResponse Client::SendRequest ( SharedRequest  request,
const std::chrono::seconds &  timeout_s = std::chrono::seconds(5) 
)

Request the Service with a shared ptr Request type

参数
requestshared ptr of Request type
timeout_srequest timeout, if timeout, response will be empty
返回
SharedResponse result of this request

在文件 client.h225 行定义.

226 {
227 if (!IsInit()) {
228 return nullptr;
229 }
230 auto future = AsyncSendRequest(request);
231 if (!future.valid()) {
232 return nullptr;
233 }
234 auto status = future.wait_for(timeout_s);
235 if (status == std::future_status::ready) {
236 return future.get();
237 } else {
238 return nullptr;
239 }
240}

◆ ServiceIsReady()

template<typename Request , typename Response >
bool Client::ServiceIsReady ( ) const
virtual

Is the Service is ready?

实现了 apollo::cyber::ClientBase.

在文件 client.h286 行定义.

286 {
287 return true;
288}

◆ WaitForService()

template<typename Request , typename Response >
template<typename RatioT = std::milli>
bool apollo::cyber::Client< Request, Response >::WaitForService ( std::chrono::duration< int64_t, RatioT >  timeout = std::chrono::duration<int64_t, RatioT>(-1))
inline

wait for the connection with the Service established

模板参数
RatioTtimeout unit, default is std::milli
参数
timeoutwait time in unit of RatioT
返回
true if the connection established
false if timeout

在文件 client.h145 行定义.

146 {
148 std::chrono::duration_cast<std::chrono::nanoseconds>(timeout));
149 }
bool WaitForServiceNanoseconds(std::chrono::nanoseconds time_out)
Definition client_base.h:62

该类的文档由以下文件生成: