-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.cpp
511 lines (421 loc) · 14.9 KB
/
tracker.cpp
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include<bits/stdc++.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<pthread.h>
#include <experimental/filesystem>
using namespace std;
#define BUFFER_SIZE 1024
unordered_map<string, string> users;
unordered_map<string, set<string>> groups;
unordered_map<string, string> admin;
unordered_map<string, set<string>> groupReqs;
unordered_map<string, unordered_map<string, set<pair<string,string>>>> files;
unordered_map<string, string> namePathMap;
unordered_map<string, long long> nameSizeMap;
map<pair<string, string>, bool> onlineStatus;
string TRACKER_IP;
int TRACKER_PORT;
void error(string s){
cout<<s<<endl<<endl;
exit(1);
}
vector<string> split_args(string str){
vector<string> v;
stringstream ss(str);
while (ss.good()) {
string sb;
getline(ss, sb, ':');
v.push_back(sb);
}
return v;
}
string createUser(string userId, string password){
if(users.find(userId) != users.end()){
return ("User already exists.");
}
else{
users[userId] = password;
return ("Registered Successfully.");
}
}
string login(string userId, string password, string iport){
if(users.find(userId) != users.end()){
if(users[userId] == password){
pair<string, string> p = {userId, iport};
onlineStatus[p] = true;
return ("Login Successfull.");
}
else{
return ("Please enter correct password.");
}
}
else return ("User Doesn't exist.");
}
string createGroup(string groupId, string userId){
if(groups.find(groupId) != groups.end()){
return("Group already exist.");
}
else{
groups[groupId].insert(userId);
admin[groupId] = userId;
return("Group created sucessfully.");
}
}
string joinGroup(string groupId, string userId){
if(groups.find(groupId) != groups.end()){
if(groups[groupId].find(userId) != groups[groupId].end())
return ("You're already a member of this group.");
else if(groupReqs[groupId].find(userId) != groupReqs[groupId].end())
return ("Your joining request is pending.");
else{
//groups[groupId].insert(userId);
groupReqs[groupId].insert(userId);
return("Group joining request Sent.");
}
}
else return("Group Doesn't exist.");
}
string leaveGroup(string groupId, string userId){
if(groups.find(groupId) != groups.end()){
if(groups[groupId].find(userId) != groups[groupId].end()){
groups[groupId].erase(userId);
return ("You've left the group.");
}
return("You're not present in this group.");
}
else return("Group Doesn't exist.");
}
string logout(string userId, string iport){
if(users.find(userId) != users.end()){
pair<string, string> p = {userId, iport};
onlineStatus[p] = false;
return ("Logout Successfull.");
}
else return ("User Doesn't exist.");
}
string showRequests(string groupId, string userId){
if(groups.find(groupId) != groups.end()){
if(admin[groupId] != userId){
return("Permission Denied! You're not an admin of this group.");
}
else{
if(groupReqs[groupId].empty()) return("No pending requests.");
string s = "";
for (auto uid = groupReqs[groupId].begin(); uid != groupReqs[groupId].end(); ++uid)
{
s += "<" + *uid + ">" + "|";
}
string resp = s.substr(0, s.length()-1);
return resp;
}
}
else return("Group Doesn't exist.");
}
string acceptRequests(string groupId, string userId, string loggedIn){
if(groups.find(groupId) != groups.end()){
//cout<<"###########################"<<endl;
if(admin[groupId] != loggedIn){
return("Permission Denied! You're not an admin of this group.");
//cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"<<endl;
}
else{
//cout<<"****************UHUH****************"<<endl;
if(groupReqs[groupId].find(userId) != groupReqs[groupId].end()){
//cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
groups[groupId].insert(userId);
groupReqs[groupId].erase(userId);
return("Joining request accepted for " + userId);
}
else{
//cout<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
return("No request found for " + userId);
}
}
}
else return("Group Doesn't exist.");
}
string listGroups(){
if(groups.empty()) return("No groups present in this Network.");
string s = "";
for(auto grp : groups){
string fs = grp.first.substr(0, grp.first.length()-1);
s += "<" + fs + ">" + "|";
//cout<<"groups : "<<endl;cout<<grp.first<<grp.first.length()<<endl;
}
/*for(auto adm : admin){
cout<<"admin : "<<endl;cout<<adm.first<<adm.first.length()<<endl;
}
for(auto us : users){
cout<<"users : "<<endl;cout<<us.first<<us.first.length()<<endl;
}*/
string resp = s.substr(0, s.length()-1);
return resp;
}
bool containsFile(string filePath, string groupId){
if(files.find(groupId) != files.end()){
if(files[groupId].find(filePath) != files[groupId].end()){
return true;
}
}
return false;
}
bool isMember(string groupId, string userId){
if(groups[groupId].find(userId) != groups[groupId].end())
return true;
return false;
}
bool isFileExist(string path)
{
std::ifstream infile(path.c_str());
return infile.good();
}
string getFileName(string path){
int i;
for(i=path.length()-1; i>=0; i--){
if(path[i] == '/') break;
}
if(i<path.length()-1){
string fileName = path.substr(i+1, path.length());
namePathMap[fileName] = path;
return fileName;
}
else return "Invalid";
}
void saveFileSize(string path, string fname){
ifstream f(path,ios::ate|ios::binary);
long long size = f.tellg(); f.close();
nameSizeMap[fname] = size;
}
string uploadFile(string filePath, string groupId, string userId, string iport){
if(groups.find(groupId) != groups.end()){
if(!isMember(groupId, userId)) return("You're not a member of this group.");
if(!isFileExist(filePath)) return ("You've provided incorrect file path. Please provide correct path.");
string gid = groupId.substr(0, groupId.length()-1);
string fileName = getFileName(filePath);
if(fileName == "Invalid") return ("Please provide absolute path.");
saveFileSize(filePath, fileName);
if(containsFile(fileName, groupId)){
if(files[groupId][fileName].find({userId, iport}) != files[groupId][fileName].end()){
return ("You're already present in seeder list.");
}
else{
files[groupId][fileName].insert({userId, iport});
string resp = "One more seeder added for file " + fileName + " in " + gid + ".";
return(resp);
}
}
else{
files[groupId][fileName].insert({userId, iport});
string resp = "New file uploaded successfully in " + gid + ".";
return(resp);
}
}
else return("Group Doesn't exist.");
}
bool isSeederExist(string groupId, string fileName){
for(auto seeder : files[groupId][fileName]){
if(onlineStatus[{seeder.first, seeder.second}]==true) return true;
}
return false;
}
string listFiles(string groupId, string userId){
if(groups.find(groupId) != groups.end()){
if(!isMember(groupId, userId)) return("You're not a member of this group.");
string gid = groupId.substr(0, groupId.length()-1);
if(files.find(groupId) == files.end()) return ("No files present in " + gid);
string s = "";
for(auto file : files[groupId]){
if(!isSeederExist(groupId, file.first)) continue;
string fs = file.first;
s += "<" + fs + ">" + "|";
}
if(s == "") return "Currently all seeders are offline.";
string resp = s.substr(0, s.length()-1);
return resp;
}
else return("Group Doesn't exist.");
}
string seederList(string userId, string groupId, string fileName){
if(groups.find(groupId)==groups.end()) return "This group doesn't exist.";
if(!isMember(groupId, userId)) return "You're not a member of this group.";
if(files[groupId].find(fileName)==files[groupId].end()) return "This file doesn't exist.";
string resp = "Seed count : " + to_string(files[groupId][fileName].size()) + " and File size : " + to_string(nameSizeMap[fileName]) + "|";
bool inside = false;
for(auto seeder : files[groupId][fileName]){
if(onlineStatus[{seeder.first, seeder.second}]==false) continue;
inside = true;
resp += seeder.second + "|";
}
if(inside==false) return("Sorry for inconvinience! Currently all seeders are offline.");
resp += to_string(nameSizeMap[fileName]) + "|" + namePathMap[fileName];
return resp;
}
string processCommand(string command){
//cout<<endl<<"RECEIVED COMMAND : "<<command<<endl;
vector<string> tokens;
stringstream ss(command);
while (ss.good()) {
string sb; getline(ss, sb, ' ');
tokens.push_back(sb);
}
string cmd = tokens[0];
if(cmd == "create_user"){
if(tokens.size()!=3) return("Please enter valid command!");
string uid = tokens[1];
string pass = tokens[2];
return createUser(uid, pass);
}
if(cmd == "login"){
if(tokens.size()!=4) return("Please enter valid command!");
string uid = tokens[1];
string pass = tokens[2];
string iport = tokens[3];
//cout<<endl<<uid<<"|"<<pass<<"|"<<iport;
return login(uid, pass, iport);
}
if(cmd == "create_group"){
if(tokens.size()!=3) return("Please enter valid command!");
string gid = tokens[1];
string uid = tokens[2];
return createGroup(gid, uid);
}
if(cmd == "join_group"){
if(tokens.size()!=3) return("Please enter valid command!");
string gid = tokens[1];
string uid = tokens[2];
return joinGroup(gid, uid);
}
if(cmd == "leave_group"){
if(tokens.size()!=3) return("Please enter valid command!");
string gid = tokens[1];
string uid = tokens[2];
return leaveGroup(gid, uid);
}
if(cmd == "requests"){
if(tokens.size()!=4) return("Please enter valid command!");
string gid = tokens[2];
string uid = tokens[3];
return showRequests(gid, uid);
}
if(cmd == "accept_request"){
if(tokens.size()!=4) return("Please enter valid command!");
string gid = tokens[1] + "\n";
string ud = tokens[2];
string loggedIn = tokens[3];
string uid = ud.substr(0, ud.length()-1);
// cout<<gid<<gid.length()<<endl;
// gid=="ib" ? cout<<"TRUE" : cout<<"FALSE";cout<<endl;
// cout<<uid<<uid.length()<<endl;*/
//cout<<loggedIn<<loggedIn.length()<<endl;
return acceptRequests(gid, uid, loggedIn);
}
if(cmd == "list_groups"){
if(tokens.size()!=2) return("Please enter valid command!");
string uid = tokens[1];
return listGroups();
}
if(cmd == "list_files"){
if(tokens.size()!=3) return("Please enter valid command!");
string gid = tokens[1];
string uid = tokens[2];
return listFiles(gid, uid);
}
if(cmd == "upload_file"){
if(tokens.size()!=5) return("Please enter valid command!");
string path = tokens[1];
string gid = tokens[2];
string uid = tokens[3];
string iport = tokens[4];
return uploadFile(path, gid, uid, iport);
}
if(cmd == "logout"){
if(tokens.size()!=3) return("Please enter valid command!");
string uid = tokens[1];
string iport = tokens[2];
return logout(uid, iport);
}
if(cmd == "seeder"){
string gid = tokens[1];
gid += "\n";
string fileName = tokens[2];
string uid = tokens[3];
return seederList(uid, gid, fileName);
}
return ("Invalid command!");
}
void* manage_connections(void* p_newsockfd){
int newsockfd = * (int*)p_newsockfd;
free(p_newsockfd);
int n;
char buffer[BUFFER_SIZE];
while(true){
bzero(buffer, BUFFER_SIZE);
n = read(newsockfd, buffer, BUFFER_SIZE);
if(n<0) error("Error on reading!");
int i = strncmp("closed", buffer, 6);
if(i == 0){
cout<<endl<<endl<<"** CONNECTION CLOSED **"<<endl;
break;
}
string command = buffer;
string resp = processCommand(command);
//cout<<"Client : "<<buffer;
//cout<<"You : ";
bzero(buffer, BUFFER_SIZE);
//fgets(buffer, BUFFER_SIZE, stdin);
strcpy(buffer, resp.c_str());
//buffer = resp.c_str();
n = write(newsockfd, buffer, strlen(buffer));
if(n<0) error("Error on writing!");
i = strncmp("Bye", buffer, 3);
if(i == 0) break;
cout<<endl;
}
return NULL;
}
void save_tracker_details(string path){
string line;
ifstream readFile(path);
while (getline (readFile, line));
readFile.close();
vector<string> tdv = split_args(line);
TRACKER_IP = tdv[0];
TRACKER_PORT = stoi(tdv[1]);
}
int main(int argc, char* argv[])
{
if(argc != 2) error("Port no. not provided! Program terminated");
int sockfd, newsockfd, portno;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len;
string tracker_info_file = argv[1];
save_tracker_details(tracker_info_file);
portno = TRACKER_PORT;
sockfd = socket(AF_INET, SOCK_STREAM, 0) ;
if(sockfd < 0) error("Error Opening socket!");
bzero((char *) &server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
//server_addr.sin_addr.s_addr = TRACKER_IP;
server_addr.sin_port = htons(portno);
inet_pton(AF_INET, TRACKER_IP.c_str(), &server_addr.sin_addr);
if(bind(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) <0 ) error("Binding failed!");
cout<<endl<<endl<<"--------------------------------- < TRACKER > ----------------------------------"<<endl<<endl;
listen(sockfd, 10);
while(true){
client_len = sizeof(client_addr);
newsockfd = accept(sockfd, (struct sockaddr * ) &client_addr, &client_len);
if(newsockfd < 0)
error("Error on Accept");
else cout<<endl<<"** CONNECTION ESTABLISHED **"<<endl;
//manage_connections(newsockfd);
pthread_t peer;
int *pclient = (int*)malloc(sizeof(int));
*pclient = newsockfd;
pthread_create(&peer, NULL, manage_connections, pclient);
}
// close(newsockfd);
// close(sockfd);
return 0;
}