diff --git a/include/neocomm/error.h b/include/neocomm/error.h new file mode 100644 index 0000000..6970c8c --- /dev/null +++ b/include/neocomm/error.h @@ -0,0 +1,27 @@ +/* + * 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 + +/** + * @brief Return the error caused by the last function call. + * + * @return A string with the error. + */ +const char* NeoComm_get_last_error(); diff --git a/include/neocomm/neocomm.h b/include/neocomm/neocomm.h index d7e635c..0ffba4a 100644 --- a/include/neocomm/neocomm.h +++ b/include/neocomm/neocomm.h @@ -19,7 +19,9 @@ #pragma once -#include "nodes.h" +#include "neocomm/connection.h" +#include "neocomm/error.h" +#include "neocomm/nodes.h" /** * @file neocomm.h diff --git a/include/neocomm/nodes.h b/include/neocomm/nodes.h index fde3dde..8d924dd 100644 --- a/include/neocomm/nodes.h +++ b/include/neocomm/nodes.h @@ -54,8 +54,8 @@ struct NeoComm_node { * * @param max_nodes Maximum number of nodes available. * - * @return If the operation failed then it will return 0, else it will - * return 1. + * @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); @@ -69,8 +69,8 @@ void NeoComm_shutdown_nodes(); * * @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. + * @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); @@ -94,8 +94,9 @@ unsigned int NeoComm_get_node_count(); * * @param addr Address of the new node. * - * @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. + * @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); diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..1b9ef38 --- /dev/null +++ b/src/error.c @@ -0,0 +1,34 @@ +/* + * 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/error.h" +#include "internal_error.h" + +#include + +static const char *neocomm_error; + +void NeoComm_error(const char *error) { + assert(error); + neocomm_error = error; +} + +const char* NeoComm_get_last_error() { + return neocomm_error; +} diff --git a/src/internal_error.h b/src/internal_error.h new file mode 100644 index 0000000..4640798 --- /dev/null +++ b/src/internal_error.h @@ -0,0 +1,22 @@ +/* + * 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 + +void NeoComm_error(const char* error); diff --git a/src/nodes.c b/src/nodes.c index dcf93a4..1334cec 100644 --- a/src/nodes.c +++ b/src/nodes.c @@ -20,20 +20,30 @@ #include "neocomm/nodes.h" #include "globals.h" +#include "internal_error.h" #include #include #include int NeoComm_init_nodes(unsigned int max_nodes) { - // If we want 0 nodes then don't initialize, give error if - // already initialized - if(max_nodes == 0 || node_max != 0) + 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) @@ -54,14 +64,25 @@ void NeoComm_shutdown_nodes() { } int NeoComm_resize_node_list(unsigned int new_max_nodes) { - assert(node_list); - if(new_max_nodes <= node_max) + 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; @@ -79,7 +100,10 @@ unsigned int NeoComm_get_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 && @@ -87,6 +111,7 @@ struct NeoComm_node *NeoComm_add_node(struct NeoComm_address addr) { return &node_list[i]; } + struct NeoComm_node *new_node; for(unsigned int i = 0; i < node_max; ++i) { if(!node_list[i].address.address) @@ -97,10 +122,11 @@ struct NeoComm_node *NeoComm_add_node(struct NeoComm_address addr) { node_list[i].connections = 0; node_list[i].max_connections = 0; node_count++; - return &node_list[i]; + new_node = &node_list[i]; + break; } } - return NULL; + return new_node; } int NeoComm_remove_node(struct NeoComm_address addr) {