Resynchronize a log with the other stations, and rescore it
A station that was away — a laptop that lost the network, one that was switched off — comes back missing whatever the others logged while it was gone. N1MM fills that in by walking the log an hour at a time, and this is that walk, on the same messages: | Message | What it says | |---|---| | `CHECKSUM` | here is what I hold for your contacts in this hour, and the contest and callsign I am logging | | `DELETEQS` | drop your copies of my contacts in that hour, I am sending them again | | `RESYNCQSO` | one of those contacts | | `CONFIRMED` | here is what the hour hashes to at my end — which also asks for the next hour | Each station is the authority on the contacts it made, so an hour that disagrees is replaced rather than merged. The answer to one block asks for the next, and the walk ends when the block starts after the end of the period. The Resynchronize tab counts the hours that agreed, the hours that were sent again and the hours that still disagreed, which are N1MM's columns. **`LogChecksum`** is N1MM's: a CRC32 over the `DXLOG` columns of the block, ordered by time and then callsign, leaving out what each station works out for itself — points, the three multiplier flags, the country prefix, the continent, the WPX prefix and `IsOriginal`. Two stations may disagree about those and still hold the same log. A checksum that disagrees for any other reason costs one block of traffic, which is what a resync is for. **The link still holds no log.** It asks one through `ContactLog` for the contacts of a computer in a block of time, and `SessionContactLog` answers on the window thread, because that is where the log is touched. **Every contact logged here now carries the computer that made it.** N1MM stamps `NetBiosName` and `NetworkedCompNr` on every row and this program did not, so its own contacts belonged to no station and a resync could not ask for them. **Rescore** writes the points and multiplier flags of a period back to the database. The log in memory scores itself whenever it changes; the stored rows do not, and N1MM reads them rather than working them out. A contact that arrives from another station after the one that took a multiplier leaves those columns behind. A station can also ask the others to rescore when its resync finishes, which is the flag on the end of `CHECKSUM`. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdAYHcdRktqKry7nk414TU
This commit is contained in:
@@ -219,6 +219,11 @@ public sealed class AppSession : IDisposable
|
||||
// whoever was at the radio when the last contest was open is still
|
||||
// there, and every contact he logs carries his callsign
|
||||
Operator = Settings.CurrentOperator,
|
||||
// N1MM stamps every contact with the computer that made it and the
|
||||
// station number of that computer, and a resync asks for the
|
||||
// contacts of one computer by name
|
||||
StationName = ComputerName,
|
||||
StationNumber = Settings.StationNumber,
|
||||
};
|
||||
positions.Clear();
|
||||
for (int number = 1; number <= PositionCount; number++)
|
||||
@@ -283,6 +288,11 @@ public sealed class AppSession : IDisposable
|
||||
/// separate from `ApplyNetworkSettings` because the two are separate
|
||||
/// networks: one carries XML to other programs, the other carries contacts
|
||||
/// to the other computers of this entry.
|
||||
/// What this computer is called on the network of the entry, which is also
|
||||
/// what every contact it logs is stamped with.
|
||||
public string ComputerName =>
|
||||
Settings.NetworkStationName.Length > 0 ? Settings.NetworkStationName : Environment.MachineName;
|
||||
|
||||
public void ApplyStationLinkSettings()
|
||||
{
|
||||
link?.Dispose();
|
||||
@@ -293,7 +303,7 @@ public sealed class AppSession : IDisposable
|
||||
return;
|
||||
}
|
||||
link = new StationLink(
|
||||
Settings.NetworkStationName.Length > 0 ? Settings.NetworkStationName : Environment.MachineName,
|
||||
ComputerName,
|
||||
Settings.StationLinkVersion,
|
||||
Settings.StationNumber,
|
||||
Settings.StationLinkPort)
|
||||
@@ -304,6 +314,11 @@ public sealed class AppSession : IDisposable
|
||||
// made where the windows read it
|
||||
link.UpdateArrived += (_, update) =>
|
||||
Dispatcher.UIThread.Post(() => TakeFromNetwork(update));
|
||||
// the resync reads the log rather than being handed it, and another
|
||||
// station can ask for a rescore when its resync is done
|
||||
link.Log = new SessionContactLog(this);
|
||||
link.RescoreRequested += (_, _) =>
|
||||
Dispatcher.UIThread.Post(() => Rescore(DateTime.MinValue));
|
||||
foreach (string peer in Settings.StationLinkPeers)
|
||||
{
|
||||
if (Peer(peer) is var (name, address, port))
|
||||
@@ -348,6 +363,12 @@ public sealed class AppSession : IDisposable
|
||||
_ = link.SendBandAsync(position.Frequency, position.Mode, position.IsRunning, position.RadioNumber);
|
||||
}
|
||||
|
||||
/// Scores the log again and writes the points and multiplier flags from
|
||||
/// `fromUtc` on back to the database. Returns how many rows were written.
|
||||
/// Another station asks for this at the end of a resync, and the rescore
|
||||
/// tab of the network status window asks for it by hand.
|
||||
public int Rescore(DateTime fromUtc) => Logging?.Rescore(fromUtc) ?? 0;
|
||||
|
||||
/// The `STATUS` message: everything the other operators' network status
|
||||
/// windows show about this station, whether anything has changed or not. It
|
||||
/// goes out on N1MM's own interval, and the rates in it are read out of the
|
||||
@@ -414,6 +435,9 @@ public sealed class AppSession : IDisposable
|
||||
case ContactDeleted deleted:
|
||||
Logging.DeleteFromNetwork(deleted.Id, deleted.Call, deleted.TimestampUtc);
|
||||
break;
|
||||
case ContactsCleared cleared:
|
||||
Logging.ClearFromNetwork(cleared.StationName, cleared.FromUtc, cleared.Length);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
using Avalonia.Threading;
|
||||
using Nonemm.Core;
|
||||
using Nonemm.Network;
|
||||
|
||||
namespace Nonemm.App;
|
||||
|
||||
/// The log as a resync reads it. The link asks these questions on a socket
|
||||
/// thread and the log is only touched where the windows are, so every answer is
|
||||
/// fetched on the window thread and waited for.
|
||||
///
|
||||
/// The wait is short — a block is an hour of contacts out of a list already in
|
||||
/// memory — and nothing on the window thread waits on the socket thread, so
|
||||
/// there is nothing here for the two to deadlock over.
|
||||
internal sealed class SessionContactLog : ContactLog
|
||||
{
|
||||
private readonly AppSession session;
|
||||
|
||||
internal SessionContactLog(AppSession session) => this.session = session;
|
||||
|
||||
public string ContestName => Ask(() => session.Logging?.Contest.Name ?? "");
|
||||
|
||||
public string MyCall => Ask(() => session.Settings.Station.Callsign);
|
||||
|
||||
public IReadOnlyList<Qso> Contacts(string computerName, DateTime fromUtc, TimeSpan length) =>
|
||||
Ask(() => session.Logging is not { } logging
|
||||
? []
|
||||
: logging.Log.Qsos
|
||||
.Where(q =>
|
||||
string.Equals(q.StationName, computerName, StringComparison.OrdinalIgnoreCase)
|
||||
&& q.TimestampUtc >= fromUtc
|
||||
&& q.TimestampUtc < fromUtc + length)
|
||||
.OrderBy(q => q.TimestampUtc)
|
||||
.ThenBy(q => q.Call.Text, StringComparer.Ordinal)
|
||||
.ToList());
|
||||
|
||||
private static T Ask<T>(Func<T> question) =>
|
||||
Dispatcher.UIThread.CheckAccess() ? question() : Dispatcher.UIThread.Invoke(question);
|
||||
}
|
||||
@@ -69,6 +69,53 @@
|
||||
</StackPanel>
|
||||
</TabItem>
|
||||
|
||||
<TabItem Header="Resynchronize">
|
||||
<DockPanel Margin="8">
|
||||
<StackPanel DockPanel.Dock="Top" Spacing="6" Margin="0,0,0,8">
|
||||
<StackPanel Orientation="Horizontal" Spacing="12">
|
||||
<CheckBox Name="ResyncAllBox" Content="Resync All Stations" />
|
||||
<CheckBox Name="RescoreWhenDoneBox" Content="Rescore When Done" />
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal" Spacing="6">
|
||||
<Button Content="Contest" Click="OnResyncContest" />
|
||||
<Button Content="Since:" Click="OnResyncSince" />
|
||||
<TextBox Name="ResyncSinceBox" Width="150" />
|
||||
<Button Content="Last" Click="OnResyncLast" />
|
||||
<TextBox Name="ResyncHoursBox" Width="44" Text="4" />
|
||||
<TextBlock Text="Hours" VerticalAlignment="Center" />
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
<DataGrid Name="ResyncRows" IsReadOnly="True" GridLinesVisibility="Horizontal"
|
||||
CanUserSortColumns="False" FontSize="12" FontFamily="monospace"
|
||||
AutoGenerateColumns="False" x:DataType="local:StationRow">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="Name" Binding="{Binding Computer}" />
|
||||
<DataGridTextColumn Header="IP Address" Binding="{Binding IpAddress}" />
|
||||
<DataGridTextColumn Header="In Prog" Binding="{Binding InProgress}" />
|
||||
<DataGridTextColumn Header="Status" Binding="{Binding Status}" Width="240" />
|
||||
<DataGridTextColumn Header="# Hours OK" Binding="{Binding HoursOk}" />
|
||||
<DataGridTextColumn Header="# Hours Fixed" Binding="{Binding HoursFixed}" />
|
||||
<DataGridTextColumn Header="# Hours Failed" Binding="{Binding HoursFailed}" />
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
</DockPanel>
|
||||
</TabItem>
|
||||
|
||||
<TabItem Header="Rescore">
|
||||
<StackPanel Margin="8" Spacing="8">
|
||||
<TextBlock Text="Rescore Log, No Log Resync" FontWeight="Bold" />
|
||||
<StackPanel Orientation="Horizontal" Spacing="6">
|
||||
<Button Content="Contest" Click="OnRescoreContest" />
|
||||
<Button Content="Since:" Click="OnRescoreSince" />
|
||||
<TextBox Name="RescoreSinceBox" Width="150" />
|
||||
<Button Content="Last" Click="OnRescoreLast" />
|
||||
<TextBox Name="RescoreHoursBox" Width="44" Text="4" />
|
||||
<TextBlock Text="Hours" VerticalAlignment="Center" />
|
||||
</StackPanel>
|
||||
<TextBlock Name="RescoreText" TextWrapping="Wrap" Opacity="0.75" FontSize="11" />
|
||||
</StackPanel>
|
||||
</TabItem>
|
||||
|
||||
<TabItem Header="Messages">
|
||||
<DockPanel Margin="8">
|
||||
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Spacing="8" Margin="0,0,0,6">
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Globalization;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Threading;
|
||||
@@ -15,10 +16,9 @@ namespace Nonemm.App.Windows;
|
||||
/// operator reads its own station number and the version it is claiming off the
|
||||
/// same window as everybody else's.
|
||||
///
|
||||
/// Three of N1MM's tabs are here. Resynchronize, Rescore and the transmit
|
||||
/// interlocks of its Options tab are not: this program has neither the resync
|
||||
/// messages nor a transmit interlock, and a tab of controls that do nothing is
|
||||
/// worse than no tab.
|
||||
/// Five of N1MM's tabs are here. Its Options tab and its call-stacking target
|
||||
/// tab are not: the transmit interlocks behind Options are not written, and a
|
||||
/// tab of controls that do nothing is worse than no tab.
|
||||
///
|
||||
/// The window redraws once a second and on every message, which on a busy
|
||||
/// network is several a second. That is what a status window is for, and the
|
||||
@@ -33,6 +33,9 @@ public sealed partial class NetworkStatusWindow : RefreshableWindow
|
||||
private readonly Dictionary<string, TimeSpan> echoes = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly DispatcherTimer clock;
|
||||
private StationLink? link;
|
||||
/// Set while a resync that was asked to rescore when it finishes is
|
||||
/// running. It holds the start of the period, which is what gets rescored.
|
||||
private DateTime? rescoreAfterResync;
|
||||
|
||||
public NetworkStatusWindow(AppSession session)
|
||||
{
|
||||
@@ -45,6 +48,12 @@ public sealed partial class NetworkStatusWindow : RefreshableWindow
|
||||
e.Row.Foreground = row.Colour;
|
||||
}
|
||||
};
|
||||
// the two Since boxes start at the contest, which is the period an
|
||||
// operator asks for most often
|
||||
string start = (session.Logging?.Instance.StartDate ?? DateTime.UtcNow)
|
||||
.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
|
||||
ResyncSinceBox.Text = start;
|
||||
RescoreSinceBox.Text = start;
|
||||
Attach();
|
||||
// the Receive column is a countdown, so it has to redraw with nothing
|
||||
// arriving
|
||||
@@ -81,6 +90,8 @@ public sealed partial class NetworkStatusWindow : RefreshableWindow
|
||||
Rows.ItemsSource = rows;
|
||||
Rows.SelectedItem = rows.FirstOrDefault(r =>
|
||||
string.Equals(r.Station.ComputerName, selected, StringComparison.OrdinalIgnoreCase));
|
||||
ResyncRows.ItemsSource = rows.Where(r => !r.Station.IsMine).ToList();
|
||||
RescoreWhenResyncDone();
|
||||
ReportEchoes();
|
||||
StateText.Text = State();
|
||||
ShowMessages();
|
||||
@@ -191,6 +202,96 @@ public sealed partial class NetworkStatusWindow : RefreshableWindow
|
||||
MessageScroll.ScrollToEnd();
|
||||
}
|
||||
|
||||
/// The rescore an operator asked for when the resync is done, once every
|
||||
/// station has stopped.
|
||||
private void RescoreWhenResyncDone()
|
||||
{
|
||||
if (rescoreAfterResync is not { } from
|
||||
|| link is null
|
||||
|| link.Stations.Any(s => s.IsResyncing))
|
||||
{
|
||||
return;
|
||||
}
|
||||
rescoreAfterResync = null;
|
||||
Say($"rescored {session.Rescore(from)} contacts");
|
||||
}
|
||||
|
||||
/// The start of the period the resync and rescore buttons work on.
|
||||
/// `Since` reads the box, which is a time of day in UTC the way the rest of
|
||||
/// the program writes one.
|
||||
private DateTime? Since(TextBox box)
|
||||
{
|
||||
if (DateTime.TryParseExact(
|
||||
(box.Text ?? "").Trim(),
|
||||
["yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"],
|
||||
CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
|
||||
out DateTime when))
|
||||
{
|
||||
return when;
|
||||
}
|
||||
Say($"{box.Text} is not a time — write it as 2026-09-03 14:00");
|
||||
return null;
|
||||
}
|
||||
|
||||
private static DateTime HoursAgo(TextBox box) =>
|
||||
DateTime.UtcNow.AddHours(-Math.Max(1, int.TryParse((box.Text ?? "").Trim(), out int hours) ? hours : 4));
|
||||
|
||||
/// The whole contest, from three hours before it started. N1MM allows the
|
||||
/// same margin either side, for a clock that is out and for the contacts
|
||||
/// entered after the contest ended.
|
||||
private DateTime ContestStart() =>
|
||||
(session.Logging?.Instance.StartDate ?? DateTime.UtcNow.AddDays(-1)).AddHours(-3);
|
||||
|
||||
private void OnResyncContest(object? sender, RoutedEventArgs e) => Resync(ContestStart());
|
||||
|
||||
private void OnResyncSince(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
if (Since(ResyncSinceBox) is { } from)
|
||||
{
|
||||
Resync(from);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnResyncLast(object? sender, RoutedEventArgs e) => Resync(HoursAgo(ResyncHoursBox));
|
||||
|
||||
/// Asks every connected station whether it and this one hold the same
|
||||
/// contacts, an hour at a time, from `fromUtc` until now.
|
||||
private void Resync(DateTime fromUtc)
|
||||
{
|
||||
if (link is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
bool everyStation = ResyncAllBox.IsChecked == true;
|
||||
bool rescore = RescoreWhenDoneBox.IsChecked == true;
|
||||
rescoreAfterResync = rescore ? fromUtc : null;
|
||||
link.StartResyncAll(fromUtc, DateTime.UtcNow, everyStation, rescore);
|
||||
Say($"resync from {fromUtc:yyyy-MM-dd HH:mm}Z");
|
||||
Tabs.SelectedIndex = 2;
|
||||
}
|
||||
|
||||
private void OnRescoreContest(object? sender, RoutedEventArgs e) => Rescore(DateTime.MinValue);
|
||||
|
||||
private void OnRescoreSince(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
if (Since(RescoreSinceBox) is { } from)
|
||||
{
|
||||
Rescore(from);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRescoreLast(object? sender, RoutedEventArgs e) => Rescore(HoursAgo(RescoreHoursBox));
|
||||
|
||||
/// Scores the log again and writes the points and multiplier flags back.
|
||||
/// Nothing is sent: the other stations score their own logs.
|
||||
private void Rescore(DateTime fromUtc)
|
||||
{
|
||||
int written = session.Rescore(fromUtc);
|
||||
RescoreText.Text = $"wrote {written} contacts at {DateTime.UtcNow:HH:mm:ss}Z";
|
||||
Say($"rescored {written} contacts");
|
||||
}
|
||||
|
||||
/// The station the operator right-clicked, or null when the row is this
|
||||
/// computer's own: nothing in the menu is worth doing to yourself.
|
||||
private NetworkedStation? Chosen() =>
|
||||
@@ -216,7 +317,7 @@ public sealed partial class NetworkStatusWindow : RefreshableWindow
|
||||
TalkBox.Focus();
|
||||
}
|
||||
|
||||
private void OnShowMessages(object? sender, RoutedEventArgs e) => Tabs.SelectedIndex = 2;
|
||||
private void OnShowMessages(object? sender, RoutedEventArgs e) => Tabs.SelectedIndex = 4;
|
||||
|
||||
private async void OnEcho(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
@@ -76,6 +76,18 @@ public sealed class StationRow
|
||||
|
||||
public string Mode => station.Mode?.Name ?? "";
|
||||
|
||||
/// The resync columns. N1MM counts blocks of an hour, which is why they
|
||||
/// say hours.
|
||||
public string InProgress => station.IsResyncing ? "yes" : "";
|
||||
|
||||
public string Status => station.ResyncStatus;
|
||||
|
||||
public string HoursOk => station.HoursOk > 0 ? station.HoursOk.ToString() : "";
|
||||
|
||||
public string HoursFixed => station.HoursFixed > 0 ? station.HoursFixed.ToString() : "";
|
||||
|
||||
public string HoursFailed => station.HoursFailed > 0 ? station.HoursFailed.ToString() : "";
|
||||
|
||||
/// Red for a station that is not talking, and for the version mismatch that
|
||||
/// leaves everything else looking right. Every other row takes the theme's
|
||||
/// own text colour: the grid reuses its rows, so a row that was red has to
|
||||
|
||||
Reference in New Issue
Block a user