[SAVE]
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
#include "dht.h"
|
||||
|
||||
#define cpy(x, y, z) char *x_c = x; char *y_c = y; while(*y_c && z--) *x_c++ = *y_c++
|
||||
|
||||
void lookup_request_init(lookup_request* lr, int node_id, char *node_ip, int node_port, uint16_t hash_id, char *node_request)
|
||||
{
|
||||
int size = NODE_IP_MAX_SIZE;
|
||||
lr->node_id = node_id;
|
||||
{ cpy(lr->node_ip, node_ip, size); }
|
||||
lr->node_port = node_port;
|
||||
lr->hash_id = hash_id;
|
||||
size = HTTP_MAX_SIZE;
|
||||
cpy(lr->node_request, node_request, size);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef DHT_H
|
||||
#define DHT_H
|
||||
#include "http.h"
|
||||
|
||||
#define NODE_IP_MAX_SIZE 16
|
||||
|
||||
typedef struct{
|
||||
int node_id;
|
||||
char node_ip[NODE_IP_MAX_SIZE];
|
||||
int node_port;
|
||||
|
||||
uint16_t hash_id;
|
||||
|
||||
char node_request[HTTP_MAX_SIZE];
|
||||
}lookup_request;
|
||||
|
||||
void lookup_request_init(lookup_request*, int node_id, char *node_ip, int node_port, uint16_t hash_id, char *node_request);
|
||||
void create_lookup_request(lookup_request*, char *request);
|
||||
|
||||
#endif //DHT_H
|
||||
+37
-33
@@ -48,6 +48,8 @@ struct tuple resources[MAX_RESOURCES] = {
|
||||
{"/static/bar", "Bar", sizeof "Bar" - 1},
|
||||
{"/static/baz", "Baz", sizeof "Baz" - 1}};
|
||||
|
||||
neighbor check_neighborhood(uint16_t);
|
||||
|
||||
uint16_t hash_uri(const char *uri){
|
||||
char *uri_ptr = (char*)uri;
|
||||
char dynamic[] = "/dynamic/";
|
||||
@@ -55,8 +57,9 @@ uint16_t hash_uri(const char *uri){
|
||||
|
||||
if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic);
|
||||
elif(strstr(uri, static_)) uri_ptr += strlen(static_);
|
||||
elif(uri[0] == '/') uri_ptr++;
|
||||
|
||||
return pseudo_hash((unsigned char *)uri, strlen(uri));
|
||||
return pseudo_hash((unsigned char *)uri_ptr, strlen(uri_ptr));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,16 +82,31 @@ void send_reply(int conn, struct request *request) {
|
||||
if (strcmp(request->method, "GET") == 0) {
|
||||
// Find the resource with the given URI in the 'resources' array.
|
||||
size_t resource_length;
|
||||
const char *resource =
|
||||
get(request->uri, resources, MAX_RESOURCES, &resource_length);
|
||||
// const char *resource = get(request->uri, resources, MAX_RESOURCES, &resource_length);
|
||||
|
||||
if (resource) {
|
||||
size_t payload_offset =
|
||||
sprintf(reply, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n",
|
||||
resource_length);
|
||||
memcpy(reply + payload_offset, resource, resource_length);
|
||||
offset = payload_offset + resource_length;
|
||||
} else {
|
||||
uint16_t resource_hash = hash_uri(request->uri);
|
||||
neighbor responsible = check_neighborhood(resource_hash);
|
||||
|
||||
if (responsible == res) {
|
||||
sprintf(reply, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
||||
offset = strlen(reply);
|
||||
printf("200\n");
|
||||
}
|
||||
elif(responsible == res_303)
|
||||
{
|
||||
sprintf(reply, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%d%s\r\nContent-Length: 0\r\n\r\n", _SUCC_IP, _SUCC_PORT, request->uri);
|
||||
offset = strlen(reply);
|
||||
printf("303\n");
|
||||
}
|
||||
elif(responsible == not_res)
|
||||
{
|
||||
sprintf(reply, "HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\n\r\n");
|
||||
offset = strlen(reply);
|
||||
printf("503\n");
|
||||
// TODO: Call other dht
|
||||
}
|
||||
else {
|
||||
printf("404\n");
|
||||
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
}
|
||||
@@ -352,29 +370,11 @@ neighbor check_neighborhood(uint16_t hash) {
|
||||
}
|
||||
|
||||
// TODO: check correctness
|
||||
if (pre_id == id && suc_id == id) {
|
||||
if ((pre_id == id && suc_id == id) || (id < pre_id && ((hash > pre_id && hash < 65535)) || hash <= id) || (hash > pre_id && hash <= id))
|
||||
return res;
|
||||
}
|
||||
|
||||
if (id < pre_id) {
|
||||
if ((hash > pre_id && hash < 65535) || hash <= id) {
|
||||
return res;
|
||||
}
|
||||
} else {
|
||||
if (hash > pre_id && hash <= id) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
if (id < suc_id) {
|
||||
if (hash > id && hash <= suc_id) {
|
||||
return not_res;
|
||||
}
|
||||
} else {
|
||||
if (hash > id || hash <= suc_id) {
|
||||
return not_res;
|
||||
}
|
||||
}
|
||||
if ((id < suc_id && hash > id && hash <= suc_id) || (hash > id || hash <= suc_id))
|
||||
return not_res;
|
||||
|
||||
return dunno;
|
||||
}
|
||||
@@ -402,6 +402,7 @@ static void handle_udp_request(int _sock){
|
||||
}
|
||||
|
||||
// HASH Received
|
||||
// TODO: alter buffer
|
||||
uint16_t hash = atoi(buffer);
|
||||
printf("Hash: %u\n", hash);
|
||||
// Check belonging
|
||||
@@ -417,7 +418,10 @@ static void handle_udp_request(int _sock){
|
||||
}
|
||||
printf("Sent: %s", res);
|
||||
}
|
||||
/*
|
||||
TODO: return 404
|
||||
elif (responsibility == res_303) {
|
||||
fprintf(stderr, "Sent 303!\n");
|
||||
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){
|
||||
@@ -426,9 +430,9 @@ static void handle_udp_request(int _sock){
|
||||
}
|
||||
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)
|
||||
{
|
||||
@@ -436,8 +440,8 @@ static void handle_udp_request(int _sock){
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Sent: %s", res);
|
||||
|
||||
//call_other_dht();
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user