Apollo 10.0
自动驾驶开放平台
apollo::cyber::service_discovery::Manager类 参考abstract

Base class for management of Topology elements. 更多...

#include <manager.h>

类 apollo::cyber::service_discovery::Manager 继承关系图:
apollo::cyber::service_discovery::Manager 的协作图:

Public 类型

using ChangeSignal = base::Signal< const ChangeMsg & >
 
using ChangeFunc = std::function< void(const ChangeMsg &)>
 
using ChangeConnection = base::Connection< const ChangeMsg & >
 
using ParticipantPtr = std::shared_ptr< transport::Participant >
 
using PublisherPtr = std::shared_ptr< transport::Publisher >
 
using SubscriberPtr = std::shared_ptr< transport::Subscriber >
 

Public 成员函数

 Manager ()
 Construct a new Manager object
 
virtual ~Manager ()
 Destroy the Manager object
 
bool StartDiscovery (const ParticipantPtr &participant)
 Startup topology discovery
 
void StopDiscovery ()
 Stop topology discovery
 
virtual void Shutdown ()
 Shutdown module
 
bool Join (const RoleAttributes &attr, RoleType role, bool need_publish=true)
 Join the topology
 
bool Leave (const RoleAttributes &attr, RoleType role)
 Leave the topology
 
ChangeConnection AddChangeListener (const ChangeFunc &func)
 Add topology change listener, when topology changed, func will be called.
 
void RemoveChangeListener (const ChangeConnection &conn)
 Remove our listener for topology change.
 
virtual void OnTopoModuleLeave (const std::string &host_name, int process_id)=0
 Called when a process' topology manager instance leave
 

Protected 成员函数

bool CreatePublisher (const ParticipantPtr &participant)
 
bool CreateSubscriber (const ParticipantPtr &participant)
 
virtual bool Check (const RoleAttributes &attr)=0
 
virtual void Dispose (const ChangeMsg &msg)=0
 
virtual bool NeedPublish (const ChangeMsg &msg) const
 
void Convert (const RoleAttributes &attr, RoleType role, OperateType opt, ChangeMsg *msg)
 
void Notify (const ChangeMsg &msg)
 
bool Publish (const ChangeMsg &msg)
 
void OnRemoteChange (const std::shared_ptr< std::string > &msg_str)
 
bool IsFromSameProcess (const ChangeMsg &msg)
 

Protected 属性

std::atomic< bool > is_shutdown_
 
std::atomic< bool > is_discovery_started_
 
int allowed_role_
 
ChangeType change_type_
 
std::string host_name_
 
int process_id_
 
std::string channel_name_
 
PublisherPtr publisher_
 
SubscriberPtr subscriber_
 
ParticipantPtr participant_
 
std::mutex lock_
 
ChangeSignal signal_
 

详细描述

Base class for management of Topology elements.

Manager can Join/Leave the Topology, and Listen the topology change

在文件 manager.h54 行定义.

成员类型定义说明

◆ ChangeConnection

◆ ChangeFunc

在文件 manager.h57 行定义.

◆ ChangeSignal

◆ ParticipantPtr

◆ PublisherPtr

◆ SubscriberPtr

构造及析构函数说明

◆ Manager()

apollo::cyber::service_discovery::Manager::Manager ( )

Construct a new Manager object

在文件 manager.cc32 行定义.

◆ ~Manager()

apollo::cyber::service_discovery::Manager::~Manager ( )
virtual

Destroy the Manager object

在文件 manager.cc44 行定义.

44{ Shutdown(); }
virtual void Shutdown()
Shutdown module
Definition manager.cc:68

成员函数说明

◆ AddChangeListener()

Manager::ChangeConnection apollo::cyber::service_discovery::Manager::AddChangeListener ( const ChangeFunc func)

Add topology change listener, when topology changed, func will be called.

参数
functhe callback function
返回
ChangeConnection Store it to use when you want to stop listening.

在文件 manager.cc110 行定义.

110 {
111 return signal_.Connect(func);
112}
ConnectionType Connect(const Callback &cb)
Definition signal.h:65

◆ Check()

virtual bool apollo::cyber::service_discovery::Manager::Check ( const RoleAttributes attr)
protectedpure virtual

◆ Convert()

void apollo::cyber::service_discovery::Manager::Convert ( const RoleAttributes attr,
RoleType  role,
OperateType  opt,
ChangeMsg msg 
)
protected

在文件 manager.cc137 行定义.

138 {
139 msg->set_timestamp(cyber::Time::Now().ToNanosecond());
140 msg->set_change_type(change_type_);
141 msg->set_operate_type(opt);
142 msg->set_role_type(role);
143 auto role_attr = msg->mutable_role_attr();
144 role_attr->CopyFrom(attr);
145 if (!role_attr->has_host_name()) {
146 role_attr->set_host_name(host_name_);
147 }
148 if (!role_attr->has_process_id()) {
149 role_attr->set_process_id(process_id_);
150 }
151}
static Time Now()
get the current time.
Definition time.cc:57

