Seems finished

This commit is contained in:
Ano-sys
2025-01-29 20:17:35 +01:00
parent e265ffa526
commit c0d2ffffd3
14 changed files with 184 additions and 122 deletions
+16 -46
View File
@@ -1,7 +1,6 @@
#include <zmq.h>
#include <ctype.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
@@ -21,7 +20,7 @@ int main(int argc, char **argv){
exit(1);
}
printf("Starting...\n");
fprintf(stderr, "Starting...\n");
int worker_index = 1;
int worker_count = argc - 1;
@@ -38,10 +37,10 @@ int main(int argc, char **argv){
exit(1);
}
worker_index++;
printf("Initialized Thread: %d\n", thread_count);
fprintf(stderr, "Initialized Thread: %d\n", thread_count);
}
printf("Init DONE\n");
fprintf(stderr, "Init DONE\n");
// wait for workers to finish
for(int i = 0; i < thread_count; i++){
@@ -102,10 +101,12 @@ void* workerFunc(void *args){
continue;
}
request[bytes_recv] = '\0';
char endpoint[256];
size_t endpoint_size = sizeof(endpoint);
if (zmq_getsockopt(responder, ZMQ_LAST_ENDPOINT, endpoint, &endpoint_size) == 0) {
printf("Got request from: %s\n", endpoint);
fprintf(stderr, "Got request from: %s\n", endpoint);
}
const TYPE type = get_request_type(request);
@@ -115,17 +116,17 @@ void* workerFunc(void *args){
switch(type){
case MAP:
printf("Handling MAP\n");
fprintf(stderr, "Handling MAP\n");
ret = map(content, &head, &tail);
break;
case RED:
printf("Handling RED\n");
fprintf(stderr, "Handling RED\n");
ret = reduce(content);
break;
case RIP:
printf("Handling RIP\n");
fprintf(stderr, "Handling RIP\n");
// send errors are redundant to handle because exit
zmq_send(responder, "rip", TYPE_LENGTH, 0);
zmq_send(responder, "rip", TYPE_LENGTH + 1, 0);
zmq_close(responder);
zmq_ctx_destroy(context);
exit(0);
@@ -136,14 +137,15 @@ void* workerFunc(void *args){
exit(1);
break;
}
printf("Sending content: %s\n", ret);
if(zmq_send(responder, ret, strlen(ret), 0) != 0){
fprintf(stderr, "Sending content: %s\n", ret);
if(zmq_send(responder, ret, strlen(ret), 0) < 0){
perror("Failed to send response");
zmq_close(responder);
zmq_ctx_destroy(context);
exit(1);
// exit(1);
}
free(ret); // NULL free is safe
ret = NULL;
} //loop
}
@@ -151,7 +153,7 @@ void add_word(const char *word, key_value_pair **head, key_value_pair **tail){
char *word_copy = strdup(word);
tolower_str(word_copy);
key_value_pair *kv = init_key_value_pair(word, "1");
key_value_pair *kv = init_key_value_pair(word_copy, "1");
if(kv == NULL){
fprintf(stderr, "Failed to allocate memory for key: %s\n", word);
free(word_copy);
@@ -190,38 +192,6 @@ char* get_word(const char *request){
return word;
}
char *key_value_pair_list_to_string(key_value_pair **head){
key_value_pair *kvp_it = *head;
char *ret = NULL;
while(kvp_it != NULL){
char *kvp2str = kvp_it->toString(kvp_it);
if(kvp2str == NULL){
kvp_it = kvp_it->next;
continue;
}
if(!ret){
ret = strdup(kvp2str);
kvp_it = kvp_it->next;
}
else{
char *tmp = realloc(ret, strlen(ret) + strlen(kvp2str) + 1);
if(!tmp){
free(ret);
perror("Failed to reallocate memory for ret");
free(kvp2str);
return NULL;
}
ret = tmp;
strcat(ret, kvp2str);
free(kvp2str);
kvp_it = kvp_it->next;
}
}
return ret;
}
char* map(const char *content, key_value_pair **head, key_value_pair **tail){
char *content_iterator = (char*)content;
void *content_start_address = (void*)content;
@@ -253,7 +223,7 @@ char* reduce(char *content){
key_value_pair *head = NULL;
key_value_pair *tail = NULL;
extract_key_value_pairs(content, &head, &tail);
extract_key_value_pairs(content, &head, &tail, true);
char *ret = key_value_pair_list_to_string(&head);
free_key_value_pairs(&head, &tail);