From 5d47c438640ee77968197a0531bcd0caed07aef0 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 4 May 2020 16:00:41 +0200 Subject: [PATCH] Added UI for no debug mode --- src/debug.c | 88 ++++++++++++++++++++++++++++++++++++++--------------- src/debug.h | 9 +++++- src/node.c | 36 +++++++++++++++------- src/node.h | 2 +- 4 files changed, 97 insertions(+), 38 deletions(-) diff --git a/src/debug.c b/src/debug.c index c4a8756..e205e30 100644 --- a/src/debug.c +++ b/src/debug.c @@ -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,42 +35,69 @@ void print_error(char * msg){ } void print_peers(list * l){ - // Print out the peers we have in the neighbour_list - int nbr_peers = 0; - if (l == NULL) { - print_error("The neighbour_list is empty !"); - } else { - print_debug(">> Printing out peer list :"); - while(l != NULL){ - neighbour_peer * peer = (neighbour_peer *) l->data; - char * buff_str_ip[1024]; - char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); - int last_seen = time(NULL) - peer->last_seen; - printf("\x1b[31m[DEBUG]\x1b[0m >> %s @ %i | is temporary ? %s | last seen %i secs ago. |\n", ip_str, peer->port, peer->is_temporary ? "yes":"no", last_seen); - nbr_peers++; - if (l->next == NULL) { - break; - } else { - l = l->next; - } + if (DEBUG_LEVEL > 0) { + // Print out the peers we have in the neighbour_list + int nbr_peers = 0; + if (l == NULL) { + print_error("The neighbour_list is empty !"); + } else { + print_debug(">> Printing out peer list :"); + while(l != NULL){ + neighbour_peer * peer = (neighbour_peer *) l->data; + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); + int last_seen = time(NULL) - peer->last_seen; + printf("\x1b[31m[DEBUG]\x1b[0m >> %s @ %i | is temporary ? %s | last seen %i secs ago. |\n", ip_str, peer->port, peer->is_temporary ? "yes":"no", last_seen); + nbr_peers++; + if (l->next == NULL) { + break; + } else { + l = l->next; + } + } + printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i peers.\n", nbr_peers); + print_debug(">> Finished printing peer list."); } - 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 { + print_debug(">> Printing messages we know of so far :"); + int nbr_msg = 0; + setlocale(LC_ALL, "en_US.utf8"); + 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); + nbr_msg++; + if (l->next == NULL) { + break; + } else { + l = l->next; + } + } + 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 { - print_debug(">> Printing messages we know of so far :"); int nbr_msg = 0; setlocale(LC_ALL, "en_US.utf8"); - print_debug(">> Peer ID | Seqno | Length | Message "); + print_info(">> 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[1m\x1b[36m[大字报]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); nbr_msg++; if (l->next == NULL) { break; @@ -66,7 +105,6 @@ void print_data(list * l){ l = l->next; } } - printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i messages.\n", nbr_msg); + printf("\x1b[1m\x1b[36m[大字报]\x1b[0m >> Found %i messages.\n", nbr_msg); } - } diff --git a/src/debug.h b/src/debug.h index c0a5688..9de638f 100644 --- a/src/debug.h +++ b/src/debug.h @@ -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 diff --git a/src/node.c b/src/node.c index ccfc05f..f2e739c 100644 --- a/src/node.c +++ b/src/node.c @@ -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,14 +1435,23 @@ int run_node(int sock_fd){ } // Remove trailing \n input_buffer[strcspn(input_buffer, "\n")] = 0; - if (DEBUG_LEVEL > 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 ); - } - // Add message to the message table. - if (add_message(input_buffer, bytes-1) < 0) { - 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)); + } + // Add message to the message table. + if (add_message(input_buffer, bytes-1) < 0) { + 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); diff --git a/src/node.h b/src/node.h index 6fba513..af8b526 100644 --- a/src/node.h +++ b/src/node.h @@ -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