-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.cpp
748 lines (659 loc) · 16.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
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
#include "trackerutil.h"
struct file_data
{
string userid;
string hash;
string ip;
string port;
long int file_size;
string grpid;
bool isactive;
};
struct grp_file_data{
string userid;
string fname;
string ip;
string port;
bool isactive;
};
struct group
{
string admin;
};
map<string,vector<string>>admin_req;//map for admin to pending requests
map<string,string>user_pass;
map<string,group>grp_map;//map group id to group;
map<string,vector<string>>grp_users;//map grp id to users
map<string,vector<grp_file_data>>grp_file_map;//group_name map to file data
map<string,vector<pair<string,string>>>user_files;//map userid to to pair of grpid and filename
std::map<string, vector<file_data>> file_map;// map filename to filedata
set<string>file_set;//set contains filenames present in group
map<string,vector<pair<string,string>>>downloads;
std::vector<string> command;
string tracker1_ip,tracker1_port,tracker2_ip,tracker2_port,log_file;
pthread_t client_name;
int main(int argc,char* argv[])
{
int opt=1;
if(argc !=3)
{
cout<<"invalid arguments"<<endl;
exit(1);
}
log_file="log";
ofstream output;
output.open(log_file,fstream::out);
output.close();
//populating hashmap for user-password
ifstream inp_file("user_pass.txt");
string line;
while(getline(inp_file,line))
{
command=splitstring2(line,':');
user_pass[command[0]]=command[1];
}
string t1_ip,t1_port;
string t2_ip,t2_port;
t1_ip="127.0.0.1",t1_port="5000";
t2_ip="127.0.0.1",t2_port="6000";
//cout<<t1_ip<<" "<<t1_port<<" "<<t2_ip<<" "<<" "<<t2_port<<endl;
char* t1_ip_char=new char[t1_ip.length()+1];
strcpy(t1_ip_char,t1_ip.c_str());
int server_fd=socket(AF_INET,SOCK_STREAM,0);
if(server_fd==0)
{
cout<<"error in connection";
exit(1);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
{
cout<<"setsockopt"<<endl;
exit(1);
}
int port=stoi(t1_port);
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=INADDR_ANY;
int addrlen = sizeof(sockaddr);
if (inet_pton(AF_INET, t1_ip_char, &addr.sin_addr) <= 0)
{
printf("\nInvalid address/ Address not supported \n");
//logprinter("Invalid address/ Address not supported");
return -1;
}
if(bind (server_fd , (struct sockaddr *)&addr , sizeof ( addr ) )<0)
{
cout<<"error in binding"<<endl;
exit(1);
}
if(listen (server_fd, 34)==0)
{
cout<<"In Listening state"<<endl;
}
else
{
cout<<"error in Listening"<<endl;
exit(1);
}
int new_sock;
while(1)
{
if((new_sock=accept( server_fd , (struct sockaddr *)&addr , (socklen_t*)&addrlen))<0)
{
cout<<"error in connection establishment"<<endl;
exit(1);
}
int client_port_num=ntohs(addr.sin_port);
string client_port_str=to_string(client_port_num);
int* argument=(int *)malloc(sizeof(*argument));
*argument=new_sock;
if(pthread_create(&client_name,0,serve_client,argument)<0)
{
cout<<"could not create thread"<<endl;
return 1;
}
}
return 0;
}
std::vector<string> splitstring(string str,char c)
{
std::vector<string> v;
string first,second;
for(int i=0;i<str.length();i++)
{
//cout<<str[i]<<endl;
if(str[i]==c)
{
//cout<<<<i<<endl;
first=str.substr(0,i);
second=str.substr(i+1);
break;
}
}
v.push_back(first),v.push_back(second);
return v;
}
void *serve_client(void* sock_num)
{
int conn_sock=*((int *)sock_num);
while(1)
{
char buffer[1024]={0};
int status=read(conn_sock,buffer,1024);
if(status==0)
return sock_num;
string buffer_str=string(buffer);
command=splitstring2(buffer_str,':');
struct file_data fdata;
struct grp_file_data gdata;
if(command[0]=="login")
{
int valid=0;
if(user_pass[command[1]]==command[2])
{
valid=1;
}
if(valid==1)
{
string uid=command[1];
std::vector<pair<string,string>> v=user_files[uid];
if(v.size()!=0)
{
for(auto j=v.begin();j!=v.end();j++)
{
pair<string,string>p=*j;
string logout_file=p.first;
string logout_grp=p.second;
std::vector<grp_file_data> logout_grp_vec=grp_file_map[logout_grp];
for(auto k=logout_grp_vec.begin();k!=logout_grp_vec.end();k++)
{
struct grp_file_data gfd=*k;
if(gfd.userid==uid)
{
cout<<gfd.fname<<endl;
gfd.isactive=true;
}
*k=gfd;
}
grp_file_map[logout_grp]=logout_grp_vec;
std::vector<file_data> logout_file_vec=file_map[logout_file];
for(auto x=logout_file_vec.begin();x!=logout_file_vec.end();x++)
{
struct file_data fd=*x;
if(fd.userid==uid)
fd.isactive=true;
*x=fd;
}
file_map[logout_file]=logout_file_vec;
}
}
}
send(conn_sock,&valid,sizeof(valid),0);
}
else if(command[0]=="create_user")
{
int valid=0;
user_pass[command[1]]=command[2];
/*for(auto i:user_pass)
{
cout<<i.first<<" "<<i.second<<endl;
}*/
ofstream outf;
outf.open("user_pass.txt", ios::app|ios::out);
outf<<command[1]<<":"<<command[2]<<endl;
outf.close();
valid=1;
send(conn_sock,&valid,sizeof(valid),0);
}
else if(command[0]=="upload_file")
{
string reply;
int flag=0;
if(grp_map.find(command[5])==grp_map.end())
{
reply="group does not exist";
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
continue;
}
std::vector<string> gusers=grp_users[command[5]];
int gflag=0;
for(auto it=gusers.begin();it!=gusers.end();it++)
{
string ga=*it;
if(ga==command[7])
{
gflag=1;
break;
}
}
if(gflag==0)
{
reply="you are not a member of this group";
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
continue;
}
string fname=command[3];
fdata.ip=command[2];
fdata.hash=command[1];
fdata.port=command[4];
fdata.grpid=command[5];
fdata.userid=command[7];
stringstream so(command[6]);
long int x=0;
so>>x;
fdata.file_size=x;
fdata.isactive=true;
gdata.fname=command[3];
gdata.ip=command[2];
gdata.port=command[4];
gdata.userid=command[7];
gdata.isactive=true;
std::vector<file_data> v=file_map[fname];
pair<string,string>p;
p.first=fname;
p.second=command[5];
if(v.size()==0)
{
file_map[fname].push_back(fdata);
grp_file_map[command[5]].push_back(gdata);
user_files[command[7]].push_back(p);
reply="successfully shared";
}
else
{
for (auto j=v.begin();j!=v.end();j++)
{
struct file_data f=*j;
if(f.userid==fdata.userid && f.grpid==fdata.grpid)
{
flag=1;
break;
}
}
if(flag)
{
reply="already shared file";
}
else
{
file_map[fname].push_back(fdata);
grp_file_map[command[5]].push_back(gdata);
user_files[command[7]].push_back(p);
reply="successfully shared";
}
}
char* reply_char=new char[reply.length()+1];
strcpy(reply_char,reply.c_str());
send(conn_sock,reply_char,strlen(reply_char),0);
}
else if(command[0]=="create_group")
{
string grp_id=command[1];
group g;
g.admin=command[2];
grp_map[grp_id]=g;
//cout<<grp_map[grp_id].admin<<endl;
string reply;
std::vector<string> req;
if(grp_map.find(grp_id)!=grp_map.end())
{
admin_req[g.admin]=req;
grp_users[grp_id].push_back(command[2]);
reply="group is created is successfully";
}
else
{
reply="group cannot be created";
}
char* reply_char=new char[reply.length()+1];
strcpy(reply_char,reply.c_str());
send(conn_sock,reply_char,strlen(reply_char),0);
}
else if(command[0]=="list_groups")
{
string grps="";
string del=":";
if(grp_map.empty())
{
string reply="No groups are present";
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
continue;
}
for(auto i:grp_map)
{
cout<<i.first<<endl;
grps=grps+i.first+del;
}
grps.pop_back();
char* grps_char=new char[grps.length()+1];
strcpy(grps_char,grps.c_str());
send(conn_sock,grps_char,strlen(grps_char),0);
}
else if(command[0]=="join_group")
{
string reply;
if(grp_map.find(command[1])==grp_map.end())
{
reply="group does not exist";
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
continue;
}
else
{
admin_req[grp_map[command[1]].admin].push_back(command[2]);
reply="join request is send to admin";
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
}
}
else if(command[0]=="leave_group")
{
string reply;
if(grp_map.find(command[1])==grp_map.end())
{
reply="group does not exist";
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
continue;
}
else
{
int user_ex_fl=0;
vector<string>:: iterator it1;
std::vector<string> v=grp_users[command[1]];
int i=0;
for(i=0;i<v.size();i++)
{
cout<<v[i]<<" ";
if(v[i]==command[2])
{
user_ex_fl=1;
break;
}
}
cout<<i;
cout<<endl;
if(user_ex_fl==0)
reply="you are not a member of this group";
else
{
grp_users[command[1]].erase(grp_users[command[1]].begin()+i);
reply="you have left group "+command[1];
}
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
}
}
else if(command[0]=="list_requests")
{
string ad=command[2];
string grp=command[1];
string reply="";
string del=":";
//cout<<"admin "<<grp_map[grp].admin<<endl;
//cout<<"req "<<ad<<endl;
if(grp_map[grp].admin!=ad)
{
reply="you are not a admin of this group";
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
}
else
{
std::vector<string> temp_v=admin_req[ad];
if(temp_v.size()==0)
reply="No pending requests";
else
{
for(int i=0;i<temp_v.size();i++)
{
reply=reply+temp_v[i]+del;
}
reply.pop_back();
}
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
}
}
else if(command[0]=="accept_request")
{
string grp=command[1];
string uid=command[2];
string ad=command[3];
string reply="";
if(grp_map[grp].admin!=ad)
{
reply="you are not a admin of this group";
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
}
else
{
int user_ex_fl=0;
grp_users[grp].push_back(uid);
std::vector<string> v=admin_req[ad];
int i=0;
for(i=0;i<v.size();i++)
{
//cout<<v[i]<<" ";
if(v[i]==uid)
{
user_ex_fl=1;
break;
}
}
cout<<i;
cout<<endl;
if(user_ex_fl==0)
reply="user is not there in join_request list";
else
{
admin_req[ad].erase(admin_req[ad].begin()+i);
reply=uid+" is now member of "+grp;
}
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
}
}
else if(command[0]=="list_files")
{
string grp=command[1];
string reply="";
string del=":";
std::vector<grp_file_data> v= grp_file_map[grp];
file_set.clear();
if(v.size()==0)
{
reply="NO files are present in group to share";
}
else
{
for (auto j=v.begin();j!=v.end();j++)
{
//cout<<"size"<<v.size()<<endl;
struct grp_file_data g=*j;
if(g.isactive==true)
{
file_set.insert(g.fname);
}
}
set<string>::iterator itr;
for(itr=file_set.begin();itr!=file_set.end();++itr)
{
reply=reply+(*itr)+del;
}
reply.pop_back();
}
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
}
else if(command[0]=="stop_share")
{
string grp=command[1];
string stop_file=command[2];
string uid=command[3];
string reply="";
int flag=0;
std::vector<pair<string,string>> v=user_files[uid];
if(v.size()!=0)
{
for(int j=0;j<v.size();j++)
{
pair<string,string>p=v[j];
if(p.first==stop_file && p.second==grp)
{
flag=1;
user_files[uid].erase(user_files[uid].begin()+j);
break;
}
}
if(flag==0)
{
reply="you have not shared this file in this group";
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
continue;
}
std::vector<struct grp_file_data> v1=grp_file_map[grp];
for(int j=0;j<v1.size();j++)
{
struct grp_file_data g=v1[j];
if(g.userid==uid && g.fname==stop_file)
{
grp_file_map[grp].erase(grp_file_map[grp].begin()+j);
break;
}
}
std::vector<struct file_data> v2=file_map[stop_file];
for(int j=0;j<v1.size();j++)
{
struct file_data f=v2[j];
if(f.userid==uid && f.grpid==grp)
{
file_map[stop_file].erase(file_map[stop_file].begin()+j);
break;
}
}
reply="file is now not sharable in "+grp;
}
else{
reply="You have not shared any file yet";
}
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
}
else if(command[0]=="download_file")
{
string grp=command[2];
string down_file=command[1];
string reply="";
string del=":";
vector<struct file_data> v=file_map[down_file];
if(v.size()!=0)
{
string fsize="";
for(auto j=v.begin();j!=v.end();j++)
{
struct file_data f=*j;
fsize=to_string(f.file_size);
if(f.grpid==grp && f.isactive==true)
{
reply=reply+f.ip+del+f.port+"|";
}
}
if(reply.length()>0)
reply=reply+fsize;
}
else
reply="file is not there in group";
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
}
else if(command[0]=="logout")
{
string uid=command[1];
string reply="";
std::vector<pair<string,string>> v=user_files[uid];
if(v.size()!=0)
{
reply="logout successfull";
for(auto j=v.begin();j!=v.end();j++)
{
pair<string,string>p=*j;
string logout_file=p.first;
string logout_grp=p.second;
std::vector<grp_file_data> logout_grp_vec=grp_file_map[logout_grp];
for(auto k=logout_grp_vec.begin();k!=logout_grp_vec.end();k++)
{
struct grp_file_data gfd=*k;
if(gfd.userid==uid)
{
cout<<gfd.fname<<endl;
gfd.isactive=false;
}
*k=gfd;
}
grp_file_map[logout_grp]=logout_grp_vec;
std::vector<file_data> logout_file_vec=file_map[logout_file];
for(auto x=logout_file_vec.begin();x!=logout_file_vec.end();x++)
{
struct file_data fd=*x;
if(fd.userid==uid)
fd.isactive=false;
*x=fd;
}
file_map[logout_file]=logout_file_vec;
}
}
else
{
reply="logout successfully";
}
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
}
else if(command[0]=="show_downloads")
{
string reply="";
std::vector<pair<string,string>> v=downloads[command[1]];
for(auto j=v.begin();j!=v.end();j++)
{
pair<string,string>p=*j;
reply=reply+p.first+":"+p.second+"|";
}
reply.pop_back();
char* reply_char_err=new char[reply.length()+1];
strcpy(reply_char_err,reply.c_str());
send(conn_sock,reply_char_err,strlen(reply_char_err),0);
}
else if(command[0]=="add_download")
{
pair<string,string>p;
p.first=command[2];
p.second=command[3];
downloads[command[1]].push_back(p);
}
}
return sock_num;
}