Completed the handle function for the Hello, Neighbour and GoAway

This commit is contained in:
eichhornchen
2020-12-24 13:07:24 +01:00
parent 61ab96abdc
commit e4797b745a
3 changed files with 50 additions and 16 deletions

View File

@ -68,18 +68,37 @@ class Squirrel(Hazelnut):
curses.init_color(curses.COLOR_WHITE, 1000, 1000, 1000)
for i in range(curses.COLOR_BLACK + 1, curses.COLOR_WHITE):
curses.init_pair(i + 1, i, curses.COLOR_BLACK)
self.hazelnuts = dict()
#dictionnaries of neighbours
self.potentialhazelnuts = dict()
self.activehazelnuts = dict() #of the form [hazelnut, time of last
#hello, time of last long hello, is symetric]
self.add_system_message(f"Listening on {self.address}:{self.port}")
def new_hazel(self, address: IPv6Address, port: int) -> Hazelnut:
"""
Returns a new hazelnut (with no id nor nickname)
"""
hazelnut = Hazelnut(address=address, port=port)
return hazelnut
def is_active(self, hazel: Hazelnut) -> bool :
return (hazel.address, hazel.port) in self.activehazelnuts
def is_potential(self, hazel: Hazelnut) -> bool :
return (hazel.address, hazel.port) in self.potentialhazelnuts
def remove_from_potential(self, address: IPv6Address, port: int)-> None:
self.potentialhazelnuts.pop((address, port), None)
def find_hazelnut(self, address: str, port: int) -> Hazelnut:
"""
Translate an address into a hazelnut, and store it in the list of the hazelnuts, ie. the neighbours.
Translate an address into a hazelnut. If this hazelnut does not exist,
creates a new hazelnut.
"""
if (address, port) in self.hazelnuts:
return self.hazelnuts[(address, port)]
if (address, port) in self.activehazelnuts:
return self.activehazelnuts[(address, port)][0]
hazelnut = Hazelnut(address=address, port=port)
self.hazelnuts[(address, port)] = hazelnut
return hazelnut
def send_packet(self, client: Hazelnut, pkt: Packet) -> int:
@ -205,7 +224,7 @@ class Squirrel(Hazelnut):
self.add_message(msg)
pkt = Packet.construct(DataTLV.construct(msg, self))
for hazelnut in list(self.hazelnuts.values()):
for hazelnut in list(self.activehazelnuts.values()):
self.send_packet(hazelnut, pkt)
def handle_mouse_click(self, y: int, x: int, attr: int) -> None: