Added new interfaces.

I also optimized the node list a bit so as not to use more processing
power than necessary.
This commit is contained in:
Nicolás A. Ortega
2017-07-02 12:09:49 +02:00
parent 4f66f0cc96
commit da483c5ba4
3 changed files with 77 additions and 16 deletions

@ -40,18 +40,21 @@ static unsigned int node_max;
// Number of nodes in the list
static unsigned int node_count;
int init_nodes(unsigned int max_nodes) {
int NeoComm_init_nodes(unsigned int max_nodes) {
node_list = calloc(max_nodes, sizeof(struct node));
if(!node_list)
return 0;
for(unsigned int i = 0; i < max_nodes; ++i)
node_list[i].address = NULL;
node_max = max_nodes;
node_count = 0;
return 1;
}
int resize_node_list(unsigned int new_max_nodes) {
int NeoComm_resize_node_list(unsigned int new_max_nodes) {
if(new_max_nodes <= node_max)
return 0;
@ -64,15 +67,15 @@ int resize_node_list(unsigned int new_max_nodes) {
return 1;
}
unsigned int get_node_max() {
unsigned int NeoComm_get_node_max() {
return node_max;
}
unsigned int get_node_count() {
unsigned int NeoComm_get_node_count() {
return node_count;
}
int add_node(char *addr, unsigned short port) {
int NeoComm_add_node(char *addr, unsigned short port) {
if(node_count >= node_max)
return 0;
for(unsigned int i = 0; i < node_count; ++i)
@ -81,13 +84,39 @@ int add_node(char *addr, unsigned short port) {
node_list[i].port == port)
return 1;
}
node_list[node_count].address = addr;
node_list[node_count].port = port;
node_list[node_count].directory = 0;
node_list[node_count].connections = 0;
node_list[node_count].max_connections = 0;
for(unsigned int i = 0; i < node_max; ++i)
{
if(!node_list[i].address)
{
node_list[i].address = addr;
node_list[i].port = port;
node_list[i].directory = 0;
node_list[i].connections = 0;
node_list[i].max_connections = 0;
break;
}
}
node_count++;
return 1;
}
int NeoComm_remove_node(char *addr, unsigned short port) {
int removed = 0;
for(unsigned int i = 0; i < node_count; ++i)
{
if(!removed && strcmp(node_list[i].address, addr) == 0 &&
node_list[i].port == port)
{
node_list[i].address = NULL;
node_count--;
removed = 1;
break;
}
}
if(!removed)
return 0;
return 1;
}