-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.cpp
243 lines (213 loc) · 7.43 KB
/
server.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
#include "server.hpp"
#include <cstdint>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <arpa/inet.h>
#include <strings.h>
#include <thread>
#include <mutex>
#define CHUNK_SIZE 1024
std::vector<file_info> files;
std::mutex mtx;
int server::caculate_chunk_number(uint32_t file_length){
int chunk_number = file_length / CHUNK_SIZE;
if((chunk_number * CHUNK_SIZE) < file_length){
chunk_number += 1;
}
return chunk_number;
}
int server::register_reply(int process_fd, struct sockaddr_in client_address){
uint16_t number_of_file;
std::string file_name;
uint32_t file_length;
std::vector<file_info> temp_files = files;
std::string response = "registered";
//client's address info
std::cout<<"client:"<< inet_ntoa(client_address.sin_addr)<<":"<<client_address.sin_port<<std::endl;
uint16_t port;
if(recv(process_fd, &port, sizeof(port), 0) < 0){
perror("client connection failed");
return -1;
}
if(recv(process_fd, &number_of_file, sizeof(number_of_file), 0) < 0){
perror("client connection failed");
return -1;
}
for(int i=0; i<number_of_file; i++){
if(recv(process_fd, &file_name, sizeof(file_name), 0) < 0){
perror("client connection failed");
return -1;
}
if(recv(process_fd, &file_length, sizeof(file_length), 0)<0){
perror("client connection failed");
return -1;
}
std::cout<<"file name:"<<file_name<<", "<<"file length:"<<file_length<<"\n";
//register for all the chunks
struct peer temp_peer;
temp_peer.address = client_address.sin_addr.s_addr;
temp_peer.port = port;
int chunk_size = caculate_chunk_number(file_length);
for(int j=0; j< chunk_size; j++){
temp_peer.chunks.push_back(j);
}
//fill in other data
struct file_info temp_file_info;
temp_file_info.file_name = file_name;
temp_file_info.file_length = file_length;
temp_file_info.peers.push_back(temp_peer);
temp_files.push_back(temp_file_info);
}
mtx.lock();
files = temp_files;
mtx.unlock();
send(process_fd, &response, sizeof(response), 0);
close(process_fd);
return 0;
}
int server::file_list_reply(int process_fd){
uint16_t number_of_file = files.size();
send(process_fd, &number_of_file, sizeof(number_of_file), 0);
for(int i=0; i<number_of_file; i++){
send(process_fd, &files[i].file_name, sizeof(files[i].file_name), 0);
send(process_fd, &files[i].file_length, sizeof(files[i].file_length), 0);
}
close(process_fd);
return 0;
}
int server::location_reply(int process_fd){
std::string file_name;
recv(process_fd, &file_name, sizeof(file_name), 0);
int i;
for(i = 0; i < files.size(); i++){
if(files[i].file_name == file_name){
uint16_t number_of_address = files[i].peers.size();
send(process_fd, &number_of_address, sizeof(number_of_address), 0);
send(process_fd, &files[i].file_length, sizeof(files[i].file_length), 0);
for(int j=0; j<number_of_address; j++){
send(process_fd, &files[i].peers[j].address, sizeof(files[i].peers[j].address), 0);
send(process_fd, &files[i].peers[j].port, sizeof(files[i].peers[j].port), 0);
uint16_t number_of_chunks = files[i].peers[j].chunks.size();
send(process_fd, &number_of_chunks, sizeof(number_of_chunks), 0);
for(int k = 0; k < number_of_chunks; k++){
send(process_fd, &files[i].peers[j].chunks[k], sizeof(files[i].peers[j].chunks[k]), 0);
}
}
}
}
if( i == files.size()){
uint16_t number_of_address = 0;
send(process_fd, &number_of_address, sizeof(number_of_address), 0);
}
close(process_fd);
return 0;
}
int server::chunk_register_reply(int process_fd, struct sockaddr_in client_address){
std::string file_name;
int index;
uint16_t port;
std::string response = "registered";
if(recv(process_fd, &file_name, sizeof(file_name), 0) < 0){
perror("client connection failed");
return -1;
}
if(recv(process_fd, &index, sizeof(index), 0) < 0){
perror("client connection failed");
return -1;
}
if(recv(process_fd, &port, sizeof(port), 0) < 0){
perror("client connection failed");
return -1;
}
std::vector<file_info> temp_files = files;
for(int i=0; i<temp_files.size(); i++){
if(file_name == temp_files[i].file_name){
std::cout<<"hello"<<std::endl;
int j = 0;
for(j=0; j<temp_files[i].peers.size(); j++){
if(temp_files[i].peers[j].address == client_address.sin_addr.s_addr && temp_files[i].peers[j].port == port){
temp_files[i].peers[j].chunks.push_back(index);
break;
}
}
if (j == temp_files[i].peers.size()){
struct peer temp_peer;
temp_peer.address = client_address.sin_addr.s_addr;
temp_peer.port = port;
temp_peer.chunks.push_back(index);
temp_files[i].peers.push_back(temp_peer);
break;
}
}
}
mtx.lock();
files = temp_files;
mtx.unlock();
send(process_fd, &response, sizeof(response), 0);
close(process_fd);
return 0;
}
void server::handle_request(int new_sock, uint32_t request_type, struct sockaddr_in client_address){
switch (request_type){
case 0:
register_reply(new_sock, client_address);
break;
case 1:
file_list_reply(new_sock);
break;
case 2:
location_reply(new_sock);
break;
case 3:
chunk_register_reply(new_sock, client_address);
break;
default:
break;
}
return;
}
int server::execute(void){
server myserver;
int process_fd;
int server_fd;
struct sockaddr_in server_address, client_address;
//create socket
if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
perror("Failed to create socket");
exit(0);
}
//initailize address structure
bzero(&server_address, sizeof(server_address));
server_address.sin_family = PF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(SERVER_PORT);
//bind socket
if(bind(server_fd, (struct sockaddr*) &server_address, sizeof(server_address)) < 0){
perror("Failed to bind");
exit(0);
}
//listen socket
if(listen(server_fd, 5) < 0){
perror("Failed to listen");
exit(0);
}
while(1){
int client_address_length = sizeof(client_address);
if((process_fd = accept(server_fd,(struct sockaddr*) &client_address, (socklen_t *) &client_address_length)) < 0){
perror("Failed to accept");
exit(0);
}
uint32_t request_type;
recv(process_fd, &request_type, sizeof(request_type), 0);
//use multi-thread to handle requests
std::thread tid(&server::handle_request, myserver, process_fd, request_type, client_address);
tid.detach();
//handle_request(process_fd, request_type, client_address);
}
return 0;
}