87 lines
3.6 KiB
C#
87 lines
3.6 KiB
C#
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
|
|
/// <summary>A publication barrier between desired authority and successfully served topology.</summary>
|
|
public sealed class CatalogActivation
|
|
{
|
|
public CatalogActivation(DataWriteDispatchFence? dispatchFence = null) => DispatchFence = dispatchFence ?? new();
|
|
public DataWriteDispatchFence DispatchFence { get; }
|
|
private readonly object _sync = new();
|
|
private CatalogActivationState _current = new(0, "", 0, false);
|
|
|
|
public CatalogActivationState Current => Volatile.Read(ref _current);
|
|
|
|
/// <summary>Capture a token that expires as soon as this exact activation is replaced.</summary>
|
|
public IDataMappedCatalogAuthority CaptureAuthority()
|
|
{
|
|
var current = Current;
|
|
if (!current.IsReady) throw new InvalidOperationException("Catalog is not ready.");
|
|
return new CapturedAuthority(this, current);
|
|
}
|
|
|
|
/// <summary>Create an initially inactive token for a staged, not-yet-published catalog.</summary>
|
|
internal IDataMappedCatalogAuthority CaptureStagedAuthority(long catalogEpoch, long authorityEpoch)
|
|
{
|
|
if (catalogEpoch <= 0 || authorityEpoch <= 0) throw new ArgumentOutOfRangeException(nameof(catalogEpoch));
|
|
return new StagedAuthority(this, catalogEpoch, authorityEpoch);
|
|
}
|
|
|
|
private sealed class StagedAuthority(CatalogActivation owner, long catalogEpoch, long authorityEpoch) : IDataMappedCatalogAuthority
|
|
{
|
|
public long CatalogEpoch => catalogEpoch;
|
|
public long AuthorityEpoch => authorityEpoch;
|
|
public bool IsActive
|
|
{
|
|
get
|
|
{
|
|
var current = owner.Current;
|
|
return current.IsReady && current.CatalogEpoch == catalogEpoch && current.AuthorityEpoch == authorityEpoch;
|
|
}
|
|
}
|
|
}
|
|
|
|
private sealed class CapturedAuthority(CatalogActivation owner, CatalogActivationState captured) : IDataMappedCatalogAuthority
|
|
{
|
|
public long CatalogEpoch => captured.CatalogEpoch;
|
|
public long AuthorityEpoch => captured.AuthorityEpoch;
|
|
public bool IsActive => captured.IsReady && ReferenceEquals(captured, owner.Current);
|
|
}
|
|
|
|
/// <summary>Close access before parsing or applying a new desired revision.</summary>
|
|
public long Begin(string revision)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(revision);
|
|
lock (_sync)
|
|
{
|
|
var epoch = checked(_current.CatalogEpoch + 1);
|
|
using (DispatchFence.Enter()) Volatile.Write(ref _current, new(epoch, revision, 0, false));
|
|
return epoch;
|
|
}
|
|
}
|
|
|
|
public bool AuthorityInstalled(long catalogEpoch, long authorityEpoch)
|
|
{
|
|
if (authorityEpoch <= 0) throw new ArgumentOutOfRangeException(nameof(authorityEpoch));
|
|
lock (_sync)
|
|
{
|
|
if (_current.CatalogEpoch != catalogEpoch) return false;
|
|
using (DispatchFence.Enter()) Volatile.Write(ref _current, _current with { AuthorityEpoch = authorityEpoch, IsReady = false });
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public bool Complete(long catalogEpoch, bool topologySucceeded)
|
|
{
|
|
lock (_sync)
|
|
{
|
|
if (_current.CatalogEpoch != catalogEpoch) return false;
|
|
var ready = topologySucceeded && _current.AuthorityEpoch > 0;
|
|
using (DispatchFence.Enter()) Volatile.Write(ref _current, _current with { IsReady = ready });
|
|
return ready;
|
|
}
|
|
}
|
|
}
|
|
|
|
public sealed record CatalogActivationState(long CatalogEpoch, string Revision, long AuthorityEpoch, bool IsReady);
|