23namespace service_discovery {
36 return servers_.
Search(key);
48 clients_.
Search(key, clients);
57void ServiceManager::Dispose(
const ChangeMsg& msg) {
58 if (msg.operate_type() == OperateType::OPT_JOIN) {
66void ServiceManager::OnTopoModuleLeave(
const std::string& host_name,
71 attr.set_host_name(host_name);
72 attr.set_process_id(process_id);
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());
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);
87 for (
auto& server : servers_to_remove) {
88 Convert(server->attributes(), RoleType::ROLE_SERVER, OperateType::OPT_LEAVE,
93 for (
auto& client : clients_to_remove) {
94 Convert(client->attributes(), RoleType::ROLE_CLIENT, OperateType::OPT_LEAVE,
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);
105 auto role = std::make_shared<RoleClient>(msg.role_attr());
106 clients_.
Add(role->attributes().service_id(), role);
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());
115 auto role = std::make_shared<RoleClient>(msg.role_attr());
116 clients_.
Remove(role->attributes().service_id(), role);
void Notify(const ChangeMsg &msg)
void Convert(const RoleAttributes &attr, RoleType role, OperateType opt, ChangeMsg *msg)
std::string channel_name_
std::atomic< bool > is_discovery_started_
bool Search(uint64_t key) override
void Remove(uint64_t key) override
bool Add(uint64_t key, const RolePtr &role, bool ignore_if_exist=true) override
std::vector< RoleAttributes > RoleAttrVec
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
void Remove(uint64_t key) override
bool Search(uint64_t key) override
bool Add(uint64_t key, const RolePtr &role, bool ignore_if_exist=true) override
#define RETURN_IF_NULL(ptr)
#define RETURN_VAL_IF(condition, val)
#define RETURN_IF(condition)
std::size_t Hash(const std::string &key)