Better half time
This commit is contained in:
+16
-38
@@ -110,61 +110,46 @@ void *receiverFunc(void *args)
|
||||
}
|
||||
pthread_mutex_lock(&context_mutex);
|
||||
void *requester = zmq_socket(context, ZMQ_REQ);
|
||||
pthread_mutex_unlock(&context_mutex);
|
||||
if(!requester){
|
||||
// perror("[THREAD] Failed to create socket");
|
||||
free(params->data);
|
||||
free(params);
|
||||
pthread_mutex_unlock(&context_mutex);
|
||||
zmq_ctx_destroy(context);
|
||||
return NULL;
|
||||
}
|
||||
pthread_mutex_unlock(&context_mutex);
|
||||
|
||||
int timeout = SEND_RECV_TIMEOUT;
|
||||
zmq_setsockopt(requester, ZMQ_RCVTIMEO, &timeout, sizeof(timeout));
|
||||
zmq_setsockopt(requester, ZMQ_SNDTIMEO, &timeout, sizeof(timeout));
|
||||
|
||||
char addr[22];
|
||||
snprintf(addr, 21, "tcp://localhost:%d", params->port); // BUG: FIX PORT 0
|
||||
|
||||
pthread_mutex_lock(&context_mutex);
|
||||
int rc = zmq_connect(requester, addr);
|
||||
if (rc != 0){
|
||||
// perror("Failed to connect");
|
||||
free(params->data);
|
||||
free(params);
|
||||
zmq_close(requester);
|
||||
pthread_mutex_unlock(&context_mutex);
|
||||
return NULL;
|
||||
}
|
||||
pthread_mutex_unlock(&context_mutex);
|
||||
// fprintf(stderr, "[THREAD] Connected to worker: %s\n", addr);
|
||||
|
||||
char *data = NULL;
|
||||
if(params->type == MAP) data = sanitize_input(params->data);
|
||||
else data = strdup(params->data);
|
||||
free(params->data);
|
||||
|
||||
if(!data){
|
||||
// perror("[THREAD] Failed to sanitize input");
|
||||
free(params->data);
|
||||
free(params);
|
||||
zmq_close(requester);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
free(params->data);
|
||||
|
||||
size_t len = strlen(data) + 4;
|
||||
char request[len + 1];
|
||||
|
||||
/*
|
||||
if(!request){
|
||||
// perror("[THREAD] Failed to allocate memory for request");
|
||||
free(params);
|
||||
free(data);
|
||||
zmq_close(requester);
|
||||
zmq_ctx_destroy(context);
|
||||
return NULL;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
switch(params->type){
|
||||
case MAP:
|
||||
snprintf(request, len, "map%s", data);
|
||||
@@ -187,21 +172,15 @@ void *receiverFunc(void *args)
|
||||
free(data);
|
||||
|
||||
// fprintf(stderr, "[THREAD] Send request: %s\n", request);
|
||||
pthread_mutex_lock(&context_mutex);
|
||||
if(zmq_send(requester, request, strlen(request) + 1, 0) == -1){
|
||||
// perror("[THREAD] Failed to send request");
|
||||
free(params);
|
||||
// free(request);
|
||||
zmq_close(requester);
|
||||
pthread_mutex_unlock(&context_mutex);
|
||||
return NULL;
|
||||
}
|
||||
pthread_mutex_unlock(&context_mutex);
|
||||
|
||||
// free(request);
|
||||
|
||||
// fprintf(stderr, "[THREAD]: Waiting for response\n");
|
||||
char reply[MESSAGE_LENGTH + 1];
|
||||
char reply[MESSAGE_LENGTH];
|
||||
int bytes_recv = zmq_recv(requester, reply, MESSAGE_LENGTH, 0);
|
||||
if (bytes_recv == -1){
|
||||
// perror("[THREAD] Failed to receive reply");
|
||||
@@ -210,7 +189,7 @@ void *receiverFunc(void *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
reply[bytes_recv] = '\0';
|
||||
// reply[bytes_recv] = '\0';
|
||||
|
||||
if(strcmp(reply, "rip") == 0){
|
||||
// perror("[THREAD] Got rip -> shutting down");
|
||||
@@ -219,15 +198,15 @@ void *receiverFunc(void *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&words_mutex);
|
||||
|
||||
len = strlen(reply);
|
||||
pthread_mutex_lock(&words_mutex);
|
||||
if(!words_list){
|
||||
words_list = strdup(reply);
|
||||
if(!words_list){
|
||||
// perror("[THREAD] Failed to allocate memory for words_list");
|
||||
free(params);
|
||||
pthread_mutex_unlock(&words_mutex);
|
||||
free(params);
|
||||
zmq_close(requester);
|
||||
return NULL;
|
||||
}
|
||||
@@ -236,20 +215,19 @@ void *receiverFunc(void *args)
|
||||
char *tmp = realloc(words_list , sizeof(char) * (strlen(words_list) + len + 1));
|
||||
if(tmp == NULL){
|
||||
// perror("[THREAD] Failed to realloc memory for words_list");
|
||||
free(params);
|
||||
pthread_mutex_unlock(&words_mutex);
|
||||
free(params);
|
||||
zmq_close(requester);
|
||||
return NULL;
|
||||
|
||||
}
|
||||
words_list = tmp;
|
||||
strcat(words_list, reply);
|
||||
strncat(words_list, reply, len + 1);
|
||||
}
|
||||
|
||||
free(params);
|
||||
|
||||
pthread_mutex_unlock(&words_mutex);
|
||||
|
||||
free(params);
|
||||
zmq_close(requester);
|
||||
|
||||
return NULL;
|
||||
@@ -358,7 +336,7 @@ void map(size_t worker_count, char **worker_ports, const char *filename, void *c
|
||||
size_t offset = 0;
|
||||
|
||||
while (offset < total_len) {
|
||||
size_t chunk_size = (total_len - offset >= PAYLOAD_LENGTH) ? PAYLOAD_LENGTH : (total_len - offset);
|
||||
size_t chunk_size = (total_len - offset > PAYLOAD_LENGTH) ? PAYLOAD_LENGTH - 1 : (total_len - offset);
|
||||
|
||||
if (offset + chunk_size < total_len) {
|
||||
size_t new_chunk_size = chunk_size;
|
||||
@@ -426,7 +404,7 @@ void map(size_t worker_count, char **worker_ports, const char *filename, void *c
|
||||
char *extract_reduce_string(const char * toExtract){
|
||||
if(!toExtract) return NULL;
|
||||
size_t toExtract_len = strlen(toExtract);
|
||||
size_t max_len = toExtract_len < MESSAGE_LENGTH ? toExtract_len : MESSAGE_LENGTH;
|
||||
size_t max_len = toExtract_len < MESSAGE_LENGTH ? toExtract_len : MESSAGE_LENGTH - 1;
|
||||
|
||||
char *end_ptr = (char*)toExtract + max_len - 1;
|
||||
while(end_ptr > toExtract && isalpha(*end_ptr)) end_ptr--;
|
||||
@@ -462,7 +440,7 @@ void reduce(const char *toReduce, size_t worker_count, char **worker_ports, void
|
||||
|
||||
while (offset < toReduce_len) {
|
||||
size_t remaining = toReduce_len - offset;
|
||||
size_t chunk_size = (remaining >= PAYLOAD_LENGTH) ? PAYLOAD_LENGTH : remaining;
|
||||
size_t chunk_size = (remaining > PAYLOAD_LENGTH) ? PAYLOAD_LENGTH - 1 : remaining;
|
||||
|
||||
if (offset + chunk_size < toReduce_len) {
|
||||
size_t adjust = chunk_size;
|
||||
|
||||
Reference in New Issue
Block a user