Fixed tests

This commit is contained in:
Pascal Serrarens 2025-05-01 11:30:24 +02:00
parent 794b1ca80e
commit b0a6c1f528
6 changed files with 49 additions and 64 deletions

View File

@ -49,10 +49,8 @@ class NameMsg(IMessage):
buffer: bytearray = buffer_ref[0] buffer: bytearray = buffer_ref[0]
encoded_name = self.name.encode('utf-8') full_length = NameMsg.length + self.name_length
name_length = len(encoded_name) if self.name_length == 0 or full_length > len(buffer):
full_length = NameMsg.length + name_length
if name_length == 0 or full_length > len(buffer):
return 0 return 0
logger.debug(f'Send NameMsg [{self.network_id}/{self.thing_id}] {self.name_length}') logger.debug(f'Send NameMsg [{self.network_id}/{self.thing_id}] {self.name_length}')
@ -61,8 +59,8 @@ class NameMsg(IMessage):
NameMsg.id, NameMsg.id,
self.network_id, self.network_id,
self.thing_id, self.thing_id,
name_length self.name_length
] ]
# Append the name string # Append the name string
buffer[NameMsg.length:full_length] = encoded_name buffer[NameMsg.length:full_length] = self.name
return full_length return full_length

View File

@ -68,7 +68,7 @@ class Participant:
participants: set['Participant'] = set() participants: set['Participant'] = set()
@staticmethod @staticmethod
def GetParticipant(arg1: Union[str, int], port: Optional[int]) -> Optional['Participant']: def GetParticipant(arg1: Union[str, int], port: Optional[int] = None) -> Optional['Participant']:
if port is not None: if port is not None:
ip_address = str(arg1) ip_address = str(arg1)
for participant in Participant.participants: for participant in Participant.participants:

View File

