diff --git a/RoboidControl/Messages/ParticipantMsg.py b/RoboidControl/Messages/ParticipantMsg.py index 9dbefb0..7735053 100644 --- a/RoboidControl/Messages/ParticipantMsg.py +++ b/RoboidControl/Messages/ParticipantMsg.py @@ -11,10 +11,14 @@ class ParticipantMsg(IMessage): ## The length of the message length: int = 2 - def __init__(self, data = None): + def __init__(self, arg1): """! Create a new message for sending """ - self.network_id = data + if isinstance(arg1, bytes): + buffer = arg1 + self.network_id = buffer[1] + else: + self.network_id = arg1 def Serialize(self, buffer_ref): """! Serialize the message into a byte array. diff --git a/RoboidControl/ParticipantUDP.py b/RoboidControl/ParticipantUDP.py index 850036d..6d31332 100644 --- a/RoboidControl/ParticipantUDP.py +++ b/RoboidControl/ParticipantUDP.py @@ -33,12 +33,12 @@ class ParticipantUDP(Participant): name = "Participant" isolated_participant = None - def __init__(self, port=7681, ip_address=None, local_port=0): - super().__init__(ip_address = "127.0.0.1", port = port) + def __init__(self, port=7681, ip_address=None, local_port=7681): + super().__init__(ip_address = "127.0.0.1", port = local_port) - if local_port == 0: - local_port = port - self.local_port = local_port + # if local_port == 0: + # local_port = port + # self.local_port = local_port ## True if the participant is running isolated. # Isolated participants do not communicate with other participants @@ -48,7 +48,7 @@ class ParticipantUDP(Participant): ## The other participants communicating with this participant self.others = [] - if self.port != 0: + if port != 0: self.is_isolated = False if ip_address is not None: self.remote_site = Participant(ip_address, port) @@ -59,7 +59,7 @@ class ParticipantUDP(Participant): 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.bind(("0.0.0.0", self.port)) self.thread = threading.Thread(target = self.Receiver) self.thread.daemon = True diff --git a/RoboidControl/Participants/SiteServer.py b/RoboidControl/Participants/SiteServer.py index c3042b0..c30c0f3 100644 --- a/RoboidControl/Participants/SiteServer.py +++ b/RoboidControl/Participants/SiteServer.py @@ -23,7 +23,8 @@ class SiteServer(ParticipantUDP): def ProcessParticipantMsg(self, sender, msg): print(f'{self.name} received Participant ') - self.Send(sender, NetworkIdMsg()) + if msg.network_id != sender.network_id: + self.Send(sender, sender.network_id) def ProcessNetworkId(self, msg): pass \ No newline at end of file diff --git a/RoboidControl/Thing.py b/RoboidControl/Thing.py index b7ef075..cf6816b 100644 --- a/RoboidControl/Thing.py +++ b/RoboidControl/Thing.py @@ -39,7 +39,7 @@ class Thing: owner = parent.owner self.SetParent(parent) elif owner == None: - from ParticipantUDP import ParticipantUDP + from RoboidControl.ParticipantUDP import ParticipantUDP owner = ParticipantUDP.Isolated() self.children = [] diff --git a/RoboidControl/Things/TemperatureSensor.py b/RoboidControl/Things/TemperatureSensor.py index 5a6f650..2b3b222 100644 --- a/RoboidControl/Things/TemperatureSensor.py +++ b/RoboidControl/Things/TemperatureSensor.py @@ -1,5 +1,5 @@ -from Thing import Thing -from Messages import LowLevelMessages +from RoboidControl.Thing import Thing +from RoboidControl.Messages import LowLevelMessages class TemperatureSensor(Thing): def __init__(self, thing_id): diff --git a/test/BB2B_dummytest.py b/test/BB2B_dummytest.py index 5b7999b..50cd2bc 100644 --- a/test/BB2B_dummytest.py +++ b/test/BB2B_dummytest.py @@ -4,11 +4,11 @@ from pathlib import Path sys.path.append(str(Path(__file__).resolve().parent.parent)) import time -from Participants.SiteServer import SiteServer -from ParticipantUDP import ParticipantUDP -from Thing import * -from Things.DifferentialDrive import DifferentialDrive -from Things.TouchSensor import TouchSensor +from RoboidControl.Participants.SiteServer import SiteServer +from RoboidControl.ParticipantUDP import ParticipantUDP +from RoboidControl.Thing import * +from RoboidControl.Things.DifferentialDrive import DifferentialDrive +from RoboidControl.Things.TouchSensor import TouchSensor # Start a site server site = SiteServer(port=7691) diff --git a/test/thing_test.py b/test/thing_test.py index 2350379..3a9e243 100644 --- a/test/thing_test.py +++ b/test/thing_test.py @@ -3,13 +3,13 @@ import sys from pathlib import Path # Add the project root to sys.path -sys.path.append(str(Path(__file__).resolve().parent.parent)) +sys.path.append(str(Path(__file__).resolve().parent)) import unittest -from Thing import Thing -from ParticipantUDP import ParticipantUDP -from Participants.SiteServer import SiteServer +from RoboidControl.Thing import Thing +from RoboidControl.ParticipantUDP import ParticipantUDP +from RoboidControl.Participants.SiteServer import SiteServer class ThingTest(unittest.TestCase): @@ -33,7 +33,7 @@ class ThingTest(unittest.TestCase): def test_site_participant(self): site = SiteServer(port=7681) - participant = ParticipantUDP(port=7681, ip_address="127.0.0.1", local_port=7682) + participant = ParticipantUDP(ip_address="127.0.0.1", port=7681, local_port=7682) milliseconds = time.time() * 1000 start_time = milliseconds