-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathregistry.hpp
70 lines (62 loc) · 1.42 KB
/
registry.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include <string>
#include <map>
#include <mutex>
#include "common.h"
class registry
{
public:
registry() = default;
address register_service(const std::string& service_name, const std::string& host_name, int port)
{
return { service_name, host_name, port };
}
bool unregister_service(const std::string& service_name, const std::string& host_name, int port)
{
std::unique_lock<std::mutex> lock(mtx_);
for (auto it = map_.cbegin(); it != map_.end();)
{
if (it->first == service_name && it->second == entity{ service_name, host_name ,port})
{
it = map_.erase(it);
}
else
{
++it;
}
}
return true;
}
void set_conn(std::shared_ptr<connection<msgpack_codec>> conn, const address& addr)
{
entity en = { addr, conn };
std::unique_lock<std::mutex> lock(mtx_);
map_.emplace(addr.service_name, en);
}
void handle_disconnect(connection<msgpack_codec>* conn)
{
std::unique_lock<std::mutex> lock(mtx_);
for (auto it = map_.cbegin(); it != map_.end();)
{
if (it->second.conn.get() == conn)
{
std::cout << it->second.addr.host_name << " offline" << std::endl;
it = map_.erase(it);
}
else
{
++it;
}
}
}
private:
friend class load_blancer;
std::multimap<std::string, entity> get_services()
{
std::unique_lock<std::mutex> lock(mtx_);
return map_;
}
registry(const registry&) = delete;
std::multimap<std::string, entity> map_;
std::mutex mtx_;
};