Apollo 11.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
19#include "cyber/common/log.h"
20#include "cyber/common/util.h"
21#include "cyber/time/time.h"
22
23namespace apollo {
24namespace cyber {
25namespace service_discovery {
26
28 allowed_role_ |= 1 << RoleType::ROLE_SERVER;
29 allowed_role_ |= 1 << RoleType::ROLE_CLIENT;
30 change_type_ = ChangeType::CHANGE_SERVICE;
31 channel_name_ = "service_change_broadcast";
32}
33
35
36bool ServiceManager::HasService(const std::string& service_name) {
37 uint64_t key = common::Hash(service_name);
38 return servers_.Search(key);
39}
40
42 RETURN_IF_NULL(servers);
43 servers_.GetAllRoles(servers);
44}
45
46void ServiceManager::GetClients(const std::string& service_name,
47 RoleAttrVec* clients) {
48 RETURN_IF_NULL(clients);
49 uint64_t key = common::Hash(service_name);
50 clients_.Search(key, clients);
51}
52
53bool ServiceManager::Check(const RoleAttributes& attr) {
54 RETURN_VAL_IF(!attr.has_service_name(), false);
55 RETURN_VAL_IF(!attr.has_service_id(), false);
56 return true;
57}
58
59void ServiceManager::Dispose(const ChangeMsg& msg) {
60 if (msg.operate_type() == OperateType::OPT_JOIN) {
61 DisposeJoin(msg);
62 } else {
63 DisposeLeave(msg);
64 }
65 Notify(msg);
66}
67
68void ServiceManager::OnTopoModuleLeave(const std::string& host_name,
69 int process_id) {
71
72 RoleAttributes attr;
73 attr.set_host_name(host_name);
74 attr.set_process_id(process_id);
75
76 std::vector<RolePtr> servers_to_remove;
77 servers_.Search(attr, &servers_to_remove);
78 for (auto& server : servers_to_remove) {
79 servers_.Remove(server->attributes().service_id());
80 }
81
82 std::vector<RolePtr> clients_to_remove;
83 clients_.Search(attr, &clients_to_remove);
84 for (auto& client : clients_to_remove) {
85 clients_.Remove(client->attributes().service_id(), client);
86 }
87
88 ChangeMsg msg;
89 for (auto& server : servers_to_remove) {
90 Convert(server->attributes(), RoleType::ROLE_SERVER, OperateType::OPT_LEAVE,
91 &msg);
92 Notify(msg);
93 }
94
95 for (auto& client : clients_to_remove) {
96 Convert(client->attributes(), RoleType::ROLE_CLIENT, OperateType::OPT_LEAVE,
97 &msg);
98 Notify(msg);
99 }
100}
101
102void ServiceManager::DisposeJoin(const ChangeMsg& msg) {
103 if (msg.role_type() == RoleType::ROLE_SERVER) {
104 auto role = std::make_shared<RoleServer>(msg.role_attr());
105 servers_.Add(role->attributes().service_id(), role);
106 } else {
107 auto role = std::make_shared<RoleClient>(msg.role_attr());
108 clients_.Add(role->attributes().service_id(), role);
109 }
110}
111
112void ServiceManager::DisposeLeave(const ChangeMsg& msg) {
113 if (msg.role_type() == RoleType::ROLE_SERVER) {
114 auto role = std::make_shared<RoleServer>(msg.role_attr());
115 servers_.Remove(role->attributes().service_id());
116 } else {
117 auto role = std::make_shared<RoleClient>(msg.role_attr());
118 clients_.Remove(role->attributes().service_id(), role);
119 }
120}
121
122} // namespace service_discovery
123} // namespace cyber
124} // namespace apollo
void Notify(const ChangeMsg &msg)
Definition manager.cc:186
void Convert(const RoleAttributes &attr, RoleType role, OperateType opt, ChangeMsg *msg)
Definition manager.cc:170
std::atomic< bool > is_discovery_started_
Definition manager.h:155
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