Fixes to pass unit tests

This commit is contained in:
Pascal Serrarens 2025-05-02 12:34:48 +02:00
parent e7a6d92740
commit 5a3c2f2a2c
3 changed files with 27 additions and 21 deletions

View File

@ -19,10 +19,11 @@ namespace RoboidControl {
/// </summary>
/// <param name="ipAddress">The IP address of the participant</param>
/// <param name="port">The UDP port of the participant</param>
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;
}
/// <summary>
@ -34,7 +35,7 @@ namespace RoboidControl {
/// </summary>
public int port = 0;
private UdpClient udpClient = null;
public UdpClient udpClient = null;
/// <summary>
/// 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 {
/// <param name="ipAddress">The IP address of the participant</param>
/// <param name="port">The port used to send messages to this participant</param>
/// <returns>The added participant</returns>
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);

View File

@ -43,13 +43,6 @@ namespace RoboidControl {
/// <param name="port">The port number of the site server</param>
/// <param name="localPort">The port used by the local participant</param>
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<IMessage> messageQueue = new ConcurrentQueue<IMessage>();
@ -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);
}

View File

@ -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