From 86b12a7326a1c2867424e241e64b6c1dbe4d8fca Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 22 Apr 2025 12:00:27 +0200 Subject: [PATCH] Static participant list --- Unity/Thing.cs | 8 ------- src/Messages/PoseMsg.cs | 5 +++-- src/Participant.cs | 32 ++++++++++++++++++++++++++++ src/ParticipantUDP.cs | 44 +++++++++++++++++++-------------------- src/Things/TouchSensor.cs | 5 +++-- 5 files changed, 60 insertions(+), 34 deletions(-) diff --git a/Unity/Thing.cs b/Unity/Thing.cs index 4aeb8bd..9263c75 100644 --- a/Unity/Thing.cs +++ b/Unity/Thing.cs @@ -33,7 +33,6 @@ namespace RoboidControl.Unity { } siteServer.site.Add(thing); core.OnPoseChanged += PoseChanged; - //core.OnNameChanged += NameChanged; } public static Thing Create(RoboidControl.Thing core) { @@ -53,7 +52,6 @@ namespace RoboidControl.Unity { gameObj.transform.localRotation = core.orientation.ToQuaternion(); core.OnPoseChanged += component.PoseChanged; - //core.OnNameChanged += component.NameChanged; return component; } @@ -104,12 +102,6 @@ namespace RoboidControl.Unity { this.transform.localRotation = core.orientation.ToQuaternion(); } - private void NameChanged() { - Debug.Log($"{this} name changed"); - if (this.gameObject.name != core.name) - this.gameObject.name = core.name; - } - private IEnumerator LoadJPG() { UnityWebRequest request = UnityWebRequestTexture.GetTexture(core.modelUrl); yield return request.SendWebRequest(); diff --git a/src/Messages/PoseMsg.cs b/src/Messages/PoseMsg.cs index 09eb5cd..f335cef 100644 --- a/src/Messages/PoseMsg.cs +++ b/src/Messages/PoseMsg.cs @@ -139,9 +139,10 @@ namespace RoboidControl { /// @copydoc Passer::RoboidControl::IMessage::Serialize public override byte Serialize(ref byte[] buffer) { - + if (poseType == 0) + return 0; #if DEBUG - // System.Console.WriteLine($"Send PoseMsg [{this.networkId}/{this.thingId}] {this.poseType}"); + System.Console.WriteLine($"Send PoseMsg [{this.networkId}/{this.thingId}] {this.poseType}"); #endif byte ix = 0; diff --git a/src/Participant.cs b/src/Participant.cs index c85d437..8a40c34 100644 --- a/src/Participant.cs +++ b/src/Participant.cs @@ -54,6 +54,38 @@ namespace RoboidControl { } + public static List participants = new List(); + + public static Participant GetParticipant(string ipAddress, int port) { + //Console.WriteLine($"Get Participant {ipAddress}:{port}"); + foreach (Participant participant in Participant.participants) { + if (participant.ipAddress == ipAddress && participant.port == port) + return participant; + } + return null; + } + public static Participant GetParticipant(int participantId) { + //Console.WriteLine($"Get Participant [participantId]"); + foreach (Participant participant in Participant.participants) { + if (participant.networkId == participantId) + return participant; + } + return null; + } + public static Participant AddParticipant(string ipAddress, int port) { + Console.WriteLine($"New Participant {ipAddress}:{port}"); + Participant participant = new(ipAddress, port) { + networkId = (byte)(Participant.participants.Count + 1) + }; + Participant.participants.Add(participant); + return participant; + } + public static void AddParticipant(Participant participant) { + Participant foundParticipant = Participant.GetParticipant(participant.networkId); + if (foundParticipant == null) + Participant.participants.Add(participant); + } + /// /// Get a thing with the given ids /// diff --git a/src/ParticipantUDP.cs b/src/ParticipantUDP.cs index e670ad7..d48ee6f 100644 --- a/src/ParticipantUDP.cs +++ b/src/ParticipantUDP.cs @@ -69,24 +69,24 @@ namespace RoboidControl { return isolatedParticipant; } - public List owners = new List(); + // public List owners = new List(); - public Participant GetParticipant(string ipAddress, int port) { - //Console.WriteLine($"Get Participant {ipAddress}:{port}"); - foreach (Participant sender in owners) { - if (sender.ipAddress == ipAddress && sender.port == port) - return sender; - } - return null; - } - public Participant AddParticipant(string ipAddress, int port) { - Console.WriteLine($"New Participant {ipAddress}:{port}"); - Participant participant = new(ipAddress, port) { - networkId = (byte)(this.owners.Count + 1) - }; - owners.Add(participant); - return participant; - } + // public Participant GetParticipant(string ipAddress, int port) { + // //Console.WriteLine($"Get Participant {ipAddress}:{port}"); + // foreach (Participant sender in owners) { + // if (sender.ipAddress == ipAddress && sender.port == port) + // return sender; + // } + // return null; + // } + // public Participant AddParticipant(string ipAddress, int port) { + // Console.WriteLine($"New Participant {ipAddress}:{port}"); + // Participant participant = new(ipAddress, port) { + // networkId = (byte)(this.owners.Count + 1) + // }; + // owners.Add(participant); + // return participant; + // } // protected readonly Dictionary> thingMsgProcessors = new(); @@ -143,8 +143,8 @@ namespace RoboidControl { } } } - for (int ownerIx = 0; ownerIx < this.owners.Count; ownerIx++) { - Participant owner = this.owners[ownerIx]; + for (int ownerIx = 0; ownerIx < Participant.participants.Count; ownerIx++) { + Participant owner = Participant.participants[ownerIx]; owner.Update(currentTimeMS); } } @@ -315,9 +315,9 @@ namespace RoboidControl { } protected virtual void Process(Participant sender, PoseMsg msg) { -// #if DEBUG -// Console.WriteLine($"Participant: Process PoseMsg [{msg.networkId}/{msg.thingId}] {msg.poseType}"); -// #endif +#if DEBUG + Console.WriteLine($"Participant: Process PoseMsg [{msg.networkId}/{msg.thingId}] {msg.poseType}"); +#endif Thing thing = sender.Get(msg.networkId, msg.thingId); if (thing != null) { if ((msg.poseType & PoseMsg.Pose_Position) != 0) diff --git a/src/Things/TouchSensor.cs b/src/Things/TouchSensor.cs index 7fac61b..915a4a3 100644 --- a/src/Things/TouchSensor.cs +++ b/src/Things/TouchSensor.cs @@ -39,8 +39,9 @@ namespace RoboidControl { _touchedSomething = value; if (thisParticipant != null && this.owner != thisParticipant) { BinaryMsg msg = new(networkId, this); - foreach (Participant remoteParticipant in thisParticipant.owners) - thisParticipant.Send(remoteParticipant, msg); + this.thisParticipant.Send(this.owner, msg); + // foreach (Participant remoteParticipant in thisParticipant.participants) + // thisParticipant.Send(remoteParticipant, msg); } } }