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.
 
 
 
 
 

132 lines
3.5 KiB

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "message.h"
/* Private functions */
// Defines the node structure
struct message_t {
char *message;
uint16_t received_from, *sent_to;
observer_func *observers;
int message_length, num_sent_to, num_observers;
};
unit_static void message_state_changed(message_handle_t message_handle) {
assert(message_handle);
for (uint16_t i = 0; i < message_handle->num_observers; ++i) {
(message_handle->observers[i])();
}
}
/* API */
message_handle_t message_init(const char *message, int message_length, uint16_t received_from) {
//assert(addr);
assert(message_length <= MAX_MESSAGE_LENGTH);
message_handle_t message_handle = malloc(sizeof(message_t));
assert(message_handle);
message_handle->message = malloc((message_length + 1) * sizeof(char));
strcpy(message_handle->message, message);
message_handle->message_length = message_length;
message_handle->received_from = received_from;
message_handle->sent_to = NULL;
message_handle->num_sent_to = 0;
message_handle->observers = NULL;
message_handle->num_observers = 0;
return message_handle;
}
void message_free(message_handle_t message_handle) {
assert(message_handle);
free(message_handle->sent_to);
free(message_handle->observers);
free(message_handle->message);
free(message_handle);
}
void message_add_sent_to(message_handle_t message_handle, uint16_t sent_to) {
assert(message_handle);
if (message_handle->sent_to == NULL) {
message_handle->sent_to = (uint16_t *) malloc(sizeof(uint16_t));
if (!message_handle->sent_to) {
perror("Unable to allocate memory for message receivers.");
exit(EXIT_FAILURE);
}
++(message_handle->num_sent_to);
} else {
uint16_t *r_sent_to = realloc(message_handle->sent_to, ++(message_handle->num_sent_to) * sizeof(uint16_t));
if (!r_sent_to) {
free(r_sent_to);
perror("Unable to reallocate memory for message receivers.");
exit(EXIT_FAILURE);
}
message_handle->sent_to = r_sent_to;
}
message_handle->sent_to[message_handle->num_sent_to - 1] = sent_to;
message_state_changed(message_handle);
}
char *message_get(message_handle_t message_handle, int *message_length) {
assert(message_handle);
char *message = (char *) malloc((message_handle->message_length + 1) * sizeof(char));
if (!message) {
perror("Unable to allocate memory for message return.");
exit(EXIT_FAILURE);
}
strcpy(message, message_handle->message);
(*message_length) = message_handle->message_length;
return message;
}
bool message_sent_to(message_handle_t message_handle, uint16_t node) {
assert(message_handle);
for (uint16_t i = 0; i < message_handle->num_sent_to; ++i) {
if (message_handle->sent_to[i] == node) {
return true;
}
}
return false;
}
void message_attach_observer(message_handle_t message_handle, observer_func observer) {
assert(message_handle);
if (message_handle->observers == NULL) {
message_handle->observers = (observer_func *) malloc(sizeof(observer_func));
if (!message_handle->observers) {
perror("Unable to allocate memory for message observers.");
exit(EXIT_FAILURE);
}
++(message_handle->num_observers);
} else {
uint16_t *r_observers = realloc(message_handle->observers,
++(message_handle->num_observers) * sizeof(observer_func));
if (!r_observers) {
free(r_observers);
perror("Unable to reallocate memory for message observers.");
exit(EXIT_FAILURE);
}
}
message_handle->observers[message_handle->num_observers - 1] = observer;
}