Final assignment for the course "Real Time and Embedded Systems" of THMMY in AUTH university.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.5 KiB

#include <stdlib.h>
#include <assert.h>
5 years ago
#include <time.h>
#include <stdio.h>
#include <string.h>
5 years ago
#include <arpa/inet.h>
#include "node.h"
// Defines the node structure
struct node_t {
5 years ago
address_t addr;
node_id_t id;
timestamp_t *events_timestamps;
node_status *events_status;
uint16_t events_size;
int comm_socket;
};
5 years ago
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;
5 years ago
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);
5 years ago
free(node->events_timestamps);
free(node->events_status);
free(node);
}
5 years ago
void node_add_timestamp(node_handle_t node, timestamp_t timestamp, bool visible) {
assert(node && timestamp);
5 years ago
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);
}
5 years ago
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);
}
5 years ago
node->events_timestamps[node->events_size] = timestamp;
node->events_status[node->events_size] = visible ? NODE_PRESENT : NODE_GONE;
++node->events_size;
}
5 years ago
address_t node_get_addr(node_handle_t node) {
assert(node);
return node->addr;
}
5 years ago
node_id_t node_get_id(node_handle_t node) {
assert(node);
5 years ago
return node->id;
}
5 years ago
node_status node_get_status(node_handle_t node) {
assert(node);
5 years ago
return node->events_status[node->events_size];
}
5 years ago
int node_get_comm_socket(node_handle_t node) {
return node->comm_socket;
}
5 years ago
void node_set_comm_socket(node_handle_t node, int comm_socket) {
node->comm_socket = comm_socket;
}
5 years ago
uint16_t node_get_events(node_handle_t node, timestamp_t **events_timestamps, node_status **events_status) {
assert(node && events_timestamps && events_status);
5 years ago
(*events_timestamps) = (timestamp_t *) malloc(node->events_size * sizeof(timestamp_t));
(*events_status) = (node_status *) malloc(node->events_size * sizeof(node_status));
5 years ago
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;
}