Implemented the fourth phase of neighbour management (the type of addresses sometimes changes in the cide, this should be addressed, there also remains a lot of debugging printing)
This commit is contained in:
parent
b7a495eb11
commit
c4e03ed427
|
@ -10,7 +10,7 @@ import re
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from .messages import Packet, DataTLV, HelloTLV, GoAwayTLV, GoAwayType
|
from .messages import Packet, DataTLV, HelloTLV, GoAwayTLV, GoAwayType, NeighbourTLV
|
||||||
|
|
||||||
|
|
||||||
class Hazelnut:
|
class Hazelnut:
|
||||||
|
@ -85,7 +85,7 @@ class Squirrel(Hazelnut):
|
||||||
"""
|
"""
|
||||||
Returns a new hazelnut (with no id nor nickname)
|
Returns a new hazelnut (with no id nor nickname)
|
||||||
"""
|
"""
|
||||||
hazelnut = Hazelnut(address=address, port=port)
|
hazelnut = Hazelnut(address=str(address), port=port)
|
||||||
return hazelnut
|
return hazelnut
|
||||||
|
|
||||||
def is_active(self, hazel: Hazelnut) -> bool :
|
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.potentialhazelnuts[(str(hazelnut[0].address), hazelnut[0].port)] = hazelnut[0]
|
||||||
|
|
||||||
self.refresh_lock.release()
|
self.refresh_lock.release()
|
||||||
|
|
||||||
|
|
||||||
def send_neighbours(self) -> None:
|
def send_neighbours(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -559,9 +560,16 @@ class Squirrel(Hazelnut):
|
||||||
self.refresh_lock.acquire()
|
self.refresh_lock.acquire()
|
||||||
|
|
||||||
nbNS = 0
|
nbNS = 0
|
||||||
#for hazelnut in self.activehazelnuts.values() :
|
#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
|
||||||
# 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
|
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()
|
self.refresh_lock.release()
|
||||||
|
|
||||||
|
@ -623,7 +631,9 @@ class HazelManager(Thread):
|
||||||
self.squirrel.verify_activity()
|
self.squirrel.verify_activity()
|
||||||
|
|
||||||
#Fourth part: verify symmetric neighbours and send NeighbourTLV every minute
|
#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:
|
class Message:
|
||||||
|
|
|
@ -158,9 +158,9 @@ class HelloTLV(TLV):
|
||||||
|
|
||||||
def handle(self, squirrel: Any, sender: Any) -> None:
|
def handle(self, squirrel: Any, sender: Any) -> None:
|
||||||
timeH = time.time()
|
timeH = time.time()
|
||||||
timeHL = None
|
|
||||||
if not squirrel.is_active(sender) :
|
if not squirrel.is_active(sender) :
|
||||||
sender.id = self.source_id #The sender we are given misses an id
|
sender.id = self.source_id #The sender we are given misses an id
|
||||||
|
timeHL = time.time()
|
||||||
else :
|
else :
|
||||||
timeHL = squirrel.activehazelnuts[(str(sender.address), sender.port)]
|
timeHL = squirrel.activehazelnuts[(str(sender.address), sender.port)]
|
||||||
if self.is_long and self.dest_id == squirrel.id :
|
if self.is_long and self.dest_id == squirrel.id :
|
||||||
|
@ -211,12 +211,12 @@ class NeighbourTLV(TLV):
|
||||||
self.port.to_bytes(2, sys.byteorder)
|
self.port.to_bytes(2, sys.byteorder)
|
||||||
|
|
||||||
def handle(self, squirrel: Any, sender: Any) -> None:
|
def handle(self, squirrel: Any, sender: Any) -> None:
|
||||||
if not (ip_address,port) in squirrel.activehazelnuts and not (ip_address,port) in squirrel.potentialhazelnuts:
|
if not (str(self.ip_address),self.port) in squirrel.activehazelnuts and not (str(self.ip_address),self.port) in squirrel.potentialhazelnuts:
|
||||||
squirrel.potentialhazelnuts[(ip_address, port)] = squirrel.new_hazel(ip_address, port)
|
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}!")
|
squirrel.add_system_message(f"New potential friend {self.ip_address}:{self.port}!")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def construct(address: IPv6Address, port: int) -> "Pad1TLV":
|
def construct(address: IPv6Address, port: int) -> "NeighbourTLV":
|
||||||
tlv = NeighbourTLV()
|
tlv = NeighbourTLV()
|
||||||
tlv.type = 3
|
tlv.type = 3
|
||||||
tlv.length = 18 #A priori...
|
tlv.length = 18 #A priori...
|
||||||
|
|
Loading…
Reference in New Issue