@ -1,14 +1,15 @@
from RoboidControl.Participant import Participant from RoboidControl.Participant import Participant
from RoboidControl.Thing import Thing from RoboidControl.Thing import Thing
# from RoboidControl.Messages.NetworkIdMsg import NetworkIdMsg
# from RoboidControl.Messages.ThingMsg import ThingMsg
# from RoboidControl.Messages.NameMsg import NameMsg
# from RoboidControl.Messages.ModelUrlMsg import ModelUrlMsg
# from RoboidControl.Messages.Messages import IMessage
# from RoboidControl.Messages.InvestigateMsg import InvestigateMsg
# from RoboidControl.Messages.PoseMsg import PoseMsg
from RoboidControl.Messages import * from RoboidControl.Messages import *
from RoboidControl.Messages.BinaryMsg import BinaryMsg
from RoboidControl.Messages.ParticipantMsg import ParticipantMsg from RoboidControl.Messages.ParticipantMsg import ParticipantMsg
from RoboidControl.Messages.NetworkIdMsg import NetworkIdMsg
from RoboidControl.Messages.ThingMsg import ThingMsg
from RoboidControl.Messages.NameMsg import NameMsg
from RoboidControl.Messages.ModelUrlMsg import ModelUrlMsg
from RoboidControl.Messages.IMessage import IMessage
from RoboidControl.Messages.InvestigateMsg import InvestigateMsg
from RoboidControl.Messages.PoseMsg import PoseMsg
import socket import socket
import threading import threading
@ -67,7 +68,8 @@ class ParticipantUDP(Participant):
self.is_isolated = False self.is_isolated = False
if ip_address is not None: if ip_address is not None:
self.remote_site = Participant(ip_address, port) self.remote_site = Participant(ip_address, port)
self.others.append(self.remote_site)
Participant.AddParticipant(self)
self.buffer: bytearray = bytearray(256) self.buffer: bytearray = bytearray(256)
@ -89,22 +91,6 @@ class ParticipantUDP(Participant):
isolated_participant = None isolated_participant = None
# def GetParticipant(self, ip_address: str, port: int):
# # print(f'{self.name} Get participant {ip_address} {port}')
# # for item in self.others:
# # print(f'- {item.ip_address} {item.port}')
# found_participants = (item for item in self.others
# if item.ip_address == ip_address and item.port == port)
# participant = next(found_participants, None)
# return participant
# def AddParticipant(self, ip_address: str, port: int):
# print(f'{self.name} Add participant {ip_address} {port}')
# remote_participant = Participant(ip_address = ip_address, port = port)
# self.others.append(remote_participant)
# return remote_participant
#region Update #region Update
def Update(self, currentTimeMs: Optional[int] = None): def Update(self, currentTimeMs: Optional[int] = None):
@ -143,12 +129,10 @@ class ParticipantUDP(Participant):
else: else:
if self.remote_site is not None and thing.owner is not None: if self.remote_site is not None and thing.owner is not None:
# Send to remote site # Send to remote site
poseMsg = PoseMsg(thing.owner.network_id, thing) self.Send(self.remote_site, PoseMsg(thing.owner.network_id, thing))
self.Send(self.remote_site, poseMsg) self.Send(self.remote_site, BinaryMsg(thing.owner.network_id, thing))
binaryMsg = BinaryMsg(thing.owner.network_id, thing) if thing.terminate:
self.Send(self.remote_site, binaryMsg) self.Remove(thing)
# if thing.terminate:
# self.Remove(thing)
#endregion #endregion
@ -221,18 +205,18 @@ class ParticipantUDP(Participant):
# case InvestigateMsg.id: # case InvestigateMsg.id:
# self.ProcessInvestigateMsg(InvestigateMsg(data)) # self.ProcessInvestigateMsg(InvestigateMsg(data))
case ThingMsg.id: case ThingMsg.id:
self.ProcessThingMsg(ThingMsg(data)) self.ProcessThingMsg(sender, ThingMsg(data))
case NameMsg.id: case NameMsg.id:
self.ProcessNameMsg(NameMsg.NameMsg.Deserialize(data)) self.ProcessNameMsg(sender, NameMsg(data))
case ModelUrlMsg.id: case ModelUrlMsg.id:
self.ProcessModelUrlMsg(ModelUrlMsg(data)) self.ProcessModelUrlMsg(sender, ModelUrlMsg(data))
case BinaryMsg.id: case BinaryMsg.id:
self.ProcessBinaryMsg(BinaryMsg(data)) self.ProcessBinaryMsg(sender, BinaryMsg(data))
case _: case _:
pass pass
def ProcessParticipantMsg(self, sender: Participant, msg: ParticipantMsg): def ProcessParticipantMsg(self, sender: Participant, msg: ParticipantMsg):
logger.debug(f'{self.name} Process participantMsg {msg.networkId}') logger.debug(f'{self.name} Process participantMsg {msg.network_id}')
def ProcessSiteIdMsg(self, sender: Participant, msg: NetworkIdMsg): def ProcessSiteIdMsg(self, sender: Participant, msg: NetworkIdMsg):
logger.debug(f'{self.name} Process NetworkIdMsg {self.network_id} -> {msg.network_id}') logger.debug(f'{self.name} Process NetworkIdMsg {self.network_id} -> {msg.network_id}')
@ -243,13 +227,13 @@ class ParticipantUDP(Participant):
self.SendThingInfo(sender, thing, recursively=True) self.SendThingInfo(sender, thing, recursively=True)
def ProcessInvestigateMsg(self, sender: Participant, msg: InvestigateMsg): def ProcessInvestigateMsg(self, sender: Participant, msg: InvestigateMsg):
logger.debug(f'{self.name} Process InestigateMsg [{msg.networkId}/{msg.thingId}]') logger.debug(f'{self.name} Process InestigateMsg [{msg.network_id}/{msg.thing_id}]')
def ProcessThingMsg(self, msg: ThingMsg): def ProcessThingMsg(self, sender: Participant, msg: ThingMsg):
logger.debug(f'{self.name}: Process ThingMsg [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId}') logger.debug(f'{self.name}: Process ThingMsg [{msg.network_id}/{msg.thing_id}] {msg.thing_type} {msg.parent_id}')
def ProcessNameMsg(self, msg: NameMsg): def ProcessNameMsg(self, sender: Participant, msg: NameMsg):
logger.debug(f'{self.name}: Process NameMsg [{msg.networkId}/{msg.thingId}] {msg.nameLength} {msg.name}') logger.debug(f'{self.name}: Process NameMsg [{msg.network_id}/{msg.thing_id}] {msg.name_length} {msg.name}')
owner: Optional[Participant] = Participant.GetParticipant(msg.network_id) owner: Optional[Participant] = Participant.GetParticipant(msg.network_id)
if owner is None: if owner is None:
@ -261,11 +245,11 @@ class ParticipantUDP(Participant):
thing.name = msg.name thing.name = msg.name
def ProcessModelUrlMsg(self, msg: ModelUrlMsg): def ProcessModelUrlMsg(self, sender: Participant, msg: ModelUrlMsg):
logger.debug(f'{self.name}: Process ModelUrlMsg [{msg.networkId}/{msg.thingId}] {msg.urlLength} {msg.url}') logger.debug(f'{self.name}: Process ModelUrlMsg [{msg.network_id}/{msg.thing_id}] {msg.url_length} {msg.url}')
def ProcessPoseMsg(self, sender: Participant, msg: PoseMsg): def ProcessPoseMsg(self, sender: Participant, msg: PoseMsg):
logger.debug(f'{self.name}: Process PoseMsg [{msg.networkId}/{msg.thingId}] {msg.poseType}') logger.debug(f'{self.name}: Process PoseMsg [{msg.network_id}/{msg.thing_id}] {msg.pose_type}')
owner: Optional[Participant] = Participant.GetParticipant(msg.network_id) owner: Optional[Participant] = Participant.GetParticipant(msg.network_id)
if owner is None: if owner is None:
@ -284,8 +268,8 @@ class ParticipantUDP(Participant):
if (msg.pose_type & PoseMsg.AngularVelocity) != 0: if (msg.pose_type & PoseMsg.AngularVelocity) != 0:
thing.angular_velocity = msg.angular_velocity thing.angular_velocity = msg.angular_velocity
def ProcessBinaryMsg(self, msg: BinaryMsg): def ProcessBinaryMsg(self, sender: Participant, msg: BinaryMsg):
logger.debug(f'{self.name}: Process BinaryMsg [{msg.networkId}/{msg.thingId}] {msg.dataLength}') logger.debug(f'{self.name}: Process BinaryMsg [{msg.network_id}/{msg.thing_id}] {msg.data_length}')
thing: Optional[Thing] = self.Get(msg.thing_id) thing: Optional[Thing] = self.Get(msg.thing_id)
if thing is not None: if thing is not None:

