Added new interfaces.

- Resize directory list
 - Get size of the directory
 - Get the number of nodes in directory
 - Fixed bug in shutdown allowing to start back up
This commit is contained in:
Nicolás A. Ortega 2017-08-01 16:42:30 -05:00
parent 84ba3aa83c
commit 874a33b687
No known key found for this signature in database
GPG Key ID: 3D786FB3123FF1DD

View File

@ -34,7 +34,7 @@
#include <stdlib.h> #include <stdlib.h>
/* /*
* This is an internal OS specific structure for nodes. * This is an internal structure for nodes.
*/ */
struct NeoComm_directory_node { struct NeoComm_directory_node {
int in_use; int in_use;
@ -46,11 +46,11 @@ struct NeoComm_directory_node {
unsigned int max_connections; 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 max_nodes;
static unsigned int num_nodes; static unsigned int num_nodes;
static int run = 0; static int run;
static int sockfd; static int sockfd;
static pthread_t accept_thread; static pthread_t accept_thread;
@ -151,7 +151,7 @@ void *NeoComm_connect_manager() {
int NeoComm_init_directory(const unsigned int max_num_nodes, int NeoComm_init_directory(const unsigned int max_num_nodes,
const unsigned short portnum) { const unsigned short portnum) {
if(run) if(node_list)
{ {
NeoComm_error("Already running"); NeoComm_error("Already running");
return 0; return 0;
@ -191,6 +191,8 @@ int NeoComm_init_directory(const unsigned int max_num_nodes,
} }
void NeoComm_shutdown_directory() { void NeoComm_shutdown_directory() {
if(!node_list)
return;
run = 0; run = 0;
void *res; void *res;
for(unsigned int i = 0; i < num_nodes; ++i) for(unsigned int i = 0; i < num_nodes; ++i)
@ -201,5 +203,45 @@ void NeoComm_shutdown_directory() {
} }
pthread_join(accept_thread, &res); pthread_join(accept_thread, &res);
close(sockfd); close(sockfd);
free(node_list);
node_list = NULL;
pthread_exit(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;
}