#include #include #include #include #include #include #include "node.h" // Defines the node structure struct node_t { address_t addr; node_id_t id; timestamp_t *events_timestamps; node_status *events_status; uint16_t events_size; int comm_socket; }; node_handle_t node_init(address_t addr, node_id_t id) { node_handle_t node = malloc(sizeof(node_t)); assert(node); node->addr = addr; node->id = id; node->events_timestamps = (timestamp_t *) malloc(sizeof(timestamp_t)); node->events_timestamps[0] = time(NULL); node->events_status = (node_status *) malloc(sizeof(node_status)); node->events_status[0] = NODE_INITIALIAZED; node->events_size = 1; node->comm_socket = -1; return node; } void node_free(node_handle_t node) { assert(node); free(node->events_timestamps); free(node->events_status); free(node); } void node_add_timestamp(node_handle_t node, timestamp_t timestamp, bool visible) { assert(node && timestamp); int *realloc_r = realloc(node->events_timestamps, node->events_size + 1); if (!realloc_r) { node_free(node); perror("Error trying to reallocate memory for event timestamps."); exit(EXIT_FAILURE); } realloc_r = realloc(node->events_status, node->events_size + 1); if (!realloc_r) { node_free(node); perror("Error trying to reallocate memory for event statuses."); exit(EXIT_FAILURE); } node->events_timestamps[node->events_size] = timestamp; node->events_status[node->events_size] = visible ? NODE_PRESENT : NODE_GONE; ++node->events_size; } address_t node_get_addr(node_handle_t node) { assert(node); return node->addr; } node_id_t node_get_id(node_handle_t node) { assert(node); return node->id; } node_status node_get_status(node_handle_t node) { assert(node); return node->events_status[node->events_size]; } int node_get_comm_socket(node_handle_t node) { return node->comm_socket; } void node_set_comm_socket(node_handle_t node, int comm_socket) { node->comm_socket = comm_socket; } uint16_t node_get_events(node_handle_t node, timestamp_t **events_timestamps, node_status **events_status) { assert(node && events_timestamps && events_status); (*events_timestamps) = (timestamp_t *) malloc(node->events_size * sizeof(timestamp_t)); (*events_status) = (node_status *) malloc(node->events_size * sizeof(node_status)); memcpy((*events_timestamps), node->events_timestamps, node->events_size * sizeof(timestamp_t)); memcpy((*events_status), node->events_status, node->events_size * sizeof(node_status)); return node->events_size; }