Handlers
This commit is contained in:
parent
9b54818d54
commit
9f548cc482
11 changed files with 52 additions and 37 deletions
8
src/pdns-dhcp/Kea/IKeaDhcpLeaseHandler.cs
Normal file
8
src/pdns-dhcp/Kea/IKeaDhcpLeaseHandler.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
using nietras.SeparatedValues;
|
||||||
|
|
||||||
|
namespace pdns_dhcp.Kea;
|
||||||
|
|
||||||
|
public interface IKeaDhcpLeaseHandler
|
||||||
|
{
|
||||||
|
void Handle(in SepReader.Row row);
|
||||||
|
}
|
||||||
8
src/pdns-dhcp/Kea/KeaDhcp4LeaseHandler.cs
Normal file
8
src/pdns-dhcp/Kea/KeaDhcp4LeaseHandler.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
using nietras.SeparatedValues;
|
||||||
|
|
||||||
|
namespace pdns_dhcp.Kea;
|
||||||
|
|
||||||
|
public class KeaDhcp4LeaseHandler : IKeaDhcpLeaseHandler
|
||||||
|
{
|
||||||
|
public void Handle(in SepReader.Row row) { }
|
||||||
|
}
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
using pdns_dhcp.Options;
|
|
||||||
|
|
||||||
namespace pdns_dhcp.Kea;
|
|
||||||
|
|
||||||
public class KeaDhcp4LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
10
src/pdns-dhcp/Kea/KeaDhcp6LeaseHandler.cs
Normal file
10
src/pdns-dhcp/Kea/KeaDhcp6LeaseHandler.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
using nietras.SeparatedValues;
|
||||||
|
|
||||||
|
namespace pdns_dhcp.Kea;
|
||||||
|
|
||||||
|
public class KeaDhcp6LeaseHandler : IKeaDhcpLeaseHandler
|
||||||
|
{
|
||||||
|
public void Handle(in SepReader.Row row)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
using pdns_dhcp.Options;
|
|
||||||
|
|
||||||
namespace pdns_dhcp.Kea;
|
|
||||||
|
|
||||||
public class KeaDhcp6LeaseWatcher(KeaDhcpServerOptions options) : KeaDhcpLeaseWatcher(options)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
namespace pdns_dhcp.Kea;
|
|
||||||
|
|
||||||
public class KeaDhcpLeaseFileReader
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -11,7 +11,8 @@ using pdns_dhcp.Options;
|
||||||
|
|
||||||
namespace pdns_dhcp.Kea;
|
namespace pdns_dhcp.Kea;
|
||||||
|
|
||||||
public abstract class KeaDhcpLeaseWatcher : IHostedService
|
public sealed class KeaDhcpLeaseWatcher<T> : IHostedService
|
||||||
|
where T : IKeaDhcpLeaseHandler
|
||||||
{
|
{
|
||||||
private static readonly FileStreamOptions LeaseFileStreamOptions = new()
|
private static readonly FileStreamOptions LeaseFileStreamOptions = new()
|
||||||
{
|
{
|
||||||
|
|
@ -23,17 +24,20 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService
|
||||||
|
|
||||||
private readonly Decoder _decoder;
|
private readonly Decoder _decoder;
|
||||||
private readonly FileSystemWatcher _fsw;
|
private readonly FileSystemWatcher _fsw;
|
||||||
|
private readonly T _handler;
|
||||||
private readonly string _leaseFile;
|
private readonly string _leaseFile;
|
||||||
private readonly Pipe _pipe;
|
private readonly Pipe _pipe;
|
||||||
private Channel<FileSystemEventArgs>? _eventChannel;
|
private Channel<FileSystemEventArgs>? _eventChannel;
|
||||||
private Task? _executeTask;
|
private Task? _executeTask;
|
||||||
private CancellationTokenSource? _stoppingCts;
|
private CancellationTokenSource? _stoppingCts;
|
||||||
|
|
||||||
protected KeaDhcpServerOptions Options { get; }
|
private KeaDhcpServerOptions Options { get; }
|
||||||
|
|
||||||
protected KeaDhcpLeaseWatcher(KeaDhcpServerOptions options)
|
public KeaDhcpLeaseWatcher(KeaDhcpServerOptions options, T handler)
|
||||||
{
|
{
|
||||||
Options = options;
|
Options = options;
|
||||||
|
_handler = handler;
|
||||||
|
|
||||||
var leases = options.Leases.AsSpan();
|
var leases = options.Leases.AsSpan();
|
||||||
if (leases.IsWhiteSpace())
|
if (leases.IsWhiteSpace())
|
||||||
{
|
{
|
||||||
|
|
@ -176,6 +180,7 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService
|
||||||
{
|
{
|
||||||
CountNewLines(_decoder, memory[..read], ref newLinesEncountered, ref awaitLineFeed);
|
CountNewLines(_decoder, memory[..read], ref newLinesEncountered, ref awaitLineFeed);
|
||||||
writer.Advance(read);
|
writer.Advance(read);
|
||||||
|
await writer.FlushAsync(stoppingToken);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -218,23 +223,22 @@ public abstract class KeaDhcpLeaseWatcher : IHostedService
|
||||||
|
|
||||||
private void OnLeaseChanged(object sender, FileSystemEventArgs e)
|
private void OnLeaseChanged(object sender, FileSystemEventArgs e)
|
||||||
{
|
{
|
||||||
var writer = _eventChannel?.Writer;
|
if (_eventChannel?.Writer is not { } writer)
|
||||||
if (writer?.TryWrite(e) != false)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma warning disable CA2012 // Task is awaited immediately.
|
#pragma warning disable CA2012
|
||||||
if (writer.WriteAsync(e) is { IsCompleted: false } task)
|
var task = writer.WriteAsync(e, CancellationToken.None);
|
||||||
{
|
#pragma warning restore
|
||||||
try
|
if (task.IsCompleted)
|
||||||
{
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
task.GetAwaiter().GetResult();
|
task.GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
#pragma warning restore
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnLeaseError(object sender, ErrorEventArgs e)
|
private void OnLeaseError(object sender, ErrorEventArgs e)
|
||||||
{
|
{
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
|
using pdns_dhcp.Kea;
|
||||||
using pdns_dhcp.Options;
|
using pdns_dhcp.Options;
|
||||||
using pdns_dhcp.PowerDns;
|
using pdns_dhcp.PowerDns;
|
||||||
using pdns_dhcp.Services;
|
using pdns_dhcp.Services;
|
||||||
|
|
@ -18,4 +19,7 @@ builder.Services.AddHostedService<PowerDnsBackend>();
|
||||||
builder.Services.AddTypedFactory<IDhcpLeaseWatcherFactory>();
|
builder.Services.AddTypedFactory<IDhcpLeaseWatcherFactory>();
|
||||||
builder.Services.AddTypedFactory<IPowerDnsFactory>();
|
builder.Services.AddTypedFactory<IPowerDnsFactory>();
|
||||||
|
|
||||||
|
builder.Services.AddTransient<KeaDhcp4LeaseHandler>();
|
||||||
|
builder.Services.AddTransient<KeaDhcp6LeaseHandler>();
|
||||||
|
|
||||||
builder.Build().Run();
|
builder.Build().Run();
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ using System.Collections.Immutable;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
using pdns_dhcp.Kea;
|
||||||
using pdns_dhcp.Options;
|
using pdns_dhcp.Options;
|
||||||
|
|
||||||
namespace pdns_dhcp.Services;
|
namespace pdns_dhcp.Services;
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ namespace pdns_dhcp.Services;
|
||||||
|
|
||||||
public interface IDhcpLeaseWatcherFactory : IRequiresFullProxy
|
public interface IDhcpLeaseWatcherFactory : IRequiresFullProxy
|
||||||
{
|
{
|
||||||
KeaDhcp4LeaseWatcher KeaDhcp4Watcher(KeaDhcpServerOptions options);
|
KeaDhcpLeaseWatcher<KeaDhcp4LeaseHandler> KeaDhcp4Watcher(KeaDhcpServerOptions options);
|
||||||
|
|
||||||
KeaDhcp6LeaseWatcher KeaDhcp6Watcher(KeaDhcpServerOptions options);
|
KeaDhcpLeaseWatcher<KeaDhcp6LeaseHandler> KeaDhcp6Watcher(KeaDhcpServerOptions options);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
"Leases": "../../ext/kea/dhcp4.leases"
|
"Leases": "../../ext/kea/dhcp4.leases"
|
||||||
},
|
},
|
||||||
"DHcp6": {
|
"DHcp6": {
|
||||||
"Leases": "../..ext/kea/dhcp4.leases"
|
"Leases": "../../ext/kea/dhcp4.leases"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue