-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtracker.cpp
304 lines (255 loc) · 10.3 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
#include"utils.h"
#include"tracker_file.h"
using namespace std;
#define BUFFER_SIZE 512*1024
map<string, string> user_info;// contains user_name & password
map<string, vector<string> > group_user; // contains all group info & all users in it
map<string, string> group_owner; // group_id & owner
map<string, vector<string>> gr_file; // groupid-key containg files(file_name)
map<string, vector<string> > file_port; //file_name is key ip & port ehich contains these files.
map<string, vector<string> > user_group;
map<string, string> file_full_sha;
map<string, string> file_partial_sha;
map<string, vector<string> > join_group; //request to join group is here(group_id, curr_user)
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
string getFileName(string fname){
vector<string> fcomponent;
boost::split(fcomponent, fname, boost::is_any_of("/"));
int len = fcomponent.size();
return fcomponent[len-1];
}
// function to handle all type request from peers
string request_handler(char *request_type, int &login_flag, string &curr_user){
string input(request_type);
cout<<"type: "<<request_type<<endl;
vector<string> command;
boost::split(command, input, boost::is_any_of(" "));
// create_user
if(strcmp(command[0].c_str(), "create_user")==0){
pthread_mutex_lock(&mutex1);
user_info[command[1]] = command[2];
pthread_mutex_unlock(&mutex1);
return "user_created";
}
//login user
else if(strcmp(command[0].c_str(), "login") == 0){
if(user_info.find(command[1]) == user_info.end()){
return "user not registered";
}
else{
if(user_info[command[1]] == command[2]){
pthread_mutex_lock(&mutex1);
curr_user = command[1];
login_flag = 1;
pthread_mutex_unlock(&mutex1);
return "login done! now you can operate";
}
return "wrong password";
}
}
else {
if(login_flag==1){
if(strcmp(command[0].c_str(),"create_group")==0){
pthread_mutex_lock(&mutex1);
group_user[command[1]].push_back(curr_user);
group_owner[command[1]] = curr_user;
pthread_mutex_unlock(&mutex1);
return "group_created_with current user as owner";
}
else if(strcmp(command[0].c_str(), "join_group")==0){
pthread_mutex_lock(&mutex1);
// group_user[command[1]].push_back(curr_user);
// user_group[curr_user].push_back(command[1]);
join_group[command[1]].push_back(curr_user);
cout<<"group_id="<<command[1]<<endl;
pthread_mutex_unlock(&mutex1);
return "your request is pending with owner";
}
else if(strcmp(command[0].c_str(), "leave_group")==0){
pthread_mutex_lock(&mutex1);
map<string, vector<string>> new_group_user;
for(map<string, vector<string> >::iterator it=group_user.begin(); it!=group_user.end();it++){
if(it->first == command[1]){
for(vector<string>::iterator sec=it->second.begin(); sec!=it->second.end();sec++){
if(*sec == curr_user)
continue;
else
new_group_user[it->first].push_back(*sec);
}
}
}
group_user.clear();
group_user.insert(new_group_user.begin(),new_group_user.end());
new_group_user.clear();
pthread_mutex_unlock(&mutex1);
return "your entry from group ommited";
}
else if(strcmp(command[0].c_str(), "list_groups")==0){
string str = "";
for(map<string, vector<string> >::iterator it=group_user.begin(); it!=group_user.end();it++){
str+=it->first;
str+=" ";
}
return str;
}
else if(strcmp(command[0].c_str(), "upload_file")==0){
string fname;
bool flag=false;
vector<string> users = group_user[command[2]];
if(users.size()>0){
for(string s: users){
if(s==curr_user)
flag=true;
}
}
if(flag==true){
fname = getFileName(command[1]);
pthread_mutex_lock(&mutex1);
gr_file[command[2]].push_back(fname);
file_port[fname].push_back(command[5]);
file_full_sha[fname] = command[4];
file_partial_sha[fname] = command[3];
pthread_mutex_unlock(&mutex1);
return "file_uploaded";
}
else{
return "you're not part of group or group doesnt exist";
}
}
else if(strcmp(command[0].c_str(), "list_files")==0){
string files="";
vector<string> file_name = gr_file[command[1]];
pthread_mutex_lock(&mutex1);
for(string s: file_name){
files.append(s);
files.append("\n");
}
pthread_mutex_unlock(&mutex1);
return files;
}
else if(strcmp(command[0].c_str(), "download_file")==0){
bool flag= false;
vector<string> users = group_user[command[1]];
for(string s: users){
if(s==curr_user)
flag=true;
}
if(flag==true){
string final_ports_sha="";
vector<string> ports = file_port[command[2]];
pthread_mutex_lock(&mutex1);
for(string s: ports){
final_ports_sha.append(s);
final_ports_sha.append(" ");
}
final_ports_sha.append(file_full_sha[command[2]]);
final_ports_sha.append(" ");
final_ports_sha.append(file_partial_sha[command[2]]);
pthread_mutex_unlock(&mutex1);
return final_ports_sha;
}
else{
return "you cannot download, first join the group";
}
}
else if(strcmp(command[0].c_str(), "list_requests")==0){
string list_requests="";
cout<<"list_request="<<command[1]<<endl;
vector<string> list_req = join_group[command[1]];
for(string s: list_req){
cout<<"s=="<<s<<endl;
pthread_mutex_lock(&mutex1);
list_requests.append(s);
list_requests.append(" ");
pthread_mutex_unlock(&mutex1);
}
return list_requests;
}
else if(strcmp(command[0].c_str(), "accept_request")==0){
string user = group_owner[command[1]];
if(user == curr_user){
pthread_mutex_lock(&mutex1);
group_user[command[1]].push_back(command[2]);
user_group[command[2]].push_back(command[1]);
pthread_mutex_unlock(&mutex1);
}
return "request accpted";
}
else if(strcmp(command[0].c_str(), "show_downloads")==0){
}
else if(strcmp(command[0].c_str(), "stop_sharing")==0){
}
else if(strcmp(command[0].c_str(), "logout")==0){
}
else{
return "false";
}
}
else{
return "You're not logged in";
}
}
}
void *RequestThread(void *newsockfd1){
// information that will be maintained for this particular thread
int login_flag = 0;
string curr_user;
// request_type from client
char request_type[BUFFER_SIZE];
int newsockfd = *((int*)newsockfd1);
while(true){
memset(request_type, '\0', sizeof(request_type));
read(newsockfd, request_type, sizeof(request_type));
// function to handle type of request from peers in tracker
string response = request_handler(request_type, login_flag, curr_user);
write(newsockfd, response.c_str(), strlen(response.c_str()));
}
}
int main(int argc, char *argv[])
{
if(argc < 2){
printf("tracker info not provided");
exit(1);
}
vector<string> ports, IP;
ports = getTrackerPort(argv[1]);
IP = getTrackerIP(argv[1]);
int sockfd, new_sockfd, portno, n;
char buff[BUFFER_SIZE];
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
// socket creation at server side
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0){
printf("socket opening failure");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(ports[0].c_str());
serv_addr.sin_port = htons(portno);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(IP[0].c_str());
// socket binding
if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0){
perror("Binding failed, incorrect port or ip");
}
// socket listening
listen(sockfd, 10);
clilen = sizeof(cli_addr);
while(1){
pthread_t thread1;
// socket accepts, once accepts create new socket for read & write
printf("waiting for connections\n");
int newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if(newsockfd<0){
perror("error on accept");
}
printf("connected....\n");
int *arg = (int*)malloc(sizeof(*arg));
*arg = newsockfd;
// create new thread whenever a request for new connection comes in.
pthread_create(&thread1, NULL, RequestThread, (void*)arg);
// pthread_join(thread1, NULL);
}
return 0;
}