Changed neighborhood checks -> DONE

This commit is contained in:
Ano-sys
2025-01-12 00:45:33 +01:00
parent f7c999a16b
commit f16df9bd21
+38 -14
View File
@@ -52,12 +52,13 @@ typedef enum neighbor{
uint16_t hash_uri(const char *uri){ uint16_t hash_uri(const char *uri){
char *uri_ptr = (char*)uri; char *uri_ptr = (char*)uri;
/*
char dynamic[] = "/dynamic/"; char dynamic[] = "/dynamic/";
char static_[] = "/static/"; char static_[] = "/static/";
if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic) + 1; if(strstr(uri, dynamic)) uri_ptr += strlen(dynamic) + 1;
elif(strstr(uri, static_)) uri_ptr += strlen(static_) + 1; elif(strstr(uri, static_)) uri_ptr += strlen(static_) + 1;
*/
/* /*
char *it = (char*)uri; char *it = (char*)uri;
@@ -77,21 +78,37 @@ uint16_t hash_uri(const char *uri){
return pseudo_hash((const unsigned char *)uri_ptr, strlen(uri_ptr)); return pseudo_hash((const unsigned char *)uri_ptr, strlen(uri_ptr));
} }
neighbor check_neighborhood(uint16_t hash) { #include <stdbool.h>
int pre_id = _PRED_ID; #include <stdint.h>
int id = _NODE_ID;
int suc_id = _SUCC_ID;
if(hash > id && hash <= suc_id) return res_303; static inline bool in_range(uint16_t me, uint16_t suc, uint16_t hash) {
if (me < suc) {
if ((pre_id == id && suc_id == id) || (id < pre_id && ((hash > pre_id && hash < 65535)) || hash <= id) || (hash > pre_id && hash <= id)) return (hash > me && hash <= suc);
return res;
if ((id < suc_id && hash > id && hash <= suc_id) || (hash > id || hash <= suc_id))
return not_res;
return dunno;
} }
elif (me > suc) {
return (hash > me || hash <= suc);
}
else {
return false;
}
}
neighbor check_neighborhood(uint16_t hash) {
uint16_t pred_id = _PRED_ID;
uint16_t id = _NODE_ID;
uint16_t suc_id = _SUCC_ID;
if (in_range(id, suc_id, hash)) {
return res_303;
}
else if (in_range(pred_id, id, hash)) {
return res;
}
else {
return not_res;
}
}
/** /**
* Sends an HTTP reply to the client based on the received request. * Sends an HTTP reply to the client based on the received request.
@@ -163,10 +180,12 @@ void send_reply(int conn, struct request *request) {
const char* resource = get(request->uri, resources, MAX_RESOURCES, &resource_length); const char* resource = get(request->uri, resources, MAX_RESOURCES, &resource_length);
if (resource) { if (resource) {
perror("Sending 200\n");
size_t payload_offset = sprintf(reply, "HTTP/1.1 200 OK\r\nContent-Length: %lu\r\n\r\n", resource_length); 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); memcpy(reply + payload_offset, resource, resource_length);
offset = payload_offset + resource_length; offset = payload_offset + resource_length;
} else { } else {
perror("Sending 404\n");
reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; reply = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
offset = strlen(reply); offset = strlen(reply);
} }
@@ -175,22 +194,27 @@ void send_reply(int conn, struct request *request) {
// 'resources' array. // 'resources' array.
if (set(request->uri, request->payload, request->payload_length, if (set(request->uri, request->payload, request->payload_length,
resources, MAX_RESOURCES)) { resources, MAX_RESOURCES)) {
perror("Sending 204\n");
reply = "HTTP/1.1 204 No Content\r\n\r\n"; reply = "HTTP/1.1 204 No Content\r\n\r\n";
offset = strlen(reply); offset = strlen(reply);
} else { } else {
perror("Sending 201\n");
reply = "HTTP/1.1 201 Created\r\nContent-Length: 0\r\n\r\n"; 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) { } else if (strcmp(request->method, "DELETE") == 0) {
// Try to delete the requested resource from the 'resources' array // Try to delete the requested resource from the 'resources' array
if (delete (request->uri, resources, MAX_RESOURCES)) { if (delete (request->uri, resources, MAX_RESOURCES)) {
perror("Sending 205\n");
reply = "HTTP/1.1 204 No Content\r\n\r\n"; reply = "HTTP/1.1 204 No Content\r\n\r\n";
offset = strlen(reply); offset = strlen(reply);
} else { } else {
perror("Sending 404 - DELETE\n");
reply = "HTTP/1.1 404 Not Found\r\n\r\n"; reply = "HTTP/1.1 404 Not Found\r\n\r\n";
offset = strlen(reply); offset = strlen(reply);
} }
} else { } else {
perror("Sending 501\n");
reply = "HTTP/1.1 501 Method Not Supported\r\n\r\n"; reply = "HTTP/1.1 501 Method Not Supported\r\n\r\n";
offset = strlen(reply); offset = strlen(reply);
} }