Merge branch 'fix-node-id' into 'master'

Fix node

See merge request perdriau/dazibao!19
This commit is contained in:
PERDRIAU nelson 2020-05-05 14:59:03 +02:00
commit 7ce2427d7d
4 changed files with 97 additions and 38 deletions

View File

@ -6,6 +6,18 @@
#include "debug.h"
#include "node.h"
void welcome(){
print_info("----------------------------------------");
print_info(" 大字报");
print_info("----------------------------------------");
print_info(">> To write a message, just write it to the terminal and press enter.");
print_info(">> To show all messages, just press enter without writing anything.");
}
void print_info(char * msg) {
printf("\x1b[1m\x1b[36m[大字报]\x1b[0m %s\n", msg);
}
void print_debug(char * msg){
if (DEBUG_LEVEL > 0) {
printf("\x1b[31m[DEBUG]\x1b[0m %s \n", msg);
@ -23,6 +35,7 @@ void print_error(char * msg){
}
void print_peers(list * l){
if (DEBUG_LEVEL > 0) {
// Print out the peers we have in the neighbour_list
int nbr_peers = 0;
if (l == NULL) {
@ -46,9 +59,13 @@ void print_peers(list * l){
printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i peers.\n", nbr_peers);
print_debug(">> Finished printing peer list.");
}
}
}
void print_data(list * l){
if (DEBUG_LEVEL > 0) {
if (l == NULL) {
print_error("Message list is empty !");
} else {
@ -58,7 +75,7 @@ void print_data(list * l){
print_debug(">> Peer ID | Seqno | Length | Message ");
while(l != NULL){
pub_data * message = l->data;
printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data);
printf("\x1b[31m[DEBUG]\x1b[0m >> %lu | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data);
nbr_msg++;
if (l->next == NULL) {
break;
@ -68,5 +85,26 @@ void print_data(list * l){
}
printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i messages.\n", nbr_msg);
}
}
}
void print_data_info(list * l){
if (l == NULL) {
print_error("Message list is empty !");
} else {
int nbr_msg = 0;
setlocale(LC_ALL, "en_US.utf8");
print_info(">> Peer ID | Seqno | Length | Message ");
while(l != NULL){
pub_data * message = l->data;
printf("\x1b[1m\x1b[36m[大字报]\x1b[0m >> %lu | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data);
nbr_msg++;
if (l->next == NULL) {
break;
} else {
l = l->next;
}
}
printf("\x1b[1m\x1b[36m[大字报]\x1b[0m >> Found %i messages.\n", nbr_msg);
}
}

View File

@ -4,7 +4,11 @@
#include "node.h"
#define DEBUG_LEVEL 2
#define DEBUG_LEVEL 0
void welcome();
void print_info(char * msg);
void print_debug(char * msg);
@ -13,4 +17,7 @@ void print_error(char * msg);
void print_peers(list * l);
void print_data(list * l);
void print_data_info(list * l);
#endif

View File

@ -234,13 +234,13 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat
// If seqno is bigger or equals than our stored seqno then update seqno
if( ((seqno - found->seqno) & 32768) == 0 ) {
printf(">> Updating seqno of our own published data.\n");
print_debug(">> Updating seqno of our own published data.\n");
found->seqno = (seqno + 1) % (65535);
return 1;
}
// Else, do nothing
printf(">> Our own seqno didn't need to be updated.\n");
print_debug(">> Our own seqno didn't need to be updated.");
return 0;
}
@ -248,7 +248,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat
if(found != NULL) {
// Check if seqno is smaller or equals to our stored seqno
if( ((found->seqno - seqno) & 32768) == 0 ) {
printf(">> Data received has smaller seqno than stored seqno, nothing has to be done.\n");
print_debug(">> Data received has smaller seqno than stored seqno, nothing has to be done.");
return 0;
}
@ -1084,7 +1084,9 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
build_node_state_req(&new_tlv, be64toh(*id));
add_tlv(&pack, &new_tlv, sender, socket_num);
printf(">> Sending node state request for node %lu...\n", be64toh(*id));
if (DEBUG_LEVEL > 0) {
printf("\x1b[31m[DEBUG]\x1b[0m >> Sending node state request for node %lu...\n", be64toh(*id));
}
// The position is updated
tlv_len = data[pos+1];
@ -1221,6 +1223,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
int rc = add_data(tlv_len - 26, be64toh(*id), be16toh(*seqno), cur_message, pdata);
if (rc < 0) {
print_error("Error while adding node state !");
} else {
print_info("Received a message.");
}
// The position is updated
pos += tlv_len + 2;
@ -1431,6 +1435,12 @@ int run_node(int sock_fd){
}
// Remove trailing \n
input_buffer[strcspn(input_buffer, "\n")] = 0;
if (strcmp("",input_buffer) == 0){
print_info(">> Printing all messages we know of so far :");
print_data_info(data_list);
} else {
if (DEBUG_LEVEL > 0) {
printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : “%s”\n", input_buffer );
}
@ -1439,6 +1449,9 @@ int run_node(int sock_fd){
print_debug(">> Error while trying to add the message to the list of messages, please try again..");
// Reset buffer
memset(input_buffer, 0, sizeof(input_buffer));
} else {
print_info(">> Added message to the message list.");
}
}
}
@ -1577,6 +1590,7 @@ int main(int argc, const char *argv[]) {
int sock_fd;
bootstrap_node(&sock_fd, root_peer_ip, root_peer_port);
welcome();
run_node(sock_fd);
close(sock_fd);

View File

@ -61,7 +61,7 @@ typedef struct list {
#define LISTEN_PORT 1212
// The node ID
#define NODE_ID 42009235719890846928
#define NODE_ID 4209169790617845760
// #define NODE_ID 42675882021843277
// #define NODE_ID 1312