Remove old interfaces
This commit is contained in:
parent
874a33b687
commit
03156e45de
@ -41,8 +41,7 @@ include_directories(
|
||||
|
||||
set(SRCS
|
||||
src/connectivity.c
|
||||
src/error.c
|
||||
src/neocomm.c)
|
||||
src/error.c)
|
||||
|
||||
set(CMAKE_C_FLAGS "-std=c99 -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")
|
||||
set(CMAKE_C_FLAGS_DEBUG "-g -O0")
|
||||
|
@ -21,23 +21,3 @@
|
||||
|
||||
#include "neocomm/error.h"
|
||||
#include "neocomm/connectivity.h"
|
||||
|
||||
/**
|
||||
* @file neocomm.h
|
||||
* @brief Automated functions that manage the entire NeoComm framework.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Initializes all modules of the NeoComm framework.
|
||||
*
|
||||
* @param max_nodes Maximum number of nodes in directory.
|
||||
*
|
||||
* @return If all modules were initialized successfully then a 1 is returned,
|
||||
* else 0 is returned.
|
||||
*/
|
||||
int NeoComm_init(unsigned int max_nodes);
|
||||
|
||||
/**
|
||||
* @brief Disconnect all currently active connections and shutdown all modules.
|
||||
*/
|
||||
void NeoComm_shutdown();
|
||||
|
@ -1,119 +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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file nodes.h
|
||||
* @brief Management of nodes.
|
||||
* @details These functions are used to manage the global list of nodes.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Simple address structure providing IP/DNS and port information.
|
||||
*/
|
||||
struct NeoComm_address {
|
||||
char *address; ///< IP/DNS string.
|
||||
unsigned short port; ///< Port of address.
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Structure used for NeoComm nodes.
|
||||
*/
|
||||
struct NeoComm_node {
|
||||
/// The address of the node.
|
||||
struct NeoComm_address address;
|
||||
/// Whether or not it is a directory node/server.
|
||||
int directory;
|
||||
/// Number of connections a directory has.
|
||||
unsigned int connections;
|
||||
/// Maximum number of connections the directory can have.
|
||||
unsigned int max_connections;
|
||||
};
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*
|
||||
* @return If the operation failed then it will return 0 and you can read
|
||||
* the error from the NeoComm_get_last_error function, else it will return 1.
|
||||
*/
|
||||
int NeoComm_init_nodes(unsigned int max_nodes);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize all the nodes.
|
||||
*/
|
||||
void NeoComm_shutdown_nodes();
|
||||
|
||||
/**
|
||||
* @brief Enlarge the list.
|
||||
*
|
||||
* @param new_max_nodes New number of maximum nodes.
|
||||
*
|
||||
* @return If the operation failed then it will return 0 and you can read
|
||||
* the error from the NeoComm_get_last_error function, else it will return 1.
|
||||
*/
|
||||
int NeoComm_resize_node_list(unsigned int new_max_nodes);
|
||||
|
||||
/**
|
||||
* @brief Get the maximum number of nodes that can be used.
|
||||
*
|
||||
* @return The maximum number of nodes that can be used..
|
||||
*/
|
||||
unsigned int NeoComm_get_node_max();
|
||||
|
||||
/**
|
||||
* @brief Get the number of nodes there currently are.
|
||||
*
|
||||
* @return The number of nodes remembered.
|
||||
*/
|
||||
unsigned int NeoComm_get_node_count();
|
||||
|
||||
/**
|
||||
* @brief Add a new node where only address is given (all other variables
|
||||
* are set to 0).
|
||||
*
|
||||
* @param addr Address of the new node.
|
||||
*
|
||||
* @return If the operation failed then it will return NULL and you can
|
||||
* read the error from the NeoComm_get_last_error function, else it will
|
||||
* return 1.
|
||||
*/
|
||||
struct NeoComm_node *NeoComm_add_node(struct NeoComm_address addr);
|
||||
|
||||
/**
|
||||
* @brief Remove a node from the list.
|
||||
*
|
||||
* @param addr The address of the node.
|
||||
*
|
||||
* @return If the node was not found it returns 0, else it returns 1.
|
||||
*/
|
||||
int NeoComm_remove_node(struct NeoComm_address addr);
|
||||
|
||||
/**
|
||||
* @brief Get a pointer to the node with a given address.
|
||||
*
|
||||
* @param addr The address of the node.
|
||||
*
|
||||
* @return A modifiable pointer to the node or NULL if not found.
|
||||
*/
|
||||
struct NeoComm_node *NeoComm_get_node(struct NeoComm_address addr);
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user