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
No known key found for this signature in database
GPG Key ID: 3D786FB3123FF1DD
3 changed files with 77 additions and 16 deletions

22
include/neocomm.h Normal file
View File

@ -0,0 +1,22 @@
/*
* Copyright (C) 2017 Ortega Froysa, Nicolás <deathsbreed@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <deathsbreed@themusicinnoise.net>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "nodes.h"

View File

@ -21,13 +21,15 @@
/** /**
* @brief Initialize the node list. * @brief Initialize the node list.
* @details This function allocates memory and prepares the nodes list, you
* must run this before using any other node related functions.
* *
* @param max_nodes Maximum number of nodes available. * @param max_nodes Maximum number of nodes available.
* *
* @return If the operation failed then it will return 0, else it will * @return If the operation failed then it will return 0, else it will
* return 1. * return 1.
*/ */
int init_nodes(unsigned int max_nodes); int NeoComm_init_nodes(unsigned int max_nodes);
/** /**
* @brief Enlarge the list. * @brief Enlarge the list.
@ -37,21 +39,21 @@ int init_nodes(unsigned int max_nodes);
* @return If the new size is less than or equal to the current size, or * @return If the new size is less than or equal to the current size, or
* the list failed to be reallocated, then it returns 0, else it returns 1. * the list failed to be reallocated, then it returns 0, else it returns 1.
*/ */
int resize_node_list(unsigned int new_max_nodes); int NeoComm_resize_node_list(unsigned int new_max_nodes);
/** /**
* @brief Get the maximum number of nodes that can be used. * @brief Get the maximum number of nodes that can be used.
* *
* @return The maximum number of nodes that can be used.. * @return The maximum number of nodes that can be used..
*/ */
unsigned int get_node_max(); unsigned int NeoComm_get_node_max();
/** /**
* @brief Get the number of nodes there currently are. * @brief Get the number of nodes there currently are.
* *
* @return The number of nodes remembered. * @return The number of nodes remembered.
*/ */
unsigned int get_node_count(); unsigned int NeoComm_get_node_count();
/** /**
* @brief Add a new node to the list with an address (normally IP) and port. * @brief Add a new node to the list with an address (normally IP) and port.
@ -61,6 +63,14 @@ unsigned int get_node_count();
* *
* @return If the node list is full it will return 0, else 1. * @return If the node list is full it will return 0, else 1.
*/ */
int add_node(char *addr, unsigned short port); int NeoComm_add_node(char *addr, unsigned short port);
int remove_node(char *addr, unsigned short port); /**
* @brief Remove a node from the list.
*
* @param addr The address of the node.
* @param port The public port of the node.
*
* @return If the node was not found it returns 0, else it returns 1.
*/
int NeoComm_remove_node(char *addr, unsigned short port);

View File

@ -40,18 +40,21 @@ static unsigned int node_max;
// Number of nodes in the list // Number of nodes in the list
static unsigned int node_count; 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)); node_list = calloc(max_nodes, sizeof(struct node));
if(!node_list) if(!node_list)
return 0; return 0;
for(unsigned int i = 0; i < max_nodes; ++i)
node_list[i].address = NULL;
node_max = max_nodes; node_max = max_nodes;
node_count = 0; node_count = 0;
return 1; 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) if(new_max_nodes <= node_max)
return 0; return 0;
@ -64,15 +67,15 @@ int resize_node_list(unsigned int new_max_nodes) {
return 1; return 1;
} }
unsigned int get_node_max() { unsigned int NeoComm_get_node_max() {
return node_max; return node_max;
} }
unsigned int get_node_count() { unsigned int NeoComm_get_node_count() {
return 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) if(node_count >= node_max)
return 0; return 0;
for(unsigned int i = 0; i < node_count; ++i) 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) node_list[i].port == port)
return 1; return 1;
} }
node_list[node_count].address = addr;
node_list[node_count].port = port; for(unsigned int i = 0; i < node_max; ++i)
node_list[node_count].directory = 0; {
node_list[node_count].connections = 0; if(!node_list[i].address)
node_list[node_count].max_connections = 0; {
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++; node_count++;
return 1; 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;
}