Moving node list into global scope.

This commit is contained in:
Nicolás A. Ortega 2017-07-02 13:38:20 +02:00
parent cafe6a7243
commit a393cb8b4e
No known key found for this signature in database
GPG Key ID: 3D786FB3123FF1DD
8 changed files with 109 additions and 10 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
# Ignore builds
build/*
!build/.keep
# Ignore Vim temprary files
*.sw[a-z]

View File

@ -35,6 +35,7 @@ include_directories(
"include/")
set(SRCS
src/neocomm.c
src/nodes.c)
set(CMAKE_C_FLAGS "-std=c99 -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")

24
include/connection.h Normal file
View File

@ -0,0 +1,24 @@
/*
* 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
int NeoComm_connect_all_nodes();
int NeoComm_disconnect();

View File

@ -20,3 +20,13 @@
#pragma once
#include "nodes.h"
/**
* @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);

View File

@ -28,12 +28,13 @@ struct address {
};
struct node {
// The address of the node
/// The address of the node.
struct address address;
// Whether or not it is a directory node/server
/// Whether or not it is a directory node/server.
int directory;
// Useful variables for passing on connections of new nodes
/// Number of connections a directory has.
unsigned int connections;
/// Maximum number of connections the directory can have.
unsigned int max_connections;
};
@ -49,6 +50,8 @@ struct node {
*/
int NeoComm_init_nodes(unsigned int max_nodes);
void NeoComm_deinit_nodes();
/**
* @brief Enlarge the list.
*

29
src/globals.h Normal file
View File

@ -0,0 +1,29 @@
/*
* 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"
// List of nodes
extern struct node *node_list;
// Maximum number of nodes
extern unsigned int node_max;
// Number of nodes in the list
extern unsigned int node_count;

26
src/neocomm.c Normal file
View File

@ -0,0 +1,26 @@
/*
* 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.h"
int NeoComm_init(unsigned int max_nodes) {
if(!NeoComm_init_nodes(max_nodes))
return 0;
return 1;
}

View File

@ -19,17 +19,14 @@
#include "nodes.h"
#include "globals.h"
#include <stdlib.h>
#include <string.h>
// List of nodes
static struct 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)
return 1;
node_list = calloc(max_nodes, sizeof(struct node));
if(!node_list)
return 0;
@ -43,6 +40,12 @@ int NeoComm_init_nodes(unsigned int max_nodes) {
return 1;
}
void NeoComm_deinit_nodes() {
free(node_list);
node_max = 0;
node_count = 0;
}
int NeoComm_resize_node_list(unsigned int new_max_nodes) {
if(new_max_nodes <= node_max)
return 0;