messages are now added to the data_list
This commit is contained in:
parent
956b249259
commit
88e15dea3a
202
src/node.c
202
src/node.c
@ -219,22 +219,6 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
||||
// If id is the same as this node's id then we only update seqno
|
||||
|
||||
if(id == NODE_ID) {
|
||||
// pub_data *node_data = get_data(NODE_ID);
|
||||
//
|
||||
// if (node_data == NULL) {
|
||||
// if (datagram == NULL) {
|
||||
// data_list = (list*) malloc(sizeof(list));
|
||||
// }
|
||||
// data_list->data = (void*) new_data;
|
||||
// data_list->next = NULL;
|
||||
// }
|
||||
//
|
||||
// if(seqno >= node_data->seqno) {
|
||||
// node_data->seqno = seqno ^ 1;
|
||||
// }
|
||||
//
|
||||
// return 1;
|
||||
|
||||
// We create our pub_data.
|
||||
pub_data * message = malloc(sizeof(struct pub_data));
|
||||
message->length = len;
|
||||
@ -243,103 +227,101 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
||||
message->data = data;
|
||||
|
||||
// If the data list has never been used, or is empty ( same thing )
|
||||
if (data_list = NULL) {
|
||||
data_list = (list*) malloc(sizeof(list));
|
||||
} else {
|
||||
// Otherwise, we move until the last element of the dala_list,
|
||||
// and add or data there.
|
||||
|
||||
// We use a temporary address to avoid writing to the static list.
|
||||
// Seems weird but ok.
|
||||
list *tmp = data_list;
|
||||
while(tmp->next != NULL){
|
||||
tmp = tmp->next;
|
||||
}
|
||||
|
||||
// We create the next node of the linked list.
|
||||
list * new_node = malloc(sizeof(struct list));
|
||||
new_node->data = (void *) message;
|
||||
|
||||
// Adding the message to the list.
|
||||
tmp->next = (void *) new_node;
|
||||
if (data_list == NULL) {
|
||||
data_list = (list*) malloc(sizeof(struct list));
|
||||
}
|
||||
// we move until the last element of the dala_list,
|
||||
// and add or data there.
|
||||
// We use a temporary address to avoid writing to the static list.
|
||||
// Seems weird but ok.
|
||||
list *tmp = data_list;
|
||||
while(tmp->next != NULL){
|
||||
tmp = tmp->next;
|
||||
}
|
||||
} else {
|
||||
|
||||
// We create the next node of the linked list.
|
||||
list * new_node = malloc(sizeof(struct list));
|
||||
new_node->data = (void *) message;
|
||||
|
||||
// Adding the message to the list.
|
||||
tmp->next = (void *) new_node;
|
||||
|
||||
return 1;
|
||||
} else {
|
||||
// Copy data
|
||||
pub_data *new_data = copy_data(len, id, seqno, data);
|
||||
|
||||
if(data_list == NULL) {
|
||||
// Update list
|
||||
data_list = (list*) malloc(sizeof(list));
|
||||
data_list->data = (void*) new_data;
|
||||
data_list->next = NULL;
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Find correct position for new data
|
||||
list *tmp = data_list;
|
||||
list *last = NULL;
|
||||
list *new_node;
|
||||
int64_t cur_id;
|
||||
|
||||
while(tmp != NULL) {
|
||||
cur_id = ((pub_data*) tmp->data)->id;
|
||||
|
||||
// If id is smaller than cur_id then the new data has to be added at this position
|
||||
if(id < cur_id) {
|
||||
// If last hasn't been set then the new data becomes the head of the list
|
||||
if(last == NULL) {
|
||||
// Update list
|
||||
data_list = (list*) malloc(sizeof(list));
|
||||
data_list->data = (void*) new_data;
|
||||
data_list->next = tmp;
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Else, we update the last node
|
||||
new_node = (list*) malloc(sizeof(list));
|
||||
new_node->data = (void*) new_data;
|
||||
new_node->next = tmp;
|
||||
last->next = new_node;
|
||||
|
||||
return 1;
|
||||
} else if(id == cur_id) {
|
||||
// If data already exists for this id then we update it if it's seqno is greater than the one stored
|
||||
pub_data *cur_data = (pub_data*) tmp->data;
|
||||
|
||||
if(seqno > cur_data->seqno) {
|
||||
// Updata data
|
||||
tmp->data = (void*) new_data;
|
||||
|
||||
// Free old data
|
||||
free(cur_data);
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
// seqno is smaller so the new data allocated is freed and nothing else is done
|
||||
free(new_data);
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Get next node in list
|
||||
last = tmp;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
|
||||
// If no correct position was found then the new data has to be added at the end of the list
|
||||
|
||||
// Update list
|
||||
new_node = (list*) malloc(sizeof(list));
|
||||
new_node->data = (void*) new_data;
|
||||
new_node->next = NULL;
|
||||
last->next = new_node;
|
||||
return 3;
|
||||
}
|
||||
|
||||
// Copy data
|
||||
pub_data *new_data = copy_data(len, id, seqno, data);
|
||||
|
||||
if(data_list == NULL) {
|
||||
// Update list
|
||||
data_list = (list*) malloc(sizeof(list));
|
||||
data_list->data = (void*) new_data;
|
||||
data_list->next = NULL;
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Find correct position for new data
|
||||
list *tmp = data_list;
|
||||
list *last = NULL;
|
||||
list *new_node;
|
||||
int64_t cur_id;
|
||||
|
||||
while(tmp != NULL) {
|
||||
cur_id = ((pub_data*) tmp->data)->id;
|
||||
|
||||
// If id is smaller than cur_id then the new data has to be added at this position
|
||||
if(id < cur_id) {
|
||||
// If last hasn't been set then the new data becomes the head of the list
|
||||
if(last == NULL) {
|
||||
// Update list
|
||||
data_list = (list*) malloc(sizeof(list));
|
||||
data_list->data = (void*) new_data;
|
||||
data_list->next = tmp;
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Else, we update the last node
|
||||
new_node = (list*) malloc(sizeof(list));
|
||||
new_node->data = (void*) new_data;
|
||||
new_node->next = tmp;
|
||||
last->next = new_node;
|
||||
|
||||
return 1;
|
||||
} else if(id == cur_id) {
|
||||
// If data already exists for this id then we update it if it's seqno is greater than the one stored
|
||||
pub_data *cur_data = (pub_data*) tmp->data;
|
||||
|
||||
if(seqno > cur_data->seqno) {
|
||||
// Updata data
|
||||
tmp->data = (void*) new_data;
|
||||
|
||||
// Free old data
|
||||
free(cur_data);
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
// seqno is smaller so the new data allocated is freed and nothing else is done
|
||||
free(new_data);
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Get next node in list
|
||||
last = tmp;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
|
||||
// If no correct position was found then the new data has to be added at the end of the list
|
||||
|
||||
// Update list
|
||||
new_node = (list*) malloc(sizeof(list));
|
||||
new_node->data = (void*) new_data;
|
||||
new_node->next = NULL;
|
||||
last->next = new_node;
|
||||
return 3;
|
||||
}
|
||||
|
||||
/* ---- Fin fonctions utilitaires ---- */
|
||||
|
Loading…
Reference in New Issue
Block a user