neocomm/include/nodes.h

113 lines
3.2 KiB
C
Raw Normal View History

2017-07-01 16:46:22 +00:00
/*
* 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
/**
* @brief Simple address structure providing IP/DNS and port information.
*/
struct address {
char *address; ///< IP/DNS string.
unsigned short port; ///< Port of address.
};
2017-07-02 15:13:59 +00:00
/**
* @brief Structure used for NeoComm nodes.
*/
struct node {
2017-07-02 11:38:20 +00:00
/// The address of the node.
struct address address;
2017-07-02 11:38:20 +00:00
/// Whether or not it is a directory node/server.
int directory;
2017-07-02 11:38:20 +00:00
/// Number of connections a directory has.
unsigned int connections;
2017-07-02 11:38:20 +00:00
/// Maximum number of connections the directory can have.
unsigned int max_connections;
};
2017-07-01 16:46:22 +00:00
/**
* @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.
2017-07-01 16:46:22 +00:00
*
* @param max_nodes Maximum number of nodes available.
*
* @return If the operation failed then it will return 0, else it will
* return 1.
*/
int NeoComm_init_nodes(unsigned int max_nodes);
2017-07-01 16:46:22 +00:00
2017-07-02 15:13:59 +00:00
/**
* @brief Deinitialize all the nodes.
*/
void NeoComm_shutdown_nodes();
2017-07-02 11:38:20 +00:00
2017-07-01 16:46:22 +00:00
/**
* @brief Enlarge the list.
*
* @param new_max_nodes New number of maximum nodes.
*
* @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.
*/
int NeoComm_resize_node_list(unsigned int new_max_nodes);
2017-07-01 16:46:22 +00:00
/**
* @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();
2017-07-01 16:46:22 +00:00
/**
* @brief Get the number of nodes there currently are.
*
* @return The number of nodes remembered.
*/
unsigned int NeoComm_get_node_count();
2017-07-01 16:46:22 +00:00
/**
* @brief Add a new node where only address is given (all other variables
* are set to 0).
2017-07-01 16:46:22 +00:00
*
* @param addr Address of the new node.
2017-07-01 16:46:22 +00:00
*
* @return If the list is full then it will return NULL, else it will return
* a pointer to the new (or old, if already existing) node.
2017-07-01 16:46:22 +00:00
*/
struct node *NeoComm_add_node(struct address addr);
2017-07-01 16:46:22 +00:00
/**
* @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 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 node *NeoComm_get_node(struct address addr);