From a393cb8b4ec8f4060f7b1d6bde1e7290dd978d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega?= Date: Sun, 2 Jul 2017 13:38:20 +0200 Subject: [PATCH] Moving node list into global scope. --- .gitignore | 3 +++ CMakeLists.txt | 1 + include/connection.h | 24 ++++++++++++++++++++++++ include/neocomm.h | 10 ++++++++++ include/nodes.h | 9 ++++++--- src/globals.h | 29 +++++++++++++++++++++++++++++ src/neocomm.c | 26 ++++++++++++++++++++++++++ src/nodes.c | 17 ++++++++++------- 8 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 include/connection.h create mode 100644 src/globals.h create mode 100644 src/neocomm.c diff --git a/.gitignore b/.gitignore index cce4620..789279b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ # Ignore builds build/* !build/.keep + +# Ignore Vim temprary files +*.sw[a-z] diff --git a/CMakeLists.txt b/CMakeLists.txt index 20cbdea..a917eef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") diff --git a/include/connection.h b/include/connection.h new file mode 100644 index 0000000..b4d0fc8 --- /dev/null +++ b/include/connection.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2017 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * 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 + * . + */ + +#pragma once + +int NeoComm_connect_all_nodes(); + +int NeoComm_disconnect(); diff --git a/include/neocomm.h b/include/neocomm.h index 2989a86..7dee73d 100644 --- a/include/neocomm.h +++ b/include/neocomm.h @@ -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); diff --git a/include/nodes.h b/include/nodes.h index 9918e86..e0e5d3c 100644 --- a/include/nodes.h +++ b/include/nodes.h @@ -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. * diff --git a/src/globals.h b/src/globals.h new file mode 100644 index 0000000..d3d41e8 --- /dev/null +++ b/src/globals.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2017 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * 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 + * . + */ + +#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; diff --git a/src/neocomm.c b/src/neocomm.c new file mode 100644 index 0000000..5246742 --- /dev/null +++ b/src/neocomm.c @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2017 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * 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 + * . + */ + +#include "neocomm.h" + +int NeoComm_init(unsigned int max_nodes) { + if(!NeoComm_init_nodes(max_nodes)) + return 0; + return 1; +} diff --git a/src/nodes.c b/src/nodes.c index 9c71c30..54c47cd 100644 --- a/src/nodes.c +++ b/src/nodes.c @@ -19,17 +19,14 @@ #include "nodes.h" +#include "globals.h" + #include #include -// 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;