-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeer_dispatch.cpp
80 lines (70 loc) · 2.05 KB
/
peer_dispatch.cpp
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
70
71
72
73
74
75
76
77
78
79
80
#include "peer_dispatch.h"
#include <algorithm>
#include <functional>
#include <stdexcept>
using namespace std::placeholders;
void PeerDispatch::AddClient(int id) {
Clients_.push_back(id);
printf("%s %d total %d\n", __func__, id, Clients_.size());
}
void PeerDispatch::AddServer(int id) {
Servers_.push_back(id);
printf("%s %d total %d\n", __func__, id, Servers_.size());
}
void PeerDispatch::DeleteMember(int id) {
auto iter = std::find_if(Servers_.begin(),
Servers_.end(),
[id](int& i){return i == id;});
if (iter != Servers_.end()) {
Servers_.erase(iter);
Map_.erase(id);
}
auto iter1 = std::find_if(Clients_.begin(),
Clients_.end(),
[id](int& i){return i == id;});
if (iter1 != Clients_.end()) {
Clients_.erase(iter1);
for(auto it = Map_.begin(); it != Map_.end(); ++it) {
if (it->second == id) {
Map_.erase(it->first);
break;
}
}
}
printf("%s %d total client %d server %d\n",
__func__, id, Clients_.size(), Servers_.size());
}
bool PeerDispatch::IsServer(int id) {
auto iter = std::find_if(Servers_.begin(),
Servers_.end(),
[id](int& i){return i == id;});
if (iter != Servers_.end())
return true;
else
return false;
}
int PeerDispatch::Dispatch(int client_id) {
if (!Servers_.empty()) {
for(auto i: Servers_) {
if (Map_.find(i) == Map_.end()) {
Map_.insert(std::pair<int, int>(i, client_id));
printf("%s client %d to server %d\n",
__func__, client_id, i);
return i;
}
}
}
return 0;
}
int PeerDispatch::GetPeer(int id)
{
auto it = Map_.find(id);
if ( it != Map_.end())
return it->second;
for(auto it = Map_.begin(); it != Map_.end(); ++it) {
if (it->second == id) {
return it->first;
}
}
return 0;
}