diff --git a/squinnondation/hazel.py b/squinnondation/hazel.py index 9a2f2cb..4dc371a 100644 --- a/squinnondation/hazel.py +++ b/squinnondation/hazel.py @@ -10,7 +10,7 @@ import re import socket import time -from .messages import Packet, DataTLV, HelloTLV, GoAwayTLV, GoAwayType +from .messages import Packet, DataTLV, HelloTLV, GoAwayTLV, GoAwayType, NeighbourTLV class Hazelnut: @@ -85,7 +85,7 @@ class Squirrel(Hazelnut): """ Returns a new hazelnut (with no id nor nickname) """ - hazelnut = Hazelnut(address=address, port=port) + hazelnut = Hazelnut(address=str(address), port=port) return hazelnut def is_active(self, hazel: Hazelnut) -> bool : @@ -550,6 +550,7 @@ class Squirrel(Hazelnut): self.potentialhazelnuts[(str(hazelnut[0].address), hazelnut[0].port)] = hazelnut[0] self.refresh_lock.release() + def send_neighbours(self) -> None: """ @@ -559,9 +560,16 @@ class Squirrel(Hazelnut): self.refresh_lock.acquire() nbNS = 0 - #for hazelnut in self.activehazelnuts.values() : - # if time.time()-hazelnut[2]<2*60: #could send the same to all neighbour, but it means that neighbour A could receive a message with itself in it -> if the others do not pay attention, trouble - + #could send the same to all neighbour, but it means that neighbour A could receive a message with itself in it -> if the others do not pay attention, trouble + for hazelnut in self.activehazelnuts.values() : + if time.time()-hazelnut[2]<=2*60: + nbNS+=1 + ntlv = NeighbourTLV().construct(hazelnut[0].address,hazelnut[0].port) + pkt = Packet().construct(ntlv) + for destination in self.activehazelnuts.values() : + if destination[0].id != hazelnut[0].id : + self.send_packet(destination[0], pkt) + self.nbNS = nbNS self.refresh_lock.release() @@ -623,7 +631,9 @@ class HazelManager(Thread): self.squirrel.verify_activity() #Fourth part: verify symmetric neighbours and send NeighbourTLV every minute - + if time.time()-self.last_neighbour > 10: #60 : + self.squirrel.send_neighbours() + self.last_neighbour = time.time() class Message: diff --git a/squinnondation/messages.py b/squinnondation/messages.py index d6e6312..519a8d5 100644 --- a/squinnondation/messages.py +++ b/squinnondation/messages.py @@ -158,9 +158,9 @@ class HelloTLV(TLV): def handle(self, squirrel: Any, sender: Any) -> None: timeH = time.time() - timeHL = None if not squirrel.is_active(sender) : sender.id = self.source_id #The sender we are given misses an id + timeHL = time.time() else : timeHL = squirrel.activehazelnuts[(str(sender.address), sender.port)] if self.is_long and self.dest_id == squirrel.id : @@ -211,12 +211,12 @@ class NeighbourTLV(TLV): self.port.to_bytes(2, sys.byteorder) def handle(self, squirrel: Any, sender: Any) -> None: - if not (ip_address,port) in squirrel.activehazelnuts and not (ip_address,port) in squirrel.potentialhazelnuts: - squirrel.potentialhazelnuts[(ip_address, port)] = squirrel.new_hazel(ip_address, port) + if not (str(self.ip_address),self.port) in squirrel.activehazelnuts and not (str(self.ip_address),self.port) in squirrel.potentialhazelnuts: + squirrel.potentialhazelnuts[(str(self.ip_address), self.port)] = squirrel.new_hazel(self.ip_address, self.port) squirrel.add_system_message(f"New potential friend {self.ip_address}:{self.port}!") @staticmethod - def construct(address: IPv6Address, port: int) -> "Pad1TLV": + def construct(address: IPv6Address, port: int) -> "NeighbourTLV": tlv = NeighbourTLV() tlv.type = 3 tlv.length = 18 #A priori...