Apollo 11.0
自动驾驶开放平台
manager.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
18
20#include "cyber/common/log.h"
22#include "cyber/time/time.h"
27
28namespace apollo {
29namespace cyber {
30namespace service_discovery {
31
32using transport::AttributesFiller;
33using transport::QosProfileConf;
34
36 : is_shutdown_(false),
37 is_discovery_started_(false),
38 allowed_role_(0),
39 change_type_(proto::ChangeType::CHANGE_PARTICIPANT),
40 channel_name_(""),
41 publisher_(nullptr),
42 subscriber_(nullptr),
43 listener_(nullptr) {
44 host_name_ = common::GlobalData::Instance()->HostName();
45 process_id_ = common::GlobalData::Instance()->ProcessId();
46}
47
49
51 if (participant == nullptr) {
52 return false;
53 }
54 if (is_discovery_started_.exchange(true)) {
55 return true;
56 }
57 if (!CreatePublisher(participant) || !CreateSubscriber(participant)) {
58 AERROR << "create publisher or subscriber failed.";
60 return false;
61 }
62 return true;
63}
64
66 if (!is_discovery_started_.exchange(false)) {
67 return;
68 }
69
70 {
71 std::lock_guard<std::mutex> lg(lock_);
72 if (publisher_ != nullptr) {
73 eprosima::fastrtps::Domain::removePublisher(publisher_);
74 publisher_ = nullptr;
75 }
76 }
77
78 if (subscriber_ != nullptr) {
79 eprosima::fastrtps::Domain::removeSubscriber(subscriber_);
80 subscriber_ = nullptr;
81 }
82
83 if (listener_ != nullptr) {
84 delete listener_;
85 listener_ = nullptr;
86 }
87}
88
90 if (is_shutdown_.exchange(true)) {
91 return;
92 }
93
96}
97
98bool Manager::Join(const RoleAttributes& attr, RoleType role,
99 bool need_publish) {
100 if (is_shutdown_.load()) {
101 ADEBUG << "the manager has been shut down.";
102 return false;
103 }
104 RETURN_VAL_IF(!((1 << role) & allowed_role_), false);
105 RETURN_VAL_IF(!Check(attr), false);
106 ChangeMsg msg;
107 Convert(attr, role, OperateType::OPT_JOIN, &msg);
108 Dispose(msg);
109 if (need_publish) {
110 return Publish(msg);
111 }
112 return true;
113}
114
115bool Manager::Leave(const RoleAttributes& attr, RoleType role) {
116 if (is_shutdown_.load()) {
117 ADEBUG << "the manager has been shut down.";
118 return false;
119 }
120 RETURN_VAL_IF(!((1 << role) & allowed_role_), false);
121 RETURN_VAL_IF(!Check(attr), false);
122 ChangeMsg msg;
123 Convert(attr, role, OperateType::OPT_LEAVE, &msg);
124 Dispose(msg);
125 if (NeedPublish(msg)) {
126 return Publish(msg);
127 }
128 return true;
129}
130
134
136 auto local_conn = conn;
137 local_conn.Disconnect();
138}
139
141 RtpsPublisherAttr pub_attr;
145 false);
146 publisher_ =
147 eprosima::fastrtps::Domain::createPublisher(participant, pub_attr);
148 return publisher_ != nullptr;
149}
150
152 RtpsSubscriberAttr sub_attr;
156 false);
158 std::bind(&Manager::OnRemoteChange, this, std::placeholders::_1));
159
160 subscriber_ = eprosima::fastrtps::Domain::createSubscriber(
161 participant, sub_attr, listener_);
162 return subscriber_ != nullptr;
163}
164
165bool Manager::NeedPublish(const ChangeMsg& msg) const {
166 (void)msg;
167 return true;
168}
169
170void Manager::Convert(const RoleAttributes& attr, RoleType role,
171 OperateType opt, ChangeMsg* msg) {
172 msg->set_timestamp(cyber::Time::Now().ToNanosecond());
173 msg->set_change_type(change_type_);
174 msg->set_operate_type(opt);
175 msg->set_role_type(role);
176 auto role_attr = msg->mutable_role_attr();
177 role_attr->CopyFrom(attr);
178 if (!role_attr->has_host_name()) {
179 role_attr->set_host_name(host_name_);
180 }
181 if (!role_attr->has_process_id()) {
182 role_attr->set_process_id(process_id_);
183 }
184}
185
186void Manager::Notify(const ChangeMsg& msg) { signal_(msg); }
187
188void Manager::OnRemoteChange(const std::string& msg_str) {
189 if (is_shutdown_.load()) {
190 ADEBUG << "the manager has been shut down.";
191 return;
192 }
193
194 ChangeMsg msg;
195 RETURN_IF(!message::ParseFromString(msg_str, &msg));
196 if (IsFromSameProcess(msg)) {
197 return;
198 }
199 RETURN_IF(!Check(msg.role_attr()));
200 Dispose(msg);
201}
202
203bool Manager::Publish(const ChangeMsg& msg) {
204 if (!is_discovery_started_.load()) {
205 ADEBUG << "discovery is not started.";
206 return false;
207 }
208
211 {
212 std::lock_guard<std::mutex> lg(lock_);
213 if (publisher_ != nullptr) {
214 return publisher_->write(reinterpret_cast<void*>(&m));
215 }
216 }
217 return true;
218}
219
221 auto& host_name = msg.role_attr().host_name();
222 int process_id = msg.role_attr().process_id();
223
224 if (process_id != process_id_ || host_name != host_name_) {
225 return false;
226 }
227 return true;
228}
229
230} // namespace service_discovery
231} // namespace cyber
232} // namespace apollo
static Time Now()
get the current time.
Definition time.cc:57
ConnectionType Connect(const Callback &cb)
Definition signal.h:65
void Notify(const ChangeMsg &msg)
Definition manager.cc:186
bool StartDiscovery(RtpsParticipant *participant)
Startup topology discovery
Definition manager.cc:50
void Convert(const RoleAttributes &attr, RoleType role, OperateType opt, ChangeMsg *msg)
Definition manager.cc:170
eprosima::fastrtps::PublisherAttributes RtpsPublisherAttr
Definition manager.h:58
std::function< void(const ChangeMsg &)> ChangeFunc
Definition manager.h:54
bool Join(const RoleAttributes &attr, RoleType role, bool need_publish=true)
Join the topology
Definition manager.cc:98
bool IsFromSameProcess(const ChangeMsg &msg)
Definition manager.cc:220
bool Publish(const ChangeMsg &msg)
Definition manager.cc:203
void RemoveChangeListener(const ChangeConnection &conn)
Remove our listener for topology change.
Definition manager.cc:135
eprosima::fastrtps::Subscriber * subscriber_
Definition manager.h:163
virtual ~Manager()
Destroy the Manager object
Definition manager.cc:48
virtual void Dispose(const ChangeMsg &msg)=0
bool CreatePublisher(RtpsParticipant *participant)
Definition manager.cc:140
eprosima::fastrtps::Participant RtpsParticipant
Definition manager.h:57
bool CreateSubscriber(RtpsParticipant *participant)
Definition manager.cc:151
std::atomic< bool > is_discovery_started_
Definition manager.h:155
Manager()
Construct a new Manager object
Definition manager.cc:35
void OnRemoteChange(const std::string &msg_str)
Definition manager.cc:188
ChangeConnection AddChangeListener(const ChangeFunc &func)
Add topology change listener, when topology changed, func will be called.
Definition manager.cc:131
eprosima::fastrtps::Publisher * publisher_
Definition manager.h:161
bool Leave(const RoleAttributes &attr, RoleType role)
Leave the topology
Definition manager.cc:115
eprosima::fastrtps::SubscriberAttributes RtpsSubscriberAttr
Definition manager.h:59
virtual bool Check(const RoleAttributes &attr)=0
void StopDiscovery()
Stop topology discovery
Definition manager.cc:65
virtual bool NeedPublish(const ChangeMsg &msg) const
Definition manager.cc:165
virtual void Shutdown()
Shutdown module
Definition manager.cc:89
static bool FillInSubAttr(const std::string &channel_name, const QosProfile &qos, eprosima::fastrtps::SubscriberAttributes *sub_attr)
static bool FillInPubAttr(const std::string &channel_name, const QosProfile &qos, eprosima::fastrtps::PublisherAttributes *pub_attr)
static const QosProfile QOS_PROFILE_TOPO_CHANGE
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
#define RETURN_VAL_IF(condition, val)
Definition log.h:114
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define RETURN_IF(condition)
Definition log.h:106
std::enable_if< HasSerializeToString< T >::value, bool >::type SerializeToString(const T &message, std::string *str)
std::enable_if< HasParseFromString< T >::value, bool >::type ParseFromString(const std::string &str, T *message)
class register implement
Definition arena_queue.h:37
optional RoleAttributes role_attr