◆ CreatePublisher()

bool apollo::cyber::service_discovery::Manager::CreatePublisher ( const ParticipantPtr participant)
protected

在文件 manager.cc119 行定义.

119 {
120 publisher_ = participant->CreatePublisher(
122 return publisher_ != nullptr;
123}
static const QosProfile QOS_PROFILE_TOPO_CHANGE

◆ CreateSubscriber()

bool apollo::cyber::service_discovery::Manager::CreateSubscriber ( const ParticipantPtr participant)
protected

在文件 manager.cc125 行定义.

125 {
126 subscriber_ = participant->CreateSubscriber(
128 std::bind(&Manager::OnRemoteChange, this, std::placeholders::_1));
129 return subscriber_ != nullptr;
130}
void OnRemoteChange(const std::shared_ptr< std::string > &msg_str)
Definition manager.cc:155

◆ Dispose()

virtual void apollo::cyber::service_discovery::Manager::Dispose ( const ChangeMsg msg)
protectedpure virtual

◆ IsFromSameProcess()

bool apollo::cyber::service_discovery::Manager::IsFromSameProcess ( const ChangeMsg msg)
protected

在文件 manager.cc187 行定义.

187 {
188 auto& host_name = msg.role_attr().host_name();
189 int process_id = msg.role_attr().process_id();
190
191 if (process_id != process_id_ || host_name != host_name_) {
192 return false;
193 }
194 return true;
195}

◆ Join()

bool apollo::cyber::service_discovery::Manager::Join ( const RoleAttributes attr,
RoleType  role,
bool  need_publish = true 
)

Join the topology

参数
attris the attributes that will be sent to other Manager(include ourselves)
roleis one of RoleType enum
返回
true if Join topology successfully
false if Join topology failed

在文件 manager.cc77 行定义.

78 {
79 if (is_shutdown_.load()) {
80 ADEBUG << "the manager has been shut down.";
81 return false;
82 }
83 RETURN_VAL_IF(!((1 << role) & allowed_role_), false);
84 RETURN_VAL_IF(!Check(attr), false);
85 ChangeMsg msg;
86 Convert(attr, role, OperateType::OPT_JOIN, &msg);
87 Dispose(msg);
88 if (need_publish) {
89 return Publish(msg);
90 }
91 return true;
92}
void Convert(const RoleAttributes &attr, RoleType role, OperateType opt, ChangeMsg *msg)
Definition manager.cc:137
bool Publish(const ChangeMsg &msg)
Definition manager.cc:170
virtual void Dispose(const ChangeMsg &msg)=0
virtual bool Check(const RoleAttributes &attr)=0
#define RETURN_VAL_IF(condition, val)
Definition log.h:114
#define ADEBUG
Definition log.h:41

◆ Leave()

bool apollo::cyber::service_discovery::Manager::Leave ( const RoleAttributes attr,
RoleType  role 
)

Leave the topology

参数
attris the attributes that will be sent to other Manager(include ourselves)
roleif one of RoleType enum.
返回
true if Leave topology successfully
false if Leave topology failed

在文件 manager.cc94 行定义.

94 {
95 if (is_shutdown_.load()) {
96 ADEBUG << "the manager has been shut down.";
97 return false;
98 }
99 RETURN_VAL_IF(!((1 << role) & allowed_role_), false);
100 RETURN_VAL_IF(!Check(attr), false);
101 ChangeMsg msg;
102 Convert(attr, role, OperateType::OPT_LEAVE, &msg);
103 Dispose(msg);
104 if (NeedPublish(msg)) {
105 return Publish(msg);
106 }
107 return true;
108}
virtual bool NeedPublish(const ChangeMsg &msg) const
Definition manager.cc:132

◆ NeedPublish()

bool apollo::cyber::service_discovery::Manager::NeedPublish ( const ChangeMsg msg) const
protectedvirtual

在文件 manager.cc132 行定义.

132 {
133 (void)msg;
134 return true;
135}

◆ Notify()

void apollo::cyber::service_discovery::Manager::Notify ( const ChangeMsg msg)
protected

在文件 manager.cc153 行定义.

153{ signal_(msg); }

◆ OnRemoteChange()

void apollo::cyber::service_discovery::Manager::OnRemoteChange ( const std::shared_ptr< std::string > &  msg_str)
protected

在文件 manager.cc155 行定义.

155 {
156 if (is_shutdown_.load()) {
157 ADEBUG << "the manager has been shut down.";
158 return;
159 }
160
161 ChangeMsg msg;
162 RETURN_IF(!message::ParseFromString(*msg_str, &msg));
163 if (IsFromSameProcess(msg)) {
164 return;
165 }
166 RETURN_IF(!Check(msg.role_attr()));
167 Dispose(msg);
168}
bool IsFromSameProcess(const ChangeMsg &msg)
Definition manager.cc:187
#define RETURN_IF(condition)
Definition log.h:106
std::enable_if< HasParseFromString< T >::value, bool >::type ParseFromString(const std::string &str, T *message)

◆ OnTopoModuleLeave()

virtual void apollo::cyber::service_discovery::Manager::OnTopoModuleLeave ( const std::string &  host_name,
int  process_id 
)
pure virtual

Called when a process' topology manager instance leave

参数
host_nameis the process's host's name
process_idis the process' id

◆ Publish()

bool apollo::cyber::service_discovery::Manager::Publish ( const ChangeMsg msg)
protected

在文件 manager.cc170 行定义.

170 {
171 if (!is_discovery_started_.load()) {
172 ADEBUG << "discovery is not started.";
173 return false;
174 }
175
178 {
179 std::lock_guard<std::mutex> lg(lock_);
180 if (publisher_ != nullptr) {
181 return publisher_->Write(m);
182 }
183 }
184 return true;
185}
This class represents the structure UnderlayMessage defined by the user in the IDL file.
void data(const std::string &_data)
This function copies the value in member data
std::enable_if< HasSerializeToString< T >::value, bool >::type SerializeToString(const T &message, std::string *str)

◆ RemoveChangeListener()

void apollo::cyber::service_discovery::Manager::RemoveChangeListener ( const ChangeConnection conn)

Remove our listener for topology change.

参数
connis the return value of AddChangeListener

在文件 manager.cc114 行定义.

114 {
115 auto local_conn = conn;
116 local_conn.Disconnect();
117}

◆ Shutdown()

void apollo::cyber::service_discovery::Manager::Shutdown ( )
virtual

Shutdown module

在文件 manager.cc68 行定义.

68 {
69 if (is_shutdown_.exchange(true)) {
70 return;
71 }
72
75}
void StopDiscovery()
Stop topology discovery
Definition manager.cc:62

◆ StartDiscovery()

bool apollo::cyber::service_discovery::Manager::StartDiscovery ( const ParticipantPtr participant)

Startup topology discovery

参数
participantis used to create rtps Publisher and Subscriber
返回
true if start successfully
false if start fail

在文件 manager.cc46 行定义.

46 {
47 if (participant == nullptr) {
48 return false;
49 }
50 if (is_discovery_started_.exchange(true)) {
51 return true;
52 }
53 if (!CreatePublisher(participant) || !CreateSubscriber(participant)) {
54 AERROR << "create publisher or subscriber failed.";
56 return false;
57 }
58 participant_ = participant;
59 return true;
60}
bool CreateSubscriber(const ParticipantPtr &participant)
Definition manager.cc:125
bool CreatePublisher(const ParticipantPtr &participant)
Definition manager.cc:119
#define AERROR
Definition log.h:44

◆ StopDiscovery()

void apollo::cyber::service_discovery::Manager::StopDiscovery ( )

Stop topology discovery

在文件 manager.cc62 行定义.

62 {
63 if (!is_discovery_started_.exchange(false)) {
64 return;
65 }
66}

类成员变量说明

◆ allowed_role_

int apollo::cyber::service_discovery::Manager::allowed_role_
protected

在文件 manager.h157 行定义.

◆ change_type_

ChangeType apollo::cyber::service_discovery::Manager::change_type_
protected

在文件 manager.h158 行定义.

◆ channel_name_

std::string apollo::cyber::service_discovery::Manager::channel_name_
protected

在文件 manager.h161 行定义.

◆ host_name_

std::string apollo::cyber::service_discovery::Manager::host_name_
protected

在文件 manager.h159 行定义.

◆ is_discovery_started_

std::atomic<bool> apollo::cyber::service_discovery::Manager::is_discovery_started_
protected

在文件 manager.h156 行定义.

◆ is_shutdown_

std::atomic<bool> apollo::cyber::service_discovery::Manager::is_shutdown_
protected

在文件 manager.h155 行定义.

◆ lock_

std::mutex apollo::cyber::service_discovery::Manager::lock_
protected

在文件 manager.h165 行定义.

◆ participant_

ParticipantPtr apollo::cyber::service_discovery::Manager::participant_
protected

在文件 manager.h164 行定义.

◆ process_id_

int apollo::cyber::service_discovery::Manager::process_id_
protected

在文件 manager.h160 行定义.

◆ publisher_

PublisherPtr apollo::cyber::service_discovery::Manager::publisher_
protected

在文件 manager.h162 行定义.

◆ signal_

ChangeSignal apollo::cyber::service_discovery::Manager::signal_
protected

在文件 manager.h167 行定义.

◆ subscriber_

SubscriberPtr apollo::cyber::service_discovery::Manager::subscriber_
protected

在文件 manager.h163 行定义.


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