Adding a node returns a pointer to the new node.

This should help to edit aspects of it, such as whether or not it is a
directory and such.
This commit is contained in:
Nicolás A. Ortega
2017-07-02 13:06:36 +02:00
parent 605a91bc41
commit cafe6a7243
2 changed files with 64 additions and 38 deletions

View File

@ -22,17 +22,6 @@
#include <stdlib.h>
#include <string.h>
struct node {
// The address of the node
char *address;
unsigned short port;
// Whether or not it is a directory node/server
int directory;
// Useful variables for passing on connections of new nodes
unsigned int connections;
unsigned int max_connections;
};
// List of nodes
static struct node *node_list;
// Maximum number of nodes
@ -46,7 +35,7 @@ int NeoComm_init_nodes(unsigned int max_nodes) {
return 0;
for(unsigned int i = 0; i < max_nodes; ++i)
node_list[i].address = NULL;
node_list[i].address.address = NULL;
node_max = max_nodes;
node_count = 0;
@ -75,42 +64,41 @@ unsigned int NeoComm_get_node_count() {
return node_count;
}
int NeoComm_add_node(char *addr, unsigned short port) {
struct node *NeoComm_add_node(struct address addr) {
if(node_count >= node_max)
return 0;
for(unsigned int i = 0; i < node_count; ++i)
return NULL;
for(unsigned int i = 0; i < node_max; ++i)
{
if(strcmp(node_list[i].address, addr) == 0 &&
node_list[i].port == port)
return 1;
if(strcmp(node_list[i].address.address, addr.address) == 0 &&
node_list[i].address.port == addr.port)
return &node_list[i];
}
for(unsigned int i = 0; i < node_max; ++i)
{
if(!node_list[i].address)
if(!node_list[i].address.address)
{
node_list[i].address = addr;
node_list[i].port = port;
node_list[i].address.address = addr.address;
node_list[i].address.port = addr.port;
node_list[i].directory = 0;
node_list[i].connections = 0;
node_list[i].max_connections = 0;
break;
node_count++;
return &node_list[i];
}
}
node_count++;
return 1;
return NULL;
}
int NeoComm_remove_node(char *addr, unsigned short port) {
int NeoComm_remove_node(struct address addr) {
int removed = 0;
for(unsigned int i = 0; i < node_count; ++i)
for(unsigned int i = 0; i < node_max; ++i)
{
if(!removed && strcmp(node_list[i].address, addr) == 0 &&
node_list[i].port == port)
if(!removed && strcmp(node_list[i].address.address,
addr.address) == 0 &&
node_list[i].address.port == addr.port)
{
node_list[i].address = NULL;
node_list[i].address.address = NULL;
node_count--;
removed = 1;
break;
@ -120,3 +108,14 @@ int NeoComm_remove_node(char *addr, unsigned short port) {
return 0;
return 1;
}
struct node *NeoComm_get_node(struct address addr) {
for(unsigned int i = 0; i < node_max; ++i)
{
if(strcmp(node_list[i].address.address, addr.address) == 0 &&
node_list[i].address.port == addr.port) {
return &node_list[i];
}
}
return NULL;
}