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
6 changed files with 126 additions and 14 deletions

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 "globals.h"
#include "internal_error.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
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) {