25namespace service_discovery {
38 return servers_.
Search(key);
50 clients_.
Search(key, clients);
59void ServiceManager::Dispose(
const ChangeMsg& msg) {
60 if (msg.operate_type() == OperateType::OPT_JOIN) {
68void ServiceManager::OnTopoModuleLeave(
const std::string& host_name,
73 attr.set_host_name(host_name);
74 attr.set_process_id(process_id);
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());
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);
89 for (
auto& server : servers_to_remove) {
90 Convert(server->attributes(), RoleType::ROLE_SERVER, OperateType::OPT_LEAVE,
95 for (
auto& client : clients_to_remove) {
96 Convert(client->attributes(), RoleType::ROLE_CLIENT, OperateType::OPT_LEAVE,
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);
107 auto role = std::make_shared<RoleClient>(msg.role_attr());
108 clients_.
Add(role->attributes().service_id(), role);
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());
117 auto role = std::make_shared<RoleClient>(msg.role_attr());
118 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)