-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerlist.hpp
83 lines (66 loc) · 2.19 KB
/
peerlist.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* peerlist.hpp
*
* Created on: Dec 6, 2010
* Author: Fei Huang
* Email: [email protected]
*/
#pragma once
#include <map>
#include <vector>
#include <boost/shared_ptr.hpp>
#include "peerproxy.hpp"
using namespace std;
namespace openchat {
/**
* PeerList stores a list of PeerProxies and supports queries and delegations regarding them
*/
class PeerList {
public:
typedef boost::shared_ptr<PeerProxy> peerPointerType;
void sendTo(const string &id, udp::socket &serverSocket, const string &message) const {
peers_.find(id)->second->sendMessage(serverSocket, message);
}
void sendToAll(udp::socket &serverSocket, const string &message) const {
for (map<string, boost::shared_ptr<PeerProxy> >::const_iterator iter = peers_.begin(); iter != peers_.end(); ++iter) {
iter->second->sendMessage(serverSocket, message);
}
}
void addPeer(const string &id, const string &hostname, const string &port, boost::asio::io_service &ioService) {
boost::shared_ptr<PeerProxy> peer(new PeerProxy(id, hostname, port, ioService));
addPeer(id, peer);
}
void addPeer(const string &id, boost::shared_ptr<PeerProxy> peer) {
peers_.insert(make_pair(id, peer));
}
void deletePeer(const string &id) {
peers_.erase(id);
}
bool hasPeer(const string &id) const {
return peers_.find(id) != peers_.end();
}
peerPointerType getPeer(const string &id) const {
return peers_.find(id)->second;
}
vector<string> getPeerIDs() const {
vector<string> ids;
for (map<string, boost::shared_ptr<PeerProxy> >::const_iterator iter = peers_.begin(); iter != peers_.end(); ++iter) {
ids.push_back(iter->first);
}
return ids;
}
vector<pair<string, pair<string, string> > > getPeerListInformation() const {
vector<pair<string, pair<string, string> > > info;
for (map<string, boost::shared_ptr<PeerProxy> >::const_iterator iter = peers_.begin(); iter != peers_.end(); ++iter) {
string id = iter->first;
string hostname = iter->second->getHostname();
string port = iter->second->getPort();
info.push_back(make_pair(id, make_pair(hostname, port)));
}
return info;
}
size_t getSize() const { return peers_.size(); }
private:
map<string, boost::shared_ptr<PeerProxy> > peers_;
};
}