From a28975c8959b752b4376f2d55588f2b4db220663 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Mon, 21 Dec 2020 16:04:00 +0100 Subject: [PATCH] Clients can communicate together (without any innondation) Signed-off-by: Yohann D'ANELLO --- squinnondation/squinnondation.py | 41 +++++++++++++------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/squinnondation/squinnondation.py b/squinnondation/squinnondation.py index 740c94b..b9a3384 100644 --- a/squinnondation/squinnondation.py +++ b/squinnondation/squinnondation.py @@ -47,21 +47,25 @@ class Squinnondation: if not instance.args.bind_only: hazelnut = Hazelnut(address=instance.client_address, port=instance.client_port) - pkt = Packet() - pkt.magic = 95 - pkt.version = 0 - tlv = DataTLV() - msg = f"Hello world, my name is {squirrel.nickname}!" - tlv.data = msg.encode("UTF-8") - tlv.sender_id = 42 - tlv.nonce = 18 - tlv.length = len(msg) + 1 + 1 + 8 + 4 - pkt.body = [tlv] - pkt.body_length = tlv.length + 2 - squirrel.send_packet(hazelnut, pkt) + squirrel.hazelnuts[(instance.client_address, instance.client_port)] = hazelnut Worm(squirrel).start() + while True: + msg = f"<{squirrel.nickname}> {input(f'<{squirrel.nickname}> ')}" + for hazelnut in list(squirrel.hazelnuts.values()): + pkt = Packet() + pkt.magic = 95 + pkt.version = 0 + tlv = DataTLV() + tlv.data = msg.encode("UTF-8") + tlv.sender_id = 42 + tlv.nonce = 18 + tlv.length = len(msg) + 1 + 1 + 8 + 4 + pkt.body = [tlv] + pkt.body_length = tlv.length + 2 + squirrel.send_packet(hazelnut, pkt) + class TLV: """ @@ -418,16 +422,3 @@ class Worm(Thread): print("An error occured while receiving a packet: ", error) else: print(pkt.body[0].data.decode('UTF-8')) - pkt = Packet() - pkt.magic = 95 - pkt.version = 0 - pkt.body = [] - tlv = DataTLV() - msg = f"Hello my dear hazelnut, I am {self.squirrel.nickname}!" - tlv.data = msg.encode("UTF-8") - tlv.sender_id = 42 - tlv.nonce = 18 - tlv.length = len(msg) + 1 + 1 + 8 + 4 - pkt.body.append(tlv) - pkt.body_length = tlv.length + 2 - self.squirrel.send_packet(hazelnut, pkt)