- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.cpp
More file actions
Latest commit
315 lines (305 loc) · 12.6 KB
/
Copy pathcontroller.cpp
File metadata and controls
315 lines (305 loc) · 12.6 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* controller.cpp
*
* Created on: Dec 7, 2010
* Author: Fei Huang
* Email: felix.fei.huang@yale.edu
*/
#include"controller.hpp"
#include"chatprotocol.hpp"
#include<sstream>
#include<utility>
namespaceopenchat {
voidController::processIncomingMessage(const string &rawMessage, boost::shared_ptr<udp::endpoint> remoteEndpoint) {
// parse incoming message
if (ChatProtocol::isPlainChatMessage(rawMessage)) {
pair<string, string> idMessage = ChatProtocol::parsePlainChatMessage(rawMessage);
view_->presentLine("[From " + idMessage.first + "]: " + idMessage.second);
// if the message is from nowhere, add it to the stranger list
if (!model_->hasFriend(idMessage.first) && !model_->hasStranger(idMessage.first)) {
string hostname, port;
hostname = remoteEndpoint->address().to_string();
ostringstream oss;
oss << remoteEndpoint->port();
port = oss.str();
model_->addStranger(idMessage.first, hostname, port);
view_->presentLine("ID: " + idMessage.first + " has been added to stranger list.");
}
view_->present(">>> "); // begin a new line
} elseif (ChatProtocol::isFriendListExtractionMessage(rawMessage)) {
vector<pair<string, pair<string, string> > > info = model_->getFriendListInformation();
// send the information back
model_->sendTo(ChatProtocol::wrapFriendListExtractionResponseMessage(model_->getID(), info), remoteEndpoint);
} elseif (ChatProtocol::isFriendListExtractionResponseMessage(rawMessage)) {
pair<string, vector<pair<string, pair<string, string> > > > idInfo = ChatProtocol::parseFriendListExtractionResponseMessage(rawMessage);
vector<pair<string, pair<string, string> > > &info = idInfo.second;
// present the information to view
view_->presentLine("Friend list of [ID=" + idInfo.first + "]:");
for (size_t i = 0; i != info.size(); ++i) {
string id, hostname, port;
id = info[i].first;
hostname = info[i].second.first;
port = info[i].second.second;
view_->presentLine("[ID=" + id + "], [hostname=" + hostname + "], [port=" + port + "]");
}
view_->present(">>> "); // begin a new line
}
}
boolController::processUserInput(const string &input) {
/**
* Some inputs (commands) are special, because they query the information of the program rather than
* doing anything actually, so they are handled specially.
*/
if (input == "help") {
for (map<string, string>::const_iterator iter = commandDescriptions_.begin(); iter != commandDescriptions_.end(); ++iter) {
view_->presentLine(iter->first + ": " + iter->second);
}
returntrue;
}
if (input == "history") {
for (vector<boost::shared_ptr<Command> >::reverse_iterator riter = commandHistory_.rbegin();
riter != commandHistory_.rend(); ++riter) {
view_->presentLine((*riter)->getName());
}
returntrue;
}
if (input == "undo") {
if (commandHistory_.empty()) {
view_->presentLine("No command to undo.");
} else {
boost::shared_ptr<Command> command = commandHistory_.back(); commandHistory_.pop_back();
command->undo();
}
returntrue;
}
if (input == "exit") {
view_->presentLine("Bye bye");
returnfalse;
}
// process commands
boost::shared_ptr<Command> command = createCommand(input);
command->showBeforeExecution();
command->execute();
command->showAfterExecution();
// if the command is not NullCommand, add it to the history
if (typeid(command).name() != typeid(NullCommand).name()) {
commandHistory_.push_back(command);
}
returntrue;
}
voidController::initCommandDescriptions() {
// add command name - command description pairs
commandDescriptions_["friend"] = "show the list of friends' IDs.";
commandDescriptions_["stranger"] = "show the list of strangers' IDs.";
commandDescriptions_["group"] = "show the list of groups.";
commandDescriptions_["group [ID]"] = "show the list of members of the group with ID [ID].";
commandDescriptions_["self"] = "show the information of self.";
commandDescriptions_["to [ID] [message]"] = "send [message] to the contact with ID [ID].";
commandDescriptions_["tofriends [message]"] = "send [message] to all friends.";
commandDescriptions_["tostrangers [message]"] = "send [message] to all strangers.";
commandDescriptions_["togroup [GroupID] [message]"] = "send [message] to the members of the group with ID [GroupID].";
commandDescriptions_["toall [message]"] = "send [message] to all.";
commandDescriptions_["+friend [ID] [hostname] [port]"] = "add the friend with ID [ID] and address [hostname]:[port]";
commandDescriptions_["-friend [ID]"] = "delete the friend with ID [ID].";
commandDescriptions_["+group [ID]"] = "add the group with ID [ID].";
commandDescriptions_["-group [ID]"] = "delete the group with ID [ID].";
commandDescriptions_["+member [GroupID] [MemberID]"] = "add a member with ID [MemberID] to the group with ID [GroupID].";
commandDescriptions_["-member [GroupID] [MemberID]"] = "delete a member with ID [MemberID] from the group with ID [GroupID].";
commandDescriptions_["friends [ID]"] = "show the information of the friend list of the friend with ID [ID].";
}
boost::shared_ptr<Command> Controller::createCommand(const string &commandName) {
Command *command;
// simple commands, just one string, no need for further parsing
if (commandName == "friend") command = newFriendCommand(model_, view_, is_, commandName);
elseif (commandName == "stranger") command = newStrangerCommand(model_, view_, is_, commandName);
elseif (commandName == "group") command = newGroupCommand(model_, view_, is_, commandName);
elseif (commandName == "self") command = newShowSelfCommand(model_, view_, is_, commandName);
// complex commands, need parsing
elseif (commandName.size() > 3 && commandName.substr(0, 2) == "to" && commandName[2] == '') {
istringstream iss(commandName);
string junk;
iss >> junk; // skip "to"
string id;
iss >> id;
string message;
iss >> ws; // consume all leading spaces
getline(iss, message); // all the rest is message
// check if id exists in the friend or stranger list
if (model_->hasFriend(id)) {
command = newToFriendCommand(model_, view_, is_, commandName, id, model_->getID(), message);
} elseif (model_->hasStranger(id)) {
command = newToStrangerCommand(model_, view_, is_, commandName, id, model_->getID(), message);
} else {
view_->presentLine("ID: " + id + " not found.");
command = newNullCommand(model_, view_, is_, commandName);
}
}
elseif (commandName.size() > 6 && commandName.substr(0, 5) == "group" && commandName[5] == '') {
istringstream iss(commandName);
string junk;
iss >> junk; // skip "group"
string id;
iss >> id;
// check if group id exists
if (model_->hasGroup(id)) {
command = newShowGroupCommand(model_, view_, is_, commandName, id);
} else {
view_->presentLine("Group ID: " + id + " not found.");
command = newNullCommand(model_, view_, is_, commandName);
}
}
elseif (commandName.size() > 10 && commandName.substr(0, 9) == "tofriends" && commandName[9] == '') {
istringstream iss(commandName);
string junk;
iss >> junk; // skip "tofriends"
string message;
iss >> ws;
getline(iss, message);
command = newToAllFriendsCommand(model_, view_, is_, commandName, model_->getID(), message);
}
elseif (commandName.size() > 12 && commandName.substr(0, 11) == "tostrangers" && commandName[11] == '') {
istringstream iss(commandName);
string junk;
iss >> junk; // skip "tostrangers"
string message;
iss >> ws;
getline(iss, message);
command = newToAllStrangersCommand(model_, view_, is_, commandName, model_->getID(), message);
}
elseif (commandName.size() > 8 && commandName.substr(0, 7) == "togroup" && commandName[7] == '') {
istringstream iss(commandName);
string junk;
iss >> junk; // skip "togroup"
string groupID, message;
iss >> groupID;
iss >> ws;
getline(iss, message);
// check if groupID exists
if (model_->hasGroup(groupID)) {
command = newToGroupCommand(model_, view_, is_, commandName, groupID, model_->getID(), message);
} else {
view_->presentLine("Group ID: " + groupID + " not found.");
command = newNullCommand(model_, view_, is_, commandName);
}
}
elseif (commandName.size() > 6 && commandName.substr(0, 5) == "toall" && commandName[5] == '') {
istringstream iss(commandName);
string junk;
iss >> junk; // skip "toall"
string message;
iss >> ws;
getline(iss, message);
command = newToAllCommand(model_, view_, is_, commandName, model_->getID(), message);
}
elseif (commandName.size() > 8 && (commandName.substr(0, 7) == "+friend" || commandName.substr(0, 7) == "-friend") && commandName[7] == '') {
istringstream iss(commandName);
string name;
iss >> name;
string id, hostname, port;
iss >> id >> hostname >> port;
if (name == "+friend") {
// check if id exists in the friend list
if (model_->hasFriend(id)) {
view_->presentLine("ID: " + id + " has been in the friend list.");
command = newNullCommand(model_, view_, is_, commandName);
} elseif (model_->getID() == id) {
view_->presentLine("ID: " + id + " is yourself.");
command = newNullCommand(model_, view_, is_, commandName);
} else {
command = newAddFriendCommand(model_, view_, is_, commandName, id, hostname, port);
}
} else { // name == "-friend"
// check if id exists in the friend list
if (model_->hasFriend(id)) {
command = newDeleteFriendCommand(model_, view_, is_, commandName, id);
} else {
view_->presentLine("ID: " + id + " not found.");
command = newNullCommand(model_, view_, is_, commandName);
}
}
}
elseif (commandName.size() > 7 && (commandName.substr(0, 6) == "+group" || commandName.substr(0, 6) == "-group") && commandName[6] == '') {
istringstream iss(commandName);
string name;
iss >> name;
string id;
iss >> id;
if (name == "+group") {
// check if id exists in the group list
if (model_->hasGroup(id)) {
view_->presentLine("Group ID: " + id + " has been in the group list.");
command = newNullCommand(model_, view_, is_, commandName);
} else {
command = newAddGroupCommand(model_, view_, is_, commandName, id);
}
} else { // name == "-group"
if (model_->hasGroup(id)) {
command = newDeleteGroupCommand(model_, view_, is_, commandName, id);
} else {
view_->presentLine("Group ID: " + id + " not found.");
command = newNullCommand(model_, view_, is_, commandName);
}
}
}
elseif (commandName.size() > 8 && (commandName.substr(0, 7) == "+member" || commandName.substr(0, 7) == "-member") && commandName[7] == '') {
istringstream iss(commandName);
string name, groupID, memberID;
iss >> name >> groupID >> memberID;
if (name == "+member") {
// check if group exists
if (model_->hasGroup(groupID)) {
// check if id exists in the group
if (model_->hasGroupMember(groupID, memberID)) {
view_->presentLine("ID: " + memberID + " has been in the group with ID: " + groupID + ".");
command = newNullCommand(model_, view_, is_, commandName);
} else {
// check if id exists in the friend list
if (model_->hasFriend(memberID)) {
command = newAddGroupMemberCommand(model_, view_, is_, commandName, groupID, memberID);
} else {
view_->presentLine("ID: " + memberID + " not found.");
command = newNullCommand(model_, view_, is_, commandName);
}
}
} else {
view_->presentLine("Group ID: " + groupID + " not found.");
command = newNullCommand(model_, view_, is_, commandName);
}
} else { // name == "-member"
// check if group exists
if (model_->hasGroup(groupID)) {
// check if id exists in the group
if (model_->hasGroupMember(groupID, memberID)) {
command = newDeleteGroupMemberCommand(model_, view_, is_, commandName, groupID, memberID);
} else {
view_->presentLine("ID: " + memberID + " is not found in the group with ID: " + groupID + ".");
command = newNullCommand(model_, view_, is_, commandName);
}
} else {
view_->presentLine("Group ID: " + groupID + " not found.");
command = newNullCommand(model_, view_, is_, commandName);
}
}
}
elseif (commandName.size() > 8 && commandName.substr(0, 7) == "friends" && commandName[7] == '') {
istringstream iss(commandName);
string junk;
iss >> junk; // skip "friends"
string id;
iss >> id;
// check if id exists in the friend list
if (model_->hasFriend(id)) {
command = newExtractFriendFriendsCommand(model_, view_, is_, commandName, id);
} else {
view_->presentLine("ID: " + id + " is not a friend.");
command = newNullCommand(model_, view_, is_, commandName);
}
}
// invalid commandName
else {
view_->presentLine("Command: " + commandName + " not found or not understandable.");
command = newNullCommand(model_, view_, is_, commandName);
}
return boost::shared_ptr<Command>(command);
}
}