Files
lmxopcua/src/Core/ZB.MOM.WW.OtOpcUa.Core/Hosting/DriverHost.cs
T
Joseph Doherty 64e3fbe035
v2-ci / build (push) Failing after 1m43s
v2-ci / unit-tests (tests/Core/ZB.MOM.WW.OtOpcUa.Cluster.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Security.Tests) (push) Has been skipped
v2-ci / integration (tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests) (push) Has been skipped
v2-ci / integration (tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests) (push) Has been skipped
docs: backfill XML documentation across 756 files
Adds <summary>, <param>, <typeparam>, and <inheritdoc/> tags to public
members surfaced by commentchecker — resolves 5,847 of 5,869 issues
(99.6%) across three /fixdocs passes.
2026-05-28 08:10:17 -04:00

103 lines
4.1 KiB
C#

using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Core.Hosting;
/// <summary>
/// Process-local registry + lifecycle manager for loaded <see cref="IDriver"/> instances
/// (decision #65). Phase 1 scaffold — per-process isolation for Tier C drivers (Galaxy, FOCAS)
/// is implemented in Phase 2 via named-pipe RPC; this class handles in-process drivers today
/// and exposes the same registration interface so the Tier C wrapper can slot in later.
/// </summary>
public sealed class DriverHost : IAsyncDisposable
{
private readonly Dictionary<string, IDriver> _drivers = new();
private readonly object _lock = new();
/// <summary>Gets the collection of registered driver instance identifiers.</summary>
public IReadOnlyCollection<string> RegisteredDriverIds
{
get { lock (_lock) return [.. _drivers.Keys]; }
}
/// <summary>Gets the health status of a registered driver.</summary>
/// <param name="driverInstanceId">The driver instance identifier to query.</param>
public DriverHealth? GetHealth(string driverInstanceId)
{
lock (_lock)
return _drivers.TryGetValue(driverInstanceId, out var d) ? d.GetHealth() : null;
}
/// <summary>
/// Look up a registered driver by instance id. Used by the OPC UA server runtime
/// (<c>OtOpcUaServer</c>) to instantiate one <c>DriverNodeManager</c> per driver at
/// startup. Returns null when the driver is not registered.
/// </summary>
/// <param name="driverInstanceId">The driver instance identifier to look up.</param>
public IDriver? GetDriver(string driverInstanceId)
{
lock (_lock)
return _drivers.TryGetValue(driverInstanceId, out var d) ? d : null;
}
/// <summary>
/// Registers the driver and calls <see cref="IDriver.InitializeAsync"/>. If initialization
/// throws, the driver is kept in the registry so the operator can retry; quality on its
/// nodes will reflect <see cref="DriverState.Faulted"/> until <c>Reinitialize</c> succeeds.
/// </summary>
/// <param name="driver">The driver instance to register.</param>
/// <param name="driverConfigJson">The configuration JSON for the driver.</param>
/// <param name="ct">Cancellation token for the operation.</param>
public async Task RegisterAsync(IDriver driver, string driverConfigJson, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(driver);
var id = driver.DriverInstanceId;
lock (_lock)
{
if (_drivers.ContainsKey(id))
throw new InvalidOperationException($"Driver '{id}' is already registered.");
_drivers[id] = driver;
}
try { await driver.InitializeAsync(driverConfigJson, ct).ConfigureAwait(false); }
catch
{
// Keep the driver registered — operator will see Faulted state and can reinitialize.
throw;
}
}
/// <summary>Unregisters a driver and calls shutdown.</summary>
/// <param name="driverInstanceId">The driver instance identifier to unregister.</param>
/// <param name="ct">Cancellation token for the operation.</param>
public async Task UnregisterAsync(string driverInstanceId, CancellationToken ct)
{
IDriver? driver;
lock (_lock)
{
if (!_drivers.TryGetValue(driverInstanceId, out driver)) return;
_drivers.Remove(driverInstanceId);
}
try { await driver.ShutdownAsync(ct).ConfigureAwait(false); }
catch { /* shutdown is best-effort; logs elsewhere */ }
}
/// <summary>Disposes the driver host and all registered drivers.</summary>
public async ValueTask DisposeAsync()
{
List<IDriver> snapshot;
lock (_lock)
{
snapshot = [.. _drivers.Values];
_drivers.Clear();
}
foreach (var driver in snapshot)
{
try { await driver.ShutdownAsync(CancellationToken.None).ConfigureAwait(false); } catch { /* ignore */ }
(driver as IDisposable)?.Dispose();
}
}
}