From 9f548cc482334c062985745e9f007de9a147f4b8 Mon Sep 17 00:00:00 2001 From: AliveDevil Date: Tue, 28 Nov 2023 18:15:27 +0100 Subject: [PATCH] Handlers --- src/pdns-dhcp/Kea/IKeaDhcpLeaseHandler.cs | 8 +++++ src/pdns-dhcp/Kea/KeaDhcp4LeaseHandler.cs | 8 +++++ src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs | 7 ---- src/pdns-dhcp/Kea/KeaDhcp6LeaseHandler.cs | 10 ++++++ src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs | 7 ---- src/pdns-dhcp/Kea/KeaDhcpLeaseFileReader.cs | 6 ---- ...LeaseWatcher.cs => KeaDhcpLeaseWatcher.cs} | 32 +++++++++++-------- src/pdns-dhcp/Program.cs | 4 +++ src/pdns-dhcp/Services/DhcpLeaseWatcher.cs | 1 + .../Services/IDhcpLeaseWatcherFactory.cs | 4 +-- src/pdns-dhcp/appsettings.Development.json | 2 +- 11 files changed, 52 insertions(+), 37 deletions(-) create mode 100644 src/pdns-dhcp/Kea/IKeaDhcpLeaseHandler.cs create mode 100644 src/pdns-dhcp/Kea/KeaDhcp4LeaseHandler.cs delete mode 100644 src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs create mode 100644 src/pdns-dhcp/Kea/KeaDhcp6LeaseHandler.cs delete mode 100644 src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs delete mode 100644 src/pdns-dhcp/Kea/KeaDhcpLeaseFileReader.cs rename src/pdns-dhcp/Kea/{KeaLeaseWatcher.cs => KeaDhcpLeaseWatcher.cs} (92%) diff --git a/src/pdns-dhcp/Kea/IKeaDhcpLeaseHandler.cs b/src/pdns-dhcp/Kea/IKeaDhcpLeaseHandler.cs new file mode 100644 index 0000000..17e689f --- /dev/null +++ b/src/pdns-dhcp/Kea/IKeaDhcpLeaseHandler.cs @@ -0,0 +1,8 @@ +using nietras.SeparatedValues; + +namespace pdns_dhcp.Kea; + +public interface IKeaDhcpLeaseHandler +{ + void Handle(in SepReader.Row row); +} diff --git a/src/pdns-dhcp/Kea/KeaDhcp4LeaseHandler.cs b/src/pdns-dhcp/Kea/KeaDhcp4LeaseHandler.cs new file mode 100644 index 0000000..202a3f4 --- /dev/null +++ b/src/pdns-dhcp/Kea/KeaDhcp4LeaseHandler.cs @@ -0,0 +1,8 @@ +using nietras.SeparatedValues; + +namespace pdns_dhcp.Kea; + +public class KeaDhcp4LeaseHandler : IKeaDhcpLeaseHandler +{ + public void Handle(in SepReader.Row row) { } +} diff --git a/src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs b/src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs deleted file mode 100644 index 0c058c8..0000000 --- a/src/pdns-dhcp/Kea/KeaDhcp4LeaseWatcher.cs +++ /dev/null @@ -1,7 +0,0 @@ -using pdns_dhcp.Options; - -namespace pdns_dhcp.Kea; - -public class KeaDhcp4LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options) -{ -} diff --git a/src/pdns-dhcp/Kea/KeaDhcp6LeaseHandler.cs b/src/pdns-dhcp/Kea/KeaDhcp6LeaseHandler.cs new file mode 100644 index 0000000..4e7ad1d --- /dev/null +++ b/src/pdns-dhcp/Kea/KeaDhcp6LeaseHandler.cs @@ -0,0 +1,10 @@ +using nietras.SeparatedValues; + +namespace pdns_dhcp.Kea; + +public class KeaDhcp6LeaseHandler : IKeaDhcpLeaseHandler +{ + public void Handle(in SepReader.Row row) + { + } +} diff --git a/src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs b/src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs deleted file mode 100644 index 8c400e1..0000000 --- a/src/pdns-dhcp/Kea/KeaDhcp6LeaseWatcher.cs +++ /dev/null @@ -1,7 +0,0 @@ -using pdns_dhcp.Options; - -namespace pdns_dhcp.Kea; - -public class KeaDhcp6LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options) -{ -} diff --git a/src/pdns-dhcp/Kea/KeaDhcpLeaseFileReader.cs b/src/pdns-dhcp/Kea/KeaDhcpLeaseFileReader.cs deleted file mode 100644 index a253949..0000000 --- a/src/pdns-dhcp/Kea/KeaDhcpLeaseFileReader.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace pdns_dhcp.Kea; - -public class KeaDhcpLeaseFileReader -{ - -} diff --git a/src/pdns-dhcp/Kea/KeaLeaseWatcher.cs b/src/pdns-dhcp/Kea/KeaDhcpLeaseWatcher.cs similarity index 92% rename from src/pdns-dhcp/Kea/KeaLeaseWatcher.cs rename to src/pdns-dhcp/Kea/KeaDhcpLeaseWatcher.cs index 92bf812..116abcc 100644 --- a/src/pdns-dhcp/Kea/KeaLeaseWatcher.cs +++ b/src/pdns-dhcp/Kea/KeaDhcpLeaseWatcher.cs @@ -11,7 +11,8 @@ using pdns_dhcp.Options; namespace pdns_dhcp.Kea; -public abstract class KeaDhcpLeaseWatcher : IHostedService +public sealed class KeaDhcpLeaseWatcher : IHostedService + where T : IKeaDhcpLeaseHandler { private static readonly FileStreamOptions LeaseFileStreamOptions = new() { @@ -23,17 +24,20 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService private readonly Decoder _decoder; private readonly FileSystemWatcher _fsw; + private readonly T _handler; private readonly string _leaseFile; private readonly Pipe _pipe; private Channel? _eventChannel; private Task? _executeTask; private CancellationTokenSource? _stoppingCts; - protected KeaDhcpServerOptions Options { get; } + private KeaDhcpServerOptions Options { get; } - protected KeaDhcpLeaseWatcher(KeaDhcpServerOptions options) + public KeaDhcpLeaseWatcher(KeaDhcpServerOptions options, T handler) { Options = options; + _handler = handler; + var leases = options.Leases.AsSpan(); if (leases.IsWhiteSpace()) { @@ -176,6 +180,7 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService { CountNewLines(_decoder, memory[..read], ref newLinesEncountered, ref awaitLineFeed); writer.Advance(read); + await writer.FlushAsync(stoppingToken); } else { @@ -218,24 +223,23 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService private void OnLeaseChanged(object sender, FileSystemEventArgs e) { - var writer = _eventChannel?.Writer; - if (writer?.TryWrite(e) != false) + if (_eventChannel?.Writer is not { } writer) { return; } -#pragma warning disable CA2012 // Task is awaited immediately. - if (writer.WriteAsync(e) is { IsCompleted: false } task) - { - try - { - task.GetAwaiter().GetResult(); - } - catch { } - } +#pragma warning disable CA2012 + var task = writer.WriteAsync(e, CancellationToken.None); #pragma warning restore + if (task.IsCompleted) + { + return; + } + + task.GetAwaiter().GetResult(); } + private void OnLeaseError(object sender, ErrorEventArgs e) { _eventChannel?.Writer.Complete(e.GetException()); diff --git a/src/pdns-dhcp/Program.cs b/src/pdns-dhcp/Program.cs index 8c0481a..3ea6830 100644 --- a/src/pdns-dhcp/Program.cs +++ b/src/pdns-dhcp/Program.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using pdns_dhcp.Kea; using pdns_dhcp.Options; using pdns_dhcp.PowerDns; using pdns_dhcp.Services; @@ -18,4 +19,7 @@ builder.Services.AddHostedService(); builder.Services.AddTypedFactory(); builder.Services.AddTypedFactory(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); + builder.Build().Run(); diff --git a/src/pdns-dhcp/Services/DhcpLeaseWatcher.cs b/src/pdns-dhcp/Services/DhcpLeaseWatcher.cs index 5ea282a..ee8fb1e 100644 --- a/src/pdns-dhcp/Services/DhcpLeaseWatcher.cs +++ b/src/pdns-dhcp/Services/DhcpLeaseWatcher.cs @@ -3,6 +3,7 @@ using System.Collections.Immutable; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; +using pdns_dhcp.Kea; using pdns_dhcp.Options; namespace pdns_dhcp.Services; diff --git a/src/pdns-dhcp/Services/IDhcpLeaseWatcherFactory.cs b/src/pdns-dhcp/Services/IDhcpLeaseWatcherFactory.cs index 719637e..4d023aa 100644 --- a/src/pdns-dhcp/Services/IDhcpLeaseWatcherFactory.cs +++ b/src/pdns-dhcp/Services/IDhcpLeaseWatcherFactory.cs @@ -7,7 +7,7 @@ namespace pdns_dhcp.Services; public interface IDhcpLeaseWatcherFactory : IRequiresFullProxy { - KeaDhcp4LeaseWatcher KeaDhcp4Watcher(KeaDhcpServerOptions options); + KeaDhcpLeaseWatcher KeaDhcp4Watcher(KeaDhcpServerOptions options); - KeaDhcp6LeaseWatcher KeaDhcp6Watcher(KeaDhcpServerOptions options); + KeaDhcpLeaseWatcher KeaDhcp6Watcher(KeaDhcpServerOptions options); } diff --git a/src/pdns-dhcp/appsettings.Development.json b/src/pdns-dhcp/appsettings.Development.json index f93107d..c9f4ed8 100644 --- a/src/pdns-dhcp/appsettings.Development.json +++ b/src/pdns-dhcp/appsettings.Development.json @@ -5,7 +5,7 @@ "Leases": "../../ext/kea/dhcp4.leases" }, "DHcp6": { - "Leases": "../..ext/kea/dhcp4.leases" + "Leases": "../../ext/kea/dhcp4.leases" } } },