-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
377 lines (331 loc) · 8.61 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
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
#include <iostream>
#include <string>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fstream>
#include <vector>
#include <sstream>
// change later
#include "utils.cpp"
/*
#define PORT 8080
#define FILE_STORE "./fileStore/"
#define BUFFER_SIZE 1024
*/
void recv_file(int, const std::string&);
void upload_file(int, const std::string&);
void initate_upload_command(int, const std::string&);
void initate_get_command(int, const std::string&);
void initate_delete_command(int, const std::string&);
void initate_list_command(int);
void parse_list_file_payload(const std::string&);
std::string extract_filename(const char * buffer, const size_t& size);
std::streampos get_filesize(const std::string&, bool);
/*std::string FILE_STORE;
int PORT;
size_t BUFFER_SIZE;*/
std::string FILE_STORE;
int PORT;
size_t BUFFER_SIZE;
int main(int argc, char * argv[]){
if(argc < 4){
std::cout << "Define required arguments in the order file storage, port, buffer size" << std::endl;
return 1;
}
FILE_STORE = argv[1];
std::string str = argv[2];
PORT = std::stoi(str);
std::stringstream stream(argv[3]);
stream >> BUFFER_SIZE;
std::cout << "args: " << FILE_STORE << " " << PORT << " " << BUFFER_SIZE << std::endl;
int client_fd;
struct sockaddr_in server_addr;
std::string hello = "Hello from client";
char buffer[1024] = { 0 };
std::cout << "Setting up connection with server..." << std::endl;
client_fd = socket(AF_INET, SOCK_STREAM, 0);
if(client_fd < 0){
std::cerr << "Error in getting socket for client" << std::endl;
abort();
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
//3.142.184.183
if(inet_pton(AF_INET, "3.142.184.183", &server_addr.sin_addr) <= 0){
std::cerr << "Invalid address given" << std::endl;
abort();
}
int status = connect(client_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if(status < 0){
std::cerr << "Error in connecting to the server" << std::endl;
abort();
}
std::cout << "Connection to server established" << std::endl;
send(client_fd, &hello[0], hello.length(), 0);
buffer[recv(client_fd, buffer, sizeof(buffer) - 1, 0)] = '\0';
std::cout << "Message from server: " << buffer << std::endl;
while (true){
std::cout << "test-user> ";
std::string command;
std::getline(std::cin, command);
std::cout << "command: " << command << std::endl;
if(command[0] == 'g' and command.length() > 2 and command[1] == ' '){
initate_get_command(client_fd, command);
}
else if(command[0] == 'u' and command.length() > 2 and command[1] == ' '){
initate_upload_command(client_fd, command);
}
else if(command[0] == 'd' and command.length() > 2 and command[1] == ' '){
initate_delete_command(client_fd, command);
}
else if(command == "list files"){
initate_list_command(client_fd);
}
}
close(client_fd);
return 0;
}
void initate_list_command(int client_fd){
std::cout << "list command" << std::endl;
std::string command = "list files";
char buffer[BUFFER_SIZE];
send(
client_fd,
&command[0],
command.length(),
0
);
std::ostringstream files_data;
ssize_t bytes_recv = recv(
client_fd,
buffer,
BUFFER_SIZE - 1,
0
);
buffer[bytes_recv] = '\0';
if(buffer[0] == '1'){
std::cout << "no files on the server" << std::endl;
return;
}
std::ostringstream oss;
size_t index = 0;
while(buffer[index] != '|'){
oss << buffer[index];
index++;
}
int payload_size = std::stoi(oss.str());
oss.str("");
oss.clear();
index++;
int read_bytes = 0;
while(true){
oss << buffer[index];
read_bytes++;
index++;
if(read_bytes == payload_size) break;
if(index == bytes_recv) {
index = 0;
bytes_recv = recv(
client_fd,
buffer,
BUFFER_SIZE - 1,
0
);
buffer[bytes_recv] = '\0';
}
}
std::cout << "Current files on the server:" << std::endl;
std::cout << "FILENAME | SIZE" << std::endl;
parse_list_file_payload(oss.str());
}
void parse_list_file_payload(const std::string& payload){
std::vector<std::string> tokens;
using Stream = std::ostringstream;
Stream stream;
int state = 0;
// state - 0 -> filename length, state - 0 -> filesize state - 2 -> the filename
size_t i = 0;
while(i < payload.length() - 1){
size_t j = i;
while(payload[j] != ':'){
stream << payload[j];
j++;
}
j++;
int filename_len = std::stoi(stream.str());
stream.str("");
stream.clear();
while(payload[j] != ':'){
stream << payload[j];
j++;
}
j++;
std::string filesize = std::to_string(std::stoull(stream.str())/1000000.0) + "MB";
stream.str("");
stream.clear();
for(size_t k = 0; k < filename_len; k++){
stream << payload[j];
j++;
}
std::string filename = stream.str();
stream.str("");
stream.clear();
j++;
i = j;
std::cout << filename << "|" << filesize << std::endl;
}
}
void initate_delete_command(int client_fd, const std::string& command){
std::string filename = extract_filename(&command[0], command.length());
send(
client_fd,
&command[0],
command.length(),
0
);
char buffer[BUFFER_SIZE];
buffer[recv(
client_fd,
buffer,
BUFFER_SIZE - 1,
0
)] = '\0';
if(buffer[0] == '0'){
std::cout << "File: " << filename << " successfully deleted" << std::endl;
} else if(buffer[0] == '1'){
std::cout << "File: " << filename << " deletion failed" << std::endl;
} else if(buffer[0] == '2'){
std::cout << "File: " << filename << " could not deleted because it does not exist on the server" << std::endl;
}
}
void initate_upload_command(int client_fd, const std::string& command){
send(
client_fd,
&command[0],
command.length(),
0
);
std::cout << "sent command" << std::endl;
std::string filename = extract_filename(&command[0], command.length());
char buffer[BUFFER_SIZE];
std::cout << "waiting on res" << std::endl;
ssize_t bytes_recv = recv(
client_fd,
buffer,
BUFFER_SIZE - 1,
0
);
std::cout << "got res" << std::endl;
if(buffer[0] == '1'){
std::cout << "file " << filename << " could not be uploaded" << std::endl;
return;
} else if(buffer[0] == '0'){
std::cout << "Server is ready for upload " << filename << std::endl;
}
upload_file(client_fd, filename);
}
void initate_get_command(int client_fd, const std::string& command){
send(
client_fd,
&command[0],
command.length(),
0
);
recv_file(
client_fd,
extract_filename(&command[0], command.length())
);
}
std::string extract_filename(const char * buffer, const size_t& size){
std::vector<char> data;
for(size_t index = 2; index < size; index++){
data.push_back(buffer[index]);
}
return std::string(data.data(), size - 2);
}
void recv_file(int client_fd, const std::string& filename){
// Create output stream for file and get expected filesize
char buffer[BUFFER_SIZE];
ssize_t bytes_recv = recv(
client_fd,
buffer,
BUFFER_SIZE - 1,
0
);
buffer[bytes_recv] = '\0';
if(buffer[0] == '1'){
std::cout << filename << " could not be found on the server" << std::endl;
return;
}
std::ofstream output_file(FILE_STORE + filename, std::ios::binary | std::ios::out);
size_t index = 2;
std::ostringstream oss;
while(buffer[index] != '|'){
oss << buffer[index];
index++;
}
int filesize = std::stoi(oss.str());
index++;
int read_bytes = 0;
while(true){
output_file.write(&buffer[index], 1);
read_bytes++;
index++;
if(read_bytes == filesize) break;
if(index == bytes_recv) {
index = 0;
bytes_recv = recv(
client_fd,
buffer,
BUFFER_SIZE - 1,
0
);
buffer[bytes_recv] = '\0';
}
}
output_file.close();
std::cout << "downloaded file" << std::endl;
}
std::streampos get_filesize(const std::string& filename, bool binary_mode){
std::ios::openmode flags = std::ios::in | std::ios::ate;
if(binary_mode)
flags |= std::ios::binary;
std::cout << "checking dir: " << FILE_STORE + filename << '\n';
std::ifstream file(FILE_STORE + filename, flags);
if(!file.is_open()){
return -1;
}
std::streampos pos = file.tellg();
file.close();
return pos;
}
void upload_file(int client_fd, const std::string& filename){
std::streampos filesize = get_filesize(filename, true);
std::ifstream input_file(FILE_STORE + filename, std::ios::binary | std::ios::in);
if(filesize == -1 or !input_file.is_open()){
std::string err_msg = "1";
send(
client_fd,
&err_msg[0],
1,0
);
std::cout << "Error in locating or reading file: " << filename << std::endl;
return;
}
std::ostringstream oss;
oss << "0|" << std::to_string(filesize) << "|";
char byte;
while(input_file.read(&byte, sizeof(char))){
oss << byte;
}
std::string buffer = oss.str();
send(
client_fd,
&buffer[0],
buffer.length(),
0
);
std::cout << "File uploaded successfully to server" << std::endl;
}