diff --git a/LocalParticipant.py b/LocalParticipant.py index b023445..a18e113 100644 --- a/LocalParticipant.py +++ b/LocalParticipant.py @@ -33,9 +33,10 @@ class LocalParticipant(Participant): others = None thread = None name = "Participant" + isolated_participant = None - def __init__(self, port=7681, ip_address="0.0.0.0", local_port=0): #, remote=False, udp_socket = None): - super().__init__(ip_address = "0.0.0.0", port = port) + def __init__(self, port=7681, ip_address=None, local_port=0): + super().__init__(ip_address = "127.0.0.1", port = port) if local_port == 0: local_port = port @@ -50,7 +51,8 @@ class LocalParticipant(Participant): if self.port != 0: self.isolated = False - self.remote_site = Participant(ip_address, port) + if ip_address is not None: + self.remote_site = Participant(ip_address, port) self.others = [] self.network_id = 0 @@ -66,6 +68,11 @@ class LocalParticipant(Participant): self.thread = threading.Thread(target = self.Receiver) self.thread.daemon = True self.thread.start() + + def Isolated(): + if LocalParticipant.isolated_participant == None: + LocalParticipant.isolated_participant = LocalParticipant(0) + return LocalParticipant.isolated_participant #region Update @@ -90,15 +97,16 @@ class LocalParticipant(Participant): if currentTimeMs is None: currentTimeMs = time.time() * 1000 - if self.publishInterval > 0 and currentTimeMs > self.nextPublishMe: - msg = ParticipantMsg(self.network_id) - if self.remote_site is None: - self.Publish(msg) - else: - self.Send(self.remote_site, msg) - - print(f'Publish ParticipantMsg {self.network_id}') - self.nextPublishMe = currentTimeMs + self.publishInterval + if self.isolated == False: + if self.publishInterval > 0 and currentTimeMs > self.nextPublishMe: + msg = ParticipantMsg(self.network_id) + if self.remote_site is None: + self.Publish(msg) + else: + self.Send(self.remote_site, msg) + + print(f'Publish ParticipantMsg {self.network_id}') + self.nextPublishMe = currentTimeMs + self.publishInterval for thing in self.things: thing.Update(currentTimeMs) @@ -129,7 +137,7 @@ class LocalParticipant(Participant): if buffer_size <= 0: return True - # print(f'{self.name} send {self.buffer[0]} to {owner.ip_address} {owner.port}') + 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 diff --git a/SiteServer.py b/SiteServer.py index 93b5ec9..ca5356f 100644 --- a/SiteServer.py +++ b/SiteServer.py @@ -17,24 +17,27 @@ class SiteServer(LocalParticipant): """! Create a new site server @param port The UDP port on which communication is received """ - self.ip_address = "0.0.0.0" - self.port = port - self.local_port = port + super().__init__(ip_address = "127.0.0.1", port = port) + # self.ip_address = "0.0.0.0" + # self.port = port + # self.local_port = port + self.isolated = False # site servers are never isolated + self.remote_site = None # site servers never have remote sites - self.others = [] - self.network_id = 0 - self.buffer = bytearray(256) - self.thing_msg_processors = {} - self.new_thing_handlers = [] + # self.others = [] + # self.network_id = 0 + # self.buffer = bytearray(256) + # self.thing_msg_processors = {} + # self.new_thing_handlers = [] self.publishInterval = 0 - 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", self.local_port)) + # 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", self.local_port)) - self.thread = threading.Thread(target = self.Receiver) - self.thread.daemon = True - self.thread.start() + # self.thread = threading.Thread(target = self.Receiver) + # self.thread.daemon = True + # self.thread.start() self.Register(TemperatureSensor, Thing.Type.TemperatureSensor) diff --git a/Thing.py b/Thing.py index 803ad8b..5fe59fd 100644 --- a/Thing.py +++ b/Thing.py @@ -38,6 +38,10 @@ class Thing: if parent is not None: owner = parent.owner self.SetParent(parent) + elif owner == None: + from LocalParticipant import LocalParticipant + owner = LocalParticipant.Isolated() + self.children = [] ## The participant owning this thing diff --git a/test/thing_test.py b/test/thing_test.py index 95bccfb..fc0be68 100644 --- a/test/thing_test.py +++ b/test/thing_test.py @@ -32,8 +32,8 @@ class ThingTest(unittest.TestCase): site.Update(milliseconds) def test_site_participant(self): - site = SiteServer(7681) - participant = LocalParticipant("127.0.0.1", 7681) + site = SiteServer(port=7681) + participant = LocalParticipant(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("127.0.0.1") + participant = LocalParticipant(ip_address="127.0.0.1", port=7683) thing = Thing() thing.name = "First thing" thing.model_url = "https://passer.life/extras/ant.jpg"