[SAVE]
This commit is contained in:
+69
-36
@@ -36,9 +36,10 @@ const char *_NODE_PORT;
|
||||
#define MAX_RESOURCES 100
|
||||
|
||||
typedef enum neighbor{
|
||||
not_res = 0,
|
||||
res = 1,
|
||||
dunno = 2,
|
||||
dunno = -1,
|
||||
not_res,
|
||||
res,
|
||||
res_303,
|
||||
}neighbor;
|
||||
|
||||
|
||||
@@ -47,6 +48,17 @@ struct tuple resources[MAX_RESOURCES] = {
|
||||
{"/static/bar", "Bar", sizeof "Bar" - 1},
|
||||
{"/static/baz", "Baz", sizeof "Baz" - 1}};
|
||||
|
||||
uint16_t hash_uri(const char *uri){
|
||||
char *uri_ptr = (char*)uri;
|
||||
char dynamic[] = "/dynamic/";
|
||||
char static_[] = "/static/";
|
||||
|
||||
if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic);
|
||||
elif(strstr(uri, static_)) uri_ptr += strlen(static_);
|
||||
|
||||
return pseudo_hash((unsigned char *)uri, strlen(uri));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an HTTP reply to the client based on the received request.
|
||||
*
|
||||
@@ -333,6 +345,13 @@ neighbor check_neighborhood(uint16_t hash) {
|
||||
int id = _NODE_ID;
|
||||
int suc_id = _SUCC_ID;
|
||||
|
||||
if(pre_id == suc_id){
|
||||
if(hash <= id && hash > suc_id) return res_303;
|
||||
if(hash > id && hash <= suc_id) return not_res;
|
||||
return res_303; // hash is in between suc and 0
|
||||
}
|
||||
|
||||
// TODO: check correctness
|
||||
if (pre_id == id && suc_id == id) {
|
||||
return res;
|
||||
}
|
||||
@@ -370,43 +389,55 @@ static void handle_udp_request(int _sock){
|
||||
|
||||
// int sock_bak = _sock;
|
||||
|
||||
if((received_bytes = recvfrom(_sock, buffer, HTTP_MAX_SIZE, 0, (struct sockaddr *) &caller_addr, &addrlen)) == -1){
|
||||
perror("Func: handle_udp_request - failed to receive data from caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
char *b = buffer;
|
||||
size_t space_left = HTTP_MAX_SIZE - 1;
|
||||
|
||||
while(!strstr(buffer, "\r\n\r\n")){
|
||||
b += received_bytes;
|
||||
space_left -= received_bytes;
|
||||
if((received_bytes = recvfrom(_sock, b, space_left, 0, (struct sockaddr *) &caller_addr, &addrlen)) == -1){
|
||||
perror("Func: handle_udp_request - failed to receive data from caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
else{
|
||||
// HASH Received
|
||||
uint16_t hash = pseudo_hash((uint8_t*)buffer, strlen(buffer));
|
||||
printf("Hash: %u\n", hash);
|
||||
// Check belonging
|
||||
int pre_id = _PRED_ID;
|
||||
int suc_id = _SUCC_ID;
|
||||
neighbor responsibility = check_neighborhood(hash);
|
||||
if (responsibility == res) {
|
||||
char res[] = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
|
||||
if (sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1) {
|
||||
perror("Func: handle_udp_request - failed to send 200 data to caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// HASH Received
|
||||
uint16_t hash = atoi(buffer);
|
||||
printf("Hash: %u\n", hash);
|
||||
// Check belonging
|
||||
int pre_id = _PRED_ID;
|
||||
int suc_id = _SUCC_ID;
|
||||
neighbor responsibility = check_neighborhood(hash);
|
||||
|
||||
if (responsibility == res) {
|
||||
char res[] = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
|
||||
if (sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1) {
|
||||
perror("Func: handle_udp_request - failed to send 200 data to caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else if (responsibility == not_res) {
|
||||
// TODO: 303 - See other
|
||||
char res[1024] = {0};
|
||||
snprintf(res, 1024, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%s/%d\r\nContent-Length: 0\r\n\r\n", _NODE_IP, _NODE_PORT, suc_id);
|
||||
if(sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1){
|
||||
perror("Func: handle_udp_request - failed to send 303 data to caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Sent: %s", res);
|
||||
}
|
||||
elif (responsibility == res_303) {
|
||||
char res[1024] = {0};
|
||||
snprintf(res, 1024, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%s/%d\r\nContent-Length: 0\r\n\r\n", _NODE_IP, _NODE_PORT, suc_id);
|
||||
if(sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1){
|
||||
perror("Func: handle_udp_request - failed to send 303 data to caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else {
|
||||
const char res[] = "HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\n\r\n";
|
||||
if(sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1)
|
||||
{
|
||||
perror("Func: handle_udp_request - failed to send 503 data to caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
//call_other_dht();
|
||||
printf("Sent: %s", res);
|
||||
}
|
||||
elif (responsibility == not_res) {
|
||||
// TODO: DIRECT TO NEXT NODE
|
||||
/*
|
||||
const char res[] = "HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\n\r\n";
|
||||
if(sendto(_sock, res, strlen(res), 0, (struct sockaddr *) &caller_addr, addrlen) == -1)
|
||||
{
|
||||
perror("Func: handle_udp_request - failed to send 503 data to caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Sent: %s", res);
|
||||
//call_other_dht();
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -470,6 +501,7 @@ int main(int argc, char **argv) {
|
||||
perror("accept");
|
||||
exit(EXIT_FAILURE);
|
||||
} else {
|
||||
printf("Handling TCP request\n");
|
||||
connection_setup(&state, connection);
|
||||
|
||||
// limit to one connection at a time
|
||||
@@ -480,6 +512,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
elif(s == server_socket_udp){
|
||||
// DO UDP STUFF
|
||||
printf("Handling UDP request\n");
|
||||
sockets[1].events = 0;
|
||||
handle_udp_request(s);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user