From 5a3c2f2a2ce61c249aa5f3346b4fda3c8827ffec Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 2 May 2025 12:34:48 +0200 Subject: [PATCH] Fixes to pass unit tests --- src/Participant.cs | 18 +++++++++++------- src/Participants/ParticipantUDP.cs | 17 ++++++++--------- src/Participants/SiteServer.cs | 13 ++++++++----- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/Participant.cs b/src/Participant.cs index bdc5c0b..4318467 100644 --- a/src/Participant.cs +++ b/src/Participant.cs @@ -19,10 +19,11 @@ namespace RoboidControl { /// /// The IP address of the participant /// The UDP port of the participant - public Participant(string ipAddress, int port) { + public Participant(string ipAddress, int port, Participant localParticipant = null) { this.ipAddress = ipAddress; this.port = port; - this.udpClient = new UdpClient(); + if (localParticipant != null) + this.udpClient = localParticipant.udpClient; } /// @@ -34,7 +35,7 @@ namespace RoboidControl { /// public int port = 0; - private UdpClient udpClient = null; + public UdpClient udpClient = null; /// /// he network Id to identify the participant @@ -119,8 +120,11 @@ namespace RoboidControl { return true; IPEndPoint participantEndpoint = new IPEndPoint(IPAddress.Parse(this.ipAddress), this.port); - Console.WriteLine($"msg to {participantEndpoint.Address.ToString()} {participantEndpoint.Port}"); - this.udpClient?.Send(this.buffer, bufferSize, participantEndpoint); + Console.WriteLine($"msg to remote participant {participantEndpoint.Address.ToString()} {participantEndpoint.Port}"); + if (udpClient != null) { + Console.WriteLine("sending..."); + this.udpClient?.Send(this.buffer, bufferSize, participantEndpoint); + } return true; } @@ -165,9 +169,9 @@ namespace RoboidControl { /// The IP address of the participant /// The port used to send messages to this participant /// The added participant - public static Participant AddParticipant(string ipAddress, int port) { + public static Participant AddParticipant(string ipAddress, int port, Participant localParticipant = null) { Console.WriteLine($"New Participant {ipAddress}:{port}"); - Participant participant = new(ipAddress, port) { + Participant participant = new(ipAddress, port, localParticipant) { networkId = (byte)(Participant.participants.Count + 1) }; Participant.participants.Add(participant); diff --git a/src/Participants/ParticipantUDP.cs b/src/Participants/ParticipantUDP.cs index c092bc8..0fde7f2 100644 --- a/src/Participants/ParticipantUDP.cs +++ b/src/Participants/ParticipantUDP.cs @@ -43,13 +43,6 @@ namespace RoboidControl { /// The port number of the site server /// The port used by the local participant public ParticipantUDP(string ipAddress, int port = 7681, int localPort = 7681) : base("127.0.0.1", localPort) { - if (this.port == 0) - this.isIsolated = true; - else - this.remoteSite = new Participant(ipAddress, port); - - Participant.AddParticipant(this); - // Determine local IP address IPAddress localIpAddress = null; IPAddress subnetMask = null; @@ -80,6 +73,12 @@ namespace RoboidControl { this.udpClient = new UdpClient(localPort); this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); + if (this.port == 0) + this.isIsolated = true; + else + this.remoteSite = new Participant(ipAddress, port, this); + + Participant.AddParticipant(this); } private static ParticipantUDP isolatedParticipant = null; @@ -119,7 +118,7 @@ namespace RoboidControl { //public byte[] buffer = new byte[1024]; public IPEndPoint endPoint = null; - public UdpClient udpClient = null; + //public UdpClient udpClient = null; public string broadcastIpAddress = "255.255.255.255"; public readonly ConcurrentQueue messageQueue = new ConcurrentQueue(); @@ -298,7 +297,7 @@ namespace RoboidControl { string ipAddress = endPoint.Address.ToString(); if (ipAddress != this.ipAddress) { Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port); - remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port); + remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port, this); ReceiveData(data, remoteParticipant); } diff --git a/src/Participants/SiteServer.cs b/src/Participants/SiteServer.cs index c97154a..91b0b8b 100644 --- a/src/Participants/SiteServer.cs +++ b/src/Participants/SiteServer.cs @@ -82,10 +82,12 @@ namespace RoboidControl { if (participant == null || participant == this) continue; - PoseMsg poseMsg = new(thing.owner.networkId, thing); - this.Send(participant, poseMsg); - BinaryMsg binaryMsg = new(thing.owner.networkId, thing); - this.Send(participant, binaryMsg); + // PoseMsg poseMsg = new(thing.owner.networkId, thing); + // this.Send(participant, poseMsg); + // BinaryMsg binaryMsg = new(thing.owner.networkId, thing); + // this.Send(participant, binaryMsg); + participant.Send(new PoseMsg(thing.owner.networkId, thing)); + participant.Send(new BinaryMsg(thing.owner.networkId, thing)); } } } @@ -99,7 +101,8 @@ namespace RoboidControl { base.Process(sender, msg); if (msg.networkId != sender.networkId) { //Console.WriteLine($"{this.name} received New Participant -> {sender.networkId}"); - this.Send(sender, new NetworkIdMsg(sender.networkId)); + // this.Send(sender, new NetworkIdMsg(sender.networkId)); + sender.Send(new NetworkIdMsg(sender.networkId)); UpdateEvent e = new() { messageId = ParticipantMsg.Id, participant = sender