-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.cpp
233 lines (188 loc) · 4.23 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
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include<bits/stdc++.h>
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#define port 50005
using namespace std;
map<string,vector<string>> m;
void* server()
{
int sockfd,newsockfd;
struct sockaddr_in myaddress,client_addr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
// int port=atoi(argv[1]);
// int port=50005;
if(sockfd<0)
{
cout<<"error in socket creation<<endl";
// return -1;
// return;
}
myaddress.sin_family=AF_INET;
myaddress.sin_port=htons(port);
myaddress.sin_addr.s_addr=INADDR_ANY;
int i=bind(sockfd,(struct sockaddr*)&myaddress,sizeof(myaddress));
if(i<0)
{
cout<<"error in binding.."<<endl;
exit(-1);
}
if(listen(sockfd,5)<0)
{
cout<<"listen error.."<<endl;
// return -1;
// return;
}
cout<<"Tracker server started with port no "<<port<<endl;
socklen_t len=(sizeof(client_addr));
while(1)
{
if((newsockfd=accept(sockfd,(struct sockaddr*)&client_addr,&len))<0)
{
cout<<"error in new socket formation"<<endl;
// return -1;
// return;
}
int a;
recv(newsockfd,&a,sizeof(int),0);
cout<<"received value of a :"<<a<<endl;
cout<<"\nnew connection established.."<<endl;
//if a is 1 that means we need to upload
if(a==1)
{
char buffer[500];
recv(newsockfd,buffer,sizeof(buffer),0);
// cout<<"after recv"<<endl;
// cout<<"value of data received in tracker :"<<buffer<<endl;
string s(buffer);
// cout<<s<<endl;
// cout<<"after tokeninsing :"<<endl;
vector<string> v;
char *ptr=strtok(buffer,"$");
while(ptr!=NULL)
{
string s1(ptr);
v.push_back(s1);
// cout<<ptr<<endl;
ptr=strtok(NULL,"$");
}
string filename=v[0];
string details=v[1];
cout<<"filename :"<<filename<<"\ndetails :"<<details<<endl;
vector<string> v1;
char buffer1[500];
strcpy(buffer1,details.c_str());
char *ptr1=strtok(buffer1," ");
while(ptr1!=NULL)
{
string s2(ptr1);
v1.push_back(s2);
// cout<<ptr1<<endl;
ptr1=strtok(NULL," ");
}
// cout<<"\nvalues in vector string "<<endl;
// for(auto x:v1)
// cout<<x<<endl;
if(m.find(filename)!=m.end())
{
for(auto it=m.begin();it!=m.end();it++)
{
if(filename==it->first)
{
vector<string> v2=it->second;
v2.push_back("$");
for(int i=0;i<v1.size();i++)
{
v2.push_back(v1[i]);
}
m[filename]=v2;
}
}
}
else
{
m[filename]=v1;
}
/*code below is to add new ipaddress and port which has the same filename*/
// for(auto it=m.begin();it!=m.end();it++)
// {
// cout<<"filename :"<<it->first<<endl;
// cout<<"details :"<<endl;
// vector<string> v=it->second;
// // for(auto x:v)
// // cout<<x<<" "<<endl;
// v.push_back("$");
// v.push_back("127.0.0.2");
// v.push_back("50006");
// m[filename]=v;
// }
cout<<"values in map :"<<endl;
for(auto it=m.begin();it!=m.end();it++)
{
cout<<"filename :"<<it->first<<endl;
cout<<"details :"<<endl;
vector<string> v=it->second;
for(auto x:v)
cout<<x<<" "<<endl;
}
}//braces for a=1.
if(a==2)//downloading
{
//for download
char buffer[500];
recv(newsockfd,buffer,sizeof(buffer),0);
cout<<"after recv"<<endl;
cout<<"value of data received in tracker :"<<buffer<<endl;
string filename(buffer);
vector<string> v2;
if(m.find(filename)!=m.end())
{
for(auto it=m.begin();it!=m.end();it++)
{
if(filename==it->first)
{
v2=it->second;
break;
}
}
}
cout<<"\nwhile downloading.."<<endl;
string str;
for(int i=0;i<v2.size();i++)
{
str+=v2[i];
if((i+1)<=v2.size()-1)
{
if(v2[i]=="$"|| v2[i+1]=="$")
{
continue;
}
else
{
str+=" ";
}
}
cout<<str<<endl;
}
cout<<"str value :"<<str<<endl;
char buffer2[500];
strcpy(buffer2,str.c_str());
send(newsockfd,buffer2,sizeof(buffer2),0);
}//braces for a=2(download)
close(newsockfd);
}//this brace is for while loop
close(sockfd);
}
int main(int agrc,char* argv[])
{
server();
return 0;
}