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

@ -69,17 +69,36 @@ class Squirrel(Hazelnut):
for i in range(curses.COLOR_BLACK + 1, curses.COLOR_WHITE): for i in range(curses.COLOR_BLACK + 1, curses.COLOR_WHITE):
curses.init_pair(i + 1, i, curses.COLOR_BLACK) 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}") 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: 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: if (address, port) in self.activehazelnuts:
return self.hazelnuts[(address, port)] return self.activehazelnuts[(address, port)][0]
hazelnut = Hazelnut(address=address, port=port) hazelnut = Hazelnut(address=address, port=port)
self.hazelnuts[(address, port)] = hazelnut
return hazelnut return hazelnut
def send_packet(self, client: Hazelnut, pkt: Packet) -> int: def send_packet(self, client: Hazelnut, pkt: Packet) -> int:
@ -205,7 +224,7 @@ class Squirrel(Hazelnut):
self.add_message(msg) self.add_message(msg)
pkt = Packet.construct(DataTLV.construct(msg, self)) 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) self.send_packet(hazelnut, pkt)
def handle_mouse_click(self, y: int, x: int, attr: int) -> None: def handle_mouse_click(self, y: int, x: int, attr: int) -> None:

View File

@ -6,6 +6,7 @@ from ipaddress import IPv6Address
from enum import Enum from enum import Enum
import socket import socket
import sys import sys
import time
class TLV: class TLV:
@ -143,7 +144,20 @@ class HelloTLV(TLV):
return data return data
def handle(self, squirrel: Any, sender: Any) -> None: def handle(self, squirrel: Any, sender: Any) -> None:
# TODO Implement HelloTLV timeH = time.time()
timeHL = None
if not squirrel.is_active(sender) :
sender.id = source_id #The sender we are given misses an id
else :
timeHL = squirrel.activehazelnuts[(sender.address, sender.port)]
if self.is_long and dest_id == squirrel.id :
timeHL = time.time()
#elif source_id != sender.id :
#That neighbour is lying about its ID
squirrel.remove_from_potential(sender.address, sender.port)
squirrel.activehazelnuts[(sender.address, sender.port)] = [sender, timeH,\
timeHL, True]
squirrel.add_system_message("Aaaawwww, someone spoke to me and said me Hello smiling_face_with_" squirrel.add_system_message("Aaaawwww, someone spoke to me and said me Hello smiling_face_with_"
+ (":3_hearts:" if self.is_long else "smiling_eyes:")) + (":3_hearts:" if self.is_long else "smiling_eyes:"))
@ -171,9 +185,9 @@ 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:
# TODO Implement NeighbourTLV if not (ip_address,port) in squirrel.activehazelnuts and not (ip_address,port) in squirrel.potentialhazelnuts:
squirrel.add_system_message("I have a friend!") squirrel.potentialhazelnuts[(ip_address, port)] = squirrel.new_hazel(ip_address, port)
squirrel.add_system_message(f"Welcome {self.ip_address}:{self.port}!") squirrel.add_system_message(f"New potential friend {self.ip_address}:{self.port}!")
class DataTLV(TLV): class DataTLV(TLV):
@ -301,9 +315,10 @@ class GoAwayTLV(TLV):
self.message.encode("UTF-8")[:self.length - 1] self.message.encode("UTF-8")[:self.length - 1]
def handle(self, squirrel: Any, sender: Any) -> None: def handle(self, squirrel: Any, sender: Any) -> None:
# TODO Implement GoAwayTLV if squirrel.is_active(sender) :
squirrel.add_system_message("Some told me that he went away. That's not very nice :( " squirrel.activehazelnuts.pop((sender.addess, sender.port))
"I should send him some cake.") squirrel.potentialhazelnuts[(sender.address, sender.port)] = sender
squirrel.add_system_message("Some told me that he went away : "+message)
class WarningTLV(TLV): class WarningTLV(TLV):

View File

@ -74,7 +74,7 @@ class Squinnondation:
if instance.args.client_address and instance.args.client_port: if instance.args.client_address and instance.args.client_port:
hazelnut = Hazelnut(address=instance.args.client_address, port=instance.args.client_port) hazelnut = Hazelnut(address=instance.args.client_address, port=instance.args.client_port)
squirrel.hazelnuts[(instance.args.client_address, instance.args.client_port)] = hazelnut squirrel.activehazelnuts[(instance.args.client_address, instance.args.client_port)] = hazelnut
Worm(squirrel).start() Worm(squirrel).start()
squirrel.wait_for_key() squirrel.wait_for_key()