diff --git a/Examples/BB2B_networking.py b/Examples/BB2B_networking.py index 7f5ea8c..c65f749 100644 --- a/Examples/BB2B_networking.py +++ b/Examples/BB2B_networking.py @@ -3,14 +3,14 @@ from os import path sys.path.append(path.abspath(path.join(path.dirname(__file__), '..'))) import time -from LocalParticipant import LocalParticipant +from ParticipantUDP import ParticipantUDP from Thing import * from Things.DifferentialDrive import DifferentialDrive from Things.TouchSensor import TouchSensor # Create a local participant for handling communcation # using default settings (UDP communciation over port 7681) -participant = LocalParticipant(port=7681, local_port=7682, ip_address="127.0.0.1") +participant = ParticipantUDP(port=7681, local_port=7682, ip_address="127.0.0.1") # The robot's propulsion is a differential drive bb2b = DifferentialDrive(participant) bb2b.name = "BB2B" diff --git a/LocalParticipant.py b/ParticipantUDP.py similarity index 88% rename from LocalParticipant.py rename to ParticipantUDP.py index a18e113..d0ec54a 100644 --- a/LocalParticipant.py +++ b/ParticipantUDP.py @@ -17,7 +17,7 @@ micropython = 'micropython' in sys.modules if micropython: from MicroPython.uPythonParticipant import Bla -class LocalParticipant(Participant): +class ParticipantUDP(Participant): """! A local participant is the local device which can communicate with other participants. It manages all local things and communcation with other participants. Each application has a local participant which is usually explicit in the code. @@ -46,19 +46,18 @@ class LocalParticipant(Participant): # Isolated participants do not communicate with other participants self.isolated = True - ## The remote site when this participant is connected to a site - self.remote_site = None + ## The other participants communicating with this participant + self.others = [] if self.port != 0: self.isolated = False if ip_address is not None: - self.remote_site = Participant(ip_address, port) + self.others.append(Participant(ip_address, port)) - self.others = [] - self.network_id = 0 + # self.network_id = 0 self.buffer = bytearray(256) - self.thing_msg_processors = {} - self.new_thing_handlers = [] + # self.thing_msg_processors = {} + # self.new_thing_handlers = [] self.publishInterval = 3000 # 3 seconds self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -70,9 +69,9 @@ class LocalParticipant(Participant): self.thread.start() def Isolated(): - if LocalParticipant.isolated_participant == None: - LocalParticipant.isolated_participant = LocalParticipant(0) - return LocalParticipant.isolated_participant + if ParticipantUDP.isolated_participant == None: + ParticipantUDP.isolated_participant = ParticipantUDP(0) + return ParticipantUDP.isolated_participant #region Update @@ -100,10 +99,11 @@ class LocalParticipant(Participant): if self.isolated == False: if self.publishInterval > 0 and currentTimeMs > self.nextPublishMe: msg = ParticipantMsg(self.network_id) - if self.remote_site is None: + if self.others.count == 0: self.Publish(msg) else: - self.Send(self.remote_site, msg) + for other_participant in self.others: + self.Send(other_participant, msg) print(f'Publish ParticipantMsg {self.network_id}') self.nextPublishMe = currentTimeMs + self.publishInterval @@ -203,15 +203,15 @@ class LocalParticipant(Participant): def ProcessThingMsg(self, msg: ThingMsg): print(f'received thing {msg.network_id} {msg.thing_id}') - if msg.thing_type in self.thing_msg_processors: - constructor = self.thing_msg_processors[msg.thing_type] - constructor(msg.network_id, msg.thing_id) + # if msg.thing_type in self.thing_msg_processors: + # constructor = self.thing_msg_processors[msg.thing_type] + # constructor(msg.network_id, msg.thing_id) - # not really 'new' thing, but it is a start - thing = Thing.Get(msg.network_id, msg.thing_id) - if thing is not None: - for handler in self.new_thing_handlers: - handler(thing) + # # not really 'new' thing, but it is a start + # thing = Thing.Get(msg.network_id, msg.thing_id) + # if thing is not None: + # for handler in self.new_thing_handlers: + # handler(thing) def ProcessNameMsg(self, msg: NameMsg): print(f'received name {msg.name}') diff --git a/SiteServer.py b/SiteServer.py index ca5356f..f5eead1 100644 --- a/SiteServer.py +++ b/SiteServer.py @@ -1,4 +1,4 @@ -from LocalParticipant import LocalParticipant +from ParticipantUDP import ParticipantUDP from Messages.ParticipantMsg import ParticipantMsg from Messages.NetworkIdMsg import NetworkIdMsg from Things.TemperatureSensor import TemperatureSensor @@ -7,7 +7,7 @@ from Thing import Thing import socket import threading -class SiteServer(LocalParticipant): +class SiteServer(ParticipantUDP): """! A site server is a participant which provides a shared simulated environment """ diff --git a/Thing.py b/Thing.py index 5fe59fd..c009eed 100644 --- a/Thing.py +++ b/Thing.py @@ -39,8 +39,8 @@ class Thing: owner = parent.owner self.SetParent(parent) elif owner == None: - from LocalParticipant import LocalParticipant - owner = LocalParticipant.Isolated() + from ParticipantUDP import ParticipantUDP + owner = ParticipantUDP.Isolated() self.children = [] diff --git a/__init__.py b/__init__.py index 0354ddf..5952521 100644 --- a/__init__.py +++ b/__init__.py @@ -1,9 +1,9 @@ __all__ = ['Thing', - 'LocalParticipant', + 'ParticipantUDP', 'SiteServer'] from .LinearAlgebra.Direction import Direction -from .LocalParticipant import LocalParticipant +from .ParticipantUDP import ParticipantUDP from .Thing import Thing from .LinearAlgebra.Spherical import Spherical from .LinearAlgebra.SwingTwist import SwingTwist diff --git a/test/BB2B_dummytest.py b/test/BB2B_dummytest.py index 00f7017..f418de9 100644 --- a/test/BB2B_dummytest.py +++ b/test/BB2B_dummytest.py @@ -5,7 +5,7 @@ sys.path.append(str(Path(__file__).resolve().parent.parent)) import time from SiteServer import SiteServer -from LocalParticipant import LocalParticipant +from ParticipantUDP import ParticipantUDP from Thing import * from Things.DifferentialDrive import DifferentialDrive from Things.TouchSensor import TouchSensor @@ -15,7 +15,7 @@ site = SiteServer(port=7691) # Create a local participant for handling communcation # using default settings (UDP communciation over port 7681) -participant = LocalParticipant(port=7691, local_port=7683, ip_address="127.0.0.1") +participant = ParticipantUDP(port=7691, local_port=7683, ip_address="127.0.0.1") # The robot's propulsion is a differential drive bb2b = DifferentialDrive(participant) bb2b.name = "BB2B" diff --git a/test/thing_test.py b/test/thing_test.py index fc0be68..3bf9031 100644 --- a/test/thing_test.py +++ b/test/thing_test.py @@ -8,13 +8,13 @@ sys.path.append(str(Path(__file__).resolve().parent.parent)) import unittest from Thing import Thing -from LocalParticipant import LocalParticipant +from ParticipantUDP import ParticipantUDP from SiteServer import SiteServer class ThingTest(unittest.TestCase): def test_participant(self): - participant: LocalParticipant = LocalParticipant() + participant: ParticipantUDP = ParticipantUDP() milliseconds = time.time() * 1000 start_time = milliseconds @@ -33,7 +33,7 @@ class ThingTest(unittest.TestCase): def test_site_participant(self): site = SiteServer(port=7681) - participant = LocalParticipant(port=7681, ip_address="127.0.0.1", local_port=7682) + participant = ParticipantUDP(port=7681, ip_address="127.0.0.1", local_port=7682) milliseconds = time.time() * 1000 start_time = milliseconds @@ -46,7 +46,7 @@ class ThingTest(unittest.TestCase): def test_thing_msg(self): site = SiteServer() - participant = LocalParticipant(ip_address="127.0.0.1", port=7683) + participant = ParticipantUDP(ip_address="127.0.0.1", port=7683) thing = Thing() thing.name = "First thing" thing.model_url = "https://passer.life/extras/ant.jpg"