Apollo 11.0
自动驾驶开放平台
topology_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"
21#include "cyber/time/time.h"
22
23namespace apollo {
24namespace cyber {
25namespace service_discovery {
26
27TopologyManager::TopologyManager()
28 : init_(false),
29 node_manager_(nullptr),
30 channel_manager_(nullptr),
31 service_manager_(nullptr),
32 participant_(nullptr),
33 participant_listener_(nullptr) {
34 Init();
35}
36
37TopologyManager::~TopologyManager() { Shutdown(); }
38
39void TopologyManager::Shutdown() {
40 ADEBUG << "topology shutdown.";
41 // avoid shutdown twice
42 if (!init_.exchange(false)) {
43 return;
44 }
45
46 node_manager_->Shutdown();
47 channel_manager_->Shutdown();
48 service_manager_->Shutdown();
49 participant_->Shutdown();
50
51 delete participant_listener_;
52 participant_listener_ = nullptr;
53
54 change_signal_.DisconnectAllSlots();
55}
56
57TopologyManager::ChangeConnection TopologyManager::AddChangeListener(
58 const ChangeFunc& func) {
59 return change_signal_.Connect(func);
60}
61
62void TopologyManager::RemoveChangeListener(const ChangeConnection& conn) {
63 auto local_conn = conn;
64 local_conn.Disconnect();
65}
66
67bool TopologyManager::Init() {
68 if (init_.exchange(true)) {
69 return true;
70 }
71
72 node_manager_ = std::make_shared<NodeManager>();
73 channel_manager_ = std::make_shared<ChannelManager>();
74 service_manager_ = std::make_shared<ServiceManager>();
75
76 CreateParticipant();
77
78 bool result =
79 InitNodeManager() && InitChannelManager() && InitServiceManager();
80 if (!result) {
81 AERROR << "init manager failed.";
82 participant_ = nullptr;
83 delete participant_listener_;
84 participant_listener_ = nullptr;
85 node_manager_ = nullptr;
86 channel_manager_ = nullptr;
87 service_manager_ = nullptr;
88 init_.store(false);
89 return false;
90 }
91
92 return true;
93}
94
95bool TopologyManager::InitNodeManager() {
96 return node_manager_->StartDiscovery(participant_->fastrtps_participant());
97}
98
99bool TopologyManager::InitChannelManager() {
100 return channel_manager_->StartDiscovery(participant_->fastrtps_participant());
101}
102
103bool TopologyManager::InitServiceManager() {
104 return service_manager_->StartDiscovery(participant_->fastrtps_participant());
105}
106
107bool TopologyManager::CreateParticipant() {
108 std::string participant_name =
109 common::GlobalData::Instance()->HostName() + '+' +
110 std::to_string(common::GlobalData::Instance()->ProcessId());
111 participant_listener_ = new ParticipantListener(std::bind(
112 &TopologyManager::OnParticipantChange, this, std::placeholders::_1));
113 participant_ = std::make_shared<transport::Participant>(
114 participant_name, 11511, participant_listener_);
115 return true;
116}
117
118void TopologyManager::OnParticipantChange(const PartInfo& info) {
119 ChangeMsg msg;
120 if (!Convert(info, &msg)) {
121 return;
122 }
123
124 if (!init_.load()) {
125 return;
126 }
127
128 if (msg.operate_type() == OperateType::OPT_LEAVE) {
129 auto& host_name = msg.role_attr().host_name();
130 int process_id = msg.role_attr().process_id();
131 node_manager_->OnTopoModuleLeave(host_name, process_id);
132 channel_manager_->OnTopoModuleLeave(host_name, process_id);
133 service_manager_->OnTopoModuleLeave(host_name, process_id);
134 }
135 change_signal_(msg);
136}
137
138bool TopologyManager::Convert(const PartInfo& info, ChangeMsg* msg) {
139 auto guid = info.rtps.m_guid;
140 auto status = info.rtps.m_status;
141 std::string participant_name("");
142 OperateType opt_type = OperateType::OPT_JOIN;
143
144 switch (status) {
145 case eprosima::fastrtps::rtps::DISCOVERY_STATUS::DISCOVERED_RTPSPARTICIPANT:
146 participant_name = info.rtps.m_RTPSParticipantName;
147 participant_names_[guid] = participant_name;
148 opt_type = OperateType::OPT_JOIN;
149 break;
150
151 case eprosima::fastrtps::rtps::DISCOVERY_STATUS::REMOVED_RTPSPARTICIPANT:
152 case eprosima::fastrtps::rtps::DISCOVERY_STATUS::DROPPED_RTPSPARTICIPANT:
153 if (participant_names_.find(guid) != participant_names_.end()) {
154 participant_name = participant_names_[guid];
155 participant_names_.erase(guid);
156 }
157 opt_type = OperateType::OPT_LEAVE;
158 break;
159
160 default:
161 break;
162 }
163
164 std::string host_name("");
165 int process_id = 0;
166 if (!ParseParticipantName(participant_name, &host_name, &process_id)) {
167 return false;
168 }
169
170 msg->set_timestamp(cyber::Time::Now().ToNanosecond());
171 msg->set_change_type(ChangeType::CHANGE_PARTICIPANT);
172 msg->set_operate_type(opt_type);
173 msg->set_role_type(RoleType::ROLE_PARTICIPANT);
174 auto role_attr = msg->mutable_role_attr();
175 role_attr->set_host_name(host_name);
176 role_attr->set_process_id(process_id);
177 return true;
178}
179
180bool TopologyManager::ParseParticipantName(const std::string& participant_name,
181 std::string* host_name,
182 int* process_id) {
183 // participant_name format: host_name+process_id
184 auto pos = participant_name.find('+');
185 if (pos == std::string::npos) {
186 ADEBUG << "participant_name [" << participant_name << "] format mismatch.";
187 return false;
188 }
189 *host_name = participant_name.substr(0, pos);
190 std::string pid_str = participant_name.substr(pos + 1);
191 try {
192 *process_id = std::stoi(pid_str);
193 } catch (const std::exception& e) {
194 AERROR << "invalid process_id:" << e.what();
195 return false;
196 }
197 return true;
198}
199
200} // namespace service_discovery
201} // namespace cyber
202} // namespace apollo
static Time Now()
get the current time.
Definition time.cc:57
std::function< void(const ChangeMsg &)> ChangeFunc
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
bool Init(const char *binary_name, const std::string &dag_info)
Definition init.cc:99
class register implement
Definition arena_queue.h:37