Speak telnet properly to the cluster node
The old client read the socket with a StreamReader, so the option negotiation a node sends on connect landed in the text as control bytes and made a mess of the first lines. TelnetStream now answers it: it agrees to ECHO and SUPPRESS-GO-AHEAD, refuses everything else, swallows subnegotiations, and tracks what it already answered so the two ends do not reply to each other for ever. A command split across two reads is still understood, because a socket hands over whatever has turned up rather than whole messages. LineAssembler keeps the tail that has no line ending yet. Nodes write their login prompt as "login: " with nothing after it, so the old ReadLineAsync sat waiting for a line that never came and only got through because the greeting happened to mention a matching word. Login now follows N1MM: it looks for LOGON, ENTER CALL, LOG IN and the rest, and sends the callsign anyway after ten seconds if no prompt turns up. A password is sent when the node asks for one and the operator configured one. The commands go out after that, not straight after the call. A connection that has heard nothing for four minutes gets a blank line so the node does not drop it as idle. The old prompt check matched "call" anywhere in a line, which any spot comment could trigger. SendSpotAsync sends the node's dx command. Alt+P, or Edit / Spot It, spots the call being typed at the current frequency, or the last contact logged when nothing is typed, and puts it on our own bandmap without waiting for it to come back round from the node. The spot parser now takes a line with no colon after the spotter, and finds the time when DXSpider has put the spotter's grid after it. The cluster tests run over a real socket against a node fake that sends the negotiation, the prompt and spot lines. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
126
tests/Nonemm.Spotting.Tests/ClusterClientTests.cs
Normal file
126
tests/Nonemm.Spotting.Tests/ClusterClientTests.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace Nonemm.Spotting.Tests;
|
||||
|
||||
/// The client driven through a real socket, so the telnet negotiation, the
|
||||
/// login and the spot parsing are exercised together.
|
||||
public class ClusterClientTests : IDisposable
|
||||
{
|
||||
private const byte Iac = 255;
|
||||
private const byte Wont = 252;
|
||||
private const byte Do = 253;
|
||||
private const byte TerminalType = 24;
|
||||
|
||||
private static readonly TimeSpan Patience = TimeSpan.FromSeconds(5);
|
||||
|
||||
private readonly TcpListener listener;
|
||||
|
||||
public ClusterClientTests()
|
||||
{
|
||||
listener = new TcpListener(IPAddress.Loopback, 0);
|
||||
listener.Start();
|
||||
}
|
||||
|
||||
private int Port => ((IPEndPoint)listener.LocalEndpoint).Port;
|
||||
|
||||
public void Dispose() => listener.Stop();
|
||||
|
||||
private ClusterClient Connect(IReadOnlyList<string>? commands = null) =>
|
||||
new("127.0.0.1", Port, "DL1ABC", commands, retryInterval: TimeSpan.FromMinutes(1));
|
||||
|
||||
private static async Task<byte[]> ReadAsync(NetworkStream stream, int bytes)
|
||||
{
|
||||
byte[] buffer = new byte[bytes];
|
||||
int read = 0;
|
||||
while (read < bytes)
|
||||
{
|
||||
int got = await stream.ReadAsync(buffer.AsMemory(read));
|
||||
if (got == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
read += got;
|
||||
}
|
||||
return buffer[..read];
|
||||
}
|
||||
|
||||
private static async Task<string> ReadTextAsync(NetworkStream stream, int bytes) =>
|
||||
Encoding.Latin1.GetString(await ReadAsync(stream, bytes));
|
||||
|
||||
[Fact]
|
||||
public async Task TheCallsignGoesOutWhenTheNodeAsksForIt()
|
||||
{
|
||||
using ClusterClient cluster = Connect();
|
||||
cluster.Start();
|
||||
using TcpClient node = await listener.AcceptTcpClientAsync().WaitAsync(Patience);
|
||||
NetworkStream stream = node.GetStream();
|
||||
|
||||
await stream.WriteAsync(new byte[] { Iac, Do, TerminalType });
|
||||
await stream.WriteAsync(Encoding.Latin1.GetBytes("login: "));
|
||||
|
||||
byte[] sent = await ReadAsync(stream, 3 + 8).WaitAsync(Patience);
|
||||
|
||||
Assert.Equal(new byte[] { Iac, Wont, TerminalType }, sent[..3]);
|
||||
Assert.Equal("DL1ABC\r\n", Encoding.Latin1.GetString(sent[3..]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheCommandsGoOutAfterTheLogin()
|
||||
{
|
||||
using ClusterClient cluster = Connect(["set/skimmer"]);
|
||||
cluster.Start();
|
||||
using TcpClient node = await listener.AcceptTcpClientAsync().WaitAsync(Patience);
|
||||
NetworkStream stream = node.GetStream();
|
||||
|
||||
await stream.WriteAsync(Encoding.Latin1.GetBytes("Please enter your call: "));
|
||||
|
||||
Assert.Equal(
|
||||
"DL1ABC\r\nset/skimmer\r\n",
|
||||
await ReadTextAsync(stream, 8 + 13).WaitAsync(Patience));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ASpotFromTheNodeReachesTheBandmap()
|
||||
{
|
||||
using ClusterClient cluster = Connect();
|
||||
TaskCompletionSource<Spot> arrived = new();
|
||||
cluster.SpotArrived += (_, spot) => arrived.TrySetResult(spot);
|
||||
cluster.Start();
|
||||
using TcpClient node = await listener.AcceptTcpClientAsync().WaitAsync(Patience);
|
||||
|
||||
await node.GetStream().WriteAsync(Encoding.Latin1.GetBytes(
|
||||
"DX de W3LPL: 14025.0 JA1XYZ CQ 1234Z\r\n"));
|
||||
|
||||
Spot spot = await arrived.Task.WaitAsync(Patience);
|
||||
Assert.Equal("JA1XYZ", spot.Call.Text);
|
||||
Assert.Equal(14_025_000, spot.Frequency.Hertz);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SpottingAStationSendsTheNodesDxCommand()
|
||||
{
|
||||
using ClusterClient cluster = Connect();
|
||||
TaskCompletionSource connected = new();
|
||||
cluster.ConnectionChanged += (_, up) =>
|
||||
{
|
||||
if (up)
|
||||
{
|
||||
connected.TrySetResult();
|
||||
}
|
||||
};
|
||||
cluster.Start();
|
||||
using TcpClient node = await listener.AcceptTcpClientAsync().WaitAsync(Patience);
|
||||
await connected.Task.WaitAsync(Patience);
|
||||
|
||||
await cluster.SendSpotAsync(
|
||||
Core.Frequency.FromKilohertz(14_025),
|
||||
Core.Callsign.Parse("JA1XYZ"),
|
||||
"CQ");
|
||||
|
||||
Assert.Equal(
|
||||
"dx 14025.0 JA1XYZ CQ\r\n",
|
||||
await ReadTextAsync(node.GetStream(), 22).WaitAsync(Patience));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user