Added printing the peer list

This commit is contained in:
n07070 2020-04-29 16:40:01 +02:00
parent ab1f278685
commit 72b00cb07d
3 changed files with 50 additions and 1 deletions

View File

@ -1,11 +1,13 @@
#include <stdio.h>
#include "debug.h"
#include "node.h"
#include <arpa/inet.h>
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){
}

View File

@ -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

View File

@ -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);
}