Remove old interfaces
This commit is contained in:
@ -1,20 +0,0 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include "neocomm/neocomm.h"
|
168
src/nodes.c
168
src/nodes.c
@ -1,168 +0,0 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include "neocomm/nodes.h"
|
||||
|
||||
#include "internal_error.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
// List of nodes
|
||||
static struct NeoComm_node *node_list;
|
||||
// Maximum number of nodes
|
||||
static unsigned int node_max;
|
||||
// Number of nodes in the list
|
||||
static unsigned int node_count;
|
||||
|
||||
int NeoComm_init_nodes(unsigned int max_nodes) {
|
||||
if(max_nodes == 0)
|
||||
{
|
||||
NeoComm_error("Insufficient number of maximum nodes");
|
||||
return 0;
|
||||
}
|
||||
else if(node_max != 0)
|
||||
{
|
||||
NeoComm_error("Node list already initialized");
|
||||
return 0;
|
||||
}
|
||||
|
||||
node_list = calloc(max_nodes, sizeof(struct NeoComm_node));
|
||||
if(!node_list)
|
||||
{
|
||||
NeoComm_error("Failed to allocate memory to node list");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Set to NULL to check for free spots
|
||||
for(unsigned int i = 0; i < max_nodes; ++i)
|
||||
node_list[i].address.address = NULL;
|
||||
|
||||
node_max = max_nodes;
|
||||
node_count = 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void NeoComm_shutdown_nodes() {
|
||||
if(!node_list)
|
||||
return;
|
||||
free(node_list);
|
||||
node_max = 0;
|
||||
node_count = 0;
|
||||
}
|
||||
|
||||
int NeoComm_resize_node_list(unsigned int new_max_nodes) {
|
||||
if(node_max == 0)
|
||||
{
|
||||
NeoComm_error("Node list has not yet been initialized");
|
||||
return 0;
|
||||
}
|
||||
else if(new_max_nodes < node_max)
|
||||
{
|
||||
NeoComm_error("Cannot downsize node list");
|
||||
return 0;
|
||||
}
|
||||
|
||||
assert(node_list);
|
||||
void* tmp_list =
|
||||
realloc(node_list, new_max_nodes * sizeof(struct NeoComm_node));
|
||||
if(!tmp_list)
|
||||
{
|
||||
NeoComm_error("Failed to resize list, continuing with old size");
|
||||
return 0;
|
||||
}
|
||||
|
||||
node_list = tmp_list;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned int NeoComm_get_node_max() {
|
||||
return node_max;
|
||||
}
|
||||
|
||||
unsigned int NeoComm_get_node_count() {
|
||||
return node_count;
|
||||
}
|
||||
|
||||
struct NeoComm_node *NeoComm_add_node(struct NeoComm_address addr) {
|
||||
assert(node_list);
|
||||
if(node_count >= node_max)
|
||||
{
|
||||
NeoComm_error("Reached maximum number of nodes");
|
||||
return NULL;
|
||||
}
|
||||
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];
|
||||
}
|
||||
|
||||
struct NeoComm_node *new_node = NULL;
|
||||
for(unsigned int i = 0; i < node_max; ++i)
|
||||
{
|
||||
if(!node_list[i].address.address)
|
||||
{
|
||||
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;
|
||||
node_count++;
|
||||
new_node = &node_list[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new_node;
|
||||
}
|
||||
|
||||
int NeoComm_remove_node(struct NeoComm_address addr) {
|
||||
assert(node_list);
|
||||
int removed = 0;
|
||||
for(unsigned int i = 0; i < node_max; ++i)
|
||||
{
|
||||
if(!removed && strcmp(node_list[i].address.address,
|
||||
addr.address) == 0 &&
|
||||
node_list[i].address.port == addr.port)
|
||||
{
|
||||
node_list[i].address.address = NULL;
|
||||
node_count--;
|
||||
removed = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!removed)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct NeoComm_node *NeoComm_get_node(struct NeoComm_address addr) {
|
||||
assert(node_list);
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user