1.0.11
discovery retention
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<Version>1.0.10</Version>
|
<Version>1.0.11</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -16,9 +16,11 @@ public class PrinterDiscoveryBackgroundTask
|
|||||||
private readonly PrintServer _printServer;
|
private readonly PrintServer _printServer;
|
||||||
private readonly TimeSpan _interval;
|
private readonly TimeSpan _interval;
|
||||||
private readonly TimeSpan _discoveryTimeout;
|
private readonly TimeSpan _discoveryTimeout;
|
||||||
|
private readonly TimeSpan _retentionPeriod;
|
||||||
|
|
||||||
private HashSet<string> _lastDiscoveredPrinterIps = new();
|
private readonly Dictionary<string, DateTime> _lastSeenTimes = new();
|
||||||
private List<DiscoveredPrinter> _lastDiscoveredPrinters = new();
|
private readonly Dictionary<string, DiscoveredPrinter> _knownPrinters = new();
|
||||||
|
private HashSet<string> _lastEffectiveIps = new();
|
||||||
|
|
||||||
public PrinterDiscoveryBackgroundTask(
|
public PrinterDiscoveryBackgroundTask(
|
||||||
IDiscoveryService discoveryService,
|
IDiscoveryService discoveryService,
|
||||||
@@ -32,6 +34,7 @@ public class PrinterDiscoveryBackgroundTask
|
|||||||
_printServer = printServer;
|
_printServer = printServer;
|
||||||
_interval = TimeSpan.FromSeconds(30);
|
_interval = TimeSpan.FromSeconds(30);
|
||||||
_discoveryTimeout = TimeSpan.FromSeconds(3);
|
_discoveryTimeout = TimeSpan.FromSeconds(3);
|
||||||
|
_retentionPeriod = TimeSpan.FromMinutes(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -69,21 +72,11 @@ public class PrinterDiscoveryBackgroundTask
|
|||||||
private async Task DiscoverAndNotifyAsync()
|
private async Task DiscoverAndNotifyAsync()
|
||||||
{
|
{
|
||||||
List<DiscoveredPrinter> printers;
|
List<DiscoveredPrinter> printers;
|
||||||
HashSet<string> currentIps;
|
|
||||||
bool configChanged;
|
|
||||||
|
|
||||||
await _printServer.EnterDiscoveryLockAsync();
|
await _printServer.EnterDiscoveryLockAsync();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
printers = await _discoveryService.DiscoverPrintersAsync(_discoveryTimeout);
|
printers = await _discoveryService.DiscoverPrintersAsync(_discoveryTimeout);
|
||||||
currentIps = printers.Select(p => p.IPAddress).ToHashSet();
|
|
||||||
configChanged = HasConfigurationChanged(currentIps);
|
|
||||||
|
|
||||||
if (configChanged)
|
|
||||||
{
|
|
||||||
_lastDiscoveredPrinterIps = currentIps;
|
|
||||||
_lastDiscoveredPrinters = printers;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -95,25 +88,59 @@ public class PrinterDiscoveryBackgroundTask
|
|||||||
_printServer.ExitDiscoveryLock();
|
_printServer.ExitDiscoveryLock();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register printers AFTER releasing the lock
|
// Update last seen times for discovered printers
|
||||||
foreach (string currentIp in currentIps)
|
DateTime now = DateTime.UtcNow;
|
||||||
|
foreach (DiscoveredPrinter printer in printers)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("Registering discovered printer with IP: {IP}", currentIp);
|
_lastSeenTimes[printer.IPAddress] = now;
|
||||||
_printServer.RegisterPrinter(currentIp);
|
_knownPrinters[printer.IPAddress] = printer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove printers not seen within the retention period
|
||||||
|
List<string> expiredIps = _lastSeenTimes
|
||||||
|
.Where(kvp => now - kvp.Value > _retentionPeriod)
|
||||||
|
.Select(kvp => kvp.Key)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
foreach (string expiredIp in expiredIps)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Printer {IP} not seen for over {Retention}s, removing", expiredIp, _retentionPeriod.TotalSeconds);
|
||||||
|
_lastSeenTimes.Remove(expiredIp);
|
||||||
|
_knownPrinters.Remove(expiredIp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute effective set and detect changes
|
||||||
|
HashSet<string> effectiveIps = _lastSeenTimes.Keys.ToHashSet();
|
||||||
|
bool configChanged = HasConfigurationChanged(effectiveIps);
|
||||||
|
|
||||||
|
// Register newly appeared printers
|
||||||
|
foreach (string ip in effectiveIps.Except(_lastEffectiveIps))
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Registering discovered printer with IP: {IP}", ip);
|
||||||
|
_printServer.RegisterPrinter(ip);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unregister expired printers
|
||||||
|
foreach (string ip in _lastEffectiveIps.Except(effectiveIps))
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Unregistering expired printer with IP: {IP}", ip);
|
||||||
|
_printServer.UnregisterPrinter(ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (configChanged)
|
if (configChanged)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Discovered printer configuration changed: {Count} printer(s)", printers.Count);
|
_lastEffectiveIps = effectiveIps;
|
||||||
await _receiver.OnPrintersDiscoveredAsync(printers.AsReadOnly());
|
List<DiscoveredPrinter> effectivePrinters = _knownPrinters.Values.ToList();
|
||||||
|
_logger.LogInformation("Discovered printer configuration changed: {Count} printer(s)", effectivePrinters.Count);
|
||||||
|
await _receiver.OnPrintersDiscoveredAsync(effectivePrinters.AsReadOnly());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool HasConfigurationChanged(HashSet<string> currentIps)
|
private bool HasConfigurationChanged(HashSet<string> effectiveIps)
|
||||||
{
|
{
|
||||||
if (currentIps.Count != _lastDiscoveredPrinterIps.Count)
|
if (effectiveIps.Count != _lastEffectiveIps.Count)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return !currentIps.SetEquals(_lastDiscoveredPrinterIps);
|
return !effectiveIps.SetEquals(_lastEffectiveIps);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user