From e82514a56a9a6b332ca8e56555671b9c31f5dfa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Sat, 5 Aug 2017 16:29:45 -0500 Subject: [PATCH] Minor fixes with list. --- src/connectivity.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/connectivity.c b/src/connectivity.c index 4529742..ea13585 100644 --- a/src/connectivity.c +++ b/src/connectivity.c @@ -130,6 +130,7 @@ void *NeoComm_connect_manager() { NeoComm_error("Error on accept"); continue; } + ++num_nodes; // TODO: Add the new connection and have it listen on a new thread. } pthread_exit(NULL); @@ -187,11 +188,13 @@ void NeoComm_shutdown_directory() { pthread_join(node_list[i].thread, &res); close(node_list[i].sockfd); } + num_nodes = 0; pthread_join(accept_thread, &res); close(sockfd); free(node_list); node_list = NULL; + max_nodes = 0; pthread_exit(NULL); } @@ -221,6 +224,8 @@ int NeoComm_resize_directory(const unsigned int new_max_num_nodes) { for(unsigned int i = max_nodes; i < new_max_num_nodes; ++i) node_list[i].in_use = 0; + max_nodes = new_max_num_nodes; + return 1; } @@ -233,7 +238,7 @@ unsigned int NeoComm_get_num_nodes() { } int NeoComm_add_node(const struct NeoComm_address addr) { - struct hostent *host; + struct hostent *host = NULL; host = gethostbyname(addr.address); if(!host) { @@ -247,21 +252,29 @@ int NeoComm_add_node(const struct NeoComm_address addr) { if(!node_list[i].in_use) new_node = &node_list[i]; } + if(!new_node) + { + NeoComm_error("No more unused slots in directory"); + return 0; + } bzero((char*) &new_node->addr, sizeof(new_node->addr)); new_node->addr.sin_family = AF_INET; - bcopy((char*) host->h_addr, + bcopy((char*) host->h_addr_list[0], (char*) &new_node->addr.sin_addr.s_addr, host->h_length); new_node->addr.sin_port = htons(addr.port); - if(connect(new_node->sockfd, (struct sockaddr*) &new_node->addr, + if(connect(new_node->sockfd, + (struct sockaddr*) &new_node->addr, sizeof(new_node->addr)) < 0) { NeoComm_error("Failed to connect to new node"); return 0; } new_node->in_use = 1; + ++num_nodes; // TODO: listen to node on new thread + return 1; }