-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsistentHashing.cpp
More file actions
41 lines (32 loc) · 974 Bytes
/
consistentHashing.cpp
File metadata and controls
41 lines (32 loc) · 974 Bytes
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
#include <map>
#include "serverManager.h"
#include <list>
class consistentHashRing{
public:
consistentHashRing() = default;
void addNode(server* new_server){
int hash = getHash(&(new_server->addr));
HashRing[hash] = new_server;
}
void removeNode(server* new_server){
int hash = getHash(&(new_server->addr));
//our map should have this!
HashRing.erase(hash);
}
server* getNode(sockaddr_in* client){
int hash = getHash(client);
//check for this hash in the hash ring
auto it = HashRing.lower_bound(hash);
if (it == HashRing.end()){
it = HashRing.begin();
}
return it->second;
}
private:
size_t getHash(sockaddr_in* input){
size_t h1 = std::hash<uint32_t>()(input->sin_addr.s_addr);
size_t h2 = std::hash<uint16_t>()(input->sin_port);
return h1 ^ (h2 << 1);
}
std::map<size_t,server*> HashRing;
};