View File

@ -19,7 +19,7 @@ class SiteServer(ParticipantUDP):
"""! Create a new site server """! Create a new site server
@param port The port of which to receive the messages @param port The port of which to receive the messages
""" """
super().__init__(ip_address = "127.0.0.1", port = port) super().__init__(local_port = port)
self.isolated = False # site servers are never isolated self.isolated = False # site servers are never isolated
self.publishInterval = 0 self.publishInterval = 0
@ -31,9 +31,9 @@ class SiteServer(ParticipantUDP):
#region Receive #region Receive
def ProcessParticipantMsg(self, sender, msg): def ProcessParticipantMsg(self, sender, msg):
ParticipantUDP.ProcessParticipantMsg(sender, msg) ParticipantUDP.ProcessParticipantMsg(self, sender, msg)
if msg.network_id != sender.network_id: if msg.network_id != sender.network_id:
self.Send(sender, sender.network_id) self.Send(sender, NetworkIdMsg(sender.network_id))
def ProcessNetworkIdMsg(self, sender, msg): def ProcessNetworkIdMsg(self, sender, msg):
pass pass
@ -41,13 +41,14 @@ class SiteServer(ParticipantUDP):
def ProcessThingMsg(self, sender, msg: ThingMsg): def ProcessThingMsg(self, sender, msg: ThingMsg):
thing: Optional[Thing] = sender.Get(msg.thing_id) thing: Optional[Thing] = sender.Get(msg.thing_id)
if thing is None: if thing is None:
Thing(sender, msg.thing_type, msg.thing_id) if msg.parent_id != 0:
parent = sender.Get(msg.parent_id)
if parent is not None:
print(f'Could not find parent [{msg.network_id}/{msg.parent_id}]')
else:
parent = None
Thing(owner=sender, thing_type=msg.thing_type, thing_id=msg.thing_id, parent = parent)
if msg.parent_id != 0:
thing.parent = sender.Get(msg.parent_id)
if thing.parent is not None:
print(f'Could not find parent [{msg.network_id}/{msg.parent_id}]')
else:
thing.parent = None
#endregion Receive #endregion Receive

View File

@ -90,6 +90,8 @@ class Thing:
## Boolean indicating the thing has an updated angular velocity ## Boolean indicating the thing has an updated angular velocity
self.angular_velocity_updated: bool = False self.angular_velocity_updated: bool = False
self.terminate: bool = False
if self.owner is not None: if self.owner is not None:
self.owner.Add(self) self.owner.Add(self)

View File

@ -46,8 +46,8 @@ class ThingTest(unittest.TestCase):
def test_thing_msg(self): def test_thing_msg(self):
site = SiteServer() site = SiteServer()
participant = ParticipantUDP(ip_address="127.0.0.1", port=7683) participant = ParticipantUDP(ip_address="127.0.0.1", port=7681, local_port=7682)
thing = Thing() thing = Thing(participant)
thing.name = "First thing" thing.name = "First thing"
thing.model_url = "https://passer.life/extras/ant.jpg" thing.model_url = "https://passer.life/extras/ant.jpg"