NameMsg support
This commit is contained in:
parent
637a0935b0
commit
85f66fa50f
30
Messages.py
30
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
|
||||
|
37
NameMsg.py
Normal file
37
NameMsg.py
Normal 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
|
@ -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)
|
||||
|
Binary file not shown.
@ -18,6 +18,17 @@ class ThingTest(unittest.TestCase):
|
||||
milliseconds = time.time() * 1000
|
||||
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):
|
||||
self.assertEqual(1 + 1, 2)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user