Made code a bit cleaner
This commit is contained in:
+110
-154
@@ -22,21 +22,26 @@
|
||||
#define elif else if
|
||||
|
||||
// Getter for environmental DHT sets
|
||||
#define _PRED_ID atoi(getenv("PRED_ID"))
|
||||
#define _PRED_ID (uint16_t)atoi(getenv("PRED_ID"))
|
||||
#define _PRED_IP getenv("PRED_IP")
|
||||
#define _PRED_PORT atoi(getenv("PRED_PORT"))
|
||||
#define _SUCC_ID atoi(getenv("SUCC_ID"))
|
||||
#define _PRED_PORT (uint16_t)atoi(getenv("PRED_PORT"))
|
||||
#define _SUCC_ID (uint16_t)atoi(getenv("SUCC_ID"))
|
||||
#define _SUCC_IP getenv("SUCC_IP")
|
||||
#define _SUCC_PORT atoi(getenv("SUCC_PORT"))
|
||||
#define _SUCC_PORT (uint16_t)atoi(getenv("SUCC_PORT"))
|
||||
|
||||
bool _DO_UDP = false;
|
||||
|
||||
int _NODE_ID;
|
||||
uint16_t _NODE_ID;
|
||||
const char *_NODE_IP;
|
||||
int _NODE_PORT;
|
||||
uint16_t _NODE_PORT;
|
||||
|
||||
#define MAX_RESOURCES 100
|
||||
|
||||
struct tuple resources[MAX_RESOURCES] = {
|
||||
{"/static/foo", "Foo", sizeof "Foo" - 1},
|
||||
{"/static/bar", "Bar", sizeof "Bar" - 1},
|
||||
{"/static/baz", "Baz", sizeof "Baz" - 1}};
|
||||
|
||||
typedef enum neighbor{
|
||||
dunno = -1,
|
||||
not_res,
|
||||
@@ -44,22 +49,14 @@ typedef enum neighbor{
|
||||
res_303,
|
||||
}neighbor;
|
||||
|
||||
|
||||
struct tuple resources[MAX_RESOURCES] = {
|
||||
{"/static/foo", "Foo", sizeof "Foo" - 1},
|
||||
{"/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/";
|
||||
char static_[] = "/static/";
|
||||
|
||||
if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic);
|
||||
elif(strstr(uri, static_)) uri_ptr += strlen(static_);
|
||||
if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic) + 1;
|
||||
elif(strstr(uri, static_)) uri_ptr += strlen(static_) + 1;
|
||||
|
||||
/*
|
||||
char *it = (char*)uri;
|
||||
@@ -80,6 +77,22 @@ uint16_t hash_uri(const char *uri){
|
||||
return pseudo_hash((const unsigned char *)uri_ptr, strlen(uri_ptr));
|
||||
}
|
||||
|
||||
neighbor check_neighborhood(uint16_t hash) {
|
||||
int pre_id = _PRED_ID;
|
||||
int id = _NODE_ID;
|
||||
int suc_id = _SUCC_ID;
|
||||
|
||||
if(hash > id && hash <= suc_id) return res_303;
|
||||
|
||||
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 < suc_id && hash > id && hash <= suc_id) || (hash > id || hash <= suc_id))
|
||||
return not_res;
|
||||
|
||||
return dunno;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an HTTP reply to the client based on the received request.
|
||||
*
|
||||
@@ -88,7 +101,7 @@ uint16_t hash_uri(const char *uri){
|
||||
* information.
|
||||
*/
|
||||
void send_reply(int conn, struct request *request) {
|
||||
|
||||
fprintf(stderr, "\n\n\n---%u---\n", _NODE_ID);
|
||||
// Create a buffer to hold the HTTP reply
|
||||
char buffer[HTTP_MAX_SIZE];
|
||||
char *reply = buffer;
|
||||
@@ -97,87 +110,65 @@ void send_reply(int conn, struct request *request) {
|
||||
fprintf(stderr, "Handling %s request for %s (%lu byte payload)\n",
|
||||
request->method, request->uri, request->payload_length);
|
||||
|
||||
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);
|
||||
printf("Uri: %s\n", request->uri);
|
||||
uint16_t resource_hash = hash_uri(request->uri);
|
||||
printf("Uri: %s\n", request->uri);
|
||||
uint16_t resource_hash = hash_uri(request->uri);
|
||||
|
||||
fprintf(stderr, "Resource hash: %u\n", resource_hash);
|
||||
fprintf(stderr, "Resource hash: %u\n", resource_hash);
|
||||
|
||||
if (resource_hash == -1)
|
||||
if (resource_hash == -1)
|
||||
{
|
||||
perror("Pseudo_hash failed - 404\n");
|
||||
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
goto direct_send;
|
||||
}
|
||||
|
||||
neighbor responsible = check_neighborhood(resource_hash);
|
||||
lookup_request *req = find_lookup(resource_hash);
|
||||
|
||||
if(responsible != res){
|
||||
if(responsible == res_303)
|
||||
{
|
||||
printf("404\n");
|
||||
perror("Responsible 303 - 303\n");
|
||||
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);
|
||||
}
|
||||
elif (req == NULL)
|
||||
{
|
||||
perror("Not Responsible - 503\n");
|
||||
sprintf(reply, "HTTP/1.1 503 Service Unavailable\r\nRetry-After: 1\r\nContent-Length: 0\r\n\r\n");
|
||||
offset = strlen(reply);
|
||||
|
||||
lookup_request lookreq;
|
||||
lookup_request_init(&lookreq, 0, _NODE_ID, ntohl(inet_addr(_NODE_IP)), _NODE_PORT, resource_hash);
|
||||
|
||||
call_network(&lookreq, ntohl(inet_addr(_SUCC_IP)), _SUCC_PORT);
|
||||
}
|
||||
elif (req->message_type == 1)
|
||||
{
|
||||
// DONE: Success
|
||||
struct sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(req->node_ip.s_addr);
|
||||
char ip[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &addr.sin_addr, ip, INET_ADDRSTRLEN);
|
||||
|
||||
perror("Message-type: 1 - 303\n");
|
||||
sprintf(reply, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%d%s\r\nContent-Length: 0\r\n\r\n", ip, req->node_port, request->uri);
|
||||
offset = strlen(reply);
|
||||
}
|
||||
}
|
||||
elif (strcmp(request->method, "GET") == 0) {
|
||||
size_t 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 {
|
||||
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
goto direct_send;
|
||||
}
|
||||
|
||||
neighbor responsible = check_neighborhood(resource_hash);
|
||||
|
||||
lookup_request *req = find_lookup(resource_hash);
|
||||
if (req != NULL)
|
||||
{
|
||||
if (req->message_type != 1)
|
||||
{
|
||||
printf("404\n");
|
||||
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
}
|
||||
else
|
||||
{
|
||||
// DONE: Success
|
||||
struct sockaddr_in addr;
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(req->node_ip.s_addr);
|
||||
char ip[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &addr.sin_addr, ip, INET_ADDRSTRLEN);
|
||||
|
||||
sprintf(reply, "HTTP/1.1 303 See Other\r\nLocation: http://%s:%d%s\r\nContent-Length: 0\r\n\r\n", ip, req->node_port, request->uri);
|
||||
offset = strlen(reply);
|
||||
printf("303\n");
|
||||
}
|
||||
}
|
||||
else{
|
||||
|
||||
if (responsible == res) {
|
||||
printf("Responsible - 404\n");
|
||||
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
}
|
||||
elif(responsible == res_303)
|
||||
{
|
||||
int suc_id = _SUCC_ID;
|
||||
if ((_NODE_ID < suc_id && resource_hash > _NODE_ID && resource_hash <= suc_id) || (_NODE_ID > suc_id && (resource_hash > _NODE_ID || resource_hash <= suc_id)))
|
||||
{
|
||||
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");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("404\n");
|
||||
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
}
|
||||
}
|
||||
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");
|
||||
|
||||
lookup_request lookreq;
|
||||
lookup_request_init(&lookreq, 0, _NODE_ID, ntohl(inet_addr(_NODE_IP)), _NODE_PORT, resource_hash);
|
||||
|
||||
call_network(&lookreq, ntohl(inet_addr(_SUCC_IP)), _SUCC_PORT);
|
||||
}
|
||||
else {
|
||||
printf("404\n");
|
||||
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
}
|
||||
}
|
||||
} else if (strcmp(request->method, "PUT") == 0) {
|
||||
// Try to set the requested resource with the given payload in the
|
||||
@@ -185,18 +176,20 @@ void send_reply(int conn, struct request *request) {
|
||||
if (set(request->uri, request->payload, request->payload_length,
|
||||
resources, MAX_RESOURCES)) {
|
||||
reply = "HTTP/1.1 204 No Content\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
} else {
|
||||
reply = "HTTP/1.1 201 Created\r\nContent-Length: 0\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
}
|
||||
offset = strlen(reply);
|
||||
} else if (strcmp(request->method, "DELETE") == 0) {
|
||||
// Try to delete the requested resource from the 'resources' array
|
||||
if (delete (request->uri, resources, MAX_RESOURCES)) {
|
||||
reply = "HTTP/1.1 204 No Content\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
} else {
|
||||
reply = "HTTP/1.1 404 Not Found\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
}
|
||||
offset = strlen(reply);
|
||||
} else {
|
||||
reply = "HTTP/1.1 501 Method Not Supported\r\n\r\n";
|
||||
offset = strlen(reply);
|
||||
@@ -204,11 +197,13 @@ void send_reply(int conn, struct request *request) {
|
||||
|
||||
direct_send: // ... do goto, yes you read that right ;)
|
||||
|
||||
//offset = strlen(reply);
|
||||
// Send the reply back to the client
|
||||
if (send(conn, reply, offset, 0) == -1) {
|
||||
perror("send");
|
||||
close(conn);
|
||||
}
|
||||
perror("\n\n---HANDLED REQUEST---\n\n");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -224,7 +219,7 @@ void send_reply(int conn, struct request *request) {
|
||||
* malformed or an error occurs during processing, the return value is -1.
|
||||
*
|
||||
*/
|
||||
size_t process_packet(int conn, char *buffer, size_t n) {
|
||||
int process_packet(int conn, char *buffer, size_t n) {
|
||||
struct request request = {
|
||||
.method = NULL, .uri = NULL, .payload = NULL, .payload_length = -1};
|
||||
ssize_t bytes_processed = parse_request(buffer, n, &request);
|
||||
@@ -429,27 +424,7 @@ static int setup_server_socket_udp(struct sockaddr_in addr){
|
||||
return sock;
|
||||
}
|
||||
|
||||
neighbor check_neighborhood(uint16_t hash) {
|
||||
int pre_id = _PRED_ID;
|
||||
int id = _NODE_ID;
|
||||
int suc_id = _SUCC_ID;
|
||||
|
||||
if(pre_id == suc_id){
|
||||
if(hash > id && hash <= suc_id) return res_303;
|
||||
return res;
|
||||
}
|
||||
|
||||
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 < suc_id && hash > id && hash <= suc_id) || (hash > id || hash <= suc_id))
|
||||
return not_res;
|
||||
|
||||
return dunno;
|
||||
}
|
||||
|
||||
|
||||
static void handle_udp_request(int _sock){
|
||||
static void handle_udp_request(){
|
||||
fprintf(stderr, "---%d---\n", _NODE_ID);
|
||||
size_t received_bytes = 0;
|
||||
lookup_request lr = {0};
|
||||
@@ -457,18 +432,18 @@ static void handle_udp_request(int _sock){
|
||||
struct sockaddr_storage caller_addr;
|
||||
socklen_t addrlen = sizeof caller_addr;
|
||||
|
||||
if((received_bytes = recvfrom(_sock, &lr, sizeof(lookup_request), 0, (struct sockaddr *) &caller_addr, &addrlen)) == -1){
|
||||
if((received_bytes = recvfrom(server_socket_udp, &lr, sizeof(lookup_request), 0, (struct sockaddr *) &caller_addr, &addrlen)) == -1){
|
||||
perror("Func: handle_udp_request - failed to receive data from caller!");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
perror("---HANDLE_UDP---\n");
|
||||
print_lookup_request(&lr);
|
||||
// perror("[Serialized]\n");
|
||||
// print_lookup_request(&lr);
|
||||
extract_lookup_request(&lr);
|
||||
perror("[Deserialized]\n");
|
||||
print_lookup_request(&lr);
|
||||
perror("----------------\n");
|
||||
|
||||
fprintf(stderr, "Received:\n\tMessage-type: %d\n\tlookup-hash: %hd\n", lr.message_type, lr.hash_id);
|
||||
|
||||
// DO LOOKUP
|
||||
if (lr.message_type == 0)
|
||||
{
|
||||
@@ -485,37 +460,22 @@ static void handle_udp_request(int _sock){
|
||||
|
||||
lookup_request backfire = {0};
|
||||
|
||||
if (lr.node_id == suc_id)
|
||||
{
|
||||
// Send back 404 not found
|
||||
clone_lookup_request(&backfire, &lr);
|
||||
backfire.message_type = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
const neighbor responsibility = check_neighborhood(lr.hash_id);
|
||||
|
||||
/* res is handles upfront by not res and suc responsibility check
|
||||
if (responsibility == res) {
|
||||
lookup_request_init(&backfire, 1, _NODE_ID, inet_addr(_NODE_IP), _NODE_PORT, _PRED_ID);
|
||||
const neighbor responsibility = check_neighborhood(lr.hash_id);
|
||||
if (responsibility != res) {
|
||||
// is succ responsible
|
||||
if (responsibility == res_303)
|
||||
{
|
||||
lookup_request_init(&backfire, 1, suc_id, suc_ip, _SUCC_PORT, _NODE_ID);
|
||||
}
|
||||
*/
|
||||
if (responsibility == not_res) {
|
||||
// is succ responsible
|
||||
if (lr.hash_id <= suc_id || (suc_id < _NODE_ID && lr.hash_id <= 65535))
|
||||
{
|
||||
lookup_request_init(&backfire, 1, suc_id, suc_ip, _SUCC_PORT, _NODE_ID);
|
||||
}
|
||||
else
|
||||
{
|
||||
// lookup_request_init(&backfire, 0, suc_id, suc_ip, _SUCC_PORT, _NODE_ID);
|
||||
clone_lookup_request(&backfire, &lr);
|
||||
else
|
||||
{
|
||||
clone_lookup_request(&backfire, &lr);
|
||||
|
||||
callback_ip = &suc_ip_s.s_addr;
|
||||
callback_port = _SUCC_PORT;
|
||||
}
|
||||
callback_ip = &suc_ip_s.s_addr;
|
||||
callback_port = _SUCC_PORT;
|
||||
}
|
||||
}
|
||||
|
||||
call_network(&backfire, *callback_ip, callback_port);
|
||||
}
|
||||
elif (lr.message_type == 1)
|
||||
@@ -527,10 +487,6 @@ static void handle_udp_request(int _sock){
|
||||
perror("PUSHED ");
|
||||
print_lookup_request(toLookup);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Message-Type: -1
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -611,8 +567,8 @@ 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);
|
||||
handle_udp_request();
|
||||
// sockets[1].events = 0;
|
||||
}
|
||||
else {
|
||||
assert(s == state.sock);
|
||||
|
||||
Reference in New Issue
Block a user