Added UI for no debug mode

This commit is contained in:
n07070 2020-05-04 16:00:41 +02:00
parent 3f6021ccd5
commit 5d47c43864
4 changed files with 97 additions and 38 deletions

View File

@ -6,6 +6,18 @@
#include "debug.h" #include "debug.h"
#include "node.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){ void print_debug(char * msg){
if (DEBUG_LEVEL > 0) { if (DEBUG_LEVEL > 0) {
printf("\x1b[31m[DEBUG]\x1b[0m %s \n", msg); printf("\x1b[31m[DEBUG]\x1b[0m %s \n", msg);
@ -23,42 +35,69 @@ void print_error(char * msg){
} }
void print_peers(list * l){ void print_peers(list * l){
// Print out the peers we have in the neighbour_list if (DEBUG_LEVEL > 0) {
int nbr_peers = 0; // Print out the peers we have in the neighbour_list
if (l == NULL) { int nbr_peers = 0;
print_error("The neighbour_list is empty !"); if (l == NULL) {
} else { print_error("The neighbour_list is empty !");
print_debug(">> Printing out peer list :"); } else {
while(l != NULL){ print_debug(">> Printing out peer list :");
neighbour_peer * peer = (neighbour_peer *) l->data; while(l != NULL){
char * buff_str_ip[1024]; neighbour_peer * peer = (neighbour_peer *) l->data;
char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); char * buff_str_ip[1024];
int last_seen = time(NULL) - peer->last_seen; char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024);
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); int last_seen = time(NULL) - peer->last_seen;
nbr_peers++; 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);
if (l->next == NULL) { nbr_peers++;
break; if (l->next == NULL) {
} else { break;
l = l->next; } 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){ 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) { if (l == NULL) {
print_error("Message list is empty !"); print_error("Message list is empty !");
} else { } else {
print_debug(">> Printing messages we know of so far :");
int nbr_msg = 0; int nbr_msg = 0;
setlocale(LC_ALL, "en_US.utf8"); setlocale(LC_ALL, "en_US.utf8");
print_debug(">> Peer ID | Seqno | Length | Message "); print_info(">> Peer ID | Seqno | Length | Message ");
while(l != NULL){ while(l != NULL){
pub_data * message = l->data; 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++; nbr_msg++;
if (l->next == NULL) { if (l->next == NULL) {
break; break;
@ -66,7 +105,6 @@ void print_data(list * l){
l = l->next; 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);
} }
} }

View File

@ -4,7 +4,11 @@
#include "node.h" #include "node.h"
#define DEBUG_LEVEL 2 #define DEBUG_LEVEL 0
void welcome();
void print_info(char * msg);
void print_debug(char * msg); void print_debug(char * msg);
@ -13,4 +17,7 @@ void print_error(char * msg);
void print_peers(list * l); void print_peers(list * l);
void print_data(list * l); void print_data(list * l);
void print_data_info(list * l);
#endif #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 is bigger or equals than our stored seqno then update seqno
if( ((seqno - found->seqno) & 32768) == 0 ) { 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); found->seqno = (seqno + 1) % (65535);
return 1; return 1;
} }
// Else, do nothing // 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; 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) { if(found != NULL) {
// Check if seqno is smaller or equals to our stored seqno // Check if seqno is smaller or equals to our stored seqno
if( ((found->seqno - seqno) & 32768) == 0 ) { 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; 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)); build_node_state_req(&new_tlv, be64toh(*id));
add_tlv(&pack, &new_tlv, sender, socket_num); 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 // The position is updated
tlv_len = data[pos+1]; 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); int rc = add_data(tlv_len - 26, be64toh(*id), be16toh(*seqno), cur_message, pdata);
if (rc < 0) { if (rc < 0) {
print_error("Error while adding node state !"); print_error("Error while adding node state !");
} else {
print_info("Received a message.");
} }
// The position is updated // The position is updated
pos += tlv_len + 2; pos += tlv_len + 2;
@ -1431,14 +1435,23 @@ int run_node(int sock_fd){
} }
// Remove trailing \n // Remove trailing \n
input_buffer[strcspn(input_buffer, "\n")] = 0; 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 ); printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : “%s”\n", input_buffer );
} }
// Add message to the message table. // Add message to the message table.
if (add_message(input_buffer, bytes-1) < 0) { 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.."); print_debug(">> Error while trying to add the message to the list of messages, please try again..");
// Reset buffer // Reset buffer
memset(input_buffer, 0, sizeof(input_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; int sock_fd;
bootstrap_node(&sock_fd, root_peer_ip, root_peer_port); bootstrap_node(&sock_fd, root_peer_ip, root_peer_port);
welcome();
run_node(sock_fd); run_node(sock_fd);
close(sock_fd); close(sock_fd);

View File

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