This commit is contained in:
2021-01-01 17:56:48 +01:00
parent 7abaa7bcd4
commit ac249716f7
3 changed files with 154 additions and 147 deletions

View File

@ -123,13 +123,14 @@ class PadNTLV(TLV):
squirrel.add_system_message(f"I received {self.length} zeros, am I so a bag guy ? :cold_sweat:")
@staticmethod
def construct(length: int) -> "Pad1TLV":
def construct(length: int) -> "PadNTLV":
tlv = PadNTLV()
tlv.type = 1
tlv.length = length
tlv.mbz = b'0'*length
tlv.mbz = b'0' * length
return tlv
class HelloTLV(TLV):
type: int = 2
length: int
@ -157,24 +158,23 @@ class HelloTLV(TLV):
return data
def handle(self, squirrel: Any, sender: Any) -> None:
timeH = time.time()
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[(sender.address, sender.port)][2]
if self.is_long and self.dest_id == squirrel.id :
timeHL = time.time()
time_h = time.time()
if not squirrel.is_active(sender):
sender.id = self.source_id # The sender we are given misses an id
time_hl = time.time()
else:
time_hl = squirrel.activehazelnuts[(sender.address, sender.port)][2]
if self.is_long and self.dest_id == squirrel.id:
time_hl = time.time()
#Make sure the sender is not in the potential hazelnuts
# Make sure the sender is not in the potential hazelnuts
squirrel.remove_from_potential(sender.address, sender.port)
#Add entry to/actualize the active hazelnuts dictionnary
squirrel.activehazelnuts[(sender.address, sender.port)] = [sender, timeH,\
timeHL, True]
# Add entry to/actualize the active hazelnuts dictionnary
squirrel.activehazelnuts[(sender.address, sender.port)] = [sender, time_h, time_hl, True]
squirrel.nbNS += 1
#squirrel.add_system_message(f"Aaaawwww, {self.source_id} spoke to me and said me Hello "
# + ("long" if self.is_long else "short"))
# squirrel.add_system_message(f"Aaaawwww, {self.source_id} spoke to me and said me Hello "
# + ("long" if self.is_long else "short"))
@property
def is_long(self) -> bool:
@ -187,11 +187,11 @@ class HelloTLV(TLV):
tlv.source_id = squirrel.id if squirrel else 0
if (destination is None) or destination.id == -1 or length == 8:
tlv.length = 8
tlv.dest_id = None #if the destination id is not known, or
#if the destination was not precised, send a short hello
else :
tlv.dest_id = None # if the destination id is not known, or
# if the destination was not precised, send a short hello
else:
tlv.length = 16
tlv.dest_id = destination.id
tlv.dest_id = destination.id
return tlv
@ -214,19 +214,22 @@ class NeighbourTLV(TLV):
self.port.to_bytes(2, sys.byteorder)
def handle(self, squirrel: Any, sender: Any) -> None:
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(str(self.ip_address), self.port)
#squirrel.add_system_message(f"New potential friend {self.ip_address}:{self.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(str(self.ip_address), self.port)
# squirrel.add_system_message(f"New potential friend {self.ip_address}:{self.port}!")
@staticmethod
def construct(address: str, port: int) -> "NeighbourTLV":
tlv = NeighbourTLV()
tlv.type = 3
tlv.length = 18 #A priori...
tlv.length = 18 # A priori...
tlv.ip_address = IPv6Address(address)
tlv.port = port
return tlv
class DataTLV(TLV):
type: int = 4
length: int
@ -259,10 +262,10 @@ class DataTLV(TLV):
TODO: Check that the tuple (sender_id, nonce) is unique to avoid duplicates.
"""
msg = self.data.decode('UTF-8')
# Acknowledge the packet
squirrel.send_packet(sender, Packet.construct(AckTLV.construct(self.sender_id, self.nonce)))
if not squirrel.receive_message_from(self, msg, self.sender_id, self.nonce, sender):
# The message was already received, do not print it
squirrel.add_system_message(f"I was inundated a message which I already knew {self.sender_id, self.nonce}")
@ -331,14 +334,15 @@ class AckTLV(TLV):
tlv.nonce = nonce
return tlv
class GoAwayType(Enum):
UNKNOWN = 0
EXIT = 1
TIMEOUT = 2
PROTOCOL_VIOLATION = 3
UNKNOWN = 0
EXIT = 1
TIMEOUT = 2
PROTOCOL_VIOLATION = 3
class GoAwayTLV(TLV):
type: int = 6
length: int
code: GoAwayType
@ -357,20 +361,21 @@ class GoAwayTLV(TLV):
self.message.encode("UTF-8")[:self.length - 1]
def handle(self, squirrel: Any, sender: Any) -> None:
if squirrel.is_active(sender) :
if squirrel.is_active(sender):
squirrel.activehazelnuts.pop((sender.address, sender.port))
squirrel.potentialhazelnuts[(sender.address, sender.port)] = sender
squirrel.add_system_message("Some told me that he went away : "+self.message)
squirrel.add_system_message("Some told me that he went away : " + self.message)
@staticmethod
def construct(GAtype: GoAwayType, message: str) -> "GoAwayTLV":
def construct(ga_type: GoAwayType, message: str) -> "GoAwayTLV":
tlv = GoAwayTLV()
tlv.type = 6
tlv.code = GAtype
tlv.code = ga_type
tlv.message = message
tlv.length = 1 + len(tlv.message)
return tlv
class WarningTLV(TLV):
type: int = 7
length: int