No memory leaks anymore
This commit is contained in:
+34
-33
@@ -7,20 +7,17 @@
|
||||
#include "key_value_pair.h"
|
||||
#include "transfer_defines.h"
|
||||
|
||||
#define tolower_str(word) for(int i = 0; i < strlen(word); i++) word[i] = tolower(word[i]);
|
||||
|
||||
void* workerFunc(void *args);
|
||||
|
||||
char* map(const char *content, key_value_pair **head, key_value_pair **tail);
|
||||
char* reduce(char *);
|
||||
|
||||
int main(int argc, char **argv){
|
||||
if(argc < 2){
|
||||
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
|
||||
// fprintf(stderr, "Usage: %s <port>\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fprintf(stderr, "Starting...\n");
|
||||
// fprintf(stderr, "Starting...\n");
|
||||
|
||||
int worker_index = 1;
|
||||
int worker_count = argc - 1;
|
||||
@@ -31,17 +28,18 @@ int main(int argc, char **argv){
|
||||
while(thread_count < worker_count){
|
||||
int *port = malloc(sizeof(int));
|
||||
*port = atoi(argv[worker_index]);
|
||||
if(pthread_create(&threads[thread_count++], NULL, workerFunc, (void*)port) != 0){
|
||||
perror("[MAIN] Failed to create thread");
|
||||
if(pthread_create(&threads[thread_count], NULL, workerFunc, (void*)port) != 0){
|
||||
// perror("[MAIN] Failed to create thread");
|
||||
free(port);
|
||||
free(threads);
|
||||
exit(1);
|
||||
}
|
||||
thread_count++;
|
||||
worker_index++;
|
||||
fprintf(stderr, "Initialized Thread: %d\n", thread_count);
|
||||
// fprintf(stderr, "Initialized Thread: %d\n", thread_count);
|
||||
}
|
||||
|
||||
fprintf(stderr, "Init DONE\n");
|
||||
// fprintf(stderr, "Init DONE\n");
|
||||
|
||||
// wait for workers to finish
|
||||
for(int i = 0; i < thread_count; i++){
|
||||
@@ -67,7 +65,7 @@ TYPE get_request_type(const char *request){
|
||||
|
||||
void* workerFunc(void *args){
|
||||
if(!args){
|
||||
perror("[THREAD] Invalid arguments");
|
||||
// perror("[THREAD] Invalid arguments");
|
||||
return NULL;
|
||||
}
|
||||
int port = *(int*)args;
|
||||
@@ -75,12 +73,12 @@ void* workerFunc(void *args){
|
||||
|
||||
void *context = zmq_ctx_new();
|
||||
if(!context){
|
||||
perror("[THREAD] Failed to create context");
|
||||
// perror("[THREAD] Failed to create context");
|
||||
return NULL;
|
||||
}
|
||||
void *responder = zmq_socket(context, ZMQ_REP);
|
||||
if(!responder){
|
||||
perror("[THREAD] Failed to create socket");
|
||||
// perror("[THREAD] Failed to create socket");
|
||||
zmq_ctx_destroy(context);
|
||||
return NULL;
|
||||
}
|
||||
@@ -90,7 +88,7 @@ void* workerFunc(void *args){
|
||||
|
||||
int rc = zmq_bind(responder, addr);
|
||||
if(rc != 0){
|
||||
perror("Failed to connect");
|
||||
// perror("Failed to connect");
|
||||
zmq_close(responder);
|
||||
zmq_ctx_destroy(context);
|
||||
return NULL;
|
||||
@@ -103,19 +101,21 @@ void* workerFunc(void *args){
|
||||
char request[MESSAGE_LENGTH + 1] = {0};
|
||||
int bytes_recv = zmq_recv(responder, request, MESSAGE_LENGTH, 0);
|
||||
if(bytes_recv < 3){ // minimal receive is TYPE
|
||||
perror("Failed to receive valid request");
|
||||
// perror("Failed to receive valid request");
|
||||
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) {
|
||||
fprintf(stderr, "Got request on: %s\n", endpoint);
|
||||
// fprintf(stderr, "Got request on: %s\n", endpoint);
|
||||
}
|
||||
*/
|
||||
|
||||
fprintf(stderr, "Received: %s\n", request);
|
||||
// fprintf(stderr, "Received: %s\n", request);
|
||||
|
||||
const TYPE type = get_request_type(request);
|
||||
char *content = request + TYPE_LENGTH;
|
||||
@@ -124,34 +124,35 @@ void* workerFunc(void *args){
|
||||
|
||||
switch(type){
|
||||
case MAP:
|
||||
fprintf(stderr, "Handling MAP\n");
|
||||
// fprintf(stderr, "Handling MAP\n");
|
||||
ret = map(content, &head, &tail);
|
||||
break;
|
||||
case RED:
|
||||
fprintf(stderr, "Handling RED\n");
|
||||
// fprintf(stderr, "Handling RED\n");
|
||||
ret = reduce(content);
|
||||
break;
|
||||
case RIP:
|
||||
fprintf(stderr, "Handling RIP\n");
|
||||
// fprintf(stderr, "Handling RIP\n");
|
||||
// send errors are redundant to handle because exit
|
||||
zmq_send(responder, "rip", TYPE_LENGTH + 1, 0);
|
||||
zmq_close(responder);
|
||||
zmq_ctx_destroy(context);
|
||||
exit(0);
|
||||
return NULL;
|
||||
break;
|
||||
case UNSUPPORTED:
|
||||
perror("Got unsupported request\n");
|
||||
// perror("Got unsupported request\n");
|
||||
// rip();
|
||||
exit(1);
|
||||
zmq_close(responder);
|
||||
zmq_ctx_destroy(context);
|
||||
return NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Sending content: %s\n", ret);
|
||||
if(zmq_send(responder, ret, strlen(ret), 0) < 0){
|
||||
perror("Failed to send response");
|
||||
// fprintf(stderr, "Sending content: %s\n", ret);
|
||||
if(zmq_send(responder, ret, strlen(ret) + 1, 0) < 0){
|
||||
// perror("Failed to send response");
|
||||
zmq_close(responder);
|
||||
zmq_ctx_destroy(context);
|
||||
// exit(1);
|
||||
}
|
||||
free(ret); // NULL free is safe
|
||||
ret = NULL;
|
||||
@@ -164,12 +165,12 @@ void add_word(const char *word, key_value_pair **head, key_value_pair **tail){
|
||||
|
||||
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);
|
||||
// fprintf(stderr, "Failed to allocate memory for key: %s\n", word);
|
||||
free(word_copy);
|
||||
return;
|
||||
}
|
||||
if(add_key_value_pair(kv, head, tail) != 0){
|
||||
fprintf(stderr, "Failed to add key value: %s\n", word);
|
||||
if(add_key_value_pair(kv, head, tail, true) != 0){
|
||||
// fprintf(stderr, "Failed to add key value: %s\n", word);
|
||||
free(word_copy);
|
||||
return;
|
||||
}
|
||||
@@ -204,10 +205,10 @@ char* get_word(const char *request){
|
||||
char* map(const char *content, key_value_pair **head, key_value_pair **tail){
|
||||
if(!content) return NULL;
|
||||
char *content_iterator = (char*)content;
|
||||
void *content_start_address = (void*)content;
|
||||
char *content_start_address = (char*)content;
|
||||
size_t content_length = strlen(content);
|
||||
|
||||
while(content_iterator != NULL && content_iterator < content_start_address + content_length){
|
||||
while(content_iterator && content_iterator < content_start_address + content_length){
|
||||
char *word = get_word(content_iterator);
|
||||
if(word != NULL){
|
||||
if(strcmp(word, "") != 0){
|
||||
@@ -221,8 +222,8 @@ char* map(const char *content, key_value_pair **head, key_value_pair **tail){
|
||||
char *ret = key_value_pair_list_to_string(head);
|
||||
|
||||
if(!ret){
|
||||
perror("Could not extract key value pairs");
|
||||
return NULL;
|
||||
// perror("Could not extract key value pairs");
|
||||
return strdup("");
|
||||
}
|
||||
free_key_value_pairs(head, tail);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user