-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracker.cpp
372 lines (305 loc) · 10.1 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
#include<sys/types.h>
#include<sys/socket.h>
#include<iostream>
#include <bits/stdc++.h>
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/un.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<fcntl.h>
#include <fstream>
#include <string>
#include <pthread.h>
#include <map>
#include <iterator>
#include <vector>
#include <set>
using namespace std;
int trackerPort;
string trackerIP;
string seederList;
struct thread_u {
char u_name[255], u_pwd[255];
};
map<string, string> users;
map<string, string> group_users;
map <string, vector<string> > pending_req;
//priority_queue<pair<string, string> > pending;
map <string,set<string> > accepted_req;
void add_group(struct thread_u * thr_ar) {
//struct thread_u * thr_ar = ((struct thread_u*)thread_aa);
char g1_na[255];
char g2_wd[255];
//cout << "PKB: " << thr_ar->u_name << "\n";
strcpy( g1_na, thr_ar->u_name );
strcpy( g2_wd, thr_ar->u_pwd );
map<string, string >::iterator it;
// cout<<users[g1_na];
it = group_users.find(g1_na);
if(it != group_users.end()) {
cout << "The group that you have entered is already present!!!! . PLease type a new group !!"<<endl ;
}
else{
group_users[g1_na]=g2_wd;
//pending_req.insert(pair<string,vector<string> >(g1_na, vector<string>()));
accepted_req[g1_na].insert(g2_wd);
cout<<"Owner of the group "<<g1_na<<" is :"<<group_users[g1_na]<<endl;
cout<<"Group has been created successfully!!!!!!!!"<<endl;
//cout<<group_users[g1_na]<<"is :"<<g2_wd<<endl;
/*for(it = users.begin(); it != users.end(); ++it)
{
std::cout << it->first<<" ";
}*/
}
}
void join_group(struct thread_u * thr_ar) {
//struct thread_u * thr_ar = ((struct thread_u*)thread_aa);
char g1_na[255];
char g2_wd[255];
//cout << "PKB: " << thr_ar->u_name << "\n";
map<string, string>::iterator it1;
strcpy( g1_na, thr_ar->u_name );
strcpy( g2_wd, thr_ar->u_pwd );
//cout<<g1_na<<" "<<g2_wd<<endl;
it1 = group_users.find(g1_na);
if(it1 == group_users.end()) {
cout << "The group that you have entered is not present"<<endl ;
}
else{
int ss=accepted_req[g1_na].size();
cout<<ss;
if(ss>=1){
pending_req[g1_na].push_back(g2_wd);
//cout<<"Pending is : "<<pending_req[g1_na].back()<<endl;
cout<<"Since group is full, so you are in waiting list!!!!!!"<<endl;
}
else{
accepted_req[g1_na].insert(g2_wd);
cout<<"You are now member of the group : "<<g1_na<<endl;
}
}
}
void leave_group(struct thread_u * thr_ar) {
//struct thread_u * thr_ar = ((struct thread_u*)thread_aa);
char g1_na[255];
char g2_wd[255];
//cout << "PKB: " << thr_ar->u_name << "\n";
//map<string, string>::iterator it1;
strcpy( g1_na, thr_ar->u_name );
strcpy( g2_wd, thr_ar->u_pwd );
// cout<<g1_na<<" bum "<<g2_wd<<endl;
//it1 = accepted_req.find(g1_na);
map<string, set<string> >::iterator it1;
for(it1 = accepted_req.begin(); it1 != accepted_req.end(); it1++){
it1->second.erase(g2_wd);
}
cout<<"You are no longer part of the group!!!!"<<endl;
int sp=accepted_req[g1_na].size();
//cout<<"11111111111"<<endl;
if(sp<1){
//cout<<"jjghg"<<endl;
//cout<<pending_req[g1_na].size();
//cout<<"klhg"<<endl;
string st=pending_req[g1_na].back();
pending_req[g1_na].pop_back();
//cout<<"haaa"<<endl;
accepted_req[g1_na].insert(st);
//cout<<"aaaa"<<endl;
}
}
void list_groups(struct thread_u * thr_ar) {
//struct thread_u * thr_ar = ((struct thread_u*)thread_aa);
char g1_na[255];
char g2_wd[255];
//cout << "PKB: " << thr_ar->u_name << "\n";
//map<string, vector<string> >::iterator it1;
strcpy( g1_na, thr_ar->u_name );
strcpy( g2_wd, thr_ar->u_pwd );
//cout<<g1_na<<" LIST REQ "<<g2_wd<<endl;
//it1 = accepted_req.find(g1_na);
map<string, string >::iterator it;
cout<<"All the groups are :- "<<endl;
//assert(pending_req.find(g1_na) != pending_req.end());
it = group_users.begin();
for(it;it != group_users.end();it++) {
cout <<it->first<<endl ;
}
}
void list_request(struct thread_u * thr_ar) {
//struct thread_u * thr_ar = ((struct thread_u*)thread_aa);
char g1_na[255];
char g2_wd[255];
//cout << "PKB: " << thr_ar->u_name << "\n";
//map<string, vector<string> >::iterator it1;
strcpy( g1_na, thr_ar->u_name );
strcpy( g2_wd, thr_ar->u_pwd );
//cout<<g1_na<<" LIST REQ "<<g2_wd<<endl;
//it1 = accepted_req.find(g1_na);
//assert(pending_req.find(g1_na) != pending_req.end());
for (int i=0;i< pending_req[g1_na].size(); i++) {
cout << pending_req[g1_na][i] <<" : is currently waiting in the queue!!!!! "<<endl;
}
}
void user_create(struct thread_u * thr_ar) {
char u_na[255];
char p_wd[255];
strcpy( u_na, thr_ar->u_name );
strcpy( p_wd, thr_ar->u_pwd );
users[u_na]=p_wd;
map<string, string>::iterator it;
cout<<users[u_na];
/*cout<<"Tracker line 27"<<endl;
//string p_wd = thr_ar->u_pwd;
cout<<p_wd<<endl;
ofstream out;
out.open("myfile12.txt");
out << u_na;
out << " : ";
out << p_wd;
cout<<"User has been created";
out.close();*/
cout<<"Account of "<<u_na<<" has been created successfully"<<endl;
for(it = users.begin(); it != users.end(); ++it)
{
std::cout << it->first<<" ";
}
}
void login_check(struct thread_u * thr_log) {
//struct thread_u * thr_ar = ((struct thread_u*)thread_aa);
char u_na[255];
char p_wd[255];
//cout << "PKB: " << thr_ar->u_name << "\n";
strcpy( u_na, thr_log->u_name );
strcpy( p_wd, thr_log->u_pwd );
//cout<<"Name ; "<<u_na<<"for"<<p_wd;
map<string, string>::iterator it;
/*cout<<"Tracker line 27"<<endl;
//string p_wd = thr_ar->u_pwd;
cout<<p_wd<<endl;
ofstream out;
out.open("myfile12.txt");
out << u_na;
out << " : ";
out << p_wd;
cout<<"User has been created";
out.close();*/
/*
it = users.find(u_na);
if(it == users.end())
cout << "You are not a valid user of the system!!!!"<<endl ;
else
cout << "Congrats!! You are a valid user of the system!!"<<endl;
*/
int flag=1;
for(it = users.begin(); it != users.end(); ++it)
{
if((it->first)==u_na){
flag=0;
break;
}
}
if(flag==0){
cout << "Congrats!! You are a valid user of the system!!"<<endl;
}
else{
cout << "You are not a valid user of the system!!!!"<<endl;
}
}
int main(int argc,char* argv[]){
if(argc!=4){
fprintf(stderr, "Invalid number of Arguments!\n");
exit(1);
}
trackerIP=argv[1];
trackerPort=atoi(argv[2]);
seederList=argv[3];
int server_sockfd,client_sockfd;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
//cout<<"Tracker line 49"<<endl;
server_sockfd=socket(AF_INET,SOCK_STREAM,0);
server_address.sin_family=AF_INET;
// bind connection to socket
server_address.sin_addr.s_addr=htonl(INADDR_ANY);
server_address.sin_port=htons(trackerPort);
bind(server_sockfd,(struct sockaddr*)&server_address,sizeof(server_address));
listen(server_sockfd,5);
cout<<"waiting...\n";
int items;
char block[1024];
char fileName[20];
char hashOfHash[20];
char ack='#';
//cout<<"Tracker line 64"<<endl;
char ak[20];
char un[255];
char pw[255];
int s;
//cout<<"Tracker line 69"<<endl;
socklen_t client_len=sizeof(client_address);
int out=open("seederlist.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
//cout<<"Tracker line 72"<<endl;
while(1){
client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_address,&client_len);
//int out=open("seederlist.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
//cout<<"Line 77";
// accepting client connection in infinite loop
if(recv(client_sockfd, ak, 20, 0) == -1) perror("Error: Receiving file size");
string str(ak);
//cout<<ak<<endl;
//int b_size = sizeof(ack) / sizeof(char);
string s_b = str;
int rr;
if((rr=recv(client_sockfd, un, 255, 0) == -1)) perror("Error: Receiving file size");
//cout<<rr<<un<<endl;
if(recv(client_sockfd, pw, 255, 0) == -1) perror("Error: Receiving file size");
//cout<<pw<<endl;
//cout<<"Tracker line 85"<<endl;
//String un1 = String.valueOf(un);
//String pw1 = String.valueOf(pw);
//pthread_t d_t;
struct thread_u ut;
strcpy(ut.u_name,un);
strcpy(ut.u_pwd,pw);
if(s_b=="u"){
user_create(&ut);
//if(pthread_create(&d_t, NULL, user_create, (void *) &ut) != 0) perror("Error: Creating download thread");
}
//cout<<"Tracker line 94"<<endl;
else if(s_b=="l"){
login_check(&ut);
//if(pthread_create(&d_t, NULL, user_create, (void *) &ut) != 0) perror("Error: Creating download thread");
}
else if(s_b=="j"){
add_group(&ut);
}
else if(s_b=="j2"){
join_group(&ut);
}
else if(s_b=="d2"){
leave_group(&ut);
}
else if(s_b=="e2"){
list_request(&ut);
}
else if(s_b=="vg2"){
list_groups(&ut);
}
else{
cout<<"Invalid Commands!!!!!!!!!!!"<<endl;}
// if(pthread_create(&download_t, NULL, usercreate, (void *) &ut) != 0) perror("Error: Creating download thread");
/*int items=read(client_sockfd,&fileName,sizeof(fileName));
fileName[items]='\0';
items=read(client_sockfd,&hashOfHash,sizeof(hashOfHash));
hashOfHash[items]='\0';
char * ip=inet_ntoa(client_address.sin_addr);
dprintf(out,"%s : %s %s %d\n",fileName,hashOfHash,ip,client_address.sin_port);
printf("Seederlist updated!\n");
write(client_sockfd,&ack,1);*/
}
close(out);
close(client_sockfd);
}