Repository navigation
Expand file tree
/
Copy pathCommandParser.cpp
More file actions
288 lines (245 loc) · 8.79 KB
/
Copy pathCommandParser.cpp
File metadata and controls
288 lines (245 loc) · 8.79 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
#include "CommandParser.h"
#include "Frame.h"
#include "UserData.h"
#include "ConnectionHandler.h"
#include "ConnectFrame.h"
#include "DisconnectFrame.h"
#include "SubscribeFrame.h"
#include "UnsubscribeFrame.h"
#include "event.h"
#include "SendFrame.h"
#include <fstream>
#include <sstream>
#include <unistd.h>
#include <iostream>
#include <ios>
#include "UserData.h"
vector<Frame*> CommandParser::parseCommand(string commandToParse)
{
if(commandToParse == "") {
throw ios_base::failure("");
}
istringstream pa(commandToParse);
string parameter;
string command;
// get the command
getline(pa, command, ' ');
if(command != "login" && command != "logout" && command != "join" &&
command != "exit" && command != "report" && command != "summary") {
cerr << "~ Unknown command" << endl;
cerr.flush();
throw ios_base::failure("");
}
// get the parameters
vector<string> commandParameters;
while(getline(pa, parameter, ' ')){
commandParameters.push_back(parameter);
}
// parse the command
if(command == "login"){
return parseLoginCommand(commandParameters);
}
if (UserData::getInstance().isConnected()==false) {
cerr << "~ You must login first" << endl;
cerr << "~ Usage: login {host:port} {username} {password}" << endl;
throw ios_base::failure("");
}
else if (command == "logout"){
return parseLogoutCommand();
}
else if (command == "join"){
return parseJoinCommand(commandParameters);
}
else if (command == "exit"){
return parseExitCommand(commandParameters);
}
else if (command == "report"){
return parseReportCommand(commandParameters);
}
else if (command == "summary"){
parseSummaryCommand(commandParameters);
}
throw ios_base::failure("");
}
vector<Frame*> CommandParser::parseLoginCommand(vector<string>& commandParameters)
{
vector<Frame*> output;
UserData& ud = UserData::getInstance();
if(ud.isConnected()){
cerr<< "~ You are already logged in as \" "+ud.getUserName() +" \"" <<endl;
throw ios_base::failure("");
}
else if(commandParameters.size() != 3){
cerr << "~ Invalid number of parameters" << endl;
cerr << "~ Usage: login {host:port} {username} {password}" << endl;
throw ios_base::failure("");
}
else {
string hostPort = commandParameters[0];
string host = hostPort.substr(0, hostPort.find(':'));
string _port = hostPort.substr(hostPort.find(':') + 1);
for (char c : _port) { // check if port is a number
if (!isdigit(c)) {
cerr << "~ Invalid port, port must be a number" << endl;
cerr << "~ Usage: login {host:port} {username} {password}" << endl;
throw ios_base::failure("");
}
}
int port = stoi(_port);
ConnectionHandler* connectionHandler = new ConnectionHandler(host, port);
string username = commandParameters[1];
string password = commandParameters[2];
ud.setHandler(*connectionHandler);
ud.setUserName(username);
Frame* frame = ConnectFrame::get(host, username, password);
output.push_back(frame);
}
return output;
}
std::vector<std::string> CommandParser::split(std::string str, char delimiter) {
std::vector<std::string> tokens;
std::string token;
size_t start = 0;
size_t end = 0;
while ((end = str.find(delimiter, start)) != std::string::npos) {
token = str.substr(start, end - start);
tokens.push_back(token);
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}
vector<Frame*> CommandParser::parseLogoutCommand() {
vector<Frame*> output;
DisconnectFrame * frame = DisconnectFrame::get();
output.push_back(frame);
return output;
}
vector<Frame*> CommandParser::parseJoinCommand(vector<string>& commandParameters) {
vector<Frame*> output;
if(commandParameters.size() != 1){
cerr << "~ Invalid number of parameters" << endl;
cerr << "~ Usage: join {game_name}" << endl;
}
else{
string gameName = commandParameters[0];
SubscribeFrame* frame = SubscribeFrame::get(gameName);
output.push_back(frame);
}
return output;
}
vector<Frame*> CommandParser::parseExitCommand(vector<string>& commandParameters) {
vector<Frame*> output;
if(commandParameters.size() != 1){
cerr << "~ Invalid number of parameters" << endl;
cerr << "~ Usage: exit {game_name}" << endl;
}
else {
string gameName = commandParameters[0];
UnsubscribeFrame* frame = UnsubscribeFrame::get(gameName);
output.push_back(frame);
}
return output;
}
vector<Frame*> CommandParser::parseReportCommand(vector<string>& commandParameters) {
vector<Frame*> output;
if(commandParameters.size() != 1){
cerr << "~ Invalid number of parameters" << endl;
cerr << "~ Usage: report {file}" << endl;
}
else{
string fileName = commandParameters[0];
if(fileName.find(".json") != string::npos || fileName.find('/') != string::npos || fileName.find("..") != string::npos){
cerr << "~ report error: output file name cannot contain file extension or path to folder, e.g '.json' , '/', '..'" << endl;
}
else{
char cwd[1024];
getcwd(cwd, sizeof(cwd));
string path(cwd, sizeof(cwd));
if(path.find("/SPL_Assignment3/") == string::npos){
cerr<< "~ report error: the client program directory needs to be in the 'SPL_Assignment3' directory, but it is not. "<<endl;
cerr<< "~ Would you like to insert the full path instead? y/n"<<endl;
string answer;
getline(cin,answer);
if(answer == "y"){
cout<<"~ Insert full path:"<<endl;
getline(cin,path);
}
else if(answer == "n"){
cout<<"~ canceling"<<endl;
return output;
}
else {
cout<<"~ invalid answer, canceling"<<endl;
return output;
}
}
else{
path = path.substr(0, path.find("/SPL_Assignment3/")+17) + "client/data/" + fileName + ".json";
}
try {
names_and_events namesAndEvents = parseEventsFile(path);
vector<Event>& gameEvents = namesAndEvents.events;
for (Event& event : gameEvents) {
SendFrame* sendFrame = SendFrame::get(event);
output.push_back(sendFrame);
}
}
catch (const std::exception& e) {
cerr << "~ report error: file probably does not exist in the data directory. full error details:\n "<<e.what() << endl;
}
}
}
return output;
}
void CommandParser::parseSummaryCommand(vector<string>& commandParameters) {
if(commandParameters.size() != 3){
cerr << "~ Invalid number of parameters" << endl;
cerr << "~ Usage: summary {game_name} {user} {output file name}" << endl;
return;
}
string gameName = commandParameters[0];
string userName = commandParameters[1];
string fileName = commandParameters[2];
if(fileName.find(".json") != string::npos || fileName.find('/') != string::npos || fileName.find("..") != string::npos){
cerr << "~ summary error: output file name cannot contain file extension or path to folder, e.g '.json' , '/', '..'" << endl;
return;
}
char cwd[1024];
getcwd(cwd, sizeof(cwd));
string path(cwd, sizeof(cwd));
if(path.find("/SPL_Assignment3/") == string::npos){
cerr<< "~ summary error: the client program directory needs to be in the 'SPL_Assignment3' directory, but it is not. "<<endl;
cerr<< "~ Would you like to insert the full path instead? y/n"<<endl;
string answer;
getline(cin,answer);
if(answer == "y"){
cout<<"~ Insert full path:"<<endl;
getline(cin,path);
}
else if(answer == "n"){
cout<<"~ canceling"<<endl;
return;
}
else {
cout<<"~ invalid answer, canceling"<<endl;
return;
}
}
else{
path = path.substr(0, path.find("/SPL_Assignment3/")+17) + "client/data/" + fileName + ".json";
}
UserData & userData = UserData::getInstance();
string summaryString;
try{
summaryString = userData.getSummary(userName, gameName);
cout << summaryString << endl;
}catch(ios_base::failure& e){
cerr << "~ summary error: " << e.what() << endl;
return;
}
ofstream summaryFile;
summaryFile.open(path);
summaryFile << summaryString << endl;
summaryFile.close();
}