-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.hpp
More file actions
454 lines (399 loc) · 13.6 KB
/
command.hpp
File metadata and controls
454 lines (399 loc) · 13.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* command.hpp
*
* Created on: Dec 6, 2010
* Author: Fei Huang
* Email: felix.fei.huang@yale.edu
*/
#pragma once
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include "view.hpp"
#include "basicchatclient.hpp"
#include "chatprotocol.hpp"
using namespace std;
namespace openchat {
/**
* Command encapsulates the operation on the chat client, using Command Pattern
*/
class Command {
public:
Command(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name)
: model_(model), view_(view), is_(is), name_(name) { }
virtual ~Command() { }
string getName() const { return name_; }
// present information on View before executing the command
virtual void showBeforeExecution() const = 0;
// actually execute the command
virtual void execute() = 0;
// reverse the effect of the command
virtual void undo() {
view_->presentLine("Command: " + name_ + " is removed from command history.");
}
// present information on View after executing the command
virtual void showAfterExecution() const = 0;
protected:
boost::shared_ptr<BasicChatClient> model_;
boost::shared_ptr<View> view_;
istream &is_;
string name_;
};
/**
* Null command is a special command which does nothing, as a placeholder, using Null Pattern
*/
class NullCommand : public Command {
public:
NullCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name)
: Command(model, view, is, name) { }
virtual void showBeforeExecution() const { }
virtual void execute() { }
virtual void undo() { }
virtual void showAfterExecution() const { }
};
// Concrete commands
/**
* FriendCommand shows the list of friends
*/
class FriendCommand : public Command {
public:
FriendCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name)
: Command(model, view, is, name) { }
virtual void showBeforeExecution() const {
vector<string> friendIDs = model_->getFriendsIDs();
for (size_t i = 0; i < friendIDs.size(); ++i) {
view_->presentLine(friendIDs[i]);
}
}
virtual void execute() { }
virtual void showAfterExecution() const { }
};
/**
* StrangerCommand shows the list of strangers
*/
class StrangerCommand : public Command {
public:
StrangerCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name)
: Command(model, view, is, name) { }
virtual void showBeforeExecution() const {
vector<string> strangerIDs = model_->getStrangersIDs();
for (size_t i = 0; i < strangerIDs.size(); ++i) {
view_->presentLine(strangerIDs[i]);
}
}
virtual void execute() { }
virtual void showAfterExecution() const { }
};
/**
* GroupCommand shows the list of groups
*/
class GroupCommand : public Command {
public:
GroupCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name)
: Command(model, view, is, name) { }
virtual void showBeforeExecution() const {
vector<string> groupsIDs = model_->getGroupsIDs();
for (size_t i = 0; i < groupsIDs.size(); ++i) {
view_->presentLine(groupsIDs[i]);
}
}
virtual void execute() { }
virtual void showAfterExecution() const { }
};
/**
* ShowGroupCommand shows the list of members of a group
*/
class ShowGroupCommand : public Command {
public:
ShowGroupCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &id)
: Command(model, view, is, name), id_(id) { }
virtual void showBeforeExecution() const { }
virtual void execute() { }
virtual void showAfterExecution() const {
vector<string> memberIDs = model_->getGroupMemberIDs(id_);
for (size_t i = 0; i < memberIDs.size(); ++i) {
view_->presentLine(memberIDs[i]);
}
}
private:
string id_;
};
/**
* ShowSelfCommand shows the information about self
*/
class ShowSelfCommand : public Command {
public:
ShowSelfCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name)
: Command(model, view, is, name) { }
virtual void showBeforeExecution() const { }
virtual void execute() { }
virtual void showAfterExecution() const {
ostringstream port;
port << model_->getPort();
view_->presentLine("[ID=" + model_->getID() + "], [ip=" + model_->getHostname() + "], [port=" + port.str() + "]");
}
};
/**
* ToCommand sends a message to a contact
*/
class ToCommand : public Command {
public:
ToCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &toID, const string &fromID, const string &message)
: Command(model, view, is, name), toID_(toID), fromID_(fromID), message_(message) { }
protected:
string toID_;
string fromID_;
string message_;
};
/**
* ToFriendCommand sends a message to a friend
*/
class ToFriendCommand : public ToCommand {
public:
ToFriendCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &toID, const string &fromID, const string &message)
: ToCommand(model, view, is, name, toID, fromID, message) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
model_->sendToFriend(toID_, ChatProtocol::wrapPlainChatMessage(fromID_, message_));
}
virtual void showAfterExecution() const { }
};
/**
* ToStrangerCommand sends a message to a stranger
*/
class ToStrangerCommand : public ToCommand {
public:
ToStrangerCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &toID, const string &fromID, const string &message)
: ToCommand(model, view, is, name, toID, fromID, message) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
model_->sendToStranger(toID_, ChatProtocol::wrapPlainChatMessage(fromID_, message_));
}
virtual void showAfterExecution() const { }
};
/**
* ToAllFriendsCommand sends a message to all friends
*/
class ToAllFriendsCommand : public ToCommand {
public:
ToAllFriendsCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &fromID, const string &message)
: ToCommand(model, view, is, name, "", fromID, message) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
model_->sendToAllFriends(ChatProtocol::wrapPlainChatMessage(fromID_, message_));
}
virtual void showAfterExecution() const { }
};
/**
* ToAllStrangersCommand sends a message to all strangers
*/
class ToAllStrangersCommand : public ToCommand {
public:
ToAllStrangersCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &fromID, const string &message)
: ToCommand(model, view, is, name, "", fromID, message) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
model_->sendToAllStrangers(ChatProtocol::wrapPlainChatMessage(fromID_, message_));
}
virtual void showAfterExecution() const { }
};
/**
* ToGroupCommand sends a message to a group
*/
class ToGroupCommand : public ToCommand {
public:
ToGroupCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &groupID, const string &fromID, const string &message)
: ToCommand(model, view, is, name, groupID, fromID, message) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
model_->sendToGroup(toID_, ChatProtocol::wrapPlainChatMessage(fromID_, message_));
}
virtual void showAfterExecution() const { }
};
/**
* ToAllCommand sends a message to all
*/
class ToAllCommand : public ToCommand {
public:
ToAllCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &fromID, const string &message)
: ToCommand(model, view, is, name, "", fromID, message) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
string message = ChatProtocol::wrapPlainChatMessage(fromID_, message_);
model_->sendToAllFriends(message);
model_->sendToAllStrangers(message);
}
virtual void showAfterExecution() const { }
};
/**
* AddFriendCommand adds and connects a friend
*/
class AddFriendCommand : public Command {
public:
AddFriendCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &id, const string &hostname, const string &port)
: Command(model, view, is, name), id_(id), hostname_(hostname), port_(port) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
model_->addFriend(id_, hostname_, port_);
}
virtual void undo() {
model_->deleteFriend(id_);
view_->presentLine("Friend: " + id_ + " has been deleted.");
}
virtual void showAfterExecution() const {
view_->presentLine("Friend: " + id_ + " has been added.");
}
private:
string id_;
string hostname_;
string port_;
};
/**
* DeleteFriendCommand deletes a friend
*/
class DeleteFriendCommand : public Command {
public:
DeleteFriendCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &id)
: Command(model, view, is, name), id_(id) {
pair<string, string> hostnameAndPort = model_->getFriendHostnameAndPort(id_);
hostname_ = hostnameAndPort.first;
port_ = hostnameAndPort.second;
}
virtual void showBeforeExecution() const { }
virtual void execute() {
model_->deleteFriend(id_);
}
virtual void undo() {
model_->addFriend(id_, hostname_, port_);
view_->presentLine("Friend: " + id_ + " has been added.");
}
virtual void showAfterExecution() const {
view_->presentLine("Friend: " + id_ + " has been deleted.");
}
private:
string id_;
string hostname_;
string port_;
};
/**
* AddGroupCommand adds a group
*/
class AddGroupCommand : public Command {
public:
AddGroupCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &id)
: Command(model, view, is, name), id_(id) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
model_->addGroup(id_);
}
virtual void undo() {
model_->deleteGroup(id_);
view_->presentLine("Group: " + id_ + " has been deleted.");
}
virtual void showAfterExecution() const {
view_->presentLine("Group: " + id_ + " has been added.");
}
private:
string id_;
};
/**
* DeleteGroupCommand deletes a group
*/
class DeleteGroupCommand : public Command {
public:
DeleteGroupCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &id)
: Command(model, view, is, name), id_(id) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
members_ = model_->getGroupMemberIDs(id_);
model_->deleteGroup(id_);
}
virtual void undo() {
model_->addGroup(id_);
for (size_t i = 0; i < members_.size(); ++i) {
model_->addGroupMember(id_, members_[i]);
}
view_->presentLine("Group: " + id_ + " together with its members has been added back.");
}
virtual void showAfterExecution() const {
view_->presentLine("Group: " + id_ + " has been deleted.");
}
private:
string id_;
vector<string> members_; // store member IDs for undoing
};
/**
* AddGroupMemberCommand adds a member to a group
*/
class AddGroupMemberCommand : public Command {
public:
AddGroupMemberCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &groupID, const string &memberID)
: Command(model, view, is, name), groupID_(groupID), memberID_(memberID) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
model_->addGroupMember(groupID_, memberID_);
}
virtual void undo() {
model_->deleteGroupMember(groupID_, memberID_);
view_->presentLine("ID: " + memberID_ + " has been deleted from the group with ID: " + groupID_ + ".");
}
virtual void showAfterExecution() const {
view_->presentLine("ID: " + memberID_ + " has been added to the group with ID: " + groupID_ + ".");
}
private:
string groupID_;
string memberID_;
};
/**
* DeleteGroupMemberCommand deletes a member from a group
*/
class DeleteGroupMemberCommand : public Command {
public:
DeleteGroupMemberCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &groupID, const string &memberID)
: Command(model, view, is, name), groupID_(groupID), memberID_(memberID) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
model_->deleteGroupMember(groupID_, memberID_);
}
virtual void undo() {
model_->addGroupMember(groupID_, memberID_);
view_->presentLine("ID: " + memberID_ + " has been added back to the group with ID: " + groupID_ + ".");
}
virtual void showAfterExecution() const {
view_->presentLine("ID: " + memberID_ + " has been deleted from the group with ID: " + groupID_ + ".");
}
private:
string groupID_;
string memberID_;
};
/**
* ExtractFriendFriendsCommand extracts the friend list information of a friend
*/
class ExtractFriendFriendsCommand : public Command {
public:
ExtractFriendFriendsCommand(boost::shared_ptr<BasicChatClient> model, boost::shared_ptr<View> view, istream &is, const string &name,
const string &id)
: Command(model, view, is, name), id_(id) { }
virtual void showBeforeExecution() const { }
virtual void execute() {
model_->sendToFriend(id_, ChatProtocol::wrapFriendListExtractionMessage(id_));
}
virtual void showAfterExecution() const { }
private:
string id_;
};
}