-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.cpp
700 lines (654 loc) · 16.7 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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#include <sys/types.h>
#include <bits/stdc++.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include<unistd.h>
#include <string.h>
#include <iostream>
#include<pthread.h>
#include<fstream>
#define BUFF_SIZE 1024
#define def_ip "127.0.0.1"
#define def_port 11123
#define trd 100
using namespace std;
int port = def_port;
int fl = 0;
string ip = def_ip;
struct fileinfo{
string username;
string filename;
string sha;
string path;
int gid;
};
//file information
vector<struct fileinfo> finfo;
//map for saving in which groups user is
map<string, vector<int>> usergroup;
//map for saving user & password
map<string, string> users;
//user and active or not
map<string, int> active;
//group id and users list who has requested for joining the group
map<int, vector<string>> requests;
//group id and owner
map<int, string> groups;
//members of the group
map<int,vector<string>> members;
//logged in peers ip and port
map<string, pair<string, int>> peerinfo;
//load data structures with data from files
/*void loadFiles(){
int gid;
string un;
ifstream file("groups");
while(!file.eof()){
if(file.tellg() != -1)
break;
file >> gid;
file >> un;
groups[gid] = un;
}
file.close();
ifstream file("requests");
while(!file.eof()){
if(file.tellg() != -1)
break;
file >> gid;
file >> un;
groups[gid] = un;
}
file.close();
}*/
//load fileinfo from the file
void loadFinfo(){
cout<<"inside load info"<<endl;
ifstream file("finfo");
//struct fileinfo *temp = (struct fileinfo *)malloc(sizeof(struct fileinfo));
string fn;
string us;
string fp;
string sh;
int g;
while(!file.eof()){
if(file.tellg() == -1)
break;
fileinfo temp;// = (struct fileinfo *)malloc(sizeof(struct fileinfo));
file>>g>>us>>fn>>fp>>sh;
//cout<<g<<" "<<us<<" "<<fn<<" "<<fp<<" "<<sh<<endl;
temp.filename = fn;
temp.path = fp;
temp.username = us;
temp.sha = sh;
temp.gid = g;
cout<<"gid in lf "<<temp.gid<<endl;
finfo.push_back(temp);
}
if(finfo.size() > 1){
finfo.pop_back();
}
file.close();
cout<<"leaving load info"<<endl;
}
//method to parse the info file and set ip and port
void parseFile(char *filename, int tno){
char fname[trd];
ifstream rfile;
int i,item=0;
string temp="",temp1,temp2;
rfile.open(filename);
while(tno--){
rfile>>temp1>>temp2;
//cout<<"from the file "<<temp1<<" "<<temp2<<endl;
}
i = 0;
//extracting ip and port of tracker from info file
ip = temp1;
port = stoi(temp2);
while(fname[i]){
if(fname[i] != ' ')
temp+=fname[i];
else{
item++;
if(item == 1){
cout<<"temp "<<temp<<endl;
}
else if(item == 2){
cout<<"temp "<<temp<<endl;
}
temp = "";
}
i++;
}
}
//read user password from file
bool readUserU(string user, string pass){
string uname, passwd;
if(users.find(user) != users.end())
return true;
else return false;
/*ifstream file("users");
while(!file.eof()){
file >> uname;
file >> passwd;
if(!user.compare(uname)){
return true;
}
}*/
return false;
}
//update the request file
void updateRequests(){
ofstream file("requests");
for(auto it: requests){
file << it.first;
for(auto e: it.second){
file <<" "<<e;
}
file<<endl;
}
file.close();
}
//update the members
void updateMembers(){
ofstream file("members");
for(auto it: members){
file << it.first;
for(auto e: it.second){
file <<" "<<e;
}
file<<endl;
}
file.close();
}
//writing to the usergroup file
void updateUsergroupFile(){
ofstream file("usergroup");
for(auto it: usergroup){
file << it.first;
for(auto e: it.second){
file <<" "<<e;
}
file<<endl;
}
file.close();
}
//read user password from file
bool readUser(string user, string pass){
//string uname, passwd;
cout<<user<<" "<<pass<<endl;
cout<<users[user] <<endl;
if(users.find(user) != users.end() && users[user] == pass)
return true;
else return false;
/*ifstream file("users");
while(!file.eof()){
file >> uname;
file >> passwd;
if(!user.compare(uname) && !pass.compare(passwd)){
return true;
}
}
return false;*/
}
//write user password into the file
void writeUser(string user, string pass){
ofstream file;
file.open("users");
users[user] = pass;
for(auto u: users){
file << u.first << " " << u.second << endl;
}
}
//maintaining the active status
void writeActive(string us){
ofstream file("active");
for(auto it: active){
file << it.first <<" "<<it.second<<endl;
}
}
//create group
int createGroup(string user, int group_id){
ifstream file("groups");
int grp_id;
string uname;
/*while(!file.eof()){
file >> grp_id;
file >> uname;
if(grp_id == group_id){
return 0;
}
}*/
if(groups.find(group_id) == groups.end()){
groups[group_id] = user;
ofstream ofile("groups");
for(auto g: groups){
ofile <<g.first<<" "<<g.second<<endl;
}
return 1;
}
else return 0;
}
//request for joining group
bool joinGroupRequest(string us, int gid){
if(groups.find(gid) != groups.end()){
requests[gid].push_back(us);
ofstream file("requests");
for(auto it: requests){
file << it.first;
for(auto e: it.second){
file <<" "<<e;
}
file<<endl;
}
return true;
}
else{
return false;
}
}
//update the group usr belongs
void updateUserGroup(int gid, string us){
if(usergroup.find(us) != usergroup.end()){
vector<int> :: iterator it;
it = find(usergroup[us].begin(),usergroup[us].end(),gid);
if(it == usergroup[us].end()){
usergroup[us].push_back(gid);
}
}
else{
usergroup[us].push_back(gid);
}
updateUsergroupFile();
}
//remove the gid from user list
void removeUserGroup(int gid, string us){
if(usergroup.find(us) != usergroup.end()){
vector<int> :: iterator it;
it = find(usergroup[us].begin(),usergroup[us].end(),gid);
if(it != usergroup[us].end()){
usergroup[us].erase(it);
if(usergroup[us].size() == 0)
usergroup.erase(us);
}
updateUsergroupFile();
}
}
//accept requests
bool acceptRequests(int gid, string uname, string us){
if(requests.find(gid) != requests.end() && groups[gid] == us){
vector<string> :: iterator it;
it = find(requests[gid].begin(),requests[gid].end(),uname);
if(it != requests[gid].end()){
requests[gid].erase(it);
if(requests[gid].size() == 0)
requests.erase(gid);
}
//updating the requests file
updateRequests();
members[gid].push_back(uname);
updateMembers();
updateUserGroup(gid, uname);
return true;
}
else return false;
}
//leave group
bool leaveGroup(int gid, string uname){
if(members.find(gid) != members.end()){
vector<string> :: iterator it;
it = find(members[gid].begin(),members[gid].end(),uname);
if(it != members[gid].end()){
members[gid].erase(it);
if(members[gid].size() == 0)
members.erase(gid);
}
//updating the requests file
updateMembers();
removeUserGroup(gid,uname);
return true;
}
else return false;
}
//save users ip port
bool saveIpPort(string us, string uip, int port){
pair<string, int> temp;
temp.first = uip;
temp.second = port;
peerinfo[us] = temp;
ofstream file("peerinfo");
for(auto e: peerinfo){
file <<e.first <<" "<<e.second.first<<" "<<e.second.second<<endl;
}
file.close();
return true;
}
//remove user ip and port
void removeIpPort(string us){
if(peerinfo.find(us) != peerinfo.end()){
peerinfo.erase(us);
}
ofstream file("peerinfo");
for(auto e: peerinfo){
file <<e.first <<" "<<e.second.first<<" "<<e.second.second<<endl;
}
file.close();
}
//getting all the filepath for a gid
vector<vector<string>> getPath(int gid, string fname){
map<string,bool> mems;
cout<<"inside get path"<<endl;
vector<vector<string>> peers;
for(auto mem: members){
cout<<"groups user "<<mem.first<<endl;
}
if(members.find(gid) != members.end()){
for(auto g: members[gid]){
mems[g] = true;
}
for(auto e: finfo){
if(mems.find(e.username) != mems.end() && active[e.username] == 1 && e.filename == fname){
vector<string> onepeer;
onepeer.push_back(e.path);
onepeer.push_back(peerinfo[e.username].first);
onepeer.push_back(to_string(peerinfo[e.username].second));
peers.push_back(onepeer);
}
}
cout<<"size of peers "<<peers.size()<<endl;
return peers;
}
cout<<"outside get path peers"<<endl;
return peers;
}
//download information send to peer
void downloadFile(int client_socket,int gid, string us, string fname){
int peerport=11123;
char buffer[BUFF_SIZE];
int no_of_matches;
string peerip = "127.0.0.1";
string filepath = "test";
cout<<"inside download file"<<endl;
loadFinfo();
vector<vector<string>> peers = getPath(gid,fname);
no_of_matches = peers.size();
send(client_socket, &no_of_matches, sizeof(no_of_matches),0);
for(auto e : peers){
cout<<"peers info array "<<e[0]<<" "<<e[1]<<" "<<e[2]<<endl;
strcpy(buffer,e[0].c_str());
send(client_socket, buffer, BUFF_SIZE,0);
strcpy(buffer,e[1].c_str());
send(client_socket, buffer,BUFF_SIZE,0);
peerport = stoi(e[2]);
send(client_socket, &peerport, sizeof(peerport),0);
}
/*strcpy(buffer,filepath.c_str());
send(client_socket, buffer, BUFF_SIZE,0);
strcpy(buffer,peerip.c_str());
send(client_socket, buffer,BUFF_SIZE,0);
send(client_socket, &peerport, sizeof(peerport),0);*/
}
bool uploadFile(int gid, string fpath, string fname, string us, string sha){
ofstream file("finfo", ios::app);
file<<gid<<" "<<us<<" "<<fname<<" "<<fpath<<" "<<sha<<endl;
file.close();
return true;
}
//thread for handling the client request
void *clientHandler(void *arg){
char Buffer[BUFF_SIZE];
string us;
string pass;
int option;
int size;
int status = 1;
int client_socket = *((int *)arg);
cout<<"connected to client handler"<<endl;
while(1){
//cout<<"first "<<endl;
recv(client_socket, &option, sizeof(int), 0);
if(option == 1){
recv(client_socket, Buffer, BUFF_SIZE, 0);
us = Buffer;
recv(client_socket, Buffer, BUFF_SIZE, 0);
pass = Buffer;
int check = readUserU(us,pass);
if(!check){
writeUser(us,pass);
status = 1;
send(client_socket, &status, sizeof(status),0);
}
else{
status = 0;
send(client_socket, &status, sizeof(status),0);
}
/*if(users.find(us) == users.end()){
users[us] = pass;
status = 1;
send(client_socket, &status, sizeof(status),0);
}
else{
status = 0;
send(client_socket, &status, sizeof(status),0);
}*/
}
else if(option == 2){
recv(client_socket, Buffer, BUFF_SIZE, 0);
us = Buffer;
recv(client_socket, Buffer, BUFF_SIZE, 0);
pass = Buffer;
bool check = readUser(us,pass);
string uip;
int uport;
bool ss;
if(check){
//writeUser(us,pass);
status = 1;
active[us] = 1;
writeActive(us);
send(client_socket, &status, sizeof(status),0);
recv(client_socket, Buffer, BUFF_SIZE, 0);
uip = Buffer;
recv(client_socket, &uport, sizeof(uport), 0);
ss = saveIpPort(us,uip,uport);
send(client_socket, &ss, sizeof(ss),0);
}
else{
status = 0;
send(client_socket, &status, sizeof(status),0);
}
/*if(users.find(us) != users.end() && users[us] == pass){
status = 1;
active[us] = 1;
send(client_socket, &status, sizeof(status),0);
}
else{
status = 0;
send(client_socket, &status, sizeof(status),0);
}*/
}
else if(option == 3){
int grp_id;
string ufpath;
string ufname;
string sha;
char buffer[BUFF_SIZE];
bool ustat;
recv(client_socket, &grp_id, sizeof(grp_id), 0);
recv(client_socket, buffer, BUFF_SIZE, 0);
ufpath = buffer;
recv(client_socket, buffer, BUFF_SIZE, 0);
ufname = buffer;
recv(client_socket, buffer, BUFF_SIZE, 0);
sha = buffer;
ustat = uploadFile(grp_id,ufpath, ufname, us,sha);
send(client_socket, &ustat, sizeof(ustat),0);
}
else if( option == 4){
int grp_i;
string reqfile;
recv(client_socket, &grp_i, sizeof(grp_i), 0);
recv(client_socket, Buffer, BUFF_SIZE, 0);
us = Buffer;
recv(client_socket, Buffer, BUFF_SIZE, 0);
reqfile = Buffer;
downloadFile(client_socket,grp_i,us,reqfile);
}
else if( option == 5){
recv(client_socket, Buffer, BUFF_SIZE, 0);
us = Buffer;
//recv(client_socket, Buffer, BUFF_SIZE, 0);
//pass = Buffer;
//writeUser(us,pass);
active[us] = 0;
writeActive(us);
removeIpPort(us);
status = 1;
send(client_socket, &status, sizeof(status),0);
}
else if(option == 6){
int grp;
recv(client_socket, Buffer, BUFF_SIZE, 0);
us = Buffer;
recv(client_socket, &grp, sizeof(grp), 0);
//cout<<"group"<<grp<<endl;
status = createGroup(us, grp);
send(client_socket, &status, sizeof(status),0);
}
else if(option == 7){
int more_grp = 1;
//ifstream file("groups");
int grp_id;
string uname;
char user[BUFF_SIZE];
int gsize = 0;
if(groups.size()>0){
gsize = groups.size();
}
send(client_socket, &gsize, sizeof(gsize),0);
for(auto u : groups){
grp_id=u.first;
strcpy(user, u.second.c_str());
send(client_socket, &grp_id, sizeof(grp_id),0);
send(client_socket, user, BUFF_SIZE,0);
}
/*while(!file.eof()){
file >> grp_id;
file >> uname;
cout<<"grp "<<grp_id<<" un "<<uname<<endl;
//if(file.tellg() == -1) break;
strcpy(user, uname.c_str());
send(client_socket, &more_grp, sizeof(more_grp),0);
send(client_socket, &grp_id, sizeof(grp_id),0);
send(client_socket, user, sizeof(user),0);
}
file.close();*/
}
else if(option == 8){
int gid;
bool st;
recv(client_socket, &gid, sizeof(gid), 0);
st = joinGroupRequest(us,gid);
send(client_socket, &st, sizeof(st),0);
}
else if(option == 9){
int gid;
int pdr = 0;
recv(client_socket, &gid, sizeof(gid), 0);
if(requests.find(gid) != requests.end()){
pdr = requests[gid].size();
send(client_socket, &pdr, sizeof(pdr),0);
int ru = 0;
char rur[50];
while(pdr){
strcpy(rur,requests[gid][ru].c_str());
send(client_socket,rur,sizeof(rur),0);
pdr--;
}
}
else{
send(client_socket, &pdr, sizeof(pdr),0);
}
}
else if(option == 10){
int gid;
string uname;
string username;
char buffer[BUFF_SIZE];
bool sts;
recv(client_socket, &gid, sizeof(gid), 0);
recv(client_socket, buffer, BUFF_SIZE, 0);
uname = buffer;
recv(client_socket, buffer, BUFF_SIZE, 0);
username = buffer;
sts = acceptRequests(gid,uname,username);
send(client_socket, &sts, sizeof(sts),0);
}
else if(option == 11){
int gid;
string uname;
char buffer[BUFF_SIZE];
bool sts;
recv(client_socket, &gid, sizeof(gid), 0);
recv(client_socket, buffer, BUFF_SIZE, 0);
uname = buffer;
sts = leaveGroup(gid,uname);
send(client_socket, &sts, sizeof(sts),0);
}
}
close(client_socket);
pthread_exit(NULL);
}
void *trackerServer(void *sock){
int svr_socket;
pthread_t thread[trd];
char response[BUFF_SIZE];
//creating socket for the communication
svr_socket = socket(AF_INET, SOCK_STREAM, 0);
//structure for defining the connection attributes
struct sockaddr_in server;
//assigning type, port and address
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(ip.c_str());
int addrlen = sizeof(sockaddr);
//binding the address
bind(svr_socket, (struct sockaddr*) &server, sizeof(server));
//listening to respond
listen(svr_socket,trd);
int temp,i=0,sts;
string filename;
while(1){
//accepting request from clients
cout<<"server is listening"<<endl;
int client_socket = accept(svr_socket, (struct sockaddr *)&server, (socklen_t*)&addrlen);
sts = pthread_create(&thread[i], NULL, clientHandler, &client_socket);
i++;
}
//closing the socket
close(svr_socket);
pthread_exit(NULL);
}
int main(int argc, char *argv[]){
if(argc != 3){
cout<<"check the arguments"<<endl;
exit(0);
}
int tracker_no = stoi(argv[2]);
string quit;
parseFile(argv[1], tracker_no);
cout<<"port "<<port<<" ip "<<ip<<endl;
pthread_t td1;
int status = pthread_create(&td1, NULL, trackerServer,NULL);
//pthread_join(td1,NULL);
while(1){
//cout<<"quit to close the tracker"<<endl;
cin>>quit;
if(!quit.compare("quit")){
cout<<"Bye !"<<endl;
break;
}
}
return 0;
}