Added error handling functionality.

This commit is contained in:
Nicolás A. Ortega 2017-07-19 11:57:41 -05:00
parent 7aed5f8124
commit f70c9943c8
No known key found for this signature in database
GPG Key ID: 3D786FB3123FF1DD
6 changed files with 126 additions and 14 deletions

27
include/neocomm/error.h Normal file
View File

@ -0,0 +1,27 @@
/*
* 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 Return the error caused by the last function call.
*
* @return A string with the error.
*/
const char* NeoComm_get_last_error();

View File

@ -19,7 +19,9 @@
#pragma once #pragma once
#include "nodes.h" #include "neocomm/connection.h"
#include "neocomm/error.h"
#include "neocomm/nodes.h"
/** /**
* @file neocomm.h * @file neocomm.h

View File

@ -54,8 +54,8 @@ struct NeoComm_node {
* *
* @param max_nodes Maximum number of nodes available. * @param max_nodes Maximum number of nodes available.
* *
* @return If the operation failed then it will return 0, else it will * @return If the operation failed then it will return 0 and you can read
* return 1. * the error from the NeoComm_get_last_error function, else it will return 1.
*/ */
int NeoComm_init_nodes(unsigned int max_nodes); 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. * @param new_max_nodes New number of maximum nodes.
* *
* @return If the new size is less than or equal to the current size, or * @return If the operation failed then it will return 0 and you can read
* the list failed to be reallocated, then it returns 0, else it returns 1. * the error from the NeoComm_get_last_error function, else it will return 1.
*/ */
int NeoComm_resize_node_list(unsigned int new_max_nodes); 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. * @param addr Address of the new node.
* *
* @return If the list is full then it will return NULL, else it will return * @return If the operation failed then it will return NULL and you can
* a pointer to the new (or old, if already existing) node. * 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); struct NeoComm_node *NeoComm_add_node(struct NeoComm_address addr);

34
src/error.c Normal file
View File

@ -0,0 +1,34 @@
/*
* 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/error.h"
#include "internal_error.h"
#include <assert.h>
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;
}

22
src/internal_error.h Normal file
View File

@ -0,0 +1,22 @@
/*
* 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
void NeoComm_error(const char* error);

View File

@ -20,20 +20,30 @@
#include "neocomm/nodes.h" #include "neocomm/nodes.h"
#include "globals.h" #include "globals.h"
#include "internal_error.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
int NeoComm_init_nodes(unsigned int max_nodes) { int NeoComm_init_nodes(unsigned int max_nodes) {
// If we want 0 nodes then don't initialize, give error if if(max_nodes == 0)
// already initialized {
if(max_nodes == 0 || node_max != 0) NeoComm_error("Insufficient number of maximum nodes");
return 0; return 0;
}
else if(node_max != 0)
{
NeoComm_error("Node list already initialized");
return 0;
}
node_list = calloc(max_nodes, sizeof(struct NeoComm_node)); node_list = calloc(max_nodes, sizeof(struct NeoComm_node));
if(!node_list) if(!node_list)
{
NeoComm_error("Failed to allocate memory to node list");
return 0; return 0;
}
// Set to NULL to check for free spots // Set to NULL to check for free spots
for(unsigned int i = 0; i < max_nodes; ++i) 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) { int NeoComm_resize_node_list(unsigned int new_max_nodes) {
assert(node_list); if(node_max == 0)
if(new_max_nodes <= node_max) {
NeoComm_error("Node list has not yet been initialized");
return 0; return 0;
}
else if(new_max_nodes < node_max)
{
NeoComm_error("Cannot downsize node list");
return 0;
}
assert(node_list);
void* tmp_list = void* tmp_list =
realloc(node_list, new_max_nodes * sizeof(struct NeoComm_node)); realloc(node_list, new_max_nodes * sizeof(struct NeoComm_node));
if(!tmp_list) if(!tmp_list)
{
NeoComm_error("Failed to resize list, continuing with old size");
return 0; return 0;
}
node_list = tmp_list; node_list = tmp_list;
@ -79,7 +100,10 @@ unsigned int NeoComm_get_node_count() {
struct NeoComm_node *NeoComm_add_node(struct NeoComm_address addr) { struct NeoComm_node *NeoComm_add_node(struct NeoComm_address addr) {
assert(node_list); assert(node_list);
if(node_count >= node_max) if(node_count >= node_max)
{
NeoComm_error("Reached maximum number of nodes");
return NULL; return NULL;
}
for(unsigned int i = 0; i < node_max; ++i) for(unsigned int i = 0; i < node_max; ++i)
{ {
if(strcmp(node_list[i].address.address, addr.address) == 0 && 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]; return &node_list[i];
} }
struct NeoComm_node *new_node;
for(unsigned int i = 0; i < node_max; ++i) for(unsigned int i = 0; i < node_max; ++i)
{ {
if(!node_list[i].address.address) 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].connections = 0;
node_list[i].max_connections = 0; node_list[i].max_connections = 0;
node_count++; 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) { int NeoComm_remove_node(struct NeoComm_address addr) {