-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
306 lines (241 loc) · 8.59 KB
/
client.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
#include "torrent.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <bits/stdc++.h>
#include <iostream>
#include <thread>
using namespace std;
string client_ip_port;
string client_ip;
int client_port;
string tracker1_ip_port;
string tracker2_ip_port;
string tracker1_ip;
int tracker1_port;
string tracker2_ip;
int tracker2_port;
string log_file;
vector<string> show_downloads;
vector<string> split(const string& s, char delimiter){
vector<string> tokens;
string token;
istringstream tokenStream(s);
while (getline(tokenStream, token, delimiter)){
tokens.push_back(token);
}
return tokens;
}
void start_client_server(string client_ip, int client_port){
//to be implemented
}
void send_message(int sockfd, string message){
if(send(sockfd,message.c_str(),message.size()+1,0)<0){
}
}
void connect_to_seeders(string clien_ip, int port, string file_to_download_hash){
int sockfd;
int len;
struct sockaddr_in address;
int result;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(clien_ip.c_str());
address.sin_port = port;
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
cout<<result;
if(result == -1) {
cout<<"Could not connect";
exit(1);
}
send_message(sockfd,file_to_download_hash);
}
void parse_seeder_list(string seeder_list){
cout<<"Recived seeder list: "<<seeder_list<<endl;
vector<string> client_info = split(seeder_list,';');
for(int i=0;i<client_info.size();i++){
vector<string> client = split(client_info[i],'|');
vector<string> client_ip_port = split(client[1],':');
string client_ip = client_ip_port[0];
string client_port_str = client_ip_port[1];
stringstream ss(client_port_str);
int client_port;
ss>>client_port;
//thread connect_to_seeder(connect_to_seeders,client_ip,client_port,seeder_list);
//connect_to_seeder.detach();
}
}
void send_request_to_tracker(string req_message){
printf("%s ",req_message.c_str());
int sockfd;
int len;
struct sockaddr_in address;
int result;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = 9735;
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
cout<<result;
if(result == -1) {
cout<<"Could not connect";
exit(1);
}
//cout<<"req message size actual: "<<req_message.size()<<endl;
int req_size = req_message.size();
string req_message_size = to_string(req_size);
//cout<<"\nSize of req message: "<<req_message_size<<endl;
send_message(sockfd,req_message_size);
cout<<"1. sent"<<endl;
char recv_buffer[1024];
int buffer_len = 1024;
int length = recv(sockfd,recv_buffer,buffer_len,0);
cout<<"2. received.."<<endl;
printf("server res: %s\n", recv_buffer);
string res(recv_buffer);
if(res=="OK"){
//cout<<"sending to server: "<<req_message<<endl;
send_message(sockfd,req_message);
//cout<<"3. sent"<<endl;
memset(recv_buffer,'\0',sizeof(recv_buffer));
length = recv(sockfd,recv_buffer,buffer_len,0);
//printf("from server %s",recv_buffer);
//cout<<endl;
//cout<<"4. received"<<endl;
vector<string> action = split(req_message,'|');
if(action[0]== "get"){
string response_from_server(recv_buffer);
thread download_from_clients(parse_seeder_list,response_from_server);
download_from_clients.detach();
}
// string res(recv_buffer);
// stringstream size(res);
// int response_size;
// size>>response_size;
// cout<<"response from server: "<<response_size<<endl;
// char response_message[response_size];
// string rsp="OK";
// send_message(sockfd,rsp);
// cout<<"5. sent.."<<endl;
// memset(response_message,'\0',sizeof(response_message));
// length = recv(sockfd,response_message,response_size,0);
// cout<<"6. received.."<<endl;
// printf("\nHere is the message: %s", response_message);
}
}
string get_sha_of_sha_from_torrent(string torrent_file_path){
ifstream in(torrent_file_path.c_str());
string s;
for(int i = 0; i < 4; ++i)
getline(in, s);
getline(in,s);
string sha_of_sha = create_sha_of_sha(s);
return sha_of_sha;
}
string get_file_name_from_torrent_file(string torrent_file_path){
ifstream in(torrent_file_path.c_str());
string s;
for(int i = 0; i < 2; ++i)
getline(in, s);
getline(in,s);
string file_to_download = s;
return file_to_download;
}
string create_remove_request(string sha_of_sha){
string req = "remove|"+sha_of_sha+"|"+client_ip_port;
return req;
}
string create_close_request(){
string req = "close|"+client_ip_port;
return req;
}
string get_sha_from_torrent_file(string torrent_file_path){
ifstream in(torrent_file_path.c_str());
string s;
for(int i = 0; i < 4; ++i)
getline(in, s);
getline(in,s);
string sha_of_sha = create_sha_of_sha(s);
string response = "get|"+sha_of_sha;
return response;
}
void print_client_menu(){
string command;
cout<<"############ ~~COOL~~TORRENT~~ ###############"<<endl;
cout<<"\n1. Share: share <local file path> <filename>.mtorrent";
cout<<"\n2. Download: get <path to .mtorrent file> <destination path>";
cout<<"\n3. Show Downloads: show downloads";
cout<<"\n4. Remove: remove <filename.mtorrent>";
cout<<"\n5. Exit";
while(true){
cout<<"\n\nEnter command: ";
getline(cin,command);
vector<string> cmd = split(command,' ');
if(cmd[0]=="share"){
string file_path = cmd[1];
string torrent_file = cmd[2];
string server_request = create_torrent_file(tracker1_ip_port,tracker2_ip_port,client_ip_port,file_path,torrent_file);
thread share_request(send_request_to_tracker,server_request);
share_request.detach();
}else if(cmd[0]=="get"){
string torrent_file = cmd[1];
string destination = cmd[2];
string torrent_file_path = parse_file_path(torrent_file);
string req_to_server = get_sha_from_torrent_file(torrent_file_path);
string file_to_download = get_file_name_from_torrent_file(torrent_file_path);
show_downloads.push_back(file_to_download);
thread get_request(send_request_to_tracker,req_to_server);
get_request.detach();
}else if(cmd[0]=="remove"){
string torrent_file = cmd[1];
string torrent_file_path = parse_file_path(torrent_file);
string sha_of_sha = get_sha_of_sha_from_torrent(torrent_file_path);
string req_to_server = create_remove_request(sha_of_sha);
cout<<"\nremovereq: "<<req_to_server;
thread remove_request(send_request_to_tracker,req_to_server);
remove_request.detach();
}else if(cmd[0]=="show"){
if(show_downloads.size()==0){
cout<<"No files downloaded yet"<<endl;
}else{
for(int i =0;i<show_downloads.size();i++){
cout<<show_downloads[i]<<endl;
}
}
}else if(cmd[0]=="close"){
string req_to_server = create_close_request();
thread close_request(send_request_to_tracker,req_to_server);
close_request.detach();
}
}
}
int main(int argc, char const *argv[]){
client_ip_port = argv[1];
tracker1_ip_port = argv[2];
tracker2_ip_port = argv[3];
log_file = argv[4];
vector<string> client_info = split(client_ip_port,':');
client_ip = client_info[0];
stringstream ss1(client_info[1]);
ss1>>client_port;
vector<string> tracker1info = split(tracker1_ip_port,':');
tracker1_ip = tracker1info[0];
stringstream ss2(tracker1info[1]);
ss2>>tracker1_port;
vector<string> tracker2info = split(tracker2_ip_port,':');
tracker2_ip = tracker2info[0];
stringstream ss(tracker2info[1]);
ss>>tracker2_port;
//thread client_server(start_client_server,client_ip, client_port);
// client_server.detach();
print_client_menu();
}