Modification de len_list

This commit is contained in:
n07070 2020-04-27 15:50:23 +02:00
parent 481903c24a
commit 723466fba8
2 changed files with 39 additions and 32 deletions

View File

@ -23,8 +23,13 @@ static list *neighbour_list;
// Looks for more peers
int ask_for_peers(int socket_num) {
// Only ask for more peers if the neighbour list is small enough
if(len_list(neighbour_list) >= 5)
int nbr_peers = len_list(neighbour_list);
if( nbr_peers >= 5){
return 0;
} else if (nbr_peers <= 0){
printf(">> No peers found in the peer list, something terrible happened.\n");
return -1;
} else {
// Get random peer
neighbour_peer *peer = get_random_neighbour();
@ -32,7 +37,6 @@ int ask_for_peers(int socket_num) {
int16_t port = peer->port;
int ifindex = if_nametoindex("eth0");
if(ifindex == 0) {
perror("if_nametoindex failed");
return -1;
@ -51,6 +55,8 @@ int ask_for_peers(int socket_num) {
build_neighbour_req(&neighbour_req);
return send_single_tlv(&neighbour_req, &dest, socket_num);
}
}
// Get list length
@ -62,7 +68,6 @@ int len_list(list *l) {
tmp = tmp->next;
len++;
}
return len;
}
@ -70,9 +75,9 @@ int len_list(list *l) {
neighbour_peer *get_random_neighbour() {
// Get a random number
time_t t;
srand((unsigned) time(&t));
srand((unsigned) time(NULL));
int n = rand() % len_list(neighbour_list);
printf("%i\n", n );
// Get nth neighbour
list *tmp = neighbour_list;
@ -93,8 +98,9 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) {
// check for same ip and same port
peer = (neighbour_peer*) tmp->data;
if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 && peer->port == port)
if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 && peer->port == port) {
return peer;
}
// if they differ, get next peer
tmp = tmp->next;
@ -889,7 +895,7 @@ int run_node(int sock_fd){
// Init the ~20s delay for node update.
srand(time(NULL));
time_t delay = time(NULL) + 20;
time_t delay = time(NULL) + 2;
/* Descriptor zero is stdin */
fds[0].fd = 0;

View File

@ -53,7 +53,8 @@ typedef struct list {
#define LISTEN_PORT 1212
// The node ID
#define NODE_ID 42675882021843277
// #define NODE_ID 42675882021843277
#define NODE_ID 42013376900101010
// The number of neighbours
// The neighbour table has 15 entries