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

@ -6,6 +6,7 @@ from ipaddress import IPv6Address
from enum import Enum
import socket
import sys
import time
class TLV:
@ -143,7 +144,20 @@ class HelloTLV(TLV):
return data
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_"
+ (":3_hearts:" if self.is_long else "smiling_eyes:"))
@ -171,9 +185,9 @@ class NeighbourTLV(TLV):
self.port.to_bytes(2, sys.byteorder)
def handle(self, squirrel: Any, sender: Any) -> None:
# TODO Implement NeighbourTLV
squirrel.add_system_message("I have a friend!")
squirrel.add_system_message(f"Welcome {self.ip_address}:{self.port}!")
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)
squirrel.add_system_message(f"New potential friend {self.ip_address}:{self.port}!")
class DataTLV(TLV):
@ -301,9 +315,10 @@ class GoAwayTLV(TLV):
self.message.encode("UTF-8")[:self.length - 1]
def handle(self, squirrel: Any, sender: Any) -> None:
# TODO Implement GoAwayTLV
squirrel.add_system_message("Some told me that he went away. That's not very nice :( "
"I should send him some cake.")
if squirrel.is_active(sender) :
squirrel.activehazelnuts.pop((sender.addess, sender.port))
squirrel.potentialhazelnuts[(sender.address, sender.port)] = sender
squirrel.add_system_message("Some told me that he went away : "+message)
class WarningTLV(TLV):