From 65c71b8ed0f4466ea681a5b1f85b021547269943 Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Wed, 22 Jan 2020 19:35:00 +0100 Subject: [PATCH] simplify listener --- listener.py | 46 +++++++--------------------------------------- 1 file changed, 7 insertions(+), 39 deletions(-) diff --git a/listener.py b/listener.py index ce79f27..572f4d1 100644 --- a/listener.py +++ b/listener.py @@ -1,43 +1,9 @@ import datetime import socket import sys -from typing import Optional from utils import mac_address, broadcast_address - -class Frame: - - def __init__(self, - destination: bytes, - protocol: bytes, - message: str, - source: bytes = mac_address): - self.destination: bytes = destination - self.source: bytes = source - self.protocol: bytes = protocol - self.message: str = message - - -def decode_packet(packet_bytes: bytes) -> Optional[Frame]: - protocol: bytes = packet_bytes[12:14] - - if protocol != b"\x60\x00": - return None - - try: - destination = packet_bytes[0:6] - source = packet_bytes[6:12] - message = packet_bytes[14:].decode("utf-8").rstrip("\x00") - return Frame(destination=destination, - source=source, - protocol=protocol, - message=message) - except: - # print("[Error] Could not decode message.") - return None - - if __name__ == "__main__": print("Starting listener...") @@ -48,15 +14,17 @@ if __name__ == "__main__": while True: byte_string, addr = connection.recvfrom(65536) - frame = decode_packet(byte_string) + destination = byte_string[0:6] + if byte_string[12:14] == b"\x60\x00" \ + and (destination == mac_address or destination == broadcast_address): - if frame is not None \ - and (frame.destination == mac_address or frame.destination == broadcast_address): + source = byte_string[6:12] + message = byte_string[14:].decode("utf-8").rstrip("\x00") dt_string = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S") print("----") - print("Revcd from {} to {} @{}".format(frame.source, frame.destination, dt_string)) + print("Revcd from {} to {} @{}".format(source, destination, dt_string)) - if frame.message == "ZeusWPI is de max!": + if message == "ZeusWPI is de max!": print("Congratulations, packet received correctly!") else: print("Wrong message, please check that you are sending \"ZeusWPI is de max!\"")