diff --git a/src/connectivity.c b/src/connectivity.c index acc7cbb..bd7c703 100644 --- a/src/connectivity.c +++ b/src/connectivity.c @@ -34,7 +34,7 @@ #include /* - * This is an internal OS specific structure for nodes. + * This is an internal structure for nodes. */ struct NeoComm_directory_node { int in_use; @@ -46,11 +46,11 @@ struct NeoComm_directory_node { unsigned int max_connections; }; -static struct NeoComm_directory_node *node_list; +static struct NeoComm_directory_node *node_list = NULL; static unsigned int max_nodes; static unsigned int num_nodes; -static int run = 0; +static int run; static int sockfd; static pthread_t accept_thread; @@ -151,7 +151,7 @@ void *NeoComm_connect_manager() { int NeoComm_init_directory(const unsigned int max_num_nodes, const unsigned short portnum) { - if(run) + if(node_list) { NeoComm_error("Already running"); return 0; @@ -191,6 +191,8 @@ int NeoComm_init_directory(const unsigned int max_num_nodes, } void NeoComm_shutdown_directory() { + if(!node_list) + return; run = 0; void *res; for(unsigned int i = 0; i < num_nodes; ++i) @@ -201,5 +203,45 @@ void NeoComm_shutdown_directory() { } pthread_join(accept_thread, &res); close(sockfd); + + free(node_list); + node_list = NULL; + pthread_exit(NULL); } + +int NeoComm_resize_directory(const unsigned int new_max_num_nodes) { + if(!node_list) + { + NeoComm_error("Directory not initialized"); + return 0; + } + if(new_max_num_nodes == max_nodes) + return 1; + else if(new_max_num_nodes < max_nodes) + { + NeoComm_error("Cannot downsize directory size"); + return 0; + } + + void *tmp_list = realloc(node_list, + new_max_num_nodes * sizeof(struct NeoComm_directory_node)); + if(!tmp_list) + { + NeoComm_error("Failed to allocate more memory"); + return 0; + } + + for(unsigned int i = max_nodes; i < new_max_num_nodes; ++i) + node_list[i].in_use = 0; + + return 1; +} + +unsigned int NeoComm_get_directory_size() { + return max_nodes; +} + +unsigned int NeoComm_get_num_nodes() { + return num_nodes; +}