From 9fc737bdc4735ef5157da9ed481f7cb1d45ec7a7 Mon Sep 17 00:00:00 2001 From: AliveDevil Date: Sun, 19 Nov 2023 23:36:11 +0100 Subject: [PATCH] Watcher --- src/pdns-dhcp/Kea/KeaLeaseWatcher.cs | 38 ++++++++++++++++++++++-- src/pdns-dhcp/Options/PowerDnsOptions.cs | 4 +-- src/pdns-dhcp/pdns-dhcp.csproj | 4 +-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/pdns-dhcp/Kea/KeaLeaseWatcher.cs b/src/pdns-dhcp/Kea/KeaLeaseWatcher.cs index 6ea3b9e..7107a46 100644 --- a/src/pdns-dhcp/Kea/KeaLeaseWatcher.cs +++ b/src/pdns-dhcp/Kea/KeaLeaseWatcher.cs @@ -2,22 +2,54 @@ using Microsoft.Extensions.Hosting; using pdns_dhcp.Options; -using Stl.IO; - namespace pdns_dhcp.Kea; public abstract class KeaDhcpLeaseWatcher : BackgroundService { - private readonly FileSystemWatcher fsw; + private readonly FileSystemWatcher _fsw; protected KeaDhcpServerOptions Options { get; } protected KeaDhcpLeaseWatcher(KeaDhcpServerOptions options) { Options = options; + var leases = options.Leases.AsSpan(); + if (leases.IsWhiteSpace()) + { + throw new ArgumentException($"{nameof(options.Leases)} must not be empty.", nameof(options)); + } + + var leaseFile = Path.GetFileName(leases); + var leaseDirectory = Path.GetDirectoryName(leases).ToString(); + if (!Directory.Exists(leaseDirectory)) + { + throw new ArgumentException($"{nameof(options.Leases)} must point to a file in an existing path.", nameof(options)); + } + + _fsw = new(leaseDirectory, leaseFile.ToString()); + _fsw.Changed += OnLeaseChanged; + _fsw.Created += OnLeaseChanged; + _fsw.Deleted += OnLeaseChanged; + _fsw.Renamed += OnLeaseRenamed; + _fsw.Error += OnLeaseError; } protected override Task ExecuteAsync(CancellationToken stoppingToken) + { + return Task.CompletedTask; + } + + private void OnLeaseChanged(object sender, FileSystemEventArgs e) + { + throw new NotImplementedException(); + } + + private void OnLeaseError(object sender, ErrorEventArgs e) + { + throw new NotImplementedException(); + } + + private void OnLeaseRenamed(object sender, RenamedEventArgs e) { throw new NotImplementedException(); } diff --git a/src/pdns-dhcp/Options/PowerDnsOptions.cs b/src/pdns-dhcp/Options/PowerDnsOptions.cs index a93ac29..6873dc7 100644 --- a/src/pdns-dhcp/Options/PowerDnsOptions.cs +++ b/src/pdns-dhcp/Options/PowerDnsOptions.cs @@ -1,8 +1,8 @@ namespace pdns_dhcp.Options; -public class PowerDnsOptions +public class PowerDnsOptions(PowerDnsListenerOptions listener) { - public PowerDnsListenerOptions Listener { get; set; } + public PowerDnsListenerOptions Listener { get; } = listener; } public class PowerDnsListenerOptions(string socket) diff --git a/src/pdns-dhcp/pdns-dhcp.csproj b/src/pdns-dhcp/pdns-dhcp.csproj index 8c83b65..82a08c7 100644 --- a/src/pdns-dhcp/pdns-dhcp.csproj +++ b/src/pdns-dhcp/pdns-dhcp.csproj @@ -13,8 +13,8 @@ - - + + \ No newline at end of file