From 85f66fa50f23717a1a2d1618c56c0e2721033d8b Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 31 Dec 2024 14:34:25 +0100 Subject: [PATCH] NameMsg support --- Messages.py | 30 -------------- NameMsg.py | 37 +++++++++++++++++ Participant.py | 42 +++++++++++++------- test/__pycache__/thing_test.cpython-312.pyc | Bin 2309 -> 2842 bytes test/thing_test.py | 11 +++++ 5 files changed, 75 insertions(+), 45 deletions(-) create mode 100644 NameMsg.py diff --git a/Messages.py b/Messages.py index fcd3ea0..5f7f934 100644 --- a/Messages.py +++ b/Messages.py @@ -52,36 +52,6 @@ class ThingMsg(IMessage): ] return ThingMsg.length -class NameMsg(IMessage): - id = 0x91 - length = 4 - - def __init__(self, network_id, thing): - self.network_id = network_id - self.thing = thing - - def Serialize(self, buffer_ref): - if self.network_id is None or self.thing is None or self.thing.name is None: - return 0 - - buffer: bytearray = buffer_ref[0] - - encoded_name = self.thing.name.encode('utf-8') - name_length = len(encoded_name) - full_length = NameMsg.length + name_length - if name_length == 0 or full_length > len(buffer): - return 0 - - buffer[0:NameMsg.length] = [ - NameMsg.id, - self.network_id, - self.thing.id, - name_length - ] - # Append the name string - buffer[NameMsg.length:full_length] = encoded_name - return full_length - class ModelUrlMsg(IMessage): id = 0x90 length = 6 diff --git a/NameMsg.py b/NameMsg.py new file mode 100644 index 0000000..3b08006 --- /dev/null +++ b/NameMsg.py @@ -0,0 +1,37 @@ +from Messages import IMessage + +class NameMsg(IMessage): + id = 0x91 + length = 4 + + def __init__(self, network_id, thing=None): + if isinstance(network_id, bytes): + self.network_id = network_id[1] + self.thing_id = network_id[2] + name_length = network_id[3] + self.name = network_id[NameMsg.length:].decode("utf-8") + else: + self.network_id = network_id + self.thing = thing + + def Serialize(self, buffer_ref): + if self.network_id is None or self.thing is None or self.thing.name is None: + return 0 + + buffer: bytearray = buffer_ref[0] + + encoded_name = self.thing.name.encode('utf-8') + name_length = len(encoded_name) + full_length = NameMsg.length + name_length + if name_length == 0 or full_length > len(buffer): + return 0 + + buffer[0:NameMsg.length] = [ + NameMsg.id, + self.network_id, + self.thing.id, + name_length + ] + # Append the name string + buffer[NameMsg.length:full_length] = encoded_name + return full_length diff --git a/Participant.py b/Participant.py index e47821c..d4eb7c2 100644 --- a/Participant.py +++ b/Participant.py @@ -1,9 +1,9 @@ import socket -import Messages import threading from ClientMsg import ClientMsg from NetworkIdMsg import NetworkIdMsg +from NameMsg import NameMsg from Thing import Thing ## A participant is device which can communicate with other participants @@ -14,7 +14,7 @@ class Participant: def __init__(self, ipAddress = "0.0.0.0", port=7681, remote=False): self.buffer = bytearray(256) - self.ipAddress = ipAddress + self.ip_address = ipAddress self.port = port self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) @@ -25,6 +25,7 @@ class Participant: self.network_id = 0 self.nextPublishMe = 0 self.others = [] + self.AddParticipant(self.ip_address, self.port) self.thread = threading.Thread(target = self.Receiver) self.thread.daemon = True @@ -32,14 +33,14 @@ class Participant: def GetParticipant(self, ip_address, port): found_participants = (item for item in self.others - if item.ipAddress == ip_address and item.port == port) + if item.ip_address == ip_address and item.port == port) participant = next(found_participants, None) return participant def AddParticipant(self, ip_address, port): remote_participant = Participant(ip_address, port, remote=True) - self.others.append(remote_participant) remote_participant.network_id = len(self.others) + self.others.append(remote_participant) return remote_participant def Send(self, msg): @@ -47,7 +48,8 @@ class Participant: if buffer_size <= 0: return True - self.udp_socket.sendto(self.buffer[:buffer_size], (self.ipAddress, self.port)) + # print(f'send {self.buffer[0]} to {self.ip_address} {self.port}') + self.udp_socket.sendto(self.buffer[:buffer_size], (self.ip_address, self.port)) return True def Publish(self, msg): @@ -73,32 +75,42 @@ class Participant: else: print(f'############################ New Client') - pass def ProcessNetworkIdMsg(self, msg: NetworkIdMsg): self.network_id = msg.network_id print(f'receive network id {msg.network_id}') - pass + for thing in Thing.allThings: + self.Send(NameMsg(self.network_id, thing)) + def ProcessInvestigateMsg(self, data: bytearray): pass + + def ProcessNameMsg(self, msg: NameMsg): + print(f'received name {msg.name}') def Receiver(self): while True: data, addr = self.udp_socket.recvfrom(1024) - remote_participant = self.GetParticipant(addr[0], addr[1]) + remote_ip_address = addr[0] + remote_port = addr[1] + # print(f'msg received from {remote_ip_address}:{remote_port}') + remote_participant = self.GetParticipant(remote_ip_address, remote_port) if remote_participant is None: - remote_participant = self.AddParticipant(addr[0], addr[1]) + remote_participant = self.AddParticipant(remote_ip_address, self.port) remote_participant.ReceiveData(data) def ReceiveData(self, data): msgId = data[0] + # print(f'msg {msgId} ') match msgId: case ClientMsg.id: self.ProcessClientMsg(ClientMsg(data)) case NetworkIdMsg.id: self.ProcessNetworkIdMsg(NetworkIdMsg(data)) - case Messages.InvestigateMsg.id: - msg = Messages.InvestigateMsg(data) - self.ProcessInvestigateMsg(msg) - case Messages.BinaryMsg.id: - msg = Messages.BinaryMsg(data) - msg.thing.ProcessBinary(msg.data) + case NameMsg.id: + self.ProcessNameMsg(NameMsg(data)) + # case Messages.InvestigateMsg.id: + # msg = Messages.InvestigateMsg(data) + # self.ProcessInvestigateMsg(msg) + # case Messages.BinaryMsg.id: + # msg = Messages.BinaryMsg(data) + # msg.thing.ProcessBinary(msg.data) diff --git a/test/__pycache__/thing_test.cpython-312.pyc b/test/__pycache__/thing_test.cpython-312.pyc index 792192610138895a79a4a8c37f77aaddb2e1417e..529efc281d148d9bc01a519f2ffe3cd91e28a09a 100644 GIT binary patch delta 935 zcmZ8f(MwZN7(ZwC-tBHqnZo8~IX6r5QhHlu6sQ$SBsmpSf*u-UyUdx};@oSrwUL3+ z0;$Gegy& z9RzIbm!1eorMfTla_L9N5x32eyY{TC*xxtNAxign!qRH3{uBtQ@|TtnUEv=s*Sl}1 z6S~H9^+8mRMW@w)Ol&5lr43yj&>pIzSz|JjR%4m8!7|ga4AWFY(+!pTg->H3MDY9b z4PcQN6c*Fsbame^Py-pJt?sV{OWcyTL@fJTV%Q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