using Participant.Send

This commit is contained in:
Pascal Serrarens 2025-06-05 10:13:13 +02:00
parent 0c74320271
commit 26f8a238bc
2 changed files with 51 additions and 44 deletions

View File

@ -13,9 +13,9 @@ class Participant:
# region Init
local_participant = None
local_participant: 'Participant' = None
def __init__(self, ip_address: str = None, port: int = None) -> None:
def __init__(self, ip_address: str = None, port: int = None, local_participant: 'Participant' = None) -> None:
"""! Create a new participant with the given communcation info
@param ip_address The IP address of the participant
@param port The UDP port of the participant
@ -26,6 +26,10 @@ class Participant:
## The port number for UDP communication with the participant. This is 0 for isolated participants.
self.port: int = port
self.udp_socket = None
if local_participant is not None:
self.udp_socket = local_participant.udp_socket
## he network Id to identify the participant.
self.network_id: int = 0
@ -130,7 +134,7 @@ class Participant:
return participant
return None
def AddParticipant(arg1: Union[str, 'Participant'], port: Optional[int] = None) -> 'Participant':
def AddParticipant(arg1: Union[str, 'Participant'], port: Optional[int] = None, local_participant: 'Participant' = None) -> 'Participant':
"""! Add a participant
@param arg1 Either:
- str: The IP address of the participant
@ -139,14 +143,14 @@ class Participant:
"""
if port is not None:
ip_address = str(arg1)
participant = Participant(ip_address, port)
participant = Participant(ip_address, port, local_participant)
participant.network_id = len(Participant.participants) + 1
Participant.AddParticipant(participant)
return participant
else:
participant: Participant = arg1
foundParticipant = Participant.GetParticipant(participant.network_id, participant.port)
if foundParticipant is not None:
if foundParticipant is None:
Participant.participants.add(participant)
return participant

View File

@ -43,6 +43,7 @@ class ParticipantUDP(Participant):
name = "Participant"
#region Init
def __init__(self,
port: int = 7681,
ip_address: Optional[str] = None,
@ -64,46 +65,47 @@ class ParticipantUDP(Participant):
## The interval in milliseconds for publishing (broadcasting) data on the local network
self.publishInterval = 3000 # 3 seconds
if port != 0:
self.is_isolated = False
if ip_address is not None:
self.remote_site = Participant(ip_address, port)
Participant.AddParticipant(self)
Participant.ReplaceLocalParticipant(self)
self.buffer: bytearray = bytearray(256)
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.bind(("0.0.0.0", local_port))
if port != 0:
self.is_isolated = False
if ip_address is not None:
self.remote_site = Participant(ip_address, port, self)
self.thread = threading.Thread(target = self.Receiver)
self.thread.daemon = True
self.thread.start()
@staticmethod
def Isolated() -> 'ParticipantUDP':
if ParticipantUDP.isolated_participant == None:
ParticipantUDP.isolated_participant = ParticipantUDP(0)
return ParticipantUDP.isolated_participant
Participant.AddParticipant(self)
Participant.ReplaceLocalParticipant(self)
# @staticmethod
# def Isolated() -> 'ParticipantUDP':
# if ParticipantUDP.isolated_participant == None:
# ParticipantUDP.isolated_participant = ParticipantUDP(0)
# return ParticipantUDP.isolated_participant
#endregion Init
isolated_participant = None
# isolated_participant = None
#region Update
def Update(self):
currentTimeMs = int(time.time() * 1000)
# Send periodic ParticipantMsg
if self.is_isolated == False:
if self.publishInterval > 0 and currentTimeMs > self.nextPublishMe:
msg = ParticipantMsg(self.network_id)
if self.remote_site == None:
self.Publish(msg)
else:
self.Send(self.remote_site, msg)
self.remote_site.Send(msg)
self.nextPublishMe = currentTimeMs + self.publishInterval
@ -116,21 +118,22 @@ class ParticipantUDP(Participant):
# if thing.hierarchyChanged and not (self.isIsolated or self.network_id == ):
# thingMsg = ThingMsg(self.network_id, thing)
# self.Send(self.remote_site, thingMsg)
if thing.owner is not None:
poseMsg = PoseMsg(thing.owner.network_id, thing)
if self.remote_site is not None:
self.Send(self.remote_site, poseMsg)
# if thing.owner is not None:
# poseMsg = PoseMsg(thing.owner.network_id, thing)
# if self.remote_site is not None:
# self.remote_site.Send(poseMsg)
thing.Update(False)
if not(self.is_isolated or self.network_id == 0):
if thing.terminate:
destroyMsg = DestroyMsg.Create(self.network_id, thing)
self.Send(self.remote_site, destroyMsg)
else:
if self.remote_site is not None and thing.owner is not None:
# Send to remote site
self.Send(self.remote_site, PoseMsg(thing.owner.network_id, thing))
self.Send(self.remote_site, BinaryMsg(thing.owner.network_id, thing))
self.remote_site.Send(destroyMsg)
# else:
# this is very inefficient
# if self.remote_site is not None and thing.owner is not None:
# # Send to remote site
# self.Send(self.remote_site, PoseMsg(thing.owner.network_id, thing))
# self.Send(self.remote_site, BinaryMsg(thing.owner.network_id, thing))
if thing.terminate:
self.Remove(thing)
@ -139,11 +142,11 @@ class ParticipantUDP(Participant):
#region Send
def SendThingInfo(self, owner: Participant, thing: 'Thing', recursively: bool = False):
self.Send(owner, ThingMsg(self.network_id, thing))
self.Send(owner, NameMsg(self.network_id, thing))
self.Send(owner, ModelUrlMsg(self.network_id, thing))
self.Send(owner, PoseMsg(self.network_id, thing, True))
self.Send(owner, BinaryMsg(self.network_id, thing))
owner.Send(ThingMsg(self.network_id, thing))
owner.Send(NameMsg(self.network_id, thing))
owner.Send(ModelUrlMsg(self.network_id, thing))
owner.Send(PoseMsg(self.network_id, thing, True))
owner.Send(BinaryMsg(self.network_id, thing))
if recursively:
for child in thing.children:
@ -156,14 +159,14 @@ class ParticipantUDP(Participant):
self.Publish(PoseMsg(self.network_id, thing))
self.Publish(BinaryMsg(self.network_id, thing))
def Send(self, owner: Participant, msg: IMessage):
buffer_size = msg.Serialize([self.buffer])
if buffer_size <= 0:
return True
# def Send(self, owner: Participant, msg: IMessage):
# buffer_size = msg.Serialize([self.buffer])
# if buffer_size <= 0:
# return True
# print(f'{self.name} send {self.buffer[0]} to {owner.ip_address} {owner.port}')
self.udp_socket.sendto(self.buffer[:buffer_size], (owner.ip_address, owner.port))
return True
# # print(f'{self.name} send {self.buffer[0]} to {owner.ip_address} {owner.port}')
# self.udp_socket.sendto(self.buffer[:buffer_size], (owner.ip_address, owner.port))
# return True
def Publish(self, msg: IMessage):
buffer_size = msg.Serialize([self.buffer])
@ -188,7 +191,7 @@ class ParticipantUDP(Participant):
remote_participant = Participant.GetParticipant(remote_ip_address, remote_port)
if remote_participant is None:
# print(f'new participant')
remote_participant = Participant.AddParticipant(remote_ip_address, remote_port)
remote_participant = Participant.AddParticipant(remote_ip_address, remote_port, self)
self.ReceiveData(data, remote_participant)
except ConnectionError:
self.network_id = 0