NameMsg support

This commit is contained in:
Pascal Serrarens 2024-12-31 14:34:25 +01:00
parent 637a0935b0
commit 85f66fa50f
5 changed files with 75 additions and 45 deletions

View File

@ -52,36 +52,6 @@ class ThingMsg(IMessage):
] ]
return ThingMsg.length 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): class ModelUrlMsg(IMessage):
id = 0x90 id = 0x90
length = 6 length = 6

37
NameMsg.py Normal file
View File

@ -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

View File

@ -1,9 +1,9 @@
import socket import socket
import Messages
import threading import threading
from ClientMsg import ClientMsg from ClientMsg import ClientMsg
from NetworkIdMsg import NetworkIdMsg from NetworkIdMsg import NetworkIdMsg
from NameMsg import NameMsg
from Thing import Thing from Thing import Thing
## A participant is device which can communicate with other participants ## 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): def __init__(self, ipAddress = "0.0.0.0", port=7681, remote=False):
self.buffer = bytearray(256) self.buffer = bytearray(256)
self.ipAddress = ipAddress self.ip_address = ipAddress
self.port = port self.port = port
self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) self.udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
@ -25,6 +25,7 @@ class Participant:
self.network_id = 0 self.network_id = 0
self.nextPublishMe = 0 self.nextPublishMe = 0
self.others = [] self.others = []
self.AddParticipant(self.ip_address, self.port)
self.thread = threading.Thread(target = self.Receiver) self.thread = threading.Thread(target = self.Receiver)
self.thread.daemon = True self.thread.daemon = True
@ -32,14 +33,14 @@ class Participant:
def GetParticipant(self, ip_address, port): def GetParticipant(self, ip_address, port):
found_participants = (item for item in self.others 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) participant = next(found_participants, None)
return participant return participant
def AddParticipant(self, ip_address, port): def AddParticipant(self, ip_address, port):
remote_participant = Participant(ip_address, port, remote=True) remote_participant = Participant(ip_address, port, remote=True)
self.others.append(remote_participant)
remote_participant.network_id = len(self.others) remote_participant.network_id = len(self.others)
self.others.append(remote_participant)
return remote_participant return remote_participant
def Send(self, msg): def Send(self, msg):
@ -47,7 +48,8 @@ class Participant:
if buffer_size <= 0: if buffer_size <= 0:
return True 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 return True
def Publish(self, msg): def Publish(self, msg):
@ -73,32 +75,42 @@ class Participant:
else: else:
print(f'############################ New Client') print(f'############################ New Client')
pass
def ProcessNetworkIdMsg(self, msg: NetworkIdMsg): def ProcessNetworkIdMsg(self, msg: NetworkIdMsg):
self.network_id = msg.network_id self.network_id = msg.network_id
print(f'receive 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): def ProcessInvestigateMsg(self, data: bytearray):
pass pass
def ProcessNameMsg(self, msg: NameMsg):
print(f'received name {msg.name}')
def Receiver(self): def Receiver(self):
while True: while True:
data, addr = self.udp_socket.recvfrom(1024) 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: 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) remote_participant.ReceiveData(data)
def ReceiveData(self, data): def ReceiveData(self, data):
msgId = data[0] msgId = data[0]
# print(f'msg {msgId} ')
match msgId: match msgId:
case ClientMsg.id: case ClientMsg.id:
self.ProcessClientMsg(ClientMsg(data)) self.ProcessClientMsg(ClientMsg(data))
case NetworkIdMsg.id: case NetworkIdMsg.id:
self.ProcessNetworkIdMsg(NetworkIdMsg(data)) self.ProcessNetworkIdMsg(NetworkIdMsg(data))
case Messages.InvestigateMsg.id: case NameMsg.id:
msg = Messages.InvestigateMsg(data) self.ProcessNameMsg(NameMsg(data))
self.ProcessInvestigateMsg(msg) # case Messages.InvestigateMsg.id:
case Messages.BinaryMsg.id: # msg = Messages.InvestigateMsg(data)
msg = Messages.BinaryMsg(data) # self.ProcessInvestigateMsg(msg)
msg.thing.ProcessBinary(msg.data) # case Messages.BinaryMsg.id:
# msg = Messages.BinaryMsg(data)
# msg.thing.ProcessBinary(msg.data)

View File

@ -18,6 +18,17 @@ class ThingTest(unittest.TestCase):
milliseconds = time.time() * 1000 milliseconds = time.time() * 1000
participant.Update(milliseconds) participant.Update(milliseconds)
def test_thing_msg(self):
participant = Participant("127.0.0.1")
thing = Thing()
thing.name = "First thing"
milliseconds = time.time() * 1000
start_time = milliseconds
while milliseconds < start_time + 5000:
milliseconds = time.time() * 1000
participant.Update(milliseconds)
def test_something(self): def test_something(self):
self.assertEqual(1 + 1, 2) self.assertEqual(1 + 1, 2)