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

Service handles Request from the Client, and send a Response to it. 更多...

#include <service.h>

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

Public 类型

using ServiceCallback = std::function< void(const std::shared_ptr< Request > &, std::shared_ptr< Response > &)>
 

Public 成员函数

 Service (const std::string &node_name, const std::string &service_name, const ServiceCallback &service_callback)
 Construct a new Service object
 
 Service (const std::string &node_name, const std::string &service_name, ServiceCallback &&service_callback)
 Construct a new Service object
 
 Service ()=delete
 Forbid default constructing
 
 ~Service ()
 
bool Init ()
 Init the Service
 
void destroy ()
 Destroy the Service
 
- Public 成员函数 继承自 apollo::cyber::ServiceBase
 ServiceBase (const std::string &service_name)
 Construct a new Service Base object
 
virtual ~ServiceBase ()
 
const std::string & service_name () const
 Get the service name
 

额外继承的成员函数

- Protected 属性 继承自 apollo::cyber::ServiceBase
std::string service_name_
 

详细描述

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

Service handles Request from the Client, and send a Response to it.

模板参数
Requestthe request type
Responsethe response type

在文件 service.h42 行定义.

成员类型定义说明

◆ ServiceCallback

template<typename Request , typename Response >
using apollo::cyber::Service< Request, Response >::ServiceCallback = std::function<void(const std::shared_ptr<Request>&, std::shared_ptr<Response>&)>

在文件 service.h44 行定义.

构造及析构函数说明

◆ Service() [1/3]

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

Construct a new Service object

参数
node_nameused to fill RoleAttribute when join the topology
service_namethe service name we provide
service_callbackreference of ServiceCallback object

在文件 service.h53 行定义.

56 node_name_(node_name),
57 service_callback_(service_callback),
58 request_channel_(service_name + SRV_CHANNEL_REQ_SUFFIX),
59 response_channel_(service_name + SRV_CHANNEL_RES_SUFFIX) {}
const std::string & service_name() const
Get the service name
ServiceBase(const std::string &service_name)
Construct a new Service Base object

◆ Service() [2/3]

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

Construct a new Service object

参数
node_nameused to fill RoleAttribute when join the topology
service_namethe service name we provide
service_callbackrvalue reference of ServiceCallback object

在文件 service.h68 行定义.

71 node_name_(node_name),
72 service_callback_(service_callback),
73 request_channel_(service_name + SRV_CHANNEL_REQ_SUFFIX),
74 response_channel_(service_name + SRV_CHANNEL_RES_SUFFIX) {}

◆ Service() [3/3]

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

Forbid default constructing

◆ ~Service()

template<typename Request , typename Response >
apollo::cyber::Service< Request, Response >::~Service ( )
inline

在文件 service.h81 行定义.

81{ destroy(); }
void destroy()
Destroy the Service
Definition service.h:124

成员函数说明

◆ destroy()

template<typename Request , typename Response >
void apollo::cyber::Service< Request, Response >::destroy ( )
virtual

Destroy the Service

实现了 apollo::cyber::ServiceBase.

在文件 service.h124 行定义.

124 {
125 inited_ = false;
126 {
127 std::lock_guard<std::mutex> lg(queue_mutex_);
128 this->tasks_.clear();
129 }
130 condition_.notify_all();
131 if (thread_.joinable()) {
132 thread_.join();
133 }
134}

◆ Init()

template<typename Request , typename Response >
bool apollo::cyber::Service< Request, Response >::Init ( )

Init the Service

在文件 service.h161 行定义.

161 {
162 if (IsInit()) {
163 return true;
164 }
165 proto::RoleAttributes role;
166 role.set_node_name(node_name_);
167 role.set_channel_name(response_channel_);
168 auto channel_id = common::GlobalData::RegisterChannel(response_channel_);
169 role.set_channel_id(channel_id);
170 role.mutable_qos_profile()->CopyFrom(
172 auto transport = transport::Transport::Instance();
173 response_transmitter_ =
174 transport->CreateTransmitter<Response>(role, proto::OptionalMode::RTPS);
175 if (response_transmitter_ == nullptr) {
176 AERROR << " Create response pub failed.";
177 return false;
178 }
179
180 request_callback_ =
181 std::bind(&Service<Request, Response>::HandleRequest, this,
182 std::placeholders::_1, std::placeholders::_2);
183
184 role.set_channel_name(request_channel_);
185 channel_id = common::GlobalData::RegisterChannel(request_channel_);
186 role.set_channel_id(channel_id);
187 request_receiver_ = transport->CreateReceiver<Request>(
188 role,
189 [=](const std::shared_ptr<Request>& request,
190 const transport::MessageInfo& message_info,
191 const proto::RoleAttributes& reader_attr) {
192 (void)reader_attr;
193 auto task = [this, request, message_info]() {
194 this->HandleRequest(request, message_info);
195 };
196 Enqueue(std::move(task));
197 },
199 inited_ = true;
200 thread_ = std::thread(&Service<Request, Response>::Process, this);
201 if (request_receiver_ == nullptr) {
202 AERROR << " Create request sub failed." << request_channel_;
203 response_transmitter_.reset();
204 return false;
205 }
206 return true;
207}
static uint64_t RegisterChannel(const std::string &channel)
static const QosProfile QOS_PROFILE_SERVICES_DEFAULT
#define AERROR
Definition log.h:44

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