Apollo 10.0
自动驾驶开放平台
service_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#include "cyber/common/log.h"
19#include "cyber/common/util.h"
20
21namespace apollo {
22namespace cyber {
23namespace service_discovery {
24
26 allowed_role_ |= 1 << RoleType::ROLE_SERVER;
27 allowed_role_ |= 1 << RoleType::ROLE_CLIENT;
28 change_type_ = ChangeType::CHANGE_SERVICE;
29 channel_name_ = "service_change_broadcast";
30}
31
33
34bool ServiceManager::HasService(const std::string& service_name) {
35 uint64_t key = common::Hash(service_name);
36 return servers_.Search(key);
37}
38
40 RETURN_IF_NULL(servers);
41 servers_.GetAllRoles(servers);
42}
43
44void ServiceManager::GetClients(const std::string& service_name,
45 RoleAttrVec* clients) {
46 RETURN_IF_NULL(clients);
47 uint64_t key = common::Hash(service_name);
48 clients_.Search(key, clients);
49}
50
51bool ServiceManager::Check(const RoleAttributes& attr) {
52 RETURN_VAL_IF(!attr.has_service_name(), false);
53 RETURN_VAL_IF(!attr.has_service_id(), false);
54 return true;
55}
56
57void ServiceManager::Dispose(const ChangeMsg& msg) {
58 if (msg.operate_type() == OperateType::OPT_JOIN) {
59 DisposeJoin(msg);
60 } else {
61 DisposeLeave(msg);
62 }
63 Notify(msg);
64}
65
66void ServiceManager::OnTopoModuleLeave(const std::string& host_name,
67 int process_id) {
69
70 RoleAttributes attr;
71 attr.set_host_name(host_name);
72 attr.set_process_id(process_id);
73
74 std::vector<RolePtr> servers_to_remove;
75 servers_.Search(attr, &servers_to_remove);
76 for (auto& server : servers_to_remove) {
77 servers_.Remove(server->attributes().service_id());
78 }
79
80 std::vector<RolePtr> clients_to_remove;
81 clients_.Search(attr, &clients_to_remove);
82 for (auto& client : clients_to_remove) {
83 clients_.Remove(client->attributes().service_id(), client);
84 }
85
86 ChangeMsg msg;
87 for (auto& server : servers_to_remove) {
88 Convert(server->attributes(), RoleType::ROLE_SERVER, OperateType::OPT_LEAVE,
89 &msg);
90 Notify(msg);
91 }
92
93 for (auto& client : clients_to_remove) {
94 Convert(client->attributes(), RoleType::ROLE_CLIENT, OperateType::OPT_LEAVE,
95 &msg);
96 Notify(msg);
97 }
98}
99
100void ServiceManager::DisposeJoin(const ChangeMsg& msg) {
101 if (msg.role_type() == RoleType::ROLE_SERVER) {
102 auto role = std::make_shared<RoleServer>(msg.role_attr());
103 servers_.Add(role->attributes().service_id(), role);
104 } else {
105 auto role = std::make_shared<RoleClient>(msg.role_attr());
106 clients_.Add(role->attributes().service_id(), role);
107 }
108}
109
110void ServiceManager::DisposeLeave(const ChangeMsg& msg) {
111 if (msg.role_type() == RoleType::ROLE_SERVER) {
112 auto role = std::make_shared<RoleServer>(msg.role_attr());
113 servers_.Remove(role->attributes().service_id());
114 } else {
115 auto role = std::make_shared<RoleClient>(msg.role_attr());
116 clients_.Remove(role->attributes().service_id(), role);
117 }
118}
119
120} // namespace service_discovery
121} // namespace cyber
122} // namespace apollo
void Notify(const ChangeMsg &msg)
Definition manager.cc:153
void Convert(const RoleAttributes &attr, RoleType role, OperateType opt, ChangeMsg *msg)
Definition manager.cc:137
std::atomic< bool > is_discovery_started_
Definition manager.h:156
bool Add(uint64_t key, const RolePtr &role, bool ignore_if_exist=true) override
bool HasService(const std::string &service_name)
Inquire whether service_name exists in topology
virtual ~ServiceManager()
Destroy the Service Manager object
void GetClients(const std::string &service_name, RoleAttrVec *clients)
Get the Clients object that subscribes service_name
ServiceManager()
Construct a new Service Manager object
void GetServers(RoleAttrVec *servers)
Get the All Server in the topology
void GetAllRoles(std::vector< RolePtr > *roles) override
bool Add(uint64_t key, const RolePtr &role, bool ignore_if_exist=true) override
#define RETURN_IF_NULL(ptr)
Definition log.h:90
#define RETURN_VAL_IF(condition, val)
Definition log.h:114
#define RETURN_IF(condition)
Definition log.h:106
std::size_t Hash(const std::string &key)
Definition util.h:27
class register implement
Definition arena_queue.h:37