From 72b00cb07d0b99f567c36b5c1da31fa476466e33 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 16:40:01 +0200 Subject: [PATCH] Added printing the peer list --- src/debug.c | 41 ++++++++++++++++++++++++++++++++++++++++- src/debug.h | 6 ++++++ src/node.c | 4 ++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/debug.c b/src/debug.c index e00c265..1c46f4e 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1,11 +1,13 @@ #include #include "debug.h" +#include "node.h" +#include void print_debug(char * msg){ if (DEBUG_LEVEL > 0) { printf("\x1b[31m[DEBUG]\x1b[0m %s \n", msg); } - if (DEBUG_LEVEL > 2) { + if (DEBUG_LEVEL > 9) { getchar(); } } @@ -16,3 +18,40 @@ void print_error(char * msg){ getchar(); } } + +// typedef struct neighbour_peer { +// struct in6_addr ip; +// int16_t port; +// char is_temporary; +// time_t last_seen; +// } neighbour_peer; + +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; + } + + } + printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i peers.\n", nbr_peers); + print_debug(">> Finished printing peer list."); + } +} + +void print_data(list * l){ + +} diff --git a/src/debug.h b/src/debug.h index 33e51db..a8f01d6 100644 --- a/src/debug.h +++ b/src/debug.h @@ -1,9 +1,15 @@ #ifndef DEBUG_H #define DEBUG_H +#include "node.h" + #define DEBUG_LEVEL 2 void print_debug(char * msg); void print_error(char * msg); + +void print_peers(list * l); + +void print_data(list * l); #endif diff --git a/src/node.c b/src/node.c index e8b3e31..6960057 100644 --- a/src/node.c +++ b/src/node.c @@ -24,9 +24,12 @@ static list *neighbour_list; // Looks for more peers int ask_for_peers(int socket_num) { print_debug(">> Asking for more peers..."); + // Print out the current peer list. + // Only ask for more peers if the neighbour list is small enough int nbr_peers = len_list(neighbour_list); if( nbr_peers >= 5){ + print_debug(">> We have enough peers, skipping..."); return 0; } else if (nbr_peers <= 0){ print_debug(">> No peers found in the peer list, something terrible happened."); @@ -1113,6 +1116,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc } int t_ask_for_more_peers(int sock_fd){ + print_peers(neighbour_list); return ask_for_peers(sock_fd); }