-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrolconnection.cpp
More file actions
302 lines (239 loc) · 12.1 KB
/
controlconnection.cpp
File metadata and controls
302 lines (239 loc) · 12.1 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
#include "controlconnection.h"
ControlConnection::ControlConnection() : BaseConnection(CONTROLPORT){}
void ControlConnection::start(){
while(true){
try{
//accepting client connection
int clientSocket = accept(ConnectionServerSocket, nullptr, nullptr);
if(clientSocket > 0 ){
this->notify(CONTROL, Log(LOG, std::to_string(clientSocket) + ": client has connected "));
}
else{
this->notify(CONTROL, Log(ERROR, std::to_string(clientSocket) + ": client could not conect"));
}
std::thread t([this](int socket) {
this->notify(CONTROL, Log(LOG, "Thread started for socket: " + std::to_string(socket)));
try{
while(true){
//receive Data from Client
char buffer[1024] = {0};
int bytes = recv(socket, buffer, sizeof(buffer), 0);
if(bytes == 0){
this->notify(CONTROL, Log(LOG, std::to_string(socket) + " disconnected gracefully"));
close(socket);
break;
}
else if(bytes < 0){
this->notify(CONTROL, Log(ERROR,std::to_string(socket) + " recv error: " + strerror(errno)));
close(socket);
break;
}
//convert char[] to string
std::string msg(buffer);
this->notify(CONTROL, Log(LOG, std::to_string(socket) + ": sent a message: " + msg.substr(0,msg.length()-1)));
//q to quit
if(msg == "q\n"|| msg == "q\r\n"){
this->notify(CONTROL, Log(LOG, std::to_string(socket) + " is Disconecting"));
std::cout << "Disconnecting the client"<< std::endl;
break;
}
//up to upload
else if(msg == "up\n"|| msg == "up\r\n"){
char resp[3]= "OK";
this->notify(CONTROL, Log(LOG,std::string(resp) + " response sent to client: " + std::to_string(socket)));
if(send(socket,resp ,sizeof(resp),0) < 0){
this->notify(CONTROL, Log(ERROR, "COULD NOT SEND OK RESPONSE TO CLIENT: " + std::to_string(socket)));
throw std::runtime_error("CONTROL SERVER: COULD NOT ACCEPT FILE");
};
}
//list files to download
else if(msg == "ls\n" || msg == "ls\r\n"){
std::string files;
{// lock and do what you have to
std::lock_guard<std::mutex> lck(mtx);
update();
files = getListOfFilesUnsafe();
}
this->notify(CONTROL, Log(LOG, "LOCK ACQUIRED for file list"));
this->notify(CONTROL, Log(LOG,std::to_string(socket) + " - CLIENT REQUESTED LIST OF COMMANDS"));
if(files == ""){
files = "No files on the server.\n";
}
if(send(socket, files.c_str(), files.size(), 0) < 0){
this->notify(CONTROL, Log(ERROR,"COULD NOT SEND THE LIST TO THE CLIENT"));
close(socket);
throw std::runtime_error("COULD NOT SEND THE LIST TO THE CLIENT");
}
}
// files to download
else if(msg == "down\n"|| msg == "down\r\n"){
//inform client about available files
this->notify(CONTROL, Log(LOG,std::to_string(socket) + " - PHASE 1: CLIENT REQUESTED FILE"));
std::string templateString;
//catch no files
if(getNumFiles() == 0){
this->notify(CONTROL,Log(LOG,"no files available for transfer"));
std::string errResp = "FAIL\n";
if(send(socket,errResp.c_str(),errResp.size(),0) < 0){
this->notify(CONTROL,Log(ERROR,"could not send FAIL response"));
}
continue;
}
this->notify(CONTROL, Log(LOG, "LOCK ACQUIRED for file list"));
{
std::lock_guard<std::mutex> lck(mtx);
update();
templateString = "\nAvailable Files:\n";
templateString += getListOfFilesUnsafe();
}
templateString+="\n";
templateString+="enter name of the file that you want (case sensitive)\n";
this->notify(CONTROL, Log(LOG,std::to_string(socket) + " - PHASE 1: CLIENT REQUESTED FILE"));
//send data to client
if(send(socket, templateString.c_str(), templateString.size(), 0) < 0){
this->notify(CONTROL, Log(ERROR,std::to_string(socket) + " - PHASE 1 FAILED"));
this->notify(CONTROL, Log(ERROR,std::to_string(socket) + " - PHASE 1: CLIENT DID NOT RECEIVE LIST OF FILES "));
close(socket);
throw std::runtime_error("CONTROL CONNECTION: COULD NOT SEND LIST OF FILES TO CLIENT");
}
//receive file to download
char fileToDownload[1024] = {0};
int bytes = recv(socket, fileToDownload, sizeof(fileToDownload), 0);
std::string ftd (fileToDownload);
this->notify(CONTROL, Log(LOG,std::to_string(socket) + " - PHASE 1 SUCCESS"));
this->notify(CONTROL, Log(LOG,std::to_string(socket) + " - PHASE 2: CLIENT REQUESTED FILE \"" + ftd.substr(0,ftd.length()-1) +"\"" ));
if(bytes == 0){
this->notify(CONTROL, Log(LOG, std::to_string(socket) + " disconnected gracefully"));
close(socket);
break;
}
else if(bytes < 0){
this->notify(CONTROL, Log(ERROR,std::to_string(socket) + " recv error: " + strerror(errno)));
close(socket);
break;
}
//convert data received from char arr to string
std::string tryDownlaod(fileToDownload);
//extract until \n
tryDownlaod = tryDownlaod.substr(0, tryDownlaod.find('\n'));
//find file
if(!fileExists(tryDownlaod)){
this->notify(CONTROL, Log(ERROR,std::to_string(socket) + " - PHASE 2 FAILED"));
this->notify(CONTROL, Log(ERROR, tryDownlaod + "DOES NOT EXIST"));
//send the error message to the client
std::string errResp = to_red(tryDownlaod + " is a file that does not exist. \n PLEASE TRY AGAIN:");
send(socket, errResp.c_str(), errResp.size(), 0);
//tell ConnectionServer that a file that doesn't exist is attempting to be downloaded
std::cout << tryDownlaod << " does not exist on the Connection Server\n";
continue;
}
this->notify(CONTROL, Log(LOG, std::to_string(socket) + " - PHASE 2 SUCCESS"));
std::string INITIATE = "8081:"+ tryDownlaod;// not necessary
this->notify(CONTROL, Log(LOG, std::to_string(socket) + " - PHASE 3: FILE TRANSFERRING"));
if(send(socket, INITIATE.c_str(), INITIATE.size(), 0) < 0){
close(socket);
this->notify(CONTROL, Log(ERROR, std::to_string(socket) + " - PHASE 3: FAILED"));
throw std::runtime_error("CONNECTION SERVER: COULD NOT SEND REQUIREMENTS");
}
this->notify(CONTROL, Log(LOG, std::to_string(socket) + " - PHASE 3: SUCCESS"));
}
//list all available commands in FTPCMDS.txt
else if(msg == "cmds\n"||msg == "cmds\r\n"){
this->notify(CONTROL, Log(LOG, std::to_string(socket) + " - CLIENT REQUESTED AVAILABLE COMMANDS"));
std::string blankInput = "=================================\n";
blankInput +="|\t";
blankInput += "LIST OF COMMANDS\t|";
blankInput += "\r\n=================================\r\n";
blankInput+="\n";
blankInput+= getavailableCommands();
if(send(socket, blankInput.c_str(), blankInput.size(), 0) < 0){
this->notify(CONTROL, Log(ERROR, std::to_string(socket) + " - COULD NOT SEND LIST AVAILABLE COMMANDS TO CLIENT"));
close(socket);
throw std::runtime_error("CONNECTION SERVER: COULD NOT SEND REQUIREMENTS");
}
}
else {
this->notify(CONTROL, Log(ERROR, std::to_string(socket) + " sent UNKNOWN command: " + msg));
}
}
this->notify(CONTROL, Log(LOG, "Thread ended for socket: " + std::to_string(socket)));
close(socket);
}
catch(std::exception& e){
std::cout << e.what() << std::endl;
}
}, clientSocket);
t.detach();
}
catch(const std::exception& x){
std::cout << x.what();
}
}
}
bool ControlConnection::fileExists(std::string x){
this->notify(CONTROL, Log(LOG, "LOCK ACQUIRED for file list"));
this->notify(CONTROL, Log(LOG,"FILE SEARCH INITIATED"));
std::lock_guard<std::mutex> lck(mtx);
for(const std::string& y : existingFiles){
if(y == x){
std::cout << "FILE HAS BEEN FOUND HENCE TRANSFER IS INITIATING " << std::endl;
return true;
}
}
std::cout << "FILE CANNOT BE FOUND HENCE NO TRANSFER" << std::endl;
return false;
}
std::string ControlConnection::getavailableCommands(){
std::lock_guard<std::mutex> lck(mtx);
const char* toRead = "FTPCMDS.txt";
std::ifstream fileToRead(toRead);
std::string temp;
std::string toRet = "";
while(getline(fileToRead, temp)){
toRet+=temp;
toRet+= "\n";
}
return toRet;
}
std::string ControlConnection::getListOfFiles(){
this->notify(CONTROL, Log(LOG, "LOCK ACQUIRED for file list"));
std::lock_guard<std::mutex> lck(mtx);
std::string finalListOfFiles = "";
for(const std::string& x: existingFiles){
finalListOfFiles += x + "\n";
}
return finalListOfFiles;
}
void ControlConnection::update(){
existingFiles.clear();
std::string fileDirectory = "files/";
try{
for(const auto& e : std::filesystem::directory_iterator(fileDirectory)){
std::filesystem::path out = e.path();
std::string outfilename_str = out.string();
const char* path = outfilename_str.c_str();
if (stat(path, &sb) != 0) {
continue;
}
if (stat(path, &sb) == 0 && !(sb.st_mode & S_IFDIR)){
std::string p(path);
existingFiles.push_back(p.substr(6));
}
}
}
catch(const std::exception& e){
std::cerr << "Directory error: " << e.what() << std::endl;
}
}
int ControlConnection::getNumFiles(){
this->notify(CONTROL, Log(LOG, "LOCK ACQUIRED for Number of Files list"));
std::lock_guard<std::mutex> lck(mtx);
return existingFiles.size();
}
std::string ControlConnection::getListOfFilesUnsafe(){
std::string finalListOfFiles = "";
for(const std::string& x: existingFiles){
finalListOfFiles += x + "\n";
}
return finalListOfFiles;
}