Add basic implementations for all TLVs

Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
Yohann D'ANELLO 2020-12-21 16:04:09 +01:00
parent 8a066bb340
commit 592cbc3792
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 33 additions and 1 deletions

View File

@ -236,6 +236,11 @@ class Pad1TLV(TLV):
""" """
return self.type.to_bytes(1, "big") return self.type.to_bytes(1, "big")
def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None:
# TODO Add some easter eggs
squirrel.add_system_message(f"For each byte in the packet that I received, you will die today. "
"And eat cookies.")
class PadNTLV(TLV): class PadNTLV(TLV):
""" """
@ -264,6 +269,10 @@ class PadNTLV(TLV):
""" """
return self.type.to_bytes(1, "big") + self.length.to_bytes(1, "big") + self.mbz[:self.length] return self.type.to_bytes(1, "big") + self.length.to_bytes(1, "big") + self.mbz[:self.length]
def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None:
# TODO Add some easter eggs
squirrel.add_system_message(f"I received {self.length} zeros, am I so a bag guy ? :cold_sweat:")
class HelloTLV(TLV): class HelloTLV(TLV):
type: int = 2 type: int = 2
@ -281,7 +290,7 @@ class HelloTLV(TLV):
self.type = raw_data[0] self.type = raw_data[0]
self.length = raw_data[1] self.length = raw_data[1]
self.source_id = int.from_bytes(raw_data[2:10], "big") self.source_id = int.from_bytes(raw_data[2:10], "big")
if self.length == 16: if self.is_long:
self.dest_id = int.from_bytes(raw_data[10:18], "big") self.dest_id = int.from_bytes(raw_data[10:18], "big")
def marshal(self) -> bytes: def marshal(self) -> bytes:
@ -290,6 +299,15 @@ class HelloTLV(TLV):
data += self.dest_id.to_bytes(8, "big") data += self.dest_id.to_bytes(8, "big")
return data return data
def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None:
# TODO Implement HelloTLV
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:"))
@property
def is_long(self):
return self.length == 16
class NeighbourTLV(TLV): class NeighbourTLV(TLV):
type: int = 3 type: int = 3
@ -309,6 +327,11 @@ class NeighbourTLV(TLV):
self.ip_address.packed + \ self.ip_address.packed + \
self.port.to_bytes(2, "big") self.port.to_bytes(2, "big")
def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None:
# TODO Implement NeighbourTLV
squirrel.add_system_message("I have a friend!")
squirrel.add_system_message(f"Welcome {self.ip_address}:{self.port}!")
class DataTLV(TLV): class DataTLV(TLV):
type: int = 4 type: int = 4
@ -357,6 +380,10 @@ class AckTLV(TLV):
self.sender_id.to_bytes(8, "big") + \ self.sender_id.to_bytes(8, "big") + \
self.nonce.to_bytes(4, "big") self.nonce.to_bytes(4, "big")
def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None:
# TODO Implement AckTLV
squirrel.add_system_message("I received an AckTLV. I don't know what to do with it. Please implement me!")
class GoAwayTLV(TLV): class GoAwayTLV(TLV):
class GoAwayType(Enum): class GoAwayType(Enum):
@ -382,6 +409,11 @@ class GoAwayTLV(TLV):
self.code.value.to_bytes(1, "big") + \ self.code.value.to_bytes(1, "big") + \
self.message.encode("UTF-8")[:self.length - 1] self.message.encode("UTF-8")[:self.length - 1]
def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> 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.")
class WarningTLV(TLV): class WarningTLV(TLV):
type: int = 7 type: int = 7