-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmytracker.cpp
More file actions
552 lines (406 loc) · 11.7 KB
/
mytracker.cpp
File metadata and controls
552 lines (406 loc) · 11.7 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
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
#include<bits/stdc++.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#define seout cout<<"\nserver: "
#define BUFF_SIZE 2048
using namespace std;
char traker_Ip[20];
int tracker_port;
struct cmp_str
{
bool operator()(char const *a, char const *b) const
{
return std::strcmp(a, b) < 0;
}
};
bool mutex_server_start=false;
int PORT;
struct client_addr
{
char *IP;
int *PORT;
int *size;
char *uid;
};
struct struct_uid
{
char uid[256];
char password[256];
char IP[32];
int port;
bool active;
};
std::map<char *,struct struct_uid *,cmp_str> map_uid;
struct struct_upload
{
struct sockaddr_in client_addr;
int sockfd;
};
std::map<char *,vector<client_addr* >*, cmp_str > map_shared_file;
struct struct_group
{
char * gid;
std::vector<char *> v;
};
void * client(void * argv)
{
while(!mutex_server_start){
//cout<<"\nwaiting for server part to start ";
}
while(1)
{
cout<<"\nEnter file name to share\n";
char *file_name = new char [256];
cin>>file_name;
cout<<"\nEnter file size\n";
int *size=new int[1];
cin>>*size;
cout<<"\nnumber of peer have this\n";
int n;
cin>>n;
vector<struct client_addr*> * v=new vector<struct client_addr*>();
for(int i=0;i<n;i++)
{
char *IP_to_connect=new char[20];
//char *IP_to_connect2=new char[20];
//cin>>file_name;
int *port=new int[1];
//int *port2=new int[1];
cout<<"\nEnter IP and PORT\n";
cin>>IP_to_connect;
cin>>*port;
struct client_addr *client1= new client_addr();
client1->IP=IP_to_connect;
client1->PORT=port;
client1->size=size;
// if( pthread_create(&tid_client,NULL,&client,NULL) !=0 ){
// printf("Failed to create client thread\n");return -1;
// }
v->push_back(client1);
}
map_shared_file.insert({file_name,v});
auto itr=map_shared_file.find(file_name);
if(map_shared_file.find(file_name)!=map_shared_file.end())
{
vector<struct client_addr *> *v=itr->second;
sleep(2);
cout<< "\n size "<< v->size()<<" ";
//sleep(2);
int n=v->size();
}
map<char *, vector<client_addr* >* >::iterator itre;
for (itre = map_shared_file.begin(); itre != map_shared_file.end(); ++itre) {
cout <<"\n" << itre->first ;
vector<struct client_addr*> * v=itre->second;
for(int i=0;i<v->size();i++)
{
cout<<"\n"<<v->at(i)->IP<<"\t"<<*(v->at(i)->PORT)<<"\t"<<*(v->at(i)->size);
}
}
}
}
void serve_download_command(int sockfd)
{
cout<<"\nin the serve_download_command function";
bool ack=true;
send(sockfd,&ack,sizeof(ack),0);
char file_name[256];
memset ( file_name , '\0', 256);
recv(sockfd,&file_name,sizeof(file_name),0);
seout;cout<<"\n"<<file_name;
ack=true;
send(sockfd,&ack,sizeof(ack),0);
auto itr=map_shared_file.find(file_name);
if(map_shared_file.find(file_name)!=map_shared_file.end())
{
vector<struct client_addr *> *v=itr->second;
sleep(2);
cout<< "\n size "<< v->size()<<" ";
//sleep(2);
int n=v->size();
int count=0;
for(int i=0;i<n;i++)
{
auto itr = map_uid.find(v->at(i)->uid);
struct struct_uid * _struct_uid = itr->second;
if(_struct_uid->active==true)
count++;
}
send(sockfd,&count,sizeof(count),0);
ack=false;
recv(sockfd,&ack,sizeof(ack),0);
for(int i=0;i<n;i++)
{
auto itr = map_uid.find(v->at(i)->uid);
struct struct_uid * _struct_uid = itr->second;
if(_struct_uid->active==true)
{
sleep(1);
cout<<"\n";
char * ip=_struct_uid->IP;
int port = _struct_uid->port;
int file_size=*(v->at(i)->size);
send(sockfd,ip,strlen(ip),0);
ack=false;
recv(sockfd,&ack,sizeof(ack),0);
send(sockfd,&port,sizeof(port),0);
ack=false;
recv(sockfd,&ack,sizeof(ack),0);
send(sockfd,&file_size,sizeof(file_size),0);
ack=false;
recv(sockfd,&ack,sizeof(ack),0);
cout<< ip <<" " <<port<<" " <<*(v->at(i)->size)<<" "<<strlen(ip)<<" "<<v->at(i)->uid;
}
}
}
else
{
exit(0);
}
}
void serve_upload_command(void * argv)
{
struct struct_upload * temp=(struct struct_upload * )argv;
bool ack=false;
send(temp->sockfd,&ack,sizeof(ack),0);
char recieve_file_name[256];
memset ( recieve_file_name , '\0', 256);
int file_size;
char *IP_to_connect=new char[20];
int *port=new int[1];
int *size=new int[1];
recv(temp->sockfd,&recieve_file_name,sizeof(recieve_file_name),0);
send(temp->sockfd,&ack,sizeof(ack),0);
recv(temp->sockfd,&file_size,sizeof(file_size),0);
send(temp->sockfd,&ack,sizeof(ack),0);
recv(temp->sockfd,port,sizeof(int),0);
send(temp->sockfd,&ack,sizeof(ack),0);
char uid[256];
memset(uid,'\0',256);
recv(temp->sockfd,&uid,sizeof(uid),0);
send(temp->sockfd,&ack,sizeof(ack),0);
char * UID = new char[256];
strcpy(UID,uid);
strcpy(IP_to_connect,inet_ntoa(temp->client_addr.sin_addr));
//*port=ntohs(temp->client_addr.sin_port);
*size=file_size;
struct client_addr *client1= new client_addr();
client1->IP=IP_to_connect;
client1->PORT=port;
client1->size=size;
client1->uid=UID;
char *file_name = new char [256];
strcpy(file_name,recieve_file_name);
cout<<"\nUpload File "<< file_name;
if(map_shared_file.find(file_name)!=map_shared_file.end())
{
auto itr=map_shared_file.find(file_name);
vector<struct client_addr*> * v=itr->second;
v->push_back(client1);
}
else
{
vector<struct client_addr*> * v=new vector<struct client_addr*>();
cout<<"\n"<<"new Vecot created of size "<<v->size();
v->push_back(client1);
map_shared_file.insert({file_name,v});
}
map<char *, vector<client_addr* >* >::iterator itr;
for (itr = map_shared_file.begin(); itr != map_shared_file.end(); ++itr) {
cout <<"\n" << itr->first ;
vector<struct client_addr*> * v=itr->second;
for(int i=0;i<v->size();i++)
{
cout<<"\n"<<v->at(i)->IP<<"\t"<<*(v->at(i)->PORT)<<"\t"<<*(v->at(i)->size);
}
}
}
void serve_create_user(void * argv)
{
struct struct_upload * temp=(struct struct_upload * )argv;
bool ack=false;
send(temp->sockfd,&ack,sizeof(ack),0);
char uid[256];
char password[256];
memset(uid,'\0',256);
memset(password,'\0',256);
recv(temp->sockfd,&uid,sizeof(uid),0);
send(temp->sockfd,&ack,sizeof(ack),0);
recv(temp->sockfd,&password,sizeof(password),0);
send(temp->sockfd,&ack,sizeof(ack),0);
struct struct_uid * UID = new struct_uid();
strcpy(UID->uid,uid);
strcpy(UID->password,password);
strcpy(UID->IP,inet_ntoa(temp->client_addr.sin_addr));
int port;
recv(temp->sockfd,&port,sizeof(port),0);
send(temp->sockfd,&ack,sizeof(ack),0);
UID->port=port;
UID->active=true;
char * uid_new = new char[256];
strcpy(uid_new,uid);
map_uid.insert({uid_new,UID});
for (auto itr = map_uid.begin(); itr != map_uid.end(); ++itr) {
cout <<"\n" << itr->first ;
}
cout<<"\nclient account created";
}
void serve_login(void * argv)
{
struct struct_upload * temp=(struct struct_upload * )argv;
bool ack=false;
send(temp->sockfd,&ack,sizeof(ack),0);
char uid[256];
char password[256];
memset(uid,'\0',256);
memset(password,'\0',256);
recv(temp->sockfd,&uid,sizeof(uid),0);
send(temp->sockfd,&ack,sizeof(ack),0);
cout<<"\nuid "<<uid;
recv(temp->sockfd,&password,sizeof(password),0);
send(temp->sockfd,&ack,sizeof(ack),0);
cout<<"\npassword "<<password;
int port;
recv(temp->sockfd,&port,sizeof(port),0);
send(temp->sockfd,&ack,sizeof(ack),0);
for (auto itr = map_uid.begin(); itr != map_uid.end(); ++itr) {
cout<<"\n"<< itr->first ;
struct struct_uid * UID= itr->second;
cout<<"\n"<<UID->uid<<" "<<UID->password<<" "<<UID->password;
}
auto itr = map_uid.find(uid);
struct struct_uid * UID= itr->second;
cout<<"\n"<<UID->uid<<" "<<UID->password<<" "<<UID->IP;
if(!strcmp(password,UID->password))
{
// if(UID->active==true)
// {
// ack=false;
// send(temp->sockfd,&ack,sizeof(ack),0);return;
// }
ack=true;
send(temp->sockfd,&ack,sizeof(ack),0);
strcpy(UID->IP,inet_ntoa(temp->client_addr.sin_addr));
UID->port=port;
UID->active=true;
}
else
{
ack=false;
send(temp->sockfd,&ack,sizeof(ack),0);
}
}
void serve_logout(void * argv)
{
struct struct_upload * temp=(struct struct_upload * )argv;
bool ack=false;
send(temp->sockfd,&ack,sizeof(ack),0);
char uid[256];
memset(uid,'\0',256);
recv(temp->sockfd,&uid,sizeof(uid),0);
send(temp->sockfd,&ack,sizeof(ack),0);
auto itr = map_uid.find(uid);
struct struct_uid * UID= itr->second;
UID->active=false;
}
void *serveRequest(void *arg)
{
//int sockfd=*((int *)arg);
struct struct_upload * client_addr=(struct struct_upload * )arg;
char command_type[128];
memset ( command_type , '\0', 128);
recv(client_addr->sockfd,&command_type,sizeof(command_type),0);
//sleep(2);
seout;cout<<"command:-"<<command_type;
if(!strcmp(command_type,"download_file"))
{
serve_download_command(client_addr->sockfd);
}
else if(!strcmp(command_type,"upload_file"))
{
serve_upload_command(client_addr);
}
else if (!strcmp(command_type,"create_user"))
{
serve_create_user(client_addr);
}
else if(!strcmp(command_type,"login"))
{
serve_login(client_addr);
}
else if(!strcmp(command_type,"logout"))
{
serve_logout(client_addr);
}
else
{
cout<<"\ncommand not found";
}
}
void * server(void * argv)
{
cout<<"ram";
//init();
int server_fd = socket (AF_INET, SOCK_STREAM, 0);
pthread_t tid;
// cout<<"\nEnter port for process\n";
// cin>>PORT;
struct sockaddr_in server_addr,client_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons( tracker_port);
server_addr.sin_addr.s_addr=inet_addr(traker_Ip);
cout<<"\nport no - "<<ntohs(server_addr.sin_port)<<"\n";
int addrlen = sizeof(sockaddr);
if(bind (server_fd , (struct sockaddr *)&server_addr , sizeof ( server_addr ) ) !=0)
{
cout<<"\nError in Binding Socket\n";
exit(1);
}
mutex_server_start=true;
listen (server_fd, 3);
cout<<"\nListening";
while(1)
{
int sockfd = accept ( server_fd , (struct sockaddr *)&client_addr , (socklen_t*)&addrlen);
cout<<"\nport no - "<<client_addr.sin_port<<"\n";
cout<<"\nConnection Established";
struct struct_upload * temp = new struct_upload();
temp->sockfd=sockfd;
temp->client_addr=client_addr;
if( pthread_create(&tid,NULL,&serveRequest,temp) !=0 )
printf("Failed to create thread\n");
pthread_join(tid,NULL);
cout<<"\nthread ended";
}
close( server_fd);
}
int main(int argc, char **argv)
{
pthread_t tid_client,tid_server;
if(argc<2)
{
cout<<"\nDetails missing";
return 0;
}
FILE *fp = fopen ( argv[1] , "rb+" );
char buffer[256];
memset(buffer,'\0',256);
fread( buffer,sizeof(char),256,fp);
char* token = strtok(buffer, "\n");
strcpy(traker_Ip,token);
token=strtok(NULL,"\n");
tracker_port=atoi(token);
if( pthread_create(&tid_server,NULL,&server,NULL) !=0 ){
printf("Failed to create server thread\n");return -1;
}
if( pthread_create(&tid_client,NULL,&client,NULL) !=0 ){
printf("Failed to create client thread\n");return -1;
}
pthread_join(tid_server, NULL);
return 0;
}