Properly handle TLVs

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

View File

@ -111,6 +111,8 @@ class Squinnondation:
elif key == "KEY_END":
squirrel.input_index = len(squirrel.input_buffer)
continue
elif key == "KEY_RESIZE":
continue
elif key == "KEY_MOUSE":
try:
_, x, y, _, attr = curses.getmouse()
@ -205,6 +207,12 @@ class TLV:
"""
return True
def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None:
"""
Indicates what to do when this TLV is received from a given hazel.
It is ensured that the data is valid.
"""
@staticmethod
def tlv_classes():
return [Pad1TLV, PadNTLV, HelloTLV, NeighbourTLV, DataTLV, AckTLV, GoAwayTLV, WarningTLV]
@ -323,6 +331,13 @@ class DataTLV(TLV):
self.nonce.to_bytes(4, "big") + \
self.data
def handle(self, squirrel: "Squirrel", sender: "Hazelnut") -> None:
"""
A message has been sent. We log it.
TODO: Check that the tuple (sender_id, nonce) is unique to avoid duplicates.
"""
squirrel.add_message(self.data.decode('UTF-8'))
class AckTLV(TLV):
type: int = 5
@ -714,9 +729,11 @@ class Worm(Thread):
while True:
try:
pkt, hazelnut = self.squirrel.receive_packet()
pkt.validate_data()
except ValueError as error:
self.squirrel.add_system_message("An error occurred while receiving a packet: {}".format(error))
else:
self.squirrel.add_message(pkt.body[0].data.decode('UTF-8'))
for tlv in pkt.body:
tlv.handle(self.squirrel, hazelnut)
self.squirrel.refresh_history()
self.squirrel.refresh_input()