-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicchatclient.hpp
210 lines (168 loc) · 5.26 KB
/
basicchatclient.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* basicchatclient.hpp
*
* Created on: Dec 6, 2010
* Author: Fei Huang
* Email: [email protected]
*/
#pragma once
#include <algorithm>
#include <iostream>
#include "server.hpp"
#include "peerlist.hpp"
using namespace std;
namespace openchat {
class Controller;
/**
* BasicChatClient implements the fundamental framework for chat
*/
class BasicChatClient : public Server {
public:
BasicChatClient(const string &id, int port) : Server(port), id_(id) { }
virtual ~BasicChatClient() { }
void addFriend(const string &id, const string &hostname, const string &port) {
friends_.addPeer(id, hostname, port, ioService_);
}
void deleteFriend(const string &id) {
friends_.deletePeer(id);
}
void addStranger(const string &id, const string &hostname, const string &port) {
strangers_.addPeer(id, hostname, port, ioService_);
}
void deleteStranger(const string &id) {
friends_.deletePeer(id);
}
void addGroup(const string &id, const PeerList &group = PeerList()) {
groups_.insert(make_pair(id, group));
}
void deleteGroup(const string &id) {
groups_.erase(id);
}
void addGroupMember(const string &groupID, const string &memberID) {
groups_[groupID].addPeer(memberID, friends_.getPeer(memberID));
}
void deleteGroupMember(const string &groupID, const string &memberID) {
groups_[groupID].deletePeer(memberID);
}
void sendToFriend(const string &id, const string &message) {
friends_.sendTo(id, serverSocket_, message);
}
void sendToAllFriends(const string &message) {
friends_.sendToAll(serverSocket_, message);
}
void sendToStranger(const string &id, const string &message) {
strangers_.sendTo(id, serverSocket_, message);
}
void sendToAllStrangers(const string &message) {
strangers_.sendToAll(serverSocket_, message);
}
void sendToGroup(const string &groupID, const string &message) {
groups_[groupID].sendToAll(serverSocket_, message);
}
void sendToAll(const string &message) {
sendToAllFriends(message);
sendToAllStrangers(message);
}
vector<string> getFriendsIDs() const {
return friends_.getPeerIDs();
}
vector<string> getStrangersIDs() const {
return strangers_.getPeerIDs();
}
vector<string> getGroupsIDs() const {
vector<string> ids;
for (map<string, PeerList>::const_iterator iter = groups_.begin(); iter != groups_.end(); ++iter) {
ids.push_back(iter->first);
}
return ids;
}
vector<string> getGroupMemberIDs(const string &id) const {
map<string, PeerList>::const_iterator iter = groups_.find(id);
return iter->second.getPeerIDs();
}
vector<string> getAllIDs() const {
vector<string> ids = getFriendsIDs();
vector<string> sids = getStrangersIDs();
for (size_t i = 0; i < sids.size(); ++i) {
ids.push_back(sids[i]);
}
sort(ids.begin(), ids.end());
return ids;
}
vector<pair<string, pair<string, string> > > getFriendListInformation() const {
return friends_.getPeerListInformation();
}
pair<string, string> getFriendHostnameAndPort(const string &id) const {
PeerList::peerPointerType peer = friends_.getPeer(id);
return make_pair(peer->getHostname(), peer->getPort());
}
bool hasFriend(const string &id) const {
return friends_.hasPeer(id);
}
bool hasStranger(const string &id) const {
return strangers_.hasPeer(id);
}
bool hasGroup(const string &id) const {
return groups_.find(id) != groups_.end();
}
bool hasGroupMember(const string &groupID, const string &memberID) const {
return groups_.find(groupID)->second.hasPeer(memberID);
}
void setMessageProcesser(boost::shared_ptr<Controller> controller) {
controller_ = controller;
}
friend istream &operator>> (istream &is, BasicChatClient &client) {
// parsing the configuration file
size_t friendCnt;
string id, hostname, port;
is >> friendCnt;
for (size_t i = 0; i < friendCnt; ++i) {
is >> id >> hostname >> port;
client.addFriend(id, hostname, port);
}
size_t groupCnt;
string groupID;
is >> groupCnt;
for (size_t i = 0; i < groupCnt; ++i) {
is >> groupID;
size_t groupMemberCnt;
string memberID;
is >> groupMemberCnt;
PeerList group;
for (size_t j = 0; j < groupMemberCnt; ++j) {
is >> memberID;
group.addPeer(memberID, client.friends_.getPeer(memberID));
}
client.addGroup(groupID, group);
}
return is;
}
friend ostream &operator<< (ostream &os, BasicChatClient &client) {
os << client.friends_.getSize() << endl;
vector<pair<string, pair<string, string> > > fInfo = client.friends_.getPeerListInformation();
for (size_t i = 0; i < fInfo.size(); ++i) {
os << fInfo[i].first << " " << fInfo[i].second.first << " " << fInfo[i].second.second << endl;
}
os << endl;
os << client.groups_.size() << endl;
for (map<string, PeerList>::const_iterator iter = client.groups_.begin(); iter != client.groups_.end(); ++iter) {
os << iter->first << " " << iter->second.getSize() << " ";
vector<string> members = iter->second.getPeerIDs();
for (size_t i = 0; i < members.size(); ++i) {
os << members[i] << " ";
}
os << endl;
}
return os;
}
string getID() const { return id_; }
protected:
virtual void handleRequest(const string &rawMessage, boost::shared_ptr<udp::endpoint> remoteEndpoint);
string id_;
PeerList friends_;
PeerList strangers_;
// groups
map<string, PeerList> groups_;
boost::shared_ptr<Controller> controller_;
};
}