Run alarm providers natively in the Rust worker

This commit is contained in:
Joseph Doherty
2026-11-21 09:53:37 -05:00
parent dbc5ea0c25
commit 9293c4c913
13 changed files with 1638 additions and 430 deletions
@@ -10,10 +10,12 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.MxGateway.Contracts;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.MxGateway.Server.Alarms;
using ZB.MOM.WW.MxGateway.Server.Configuration;
using ZB.MOM.WW.MxGateway.Server.Grpc;
using ZB.MOM.WW.MxGateway.Server.Metrics;
@@ -597,7 +599,7 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
}
}
/// <summary>Verifies managed alarm provider callbacks forwarded by the Rust worker.</summary>
/// <summary>Verifies the Rust worker’s native subtag alarm provider and standby replacement.</summary>
/// <returns>A task that represents the live test.</returns>
[RustWorkerLiveFact]
public async Task GatewayGrpc_WithRustWorker_ForwardsAlarmProviderCommandsAndEvents()
@@ -738,13 +740,185 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
}
}
private static WebApplication BuildLoopbackGateway(MxAccessGatewayService service)
/// <summary>Raises a development Galaxy alarm and acknowledges it as an authenticated operator through gRPC.</summary>
/// <returns>A task that represents the live test.</returns>
[RustWorkerLiveFact]
public async Task GatewayGrpc_WithRustWorker_StreamsAndAcknowledgesNativeAlarm()
{
const string tag = "TestMachine_001.TestAlarm001";
const string reference = "Galaxy!TestArea." + tag;
string user = Environment.GetEnvironmentVariable("MXGATEWAY_LIVE_MXACCESS_WRITE_SECURED_USER") ?? "Administrator";
string password = Environment.GetEnvironmentVariable("MXGATEWAY_LIVE_MXACCESS_WRITE_SECURED_PASSWORD") ?? string.Empty;
string comment = $"Rust worker smoke {Guid.NewGuid():N}";
string workerPath = IntegrationTestEnvironment.ResolveLiveMxAccessWorkerExecutablePath();
await using GatewayServiceFixture fixture = new(workerPath, new TestWorkerProcessFactory(output), output, new AlarmsOptions
{
Enabled = true,
SubscriptionExpression = $@"\\{Environment.MachineName}\Galaxy!TestArea",
Fallback = new AlarmFallbackOptions { Mode = "Alarmmgr" },
});
await using WebApplication app = BuildLoopbackGateway(fixture.Service, fixture.Monitor);
using CancellationTokenSource deadline = new(TimeSpan.FromSeconds(85));
CancellationToken token = deadline.Token;
await app.StartAsync(token).ConfigureAwait(false);
using GrpcChannel channel = GrpcChannel.ForAddress(app.Urls.Single());
MxAccessGateway.MxAccessGatewayClient client = new(channel);
using AsyncServerStreamingCall<AlarmFeedMessage> feed = client.StreamAlarms(
new StreamAlarmsRequest { AlarmFilterPrefix = reference }, cancellationToken: token);
bool snapshotComplete = false;
bool alarmmgrReady = false;
while ((!snapshotComplete || !alarmmgrReady) && await feed.ResponseStream.MoveNext(token).ConfigureAwait(false))
{
AlarmFeedMessage frame = feed.ResponseStream.Current;
snapshotComplete |= frame.SnapshotComplete;
alarmmgrReady |= frame.ProviderStatus?.Mode == AlarmProviderMode.Alarmmgr;
}
Assert.True(snapshotComplete && alarmmgrReady, "The gateway did not open an Alarmmgr feed.");
OpenSessionReply opened = await client.OpenSessionAsync(new OpenSessionRequest
{
ClientSessionName = "rust-native-alarm-driver",
}, cancellationToken: token).ResponseAsync.ConfigureAwait(false);
Assert.Equal(ProtocolStatusCode.Ok, opened.ProtocolStatus.Code);
string sessionId = opened.SessionId;
async Task<MxCommandReply> InvokeAsync(MxCommandRequest request, CancellationToken cancellationToken)
{
MxCommandReply reply = await client.InvokeAsync(request, deadline: DateTime.UtcNow.AddSeconds(15),
cancellationToken: cancellationToken).ResponseAsync.ConfigureAwait(false);
Assert.True(reply.ProtocolStatus.Code == ProtocolStatusCode.Ok,
$"{reply.Kind} returned {reply.ProtocolStatus.Code}: {reply.DiagnosticMessage}");
return reply;
}
async Task<OnAlarmTransitionEvent> WaitForTransitionAsync(AlarmTransitionKind expected, string? ackComment = null)
{
while (await feed.ResponseStream.MoveNext(token).ConfigureAwait(false))
{
OnAlarmTransitionEvent? transition = feed.ResponseStream.Current.Transition;
if (transition?.AlarmFullReference == reference && transition.TransitionKind == expected
&& (ackComment is null || transition.OperatorComment == ackComment))
{
Assert.Equal(AlarmProviderMode.Alarmmgr, transition.SourceProvider);
Assert.False(transition.Degraded);
return transition;
}
}
throw new InvalidOperationException($"Alarm stream ended before the {expected} transition.");
}
MxCommandRequest? restore = null;
try
{
int server = (await InvokeAsync(CreateRegisterRequest(sessionId), token).ConfigureAwait(false)).Register.ServerHandle;
MxCommandReply read = await InvokeAsync(new MxCommandRequest
{
SessionId = sessionId,
Command = new MxCommand
{
Kind = MxCommandKind.ReadBulk,
ReadBulk = new ReadBulkCommand { ServerHandle = server, TimeoutMs = 5000, TagAddresses = { tag } },
},
}, token).ConfigureAwait(false);
BulkReadResult original = Assert.Single(read.ReadBulk.Results);
Assert.True(original.WasSuccessful, original.ErrorMessage);
Assert.Equal(MxValue.KindOneofCase.BoolValue, original.Value.KindCase);
MxCommandRequest add = CreateAddItemRequest(sessionId, server);
add.Command.AddItem.ItemDefinition = tag;
int item = (await InvokeAsync(add, token).ConfigureAwait(false)).AddItem.ItemHandle;
await InvokeAsync(CreateAdviseRequest(sessionId, server, item), token).ConfigureAwait(false);
int userId = (await InvokeAsync(CreateAuthenticateUserRequest(sessionId, server, user, password), token)
.ConfigureAwait(false)).AuthenticateUser.UserId;
Assert.NotEqual(0, userId);
MxCommandRequest WriteAlarm(bool active)
{
MxCommandRequest request = CreateWriteSecuredRequest(sessionId, server, item, userId, verifierUserId: 0);
request.Command.WriteSecured.Value = new MxValue { DataType = MxDataType.Boolean, BoolValue = active };
return request;
}
async Task WriteConfirmedAsync(MxCommandRequest request, CancellationToken cancellationToken)
{
MxCommandReply reply = await InvokeAsync(request, cancellationToken).ConfigureAwait(false);
Assert.NotEmpty(reply.Statuses);
Assert.All(reply.Statuses, status => Assert.Equal(MxStatusCategory.Ok, status.Category));
}
// The dev rig’s test UDA is normally false. Clear an existing test episode before raising a fresh one.
if (original.Value.BoolValue)
{
await WriteConfirmedAsync(WriteAlarm(false), token).ConfigureAwait(false);
restore = WriteAlarm(true);
await WaitForTransitionAsync(AlarmTransitionKind.Clear).ConfigureAwait(false);
}
await WriteConfirmedAsync(WriteAlarm(true), token).ConfigureAwait(false);
restore = WriteAlarm(original.Value.BoolValue);
await WaitForTransitionAsync(AlarmTransitionKind.Raise).ConfigureAwait(false);
AcknowledgeAlarmReply rejected = await client.AcknowledgeAlarmAsync(new AcknowledgeAlarmRequest
{
AlarmFullReference = reference,
OperatorUser = $"mxgw-smoke-{Guid.NewGuid():N}",
Comment = "This acknowledgement must be rejected",
}, cancellationToken: token).ResponseAsync.ConfigureAwait(false);
Assert.Equal(ProtocolStatusCode.MxaccessFailure, rejected.ProtocolStatus.Code);
Assert.True(rejected.HasHresult && rejected.Hresult != 0);
AcknowledgeAlarmReply ack = await client.AcknowledgeAlarmAsync(new AcknowledgeAlarmRequest
{
AlarmFullReference = reference, OperatorUser = user, OperatorPassword = password, Comment = comment,
}, cancellationToken: token).ResponseAsync.ConfigureAwait(false);
Assert.True(ack.ProtocolStatus.Code == ProtocolStatusCode.Ok, ack.DiagnosticMessage);
Assert.True(ack.HasHresult);
Assert.Equal(0, ack.Hresult);
Assert.DoesNotContain("unconfirmed", ack.DiagnosticMessage, StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("not observed", ack.DiagnosticMessage, StringComparison.OrdinalIgnoreCase);
OnAlarmTransitionEvent acknowledged = await WaitForTransitionAsync(AlarmTransitionKind.Acknowledge, comment)
.ConfigureAwait(false);
Assert.Equal(user, acknowledged.OperatorUser, ignoreCase: true);
output.WriteLine($"Alarmmgr streamed Raise and Acknowledge with operator {acknowledged.OperatorUser} and the requested comment.");
if (!original.Value.BoolValue)
{
await WriteConfirmedAsync(restore, token).ConfigureAwait(false);
restore = null;
await WaitForTransitionAsync(AlarmTransitionKind.Clear).ConfigureAwait(false);
output.WriteLine("The gateway streamed Clear after the test UDA was restored.");
}
}
finally
{
try
{
if (restore is not null)
{
MxCommandReply restored = await InvokeAsync(restore, CancellationToken.None).ConfigureAwait(false);
Assert.NotEmpty(restored.Statuses);
Assert.All(restored.Statuses, status => Assert.Equal(MxStatusCategory.Ok, status.Category));
}
}
finally
{
await client.CloseSessionAsync(new CloseSessionRequest { SessionId = sessionId },
deadline: DateTime.UtcNow.AddSeconds(25)).ResponseAsync.ConfigureAwait(false);
}
}
}
private static WebApplication BuildLoopbackGateway(MxAccessGatewayService service, GatewayAlarmMonitor? monitor = null)
{
WebApplicationBuilder builder = WebApplication.CreateSlimBuilder();
builder.WebHost.ConfigureKestrel(options =>
options.Listen(IPAddress.Loopback, 0, listener => listener.Protocols = HttpProtocols.Http2));
builder.Services.AddGrpc();
builder.Services.AddSingleton(service);
if (monitor is not null)
{
builder.Services.AddSingleton<IHostedService>(monitor);
}
WebApplication app = builder.Build();
app.MapGrpcService<MxAccessGatewayService>();
return app;
@@ -2732,12 +2906,14 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
/// <param name="workerExecutablePath">Path to the worker process executable.</param>
/// <param name="processFactory">Factory for creating worker processes.</param>
/// <param name="output">Test output helper for logging.</param>
/// <param name="alarms">Optional configuration for the central alarm monitor.</param>
public GatewayServiceFixture(
string workerExecutablePath,
IWorkerProcessFactory processFactory,
ITestOutputHelper output)
ITestOutputHelper output,
AlarmsOptions? alarms = null)
{
IOptions<GatewayOptions> options = Options.Create(CreateOptions(workerExecutablePath));
IOptions<GatewayOptions> options = Options.Create(CreateOptions(workerExecutablePath, alarms));
_loggerFactory = LoggerFactory.Create(builder => builder.AddProvider(new TestOutputLoggerProvider(output)));
WorkerProcessLauncher launcher = new(
options,
@@ -2762,6 +2938,9 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
options,
_metrics);
Monitor = new GatewayAlarmMonitor(
sessionManager, EmptyAlarmWatchListResolver.Instance, _metrics, options,
_loggerFactory.CreateLogger<GatewayAlarmMonitor>());
Service = new MxAccessGatewayService(
sessionManager,
new GatewayRequestIdentityAccessor(),
@@ -2771,12 +2950,7 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
eventStreamService,
_metrics,
_loggerFactory.CreateLogger<MxAccessGatewayService>(),
new ZB.MOM.WW.MxGateway.Server.Alarms.GatewayAlarmMonitor(
sessionManager,
EmptyAlarmWatchListResolver.Instance,
_metrics,
options,
_loggerFactory.CreateLogger<ZB.MOM.WW.MxGateway.Server.Alarms.GatewayAlarmMonitor>()));
Monitor);
}
/// <summary>
@@ -2784,6 +2958,9 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
/// </summary>
public MxAccessGatewayService Service { get; }
/// <summary>The central alarm monitor for tests of the public alarm RPCs.</summary>
public GatewayAlarmMonitor Monitor { get; }
/// <summary>The manager used by the gateway's internal alarm monitor.</summary>
public SessionManager Manager { get; }
@@ -2807,19 +2984,22 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
/// <returns>A task that represents the asynchronous operation.</returns>
public async ValueTask DisposeAsync()
{
await Monitor.StopAsync(CancellationToken.None).ConfigureAwait(false);
foreach (GatewaySession session in _registry.Snapshot())
{
await session.DisposeAsync().ConfigureAwait(false);
}
Monitor.Dispose();
_loggerFactory.Dispose();
_metrics.Dispose();
}
private static GatewayOptions CreateOptions(string workerExecutablePath)
private static GatewayOptions CreateOptions(string workerExecutablePath, AlarmsOptions? alarms)
{
return new GatewayOptions
{
Alarms = alarms ?? new AlarmsOptions(),
Worker = new WorkerOptions
{
ExecutablePath = workerExecutablePath,
@@ -2833,7 +3013,7 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output)
Sessions = new SessionOptions
{
DefaultCommandTimeoutSeconds = 15,
MaxSessions = 1,
MaxSessions = alarms?.Enabled == true ? 2 : 1,
},
Events = new EventOptions
{
+42 -11
View File
@@ -17,6 +17,12 @@ version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "autocfg"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
[[package]]
name = "bitflags"
version = "2.13.2"
@@ -35,6 +41,15 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
[[package]]
name = "chrono"
version = "0.4.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327"
dependencies = [
"num-traits",
]
[[package]]
name = "either"
version = "1.19.0"
@@ -156,13 +171,25 @@ checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084"
name = "mxgateway-worker-rust"
version = "0.1.0"
dependencies = [
"chrono",
"prost",
"prost-build",
"prost-types",
"roxmltree",
"tokio",
"uuid",
"windows-sys",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.4"
@@ -300,6 +327,15 @@ version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d902046e6a28a1e4c3cab97258a0ce11e994903f01c2d97a9447a0b3df85f4f6"
[[package]]
name = "roxmltree"
version = "0.21.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9da7f59328b09405b181fee1018925fad50f157afb43c0c0f5c522c256dd5907"
dependencies = [
"memchr",
]
[[package]]
name = "rustix"
version = "1.1.6"
@@ -313,16 +349,6 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
dependencies = [
"errno",
"libc",
]
[[package]]
name = "socket2"
version = "0.6.5"
@@ -378,7 +404,6 @@ dependencies = [
"libc",
"mio",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys",
@@ -401,6 +426,12 @@ version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
[[package]]
name = "uuid"
version = "1.29.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c14b15dffa2f4bd712103e857140aa04a4f2ff6e955c77d7eb8e53ee02a3b64"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
+4 -4
View File
@@ -10,16 +10,16 @@ prost-types = "0.13.5"
tokio = { version = "1.48", features = ["io-util"] }
[target.'cfg(windows)'.dependencies]
tokio = { version = "1.48", features = ["macros", "net", "process", "rt", "sync", "time"] }
tokio = { version = "1.48", features = ["macros", "net", "rt", "sync", "time"] }
chrono = { version = "0.4", default-features = false, features = ["std"] }
roxmltree = "0.21"
uuid = { version = "1", default-features = false }
windows-sys = { version = "0.61", features = [
"Win32_Foundation",
"Win32_Globalization",
"Win32_Security",
"Win32_Security_Cryptography",
"Win32_System_Com",
"Win32_System_JobObjects",
"Win32_System_Ole",
"Win32_System_Pipes",
"Win32_System_Threading",
"Win32_System_Time",
"Win32_System_Variant",
+23 -22
View File
@@ -38,26 +38,20 @@ by MXAccess. The event also flows through StreamEvents. Bulk results report
per-item COM acceptance and clients can observe commit outcomes on the event
stream.
## Managed alarm provider
## Native alarm provider
The Rust process owns the gateway session, native data operations, and event
sequencing. Alarm commands run in the existing x86 .NET Framework worker, which
owns the WnWrap alarm manager, subtag fallback, watch-list replacement, alarm
snapshots, and acknowledgement. The managed process starts on the first alarm
command. It connects to a private local pipe whose client PID and launch nonce
are checked. A Windows job ties the managed process to the Rust worker's
lifetime.
Build the C# worker with the matching repository revision and deliver its
entire output directory under `alarm-worker` beside the Rust executable. Keep
the managed executable named `ZB.MOM.WW.MxGateway.Worker.exe`, or set
`MXGATEWAY_RUST_ALARM_WORKER_EXE` to its absolute path in the gateway process
environment.
```powershell
dotnet build src/ZB.MOM.WW.MxGateway.Worker -c Release -p:Platform=x86
```
The Rust worker handles alarm subscription, active snapshots, standby watch-list
replacement, and authenticated acknowledgement in the worker process. It uses
the installed 32-bit AVEVA WnWrap COM alarm consumer for Alarmmgr and MXAccess
advice for the optional subtag fallback. The worker polls WnWrap on its COM
thread, emits changes on the existing event queue, and retains previously seen
records that a capped snapshot may have omitted.
To acknowledge a Galaxy alarm, the worker authenticates the operator with
MXAccess, writes the comment to the alarm's `AckMsg` attribute, and waits for
`OnWriteComplete` within the configured write-completion window. Deploy the
Rust executable on a Windows host with the System Platform COM components
registered. The Rust worker has no .NET worker companion.
## Build and test
@@ -109,12 +103,17 @@ $env:MXGATEWAY_LIVE_MXACCESS_ITEM = 'TestChildObject.TestInt'
dotnet test src/ZB.MOM.WW.MxGateway.IntegrationTests --filter FullyQualifiedName~GatewayGrpc_WithRustWorker_WritesAndReceivesOnWriteComplete
```
The remaining parity smoke tests use the deployed `TestChangingInt` items
for bulk snapshots and a read-only, synthetic subtag alarm. Set the absolute
managed worker path before running the alarm test.
The bulk and subtag tests use the deployed `TestChangingInt` items. The
Alarmmgr test raises `Galaxy!TestArea.TestMachine_001.TestAlarm001`, checks that
the gateway rejects an unknown operator, and verifies the authenticated
acknowledgement and its operator attribution on `StreamAlarms`. It restores the
test UDA afterward. The development rig uses `Administrator` with an empty
password. The test accepts credentials through
`MXGATEWAY_LIVE_MXACCESS_WRITE_SECURED_USER` and
`MXGATEWAY_LIVE_MXACCESS_WRITE_SECURED_PASSWORD`.
```powershell
dotnet test src/ZB.MOM.WW.MxGateway.IntegrationTests --filter 'FullyQualifiedName~GatewayGrpc_WithRustWorker_BulkItemsAndReads|FullyQualifiedName~GatewayGrpc_WithRustWorker_ForwardsAlarmProviderCommandsAndEvents'
dotnet test src/ZB.MOM.WW.MxGateway.IntegrationTests --filter 'FullyQualifiedName~GatewayGrpc_WithRustWorker_'
```
## COM interface evidence
@@ -137,3 +136,5 @@ the corresponding native calls and event callbacks.
`ev_1b4fe7ab436437391b76f9fa3a300f45b58a4e1cff6aa84e30854f653c0a20df`
- REA extended COM and buffered event member evidence
`ev_39de39d626e3a933b259fa2650b42311ba60d6844d7b012284467284e8b82977`
- REA member evidence for the WnWrap v1 lifecycle and v2 XML snapshot interfaces
`ev_4fd97bf483b1cde2544b93a90f4de97569ea95c24f1eaa8aee49b5a1ed552558`
+464 -297
View File
@@ -1,340 +1,507 @@
//! The existing managed alarm provider owns WnWrap, fallback subscriptions, and
//! acknowledgement. This bridge carries its alarm RPCs and callbacks over the
//! worker protocol while the Rust process owns the gateway session and sequencing.
mod ack;
mod record;
mod subtag;
mod wnwrap;
use std::io;
use std::mem::size_of;
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
use std::path::PathBuf;
use std::ptr::{null, null_mut};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::time::Duration;
use std::collections::BTreeMap;
use std::time::{Duration, Instant, SystemTime};
use tokio::io::{split, WriteHalf};
use tokio::net::windows::named_pipe::{NamedPipeServer, ServerOptions};
use tokio::process::{Child, Command};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tokio::time::timeout;
use windows_sys::Win32::Security::Cryptography::{
BCryptGenRandom, BCRYPT_USE_SYSTEM_PREFERRED_RNG,
};
use windows_sys::Win32::System::JobObjects::{
AssignProcessToJobObject, CreateJobObjectW, JobObjectExtendedLimitInformation,
SetInformationJobObject, JOBOBJECT_EXTENDED_LIMIT_INFORMATION,
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
};
use windows_sys::Win32::System::Pipes::GetNamedPipeClientProcessId;
use uuid::Uuid;
use windows_sys::core::HRESULT;
use windows_sys::Win32::Foundation::E_FAIL;
use windows_sys::Win32::System::Threading::INFINITE;
use crate::mxaccess::{CommandError, CommandResult, Outcome, Proxy};
use crate::mxevent::{EventFault, Publisher};
use crate::wire::{
self,
mxaccess_gateway::v1::{
MxCommand, MxCommandKind, MxCommandReply, MxEventFamily, ProtocolStatusCode,
},
mxaccess_worker::v1::{GatewayHello, WorkerCommand, WorkerShutdown},
Body, WorkerEnvelope, Writer, DEFAULT_MAX, VERSION,
use crate::wire::mxaccess_gateway::v1::{
mx_command::Payload, mx_command_reply::Payload as Reply, mx_event,
AcknowledgeAlarmReplyPayload, AlarmFailoverConfig, AlarmProviderMode as Mode, MxCommand,
MxCommandKind as Kind, MxCommandReply, MxEvent, MxEventFamily, OnAlarmProviderModeChangedEvent,
ProtocolStatus, ProtocolStatusCode as Code, QueryActiveAlarmsReplyPayload,
SubscribeAlarmsCommand,
};
use record::{parse_snapshot, Record, State};
use subtag::Subtags;
use wnwrap::WnWrap;
const ALARM_WORKER_ENV: &str = "MXGATEWAY_RUST_ALARM_WORKER_EXE";
pub fn handles(kind: MxCommandKind) -> bool {
pub fn handles(kind: Kind) -> bool {
matches!(
kind,
MxCommandKind::SubscribeAlarms
| MxCommandKind::UnsubscribeAlarms
| MxCommandKind::UpdateAlarmWatchList
| MxCommandKind::QueryActiveAlarms
| MxCommandKind::AcknowledgeAlarm
| MxCommandKind::AcknowledgeAlarmByName
Kind::SubscribeAlarms
| Kind::UnsubscribeAlarms
| Kind::UpdateAlarmWatchList
| Kind::QueryActiveAlarms
| Kind::AcknowledgeAlarm
| Kind::AcknowledgeAlarmByName
)
}
pub struct AlarmWorker {
child: Child,
_job: OwnedHandle,
writer: Writer<WriteHalf<NamedPipeServer>>,
replies: mpsc::Receiver<WorkerEnvelope>,
reader: JoinHandle<()>,
shutting_down: Arc<AtomicBool>,
struct AlarmError {
code: Code,
hr: Option<HRESULT>,
message: String,
}
impl AlarmWorker {
pub async fn start(session: &str, max: usize, publisher: Publisher) -> io::Result<Self> {
let executable = alarm_executable()?;
let pipe_name = format!("mxgw-rust-alarm-{}", random_token()?);
let nonce = random_token()?;
let pipe = ServerOptions::new()
.first_pipe_instance(true)
.reject_remote_clients(true)
.create(format!(r"\\.\pipe\{pipe_name}"))?;
let job = kill_on_close_job()?;
let mut command = Command::new(&executable);
if let Some(directory) = executable.parent() {
command.current_dir(directory);
}
let child = command
.args([
"--session-id",
session,
"--pipe-name",
&pipe_name,
"--protocol-version",
"1",
])
.env("MXGATEWAY_WORKER_NONCE", &nonce)
.kill_on_drop(true)
.spawn()?;
let pid = child.id().ok_or_else(disconnected)?;
let child_handle = child.raw_handle().ok_or_else(disconnected)?;
// SAFETY: both handles remain live and the new job is owned by this worker.
if unsafe { AssignProcessToJobObject(job.as_raw_handle(), child_handle) } == 0 {
return Err(io::Error::last_os_error());
impl AlarmError {
fn invalid(message: &str) -> Self {
Self {
code: Code::InvalidRequest,
hr: Some(-1),
message: message.into(),
}
}
let (mut reader, writer) = timeout(Duration::from_secs(15), async {
pipe.connect().await?;
let mut connected_pid = 0;
if unsafe { GetNamedPipeClientProcessId(pipe.as_raw_handle(), &mut connected_pid) } == 0
{
return Err(io::Error::last_os_error());
fn failed(step: &str, hr: HRESULT) -> Self {
Self {
code: Code::MxaccessFailure,
hr: Some(hr),
message: format!("{step} failed (HRESULT 0x{:08X})", hr as u32),
}
}
fn unavailable(message: &str) -> Self {
Self {
code: Code::MxaccessFailure,
hr: None,
message: message.into(),
}
}
fn com(step: &str, error: CommandError) -> Self {
Self::failed(
step,
match error {
CommandError::HResult(hr) => hr,
_ => E_FAIL,
},
)
}
}
fn handle(result: Outcome, step: &str) -> Result<i32, AlarmError> {
match result.map_err(|error| AlarmError::com(step, error))? {
CommandResult::Handle(value) => Ok(value),
_ => unreachable!("handle-returning command"),
}
}
struct Primary {
expression: String,
consumer: Option<WnWrap>,
records: BTreeMap<Uuid, Record>,
truncated: bool,
}
impl Primary {
fn poll(&mut self, proxy: &Proxy, limit: i32, emit: bool) -> Result<(), HRESULT> {
if self.consumer.is_none() {
self.consumer = Some(WnWrap::subscribe(&self.expression)?);
}
let xml = self.consumer.as_ref().unwrap().snapshot(limit)?;
if xml.is_empty() {
return Ok(());
}
let (records, count) = parse_snapshot(&xml).map_err(|_| E_FAIL)?;
let previous = std::mem::replace(&mut self.records, records);
self.truncated = count >= limit as usize;
if emit {
for (&id, record) in &self.records {
let prior = previous
.get(&id)
.map_or(State::Unknown, |record| record.state);
publish_record(&proxy.publisher, record, prior, Mode::Alarmmgr);
}
if connected_pid != pid {
return Err(invalid(
"Unexpected process connected to the alarm worker pipe",
));
}
if self.truncated {
for (id, record) in previous {
self.records.entry(id).or_insert(record);
}
let (mut reader, writer) = split(pipe);
let mut writer = Writer::new(writer);
writer
.send(
session,
"",
Body::GatewayHello(GatewayHello {
supported_protocol_version: VERSION,
nonce: nonce.clone(),
max_frame_bytes: max as u32,
gateway_version: env!("CARGO_PKG_VERSION").into(),
}),
DEFAULT_MAX,
)
.await?;
let hello = wire::read(&mut reader, session, DEFAULT_MAX).await?;
match hello.body {
Some(Body::WorkerHello(hello))
if hello.protocol_version == VERSION
&& hello.worker_process_id == pid as i32
&& hello.nonce == nonce => {}
_ => return Err(invalid("Alarm worker handshake failed")),
}
Ok(())
}
}
struct Subscription {
mode: Mode,
primary: Option<Primary>,
subtags: Option<Subtags>,
automatic: bool,
next_probe: Instant,
failures: i32,
stable_probes: i32,
settings: AlarmFailoverConfig,
}
pub struct AlarmRuntime {
subscription: Option<Subscription>,
interval: Duration,
next_poll: Instant,
limit: i32,
}
impl AlarmRuntime {
pub fn new() -> Self {
Self {
subscription: None,
interval: Duration::from_millis(setting(
"MXGATEWAY_ALARM_POLL_INTERVAL_MS",
500,
100..=3_600_000,
) as u64),
next_poll: Instant::now(),
limit: setting("MXGATEWAY_ALARM_MAX_ALARMS_PER_FETCH", 1024, 64..=65_536),
}
}
pub fn wait_millis(&self) -> u32 {
if self.subscription.is_none() {
return INFINITE;
}
let remaining = self.next_poll.saturating_duration_since(Instant::now());
remaining.as_nanos().div_ceil(1_000_000) as u32
}
pub fn close(&mut self, proxy: &mut Proxy) {
if let Some(mut subscription) = self.subscription.take() {
if let Some(subtags) = &mut subscription.subtags {
subtags.close(proxy);
}
let ready = wire::read(&mut reader, session, max).await?;
match ready.body {
Some(Body::WorkerReady(ready)) if ready.worker_process_id == pid as i32 => {
Ok((reader, writer))
}
}
pub fn poll(&mut self, proxy: &Proxy) {
let Some(subscription) = &mut self.subscription else {
return;
};
if let Some(subtags) = &mut subscription.subtags {
subtags.drain(proxy, subscription.mode == Mode::Subtag);
}
let now = Instant::now();
if now < self.next_poll {
return;
}
self.next_poll = now + self.interval;
let Some(primary) = &mut subscription.primary else {
return;
};
let probe_interval =
Duration::from_secs(subscription.settings.failback_probe_interval_seconds as u64);
match subscription.mode {
Mode::Alarmmgr => match primary.poll(proxy, self.limit, true) {
Ok(()) => subscription.failures = 0,
Err(hr) if subscription.automatic => {
subscription.failures += 1;
if subscription.failures >= subscription.settings.consecutive_failure_threshold
{
subscription.mode = Mode::Subtag;
subscription.next_probe = now + probe_interval;
subscription.stable_probes = 0;
publish_mode(&proxy.publisher, Mode::Subtag, "primary poll failed", hr);
}
}
Err(hr) => {
subscription.failures += 1;
if subscription.failures >= 3 {
proxy.publisher.report_fault(EventFault::AlarmPoll(hr));
}
}
},
Mode::Subtag if now >= subscription.next_probe => {
subscription.next_probe = now + probe_interval;
if primary.poll(proxy, self.limit, false).is_ok() {
subscription.stable_probes += 1;
if subscription.stable_probes >= subscription.settings.failback_stable_probes {
subscription.mode = Mode::Alarmmgr;
subscription.failures = 0;
publish_mode(&proxy.publisher, Mode::Alarmmgr, "recovered", 0);
}
} else {
subscription.stable_probes = 0;
}
_ => Err(invalid("Managed alarm worker failed to initialize")),
}
})
.await
.map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "Alarm worker startup timed out"))??;
_ => {}
}
}
let (incoming, replies) = mpsc::channel(1);
let reader_session = session.to_owned();
let shutting_down = Arc::new(AtomicBool::new(false));
let closing = Arc::clone(&shutting_down);
let reader = tokio::spawn(async move {
while let Ok(frame) = wire::read(&mut reader, &reader_session, max).await {
match frame.body.as_ref() {
Some(Body::WorkerEvent(event)) => {
if let Some(event) = event.event.as_ref() {
if matches!(
event.family(),
MxEventFamily::OnAlarmTransition
| MxEventFamily::OnAlarmProviderModeChanged
) {
publisher.publish(event.clone());
fn subscribe(
&mut self,
proxy: &mut Proxy,
request: SubscribeAlarmsCommand,
) -> Result<(), AlarmError> {
if self.subscription.is_some() {
return Err(AlarmError::unavailable(
"Alarm provider is already subscribed",
));
}
let mode = Mode::try_from(request.forced_mode)
.map_err(|_| AlarmError::invalid("Invalid alarm provider mode"))?;
if request.subscription_expression.trim().is_empty()
|| request.subscription_expression.contains('\0')
{
return Err(AlarmError::invalid(
"A valid alarm subscription expression is required",
));
}
let automatic = mode == Mode::Unspecified
&& (request.fallback_enabled || !request.watch_list.is_empty());
let settings = request.failover.unwrap_or(AlarmFailoverConfig {
consecutive_failure_threshold: 3,
failback_probe_interval_seconds: 30,
failback_stable_probes: 3,
});
if automatic
&& (settings.consecutive_failure_threshold < 1
|| settings.failback_probe_interval_seconds < 1
|| settings.failback_stable_probes < 1)
{
return Err(AlarmError::invalid(
"Alarm failover settings must be positive",
));
}
let mut subscription = Subscription {
mode: if mode == Mode::Subtag {
Mode::Subtag
} else {
Mode::Alarmmgr
},
primary: (mode != Mode::Subtag).then(|| Primary {
expression: request.subscription_expression,
consumer: None,
records: BTreeMap::new(),
truncated: false,
}),
subtags: None,
automatic,
next_probe: Instant::now(),
failures: 0,
stable_probes: 0,
settings,
};
let mut initial_failure = None;
if let Some(primary) = &mut subscription.primary {
match WnWrap::subscribe(&primary.expression) {
Ok(consumer) => primary.consumer = Some(consumer),
Err(hr) if automatic => {
subscription.failures = 1;
initial_failure = Some(hr);
}
Err(hr) => return Err(AlarmError::failed("WnWrap subscribe", hr)),
}
}
if automatic || mode == Mode::Subtag {
subscription.subtags = Some(Subtags::attach(proxy, request.watch_list)?);
}
if let Some(hr) =
initial_failure.filter(|_| subscription.settings.consecutive_failure_threshold == 1)
{
subscription.mode = Mode::Subtag;
subscription.next_probe = Instant::now()
+ Duration::from_secs(subscription.settings.failback_probe_interval_seconds as u64);
publish_mode(
&proxy.publisher,
Mode::Subtag,
"primary subscribe failed",
hr,
);
}
self.subscription = Some(subscription);
self.next_poll = Instant::now();
Ok(())
}
pub fn execute(&mut self, proxy: &mut Proxy, command: MxCommand) -> MxCommandReply {
let mut reply = MxCommandReply {
kind: command.kind,
hresult: Some(0),
..Default::default()
};
let result = self.dispatch(proxy, command, &mut reply);
let (code, message) = match result {
Ok(()) => (Code::Ok, std::mem::take(&mut reply.diagnostic_message)),
Err(error) => {
reply.hresult = error.hr;
if matches!(
reply.kind(),
Kind::AcknowledgeAlarm | Kind::AcknowledgeAlarmByName
) {
reply.payload = Some(Reply::AcknowledgeAlarm(AcknowledgeAlarmReplyPayload {
native_status: error.hr.unwrap_or(-1),
}));
}
(error.code, error.message)
}
};
reply.protocol_status = Some(ProtocolStatus {
code: code as i32,
message: message.clone(),
});
reply.diagnostic_message = message;
reply
}
fn dispatch(
&mut self,
proxy: &mut Proxy,
command: MxCommand,
reply: &mut MxCommandReply,
) -> Result<(), AlarmError> {
match (command.kind(), command.payload) {
(Kind::SubscribeAlarms, Some(Payload::SubscribeAlarms(request))) => {
self.subscribe(proxy, request)
}
(Kind::UnsubscribeAlarms, Some(Payload::UnsubscribeAlarms(_))) => {
self.close(proxy);
Ok(())
}
(Kind::UpdateAlarmWatchList, Some(Payload::UpdateAlarmWatchList(request))) => {
let subscription = self.subscription.as_mut().filter(|subscription| subscription.automatic)
.ok_or_else(|| AlarmError::unavailable("Alarm watch list replacement requires an automatic fallback subscription"))?;
let replacement = Subtags::attach(proxy, request.watch_list)?;
if let Some(mut old) = subscription.subtags.replace(replacement) {
old.close(proxy);
}
Ok(())
}
(Kind::QueryActiveAlarms, Some(Payload::QueryActiveAlarmsCommand(request))) => {
let subscription = self
.subscription
.as_ref()
.ok_or_else(|| AlarmError::unavailable("Alarm provider is not subscribed"))?;
let mut result = QueryActiveAlarmsReplyPayload::default();
{
let mut append = |record: &Record, truncated| {
if record.state.active()
&& record.reference().starts_with(&request.alarm_filter_prefix)
{
result
.snapshots
.push(record.snapshot(subscription.mode, truncated));
}
};
if subscription.mode == Mode::Subtag {
if let Some(subtags) = &subscription.subtags {
for record in subtags.records() {
append(record, false);
}
}
}
Some(Body::WorkerHeartbeat(_)) => {}
Some(Body::WorkerCommandReply(_) | Body::WorkerShutdownAck(_)) => {
if incoming.send(frame).await.is_err() {
break;
} else if let Some(primary) = &subscription.primary {
for record in primary.records.values() {
append(record, primary.truncated);
}
result.snapshot_truncated = primary.truncated;
}
_ => break,
}
reply.payload = Some(Reply::QueryActiveAlarms(result));
Ok(())
}
if !closing.load(Ordering::Acquire) {
publisher.report_fault(EventFault::AlarmDisconnected);
(Kind::AcknowledgeAlarm, Some(Payload::AcknowledgeAlarmCommand(request))) => {
let id = Uuid::parse_str(&request.alarm_guid)
.map_err(|_| AlarmError::invalid("Invalid alarm GUID"))?;
let address = self.ack_address(Some(id), None)?;
ack::acknowledge(
proxy,
address,
request.operator_user,
request.operator_domain,
request.operator_password,
request.comment,
reply,
)
}
});
Ok(Self {
child,
_job: job,
writer,
replies,
reader,
shutting_down,
})
}
pub async fn invoke(
&mut self,
session: &str,
max: usize,
correlation: &str,
command: MxCommand,
) -> io::Result<MxCommandReply> {
let kind = command.kind;
self.writer
.send(
session,
correlation,
Body::WorkerCommand(WorkerCommand {
command: Some(command),
enqueue_timestamp: Some(std::time::SystemTime::now().into()),
}),
max,
)
.await?;
let frame = self.replies.recv().await.ok_or_else(disconnected)?;
match frame.body {
Some(Body::WorkerCommandReply(response)) if frame.correlation_id == correlation => {
response
.reply
.filter(|reply| {
reply.session_id == session
&& reply.correlation_id == correlation
&& reply.kind == kind
})
.ok_or_else(|| invalid("Alarm worker reply did not match the request"))
(
Kind::AcknowledgeAlarmByName,
Some(Payload::AcknowledgeAlarmByNameCommand(request)),
) => {
let address = self.ack_address(
None,
Some((
&request.provider_name,
&request.group_name,
&request.alarm_name,
)),
)?;
ack::acknowledge(
proxy,
address,
request.operator_user,
request.operator_domain,
request.operator_password,
request.comment,
reply,
)
}
_ => Err(invalid("Alarm worker sent an unexpected response")),
_ => Err(AlarmError::invalid(
"Alarm command payload does not match its kind",
)),
}
}
pub async fn shutdown(
mut self,
session: &str,
max: usize,
grace: Duration,
) -> io::Result<usize> {
self.shutting_down.store(true, Ordering::Release);
self.writer
.send(
session,
"",
Body::WorkerShutdown(WorkerShutdown {
grace_period: prost_types::Duration::try_from(grace).ok(),
reason: String::new(),
}),
max,
)
.await?;
let mut failures = match self.replies.recv().await.and_then(|frame| frame.body) {
Some(Body::WorkerShutdownAck(ack))
if ack
.status
.as_ref()
.is_some_and(|status| status.code() == ProtocolStatusCode::Ok) =>
{
0
}
_ => 1,
fn ack_address(
&self,
id: Option<Uuid>,
name: Option<(&str, &str, &str)>,
) -> Result<String, AlarmError> {
let subscription = self
.subscription
.as_ref()
.ok_or_else(|| AlarmError::invalid("Alarm provider is not subscribed"))?;
let not_found =
|| AlarmError::invalid("Alarm was not found or does not support acknowledgement");
if subscription.mode == Mode::Subtag {
let subtags = subscription.subtags.as_ref().ok_or_else(not_found)?;
let address = if let Some(id) = id {
subtags.ack_address(id)
} else if let Some((provider, group, name)) = name {
let reference = record::full_reference(provider, group, name);
subtags.ack_address_by_reference(&reference)
} else {
None
};
return address.map(str::to_owned).ok_or_else(not_found);
}
let (provider, name) = if let Some(id) = id {
let record = subscription
.primary
.as_ref()
.and_then(|primary| primary.records.get(&id))
.ok_or_else(not_found)?;
(record.provider.as_str(), record.name.as_str())
} else {
let (provider, _, name) = name.ok_or_else(not_found)?;
(provider, name)
};
if !self.child.wait().await?.success() {
failures += 1;
if !provider.eq_ignore_ascii_case("Galaxy") || name.trim().is_empty() {
return Err(not_found());
}
Ok(failures)
Ok(format!("{name}.AckMsg"))
}
}
impl Drop for AlarmWorker {
fn drop(&mut self) {
self.shutting_down.store(true, Ordering::Release);
self.reader.abort();
}
fn setting(name: &str, default: i32, range: std::ops::RangeInclusive<i32>) -> i32 {
std::env::var(name)
.ok()
.and_then(|value| value.parse().ok())
.filter(|value| range.contains(value))
.unwrap_or(default)
}
fn alarm_executable() -> io::Result<PathBuf> {
let current = std::env::current_exe()?.canonicalize()?;
let path = match std::env::var_os(ALARM_WORKER_ENV) {
Some(value) => {
let path = PathBuf::from(value);
if !path.is_absolute() {
return Err(invalid("Configured alarm worker path must be absolute"));
}
path
}
None => current
.with_file_name("alarm-worker")
.join("ZB.MOM.WW.MxGateway.Worker.exe"),
};
let path = path.canonicalize().map_err(|_| {
io::Error::new(
io::ErrorKind::NotFound,
"Managed alarm worker executable was not found",
)
})?;
if path == current || !path.is_file() {
return Err(invalid("Managed alarm worker path is invalid"));
}
Ok(path)
fn publish_mode(publisher: &Publisher, mode: Mode, reason: &str, hr: HRESULT) {
publisher.publish(MxEvent {
family: MxEventFamily::OnAlarmProviderModeChanged as i32,
body: Some(mx_event::Body::OnAlarmProviderModeChanged(
OnAlarmProviderModeChangedEvent {
mode: mode as i32,
reason: reason.into(),
hresult: hr,
at: Some(SystemTime::now().into()),
},
)),
..Default::default()
});
}
fn random_token() -> io::Result<String> {
let mut bytes = [0_u8; 24];
// SAFETY: BCrypt writes exactly the declared length to the local buffer.
if unsafe {
BCryptGenRandom(
null_mut(),
bytes.as_mut_ptr(),
bytes.len() as u32,
BCRYPT_USE_SYSTEM_PREFERRED_RNG,
)
} < 0
{
return Err(io::Error::other(
"Could not generate an alarm worker handshake token",
));
fn publish_record(publisher: &Publisher, record: &Record, previous: State, mode: Mode) {
if let Some(event) = record.event(previous, mode) {
publisher.publish(event);
}
Ok(bytes.iter().map(|byte| format!("{byte:02x}")).collect())
}
fn kill_on_close_job() -> io::Result<OwnedHandle> {
// SAFETY: ownership of the unnamed job transfers to OwnedHandle.
let handle = unsafe { CreateJobObjectW(null(), null()) };
if handle.is_null() {
return Err(io::Error::last_os_error());
}
let handle = unsafe { OwnedHandle::from_raw_handle(handle) };
let mut limits = JOBOBJECT_EXTENDED_LIMIT_INFORMATION::default();
limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if unsafe {
SetInformationJobObject(
handle.as_raw_handle(),
JobObjectExtendedLimitInformation,
(&limits as *const JOBOBJECT_EXTENDED_LIMIT_INFORMATION).cast(),
size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,
)
} == 0
{
return Err(io::Error::last_os_error());
}
Ok(handle)
}
fn invalid(message: &'static str) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, message)
}
fn disconnected() -> io::Error {
io::Error::new(
io::ErrorKind::BrokenPipe,
"Managed alarm worker disconnected",
)
}
+150
View File
@@ -0,0 +1,150 @@
use std::time::Instant;
use tokio::sync::oneshot::error::TryRecvError;
use windows_sys::Win32::Foundation::{E_FAIL, WAIT_OBJECT_0, WAIT_TIMEOUT};
use windows_sys::Win32::UI::WindowsAndMessaging::{
MsgWaitForMultipleObjectsEx, MWMO_INPUTAVAILABLE, QS_ALLINPUT,
};
use super::{handle, AlarmError};
use crate::mxaccess::{pump_messages, Command, CommandResult, Proxy, WriteAccess, WriteRequest};
use crate::mxevent::WriteCompletion;
use crate::wire::mxaccess_gateway::v1::{
mx_command_reply::Payload as Reply, mx_value, AcknowledgeAlarmReplyPayload, MxCommandReply,
MxDataType, MxStatusCategory, MxValue, ProtocolStatusCode as Code,
};
pub(super) fn acknowledge(
proxy: &mut Proxy,
address: String,
user: String,
domain: String,
password: String,
comment: String,
reply: &mut MxCommandReply,
) -> Result<(), AlarmError> {
if user.trim().is_empty() {
return Err(AlarmError::invalid("Alarm operator user is required"));
}
let principal = if domain.is_empty() {
user
} else {
format!("{domain}\\{user}")
};
let client = std::env::var("MXGATEWAY_ALARM_ACK_CLIENT_NAME").unwrap_or_default();
let client = match client.trim() {
"" => "MxGateway",
name => name,
};
let server = handle(
proxy.execute(Command::Register(client.into())),
"Register alarm acknowledgement",
)?;
proxy.publisher.private_registration(server, false);
let result = (|| {
let user = handle(
proxy.execute(Command::Authenticate(server, principal, password)),
"Alarm operator authentication",
)?;
if user == 0 {
return Err(AlarmError::failed(
"Alarm operator authentication (no user id)",
-1,
));
}
let item = handle(
proxy.execute(Command::AddItem(server, address)),
"Add AckMsg item",
)?;
proxy
.execute(Command::Advise {
server,
item,
supervisory: false,
})
.map_err(|error| AlarmError::com("Advise AckMsg item", error))?;
if !pump_messages() {
return Err(AlarmError::failed("AckMsg message pump", E_FAIL));
}
let value = MxValue {
data_type: MxDataType::String as i32,
kind: Some(mx_value::Kind::StringValue(comment)),
..Default::default()
};
let CommandResult::Write(completion) = proxy
.execute(Command::Write(Box::new(WriteRequest {
server,
item,
value: Some(value),
timestamp: None,
access: WriteAccess::Plain(user),
})))
.map_err(|error| AlarmError::com("AckMsg write", error))?
else {
unreachable!("write command returns a completion");
};
complete_ack(completion, reply)
})();
proxy.publisher.discard_pending_write();
let _ = proxy.execute(Command::Unregister(server));
result
}
fn complete_ack(
mut completion: WriteCompletion,
reply: &mut MxCommandReply,
) -> Result<(), AlarmError> {
let wait = crate::mxaccess::write_completion_wait();
let deadline = Instant::now() + wait;
loop {
match completion.try_recv() {
Ok(statuses) => {
if let Some(status) = statuses
.iter()
.find(|status| status.category() != MxStatusCategory::Ok)
{
return Err(AlarmError {
code: Code::MxaccessFailure,
hr: Some(status.detail),
message: format!(
"AckMsg write rejected (category {}, detail {})",
status.category, status.detail
),
});
}
break;
}
Err(TryRecvError::Closed) => {
return Err(AlarmError::failed("AckMsg completion channel", E_FAIL))
}
Err(TryRecvError::Empty) => {}
}
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
reply.diagnostic_message = format!(
"AckMsg write sent; OnWriteComplete not observed within {} ms",
wait.as_millis()
);
break;
}
// SAFETY: waits only on the current STA's queue.
let result = unsafe {
MsgWaitForMultipleObjectsEx(
0,
std::ptr::null(),
remaining.as_nanos().div_ceil(1_000_000) as u32,
QS_ALLINPUT,
MWMO_INPUTAVAILABLE,
)
};
match result {
WAIT_TIMEOUT => {}
WAIT_OBJECT_0 if pump_messages() => {}
_ => return Err(AlarmError::failed("AckMsg message pump", E_FAIL)),
}
}
reply.payload = Some(Reply::AcknowledgeAlarm(AcknowledgeAlarmReplyPayload {
native_status: 0,
}));
Ok(())
}
@@ -0,0 +1,217 @@
use std::collections::BTreeMap;
use chrono::{NaiveDate, NaiveTime};
use prost_types::Timestamp;
use uuid::Uuid;
use crate::wire::mxaccess_gateway::v1::{
mx_event, ActiveAlarmSnapshot, AlarmConditionState, AlarmProviderMode, AlarmTransitionKind,
MxEvent, MxEventFamily, OnAlarmTransitionEvent,
};
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum State {
#[default]
Unknown,
Active,
ActiveAcked,
Returned,
ReturnedAcked,
}
impl State {
pub fn active(self) -> bool {
matches!(self, Self::Active | Self::ActiveAcked)
}
fn returned(self) -> bool {
matches!(self, Self::Returned | Self::ReturnedAcked)
}
fn acked(self) -> bool {
matches!(self, Self::ActiveAcked | Self::ReturnedAcked)
}
fn transition(self, previous: Self) -> Option<AlarmTransitionKind> {
use AlarmTransitionKind::{Acknowledge, Clear, Raise};
match self {
Self::Unknown => None,
_ if previous == Self::Unknown => Some(if self.active() { Raise } else { Clear }),
_ if previous.active() && self.returned() => Some(Clear),
_ if previous.returned() && self.active() => Some(Raise),
_ if !previous.acked() && self.acked() => Some(Acknowledge),
_ => None,
}
}
}
#[derive(Default)]
pub struct Record {
pub provider: String,
pub group: String,
pub name: String,
pub alarm_type: String,
pub priority: i32,
pub state: State,
pub at: Option<Timestamp>,
pub operator: String,
pub comment: String,
pub ack_operator: String,
pub ack_comment: String,
}
impl Record {
pub fn reference(&self) -> String {
full_reference(&self.provider, &self.group, &self.name)
}
pub fn event(&self, previous: State, mode: AlarmProviderMode) -> Option<MxEvent> {
let transition = self.state.transition(previous)?;
let (operator_user, operator_comment) =
self.attribution(transition == AlarmTransitionKind::Acknowledge);
let body = OnAlarmTransitionEvent {
alarm_full_reference: self.reference(),
source_object_reference: self.name.clone(),
alarm_type_name: self.alarm_type.clone(),
transition_kind: transition as i32,
severity: self.priority,
transition_timestamp: self.at.or(Some(Timestamp {
seconds: -62_135_596_800,
nanos: 0,
})),
operator_user,
operator_comment,
category: self.group.clone(),
degraded: mode == AlarmProviderMode::Subtag,
source_provider: mode as i32,
..Default::default()
};
Some(MxEvent {
family: MxEventFamily::OnAlarmTransition as i32,
body: Some(mx_event::Body::OnAlarmTransition(body)),
..Default::default()
})
}
pub fn snapshot(&self, mode: AlarmProviderMode, truncated: bool) -> ActiveAlarmSnapshot {
let (operator_user, operator_comment) = self.attribution(self.state.acked());
ActiveAlarmSnapshot {
alarm_full_reference: self.reference(),
source_object_reference: self.name.clone(),
alarm_type_name: self.alarm_type.clone(),
severity: self.priority,
current_state: if self.state.acked() {
AlarmConditionState::ActiveAcked
} else {
AlarmConditionState::Active
} as i32,
last_transition_timestamp: self.at,
operator_user,
operator_comment,
category: self.group.clone(),
degraded: mode == AlarmProviderMode::Subtag,
source_provider: mode as i32,
from_truncated_snapshot: truncated,
..Default::default()
}
}
fn attribution(&self, ack: bool) -> (String, String) {
if ack && (!self.ack_operator.is_empty() || !self.ack_comment.is_empty()) {
(self.ack_operator.clone(), self.ack_comment.clone())
} else {
(self.operator.clone(), self.comment.clone())
}
}
}
pub fn full_reference(provider: &str, group: &str, name: &str) -> String {
let separator = if provider.is_empty() { "" } else { "!" };
let dot = if group.is_empty() { "" } else { "." };
format!("{provider}{separator}{group}{dot}{name}")
}
pub fn parse_snapshot(xml: &str) -> Result<(BTreeMap<Uuid, Record>, usize), roxmltree::Error> {
let mut records = BTreeMap::new();
if xml.trim().is_empty() {
return Ok((records, 0));
}
let document = roxmltree::Document::parse(xml)?;
let root = document.root_element();
if !unqualified(root, "ALARM_RECORDS") {
return Ok((records, 0));
}
let mut count = 0;
for alarm in root.children().filter(|&child| unqualified(child, "ALARM")) {
count += 1;
let mut fields = BTreeMap::new();
for field in alarm.children().filter(roxmltree::Node::is_element) {
fields.entry(field.tag_name().name()).or_insert_with(|| {
field
.descendants()
.filter(roxmltree::Node::is_text)
.filter_map(|node| node.text())
.collect::<String>()
});
}
let field = |name| fields.get(name).map_or("", String::as_str);
let Ok(id) = Uuid::parse_str(field("GUID").trim()) else {
continue;
};
records.insert(
id,
Record {
provider: field("PROVIDER_NAME").into(),
group: field("GROUP").into(),
name: field("TAGNAME").into(),
alarm_type: field("TYPE").into(),
priority: field("PRIORITY").trim().parse().unwrap_or_default(),
state: match field("STATE").trim().to_ascii_uppercase().as_str() {
"UNACK_ALM" => State::Active,
"ACK_ALM" => State::ActiveAcked,
"UNACK_RTN" => State::Returned,
"ACK_RTN" => State::ReturnedAcked,
_ => State::Unknown,
},
at: parse_timestamp(field("DATE"), field("TIME")),
operator: field("OPERATOR_NAME").into(),
comment: field("ALARM_COMMENT").into(),
ack_operator: field("ACK_OPERATOR_NAME").into(),
ack_comment: field("ACK_COMMENT").into(),
},
);
}
Ok((records, count))
}
fn unqualified(node: roxmltree::Node<'_, '_>, name: &str) -> bool {
node.is_element() && node.tag_name().namespace().is_none() && node.tag_name().name() == name
}
fn parse_timestamp(date: &str, time: &str) -> Option<Timestamp> {
let date = ["%Y/%m/%d", "%m/%d/%Y", "%d/%m/%Y"]
.into_iter()
.find_map(|format| NaiveDate::parse_from_str(date.trim(), format).ok())?;
let time = NaiveTime::parse_from_str(time.trim(), "%H:%M:%S%.f").ok()?;
let timestamp = date.and_time(time).and_utc();
Some(Timestamp {
seconds: timestamp.timestamp(),
nanos: timestamp.timestamp_subsec_nanos() as i32,
})
}
pub fn synthetic_guid(reference: &str) -> Uuid {
const PRIME: u64 = 1_099_511_628_211;
let mut low = 14_695_981_039_346_656_037_u64;
let mut high = 1_469_598_103_934_665_603_u64;
for (index, byte) in reference.bytes().enumerate() {
low = (low ^ u64::from(byte)).wrapping_mul(PRIME);
high = (high ^ (u64::from(byte) + index as u64)).wrapping_mul(PRIME);
}
low = (low ^ reference.len() as u64).wrapping_mul(PRIME);
let mut bytes = [0; 16];
bytes[..8].copy_from_slice(&low.to_le_bytes());
bytes[8..].copy_from_slice(&high.to_le_bytes());
Uuid::from_bytes_le(bytes)
}
@@ -0,0 +1,225 @@
use std::collections::{BTreeMap, BTreeSet};
use uuid::Uuid;
use super::record::{synthetic_guid, Record, State};
use super::{handle, AlarmError};
use crate::mxaccess::{Command, Proxy};
use crate::wire::mxaccess_gateway::v1::{
mx_value::Kind as Value, AlarmProviderMode, AlarmSubtagTarget,
};
#[derive(Clone, Copy)]
enum Role {
Active,
Acked,
Priority,
}
struct Target {
config: AlarmSubtagTarget,
record: Record,
acked_during_episode: bool,
}
pub struct Subtags {
server: Option<i32>,
items: BTreeMap<i32, (Uuid, Role)>,
targets: BTreeMap<Uuid, Target>,
}
impl Subtags {
pub fn attach(proxy: &mut Proxy, configs: Vec<AlarmSubtagTarget>) -> Result<Self, AlarmError> {
let mut targets = BTreeMap::new();
let mut references = BTreeSet::new();
let mut addresses = BTreeSet::new();
let mut bindings = Vec::new();
for config in configs {
let reference = &config.alarm_full_reference;
if reference.trim().is_empty() || !references.insert(reference.to_lowercase()) {
return Err(AlarmError::invalid(
"Alarm watch list has an empty or duplicate reference",
));
}
let id = synthetic_guid(reference);
for (address, role) in [
(&config.active_subtag, Role::Active),
(&config.acked_subtag, Role::Acked),
(&config.priority_subtag, Role::Priority),
] {
if address.trim().is_empty() {
continue;
}
if !addresses.insert(address.to_lowercase()) {
return Err(AlarmError::invalid(
"Alarm watch list has a duplicate subtag address",
));
}
bindings.push((address.clone(), id, role));
}
let (provider, path) = reference.split_once('!').unwrap_or(("", reference));
let mut offset = 0;
let mut group_and_name = ("", path);
for segment in path.split('.') {
if segment.eq_ignore_ascii_case(&config.source_object_reference) {
if offset > 0 {
group_and_name = (&path[..offset - 1], &path[offset..]);
}
break;
}
offset += segment.len() + 1;
}
let (group, name) = group_and_name;
let record = Record {
provider: provider.into(),
group: group.into(),
name: name.into(),
..Default::default()
};
targets.insert(
id,
Target {
config,
record,
acked_during_episode: false,
},
);
}
let mut subtags = Self {
server: None,
items: BTreeMap::new(),
targets,
};
if !bindings.is_empty() {
let server = handle(
proxy.execute(Command::Register(
"OtOpcUa.ZB.MOM.WW.MxGateway.Worker.SubtagAlarms".into(),
)),
"Register alarm subtags",
)?;
proxy.publisher.private_registration(server, true);
subtags.server = Some(server);
for (address, id, role) in bindings {
let result = (|| {
let item = handle(
proxy.execute(Command::AddItem(server, address)),
"Add alarm subtag",
)?;
proxy
.execute(Command::Advise {
server,
item,
supervisory: false,
})
.map_err(|error| AlarmError::com("Advise alarm subtag", error))?;
subtags.items.insert(item, (id, role));
Ok(())
})();
if let Err(error) = result {
subtags.close(proxy);
return Err(error);
}
}
}
Ok(subtags)
}
pub fn close(&mut self, proxy: &mut Proxy) {
if let Some(server) = self.server.take() {
let _ = proxy.execute(Command::Unregister(server));
}
}
pub fn drain(&mut self, proxy: &Proxy, emit: bool) {
let Some(server) = self.server else {
return;
};
for event in proxy.publisher.private_events(server) {
let Some(&(id, role)) = self.items.get(&event.item_handle) else {
continue;
};
let target = self.targets.get_mut(&id).expect("binding has a target");
let record = &mut target.record;
let previous = record.state;
let value = event.value.and_then(|value| value.kind);
let number = numeric(value.as_ref());
let flag = number.is_some_and(|number| number != 0.0);
record.state = match role {
Role::Priority => {
if let Some(priority) = number
.map(f64::round_ties_even)
.filter(|value| (i32::MIN as f64..=i32::MAX as f64).contains(value))
{
record.priority = priority as i32;
}
continue;
}
Role::Active if flag && !previous.active() => {
target.acked_during_episode = false;
State::Active
}
Role::Active if !flag && previous.active() => {
if target.acked_during_episode {
State::ReturnedAcked
} else {
State::Returned
}
}
Role::Acked if previous.active() && flag => {
target.acked_during_episode = true;
State::ActiveAcked
}
Role::Acked if previous.active() => State::Active,
_ => continue,
};
if record.state == previous {
continue;
}
record.at = event.source_timestamp.or(event.worker_timestamp);
if emit {
super::publish_record(
&proxy.publisher,
record,
previous,
AlarmProviderMode::Subtag,
);
}
}
}
pub fn records(&self) -> impl Iterator<Item = &Record> {
self.targets.values().map(|target| &target.record)
}
pub fn ack_address_by_reference(&self, reference: &str) -> Option<&str> {
let (id, _) = self.targets.iter().find(|(_, target)| {
target
.config
.alarm_full_reference
.eq_ignore_ascii_case(reference)
})?;
self.ack_address(*id)
}
pub fn ack_address(&self, id: Uuid) -> Option<&str> {
self.targets
.get(&id)
.map(|target| target.config.ack_comment_subtag.as_str())
.filter(|address| !address.trim().is_empty())
}
}
fn numeric(value: Option<&Value>) -> Option<f64> {
match value? {
Value::BoolValue(value) => Some(if *value { 1.0 } else { 0.0 }),
Value::Int32Value(value) => Some((*value).into()),
Value::Int64Value(value) => Some(*value as f64),
Value::FloatValue(value) => Some((*value).into()),
Value::DoubleValue(value) => Some(*value),
Value::StringValue(value) if value.trim().eq_ignore_ascii_case("true") => Some(1.0),
Value::StringValue(value) if value.trim().eq_ignore_ascii_case("false") => Some(0.0),
Value::StringValue(value) => value.trim().parse().ok(),
_ => None,
}
}
@@ -0,0 +1,203 @@
//! x86 WnWrap interfaces from Interop.WNWRAPCONSUMERLib. The working
//! sequence uses v1 to register/subscribe and v2 to fetch the XML snapshot.
use std::ffi::c_void;
use std::ptr::null_mut;
use windows_sys::core::{IUnknown_Vtbl, BSTR, GUID, HRESULT, PCWSTR};
use windows_sys::Win32::Foundation::E_FAIL;
use windows_sys::Win32::System::Com::{CoCreateInstance, CLSCTX_INPROC_SERVER};
use windows_sys::Win32::System::Variant::{VariantClear, VARIANT, VT_BSTR};
use crate::automation::AutomationString;
use crate::mxevent::ComReference;
const CLSID: GUID = GUID::from_u128(0x7ab52e5f_36b2_4a30_ae46_952a746f667c);
const IID_V1: GUID = GUID::from_u128(0x72006e35_651a_4d05_b3cb_8e4324b2e2d3);
const IID_V2: GUID = GUID::from_u128(0x9bcc2673_e5f9_4bba_b262_17c2780e276a);
const SUMMARY: i32 = 1;
const NEWEST_FIRST: i32 = 1;
const ACTIVE_NOW: i32 = 1;
#[repr(C)]
struct Vtable {
unknown: IUnknown_Vtbl,
dispatch: [usize; 4],
register:
unsafe extern "system" fn(*mut c_void, i32, PCWSTR, PCWSTR, PCWSTR, *mut i32) -> HRESULT,
initialize: unsafe extern "system" fn(*mut c_void, PCWSTR, *mut i32) -> HRESULT,
deregister: unsafe extern "system" fn(*mut c_void, *mut i32) -> HRESULT,
uninitialize: unsafe extern "system" fn(*mut c_void, *mut i32) -> HRESULT,
select_ack_and_get_query: [usize; 5],
set_query: unsafe extern "system" fn(*mut c_void, BSTR) -> HRESULT,
get_alarms: unsafe extern "system" fn(*mut c_void, i32, *mut VARIANT) -> HRESULT,
subscribe: unsafe extern "system" fn(
*mut c_void,
PCWSTR,
i16,
i16,
i32,
i32,
i32,
i32,
*mut i32,
) -> HRESULT,
}
pub struct WnWrap {
v1: ComReference,
v2: ComReference,
initialized: bool,
registered: bool,
}
impl WnWrap {
pub fn subscribe(subscription: &str) -> Result<Self, HRESULT> {
let mut v1 = null_mut();
// SAFETY: the two confirmed dual interfaces stay on the calling STA.
check(unsafe {
CoCreateInstance(&CLSID, null_mut(), CLSCTX_INPROC_SERVER, &IID_V1, &mut v1)
})?;
let v1 = ComReference::new(v1)?;
let mut v2 = null_mut();
check(unsafe { (table(&v1).unknown.QueryInterface)(v1.as_ptr(), &IID_V2, &mut v2) })?;
let mut consumer = Self {
v1,
v2: ComReference::new(v2)?,
initialized: false,
registered: false,
};
let application = wide("OtOpcUa.ZB.MOM.WW.MxGateway.Worker");
let product = wide("OtOpcUa.MxGateway");
let version = wide("1.0");
let expression = wide(subscription);
let query = AutomationString::new(&query_xml(subscription))?;
let v1 = consumer.v1.as_ptr();
let methods = table(&consumer.v1);
let mut result = 0;
// SAFETY: LPWSTR inputs are null-terminated for the duration of each call.
native_result(
unsafe { (methods.initialize)(v1, application.as_ptr(), &mut result) },
result,
)?;
consumer.initialized = true;
native_result(
unsafe {
(methods.register)(
v1,
0,
product.as_ptr(),
application.as_ptr(),
version.as_ptr(),
&mut result,
)
},
result,
)?;
consumer.registered = true;
native_result(
unsafe {
(methods.subscribe)(
v1,
expression.as_ptr(),
1,
999,
SUMMARY,
NEWEST_FIRST,
ACTIVE_NOW,
ACTIVE_NOW,
&mut result,
)
},
result,
)?;
check(unsafe { (table(&consumer.v2).set_query)(consumer.v2.as_ptr(), query.as_raw()) })?;
Ok(consumer)
}
pub fn snapshot(&self, limit: i32) -> Result<String, HRESULT> {
struct XmlVariant(VARIANT);
impl Drop for XmlVariant {
fn drop(&mut self) {
// SAFETY: the out parameter owns the returned Automation value.
unsafe { VariantClear(&mut self.0) };
}
}
let mut result = XmlVariant(VARIANT::default());
check(unsafe { (table(&self.v2).get_alarms)(self.v2.as_ptr(), limit, &mut result.0) })?;
let value = unsafe { &result.0.Anonymous.Anonymous };
match value.vt {
VT_BSTR => Ok(unsafe { crate::automation::bstr(value.Anonymous.bstrVal) }),
0 | 1 => Ok(String::new()),
_ => Err(E_FAIL),
}
}
}
impl Drop for WnWrap {
fn drop(&mut self) {
let mut ignored = 0;
let v1 = self.v1.as_ptr();
let methods = table(&self.v1);
// SAFETY: only tear down lifecycle steps that completed successfully.
unsafe {
if self.registered {
(methods.deregister)(v1, &mut ignored);
}
if self.initialized {
(methods.uninitialize)(v1, &mut ignored);
}
}
}
}
fn table(reference: &ComReference) -> &Vtable {
// SAFETY: references in this module own IwwAlarmConsumer or IwwAlarmConsumer2.
unsafe { &**reference.as_ptr().cast::<*const Vtable>() }
}
fn check(hr: HRESULT) -> Result<(), HRESULT> {
if hr < 0 {
Err(hr)
} else {
Ok(())
}
}
fn native_result(hr: HRESULT, status: i32) -> Result<(), HRESULT> {
check(hr)?;
match status {
0 => Ok(()),
status if status < 0 => Err(status),
_ => Err(E_FAIL),
}
}
fn wide(value: &str) -> Vec<u16> {
value.encode_utf16().chain([0]).collect()
}
fn query_xml(subscription: &str) -> String {
use std::fmt::Write;
let default_node = std::env::var("COMPUTERNAME").unwrap_or_default();
let expression = subscription.trim_start_matches('\\');
let (node, remainder) = expression
.split_once('\\')
.unwrap_or((&default_node, expression));
let (provider, group) = remainder.split_once('!').unwrap_or((remainder, ""));
let mut query = String::from(
r#"<QUERIES FROM_PRIORITY="1" TO_PRIORITY="999" ALARM_STATE="ALL" DISPLAY_MODE="Summary"><QUERY>"#,
);
for (element, value) in [("NODE", node), ("PROVIDER", provider), ("GROUP", group)] {
if !value.is_empty() {
let escaped = value
.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;");
write!(query, "<{element}>{escaped}</{element}>").unwrap();
}
}
query.push_str("</QUERY></QUERIES>");
query
}
+1 -1
View File
@@ -557,7 +557,7 @@ fn unknown(variant_type: String) -> MxValue {
}
}
unsafe fn bstr(value: BSTR) -> String {
pub(crate) unsafe fn bstr(value: BSTR) -> String {
if value.is_null() {
return String::new();
}
+52 -10
View File
@@ -1,7 +1,9 @@
use crate::alarm::AlarmRuntime;
use crate::automation::{AutomationString, StatusRecord, WriteValue, WriteValueError};
use crate::mxevent::{ComReference, Connection, Publisher, RecordInfoPin, WriteCompletion};
use crate::wire::mxaccess_gateway::v1::{
BulkReadResult, BulkWriteResult, MxStatusProxy, MxValue, SubscribeResult,
BulkReadResult, BulkWriteResult, MxCommand, MxCommandReply, MxStatusProxy, MxValue,
SubscribeResult,
};
use std::collections::{BTreeMap, BTreeSet};
use std::time::Duration;
@@ -14,12 +16,12 @@ use std::sync::{mpsc, Arc};
use std::thread;
use tokio::sync::oneshot;
use windows_sys::core::{BSTR, GUID, HRESULT};
use windows_sys::Win32::Foundation::{E_INVALIDARG, WAIT_OBJECT_0};
use windows_sys::Win32::Foundation::{E_INVALIDARG, WAIT_OBJECT_0, WAIT_TIMEOUT};
use windows_sys::Win32::System::Com::{
CoCreateInstance, CoInitializeEx, CoUninitialize, CLSCTX_INPROC_SERVER,
COINIT_APARTMENTTHREADED,
};
use windows_sys::Win32::System::Threading::{CreateEventW, SetEvent, INFINITE};
use windows_sys::Win32::System::Threading::{CreateEventW, SetEvent};
use windows_sys::Win32::System::Variant::VARIANT;
use windows_sys::Win32::UI::WindowsAndMessaging::{
DispatchMessageW, MsgWaitForMultipleObjectsEx, PeekMessageW, TranslateMessage, MSG,
@@ -35,8 +37,18 @@ const IID_V3: GUID = GUID::from_u128(0x57d006b6_f25e_4654_a81e_bcbafd60fe59);
const IID_V5: GUID = GUID::from_u128(0xeceff506_a752_46e3_9e31_0a8e257c9926);
const IID_V4: GUID = GUID::from_u128(0x9dc0d5c1_9371_4e84_86f8_7091d316a66c);
pub fn write_completion_wait() -> Duration {
let milliseconds = std::env::var("MXGATEWAY_WORKER_WRITE_COMPLETION_WAIT_MS")
.ok()
.and_then(|value| value.trim().parse::<i32>().ok())
.filter(|&value| value >= 0)
.unwrap_or(1500);
Duration::from_millis(milliseconds as u64)
}
pub enum Command {
Probe,
Alarm(Box<MxCommand>),
Register(String),
Unregister(i32),
AddItem(i32, String),
@@ -106,6 +118,7 @@ impl From<WriteValueError> for CommandError {
pub enum CommandResult {
Done,
Alarm(Box<MxCommandReply>),
Handle(i32),
Status(MxStatusProxy),
SubscribeBulk(Vec<SubscribeResult>),
@@ -114,7 +127,7 @@ pub enum CommandResult {
WriteBulk(Vec<BulkWriteResult>),
}
type Outcome = Result<CommandResult, CommandError>;
pub(crate) type Outcome = Result<CommandResult, CommandError>;
pub enum StartError {
Io(io::Error),
@@ -236,11 +249,29 @@ fn run(
}
fn pump(requests: &mpsc::Receiver<Request>, wake: &OwnedHandle, proxy: &mut Proxy) {
let mut alarms = AlarmRuntime::new();
pump_loop(requests, wake, proxy, &mut alarms);
alarms.close(proxy);
}
fn pump_loop(
requests: &mpsc::Receiver<Request>,
wake: &OwnedHandle,
proxy: &mut Proxy,
alarms: &mut AlarmRuntime,
) {
let handle = wake.as_raw_handle();
loop {
alarms.poll(proxy);
// SAFETY: the wait uses one valid event and the current STA's message queue.
let result = unsafe {
MsgWaitForMultipleObjectsEx(1, &handle, INFINITE, QS_ALLINPUT, MWMO_INPUTAVAILABLE)
MsgWaitForMultipleObjectsEx(
1,
&handle,
alarms.wait_millis(),
QS_ALLINPUT,
MWMO_INPUTAVAILABLE,
)
};
match result {
WAIT_OBJECT_0 => loop {
@@ -251,19 +282,27 @@ fn pump(requests: &mpsc::Receiver<Request>, wake: &OwnedHandle, proxy: &mut Prox
if matches!(request.command, Command::Write(_)) && !pump_messages() {
return;
}
let _ = request.reply.send(proxy.execute(request.command));
let result = match request.command {
Command::Alarm(command) => Ok(CommandResult::Alarm(Box::new(
alarms.execute(proxy, *command),
))),
command => proxy.execute(command),
};
let _ = request.reply.send(result);
alarms.poll(proxy);
}
Err(mpsc::TryRecvError::Empty) => break,
Err(mpsc::TryRecvError::Disconnected) => return,
}
},
1 if pump_messages() => {}
WAIT_TIMEOUT => {}
_ => return,
}
}
}
fn pump_messages() -> bool {
pub(crate) fn pump_messages() -> bool {
let mut message = MSG::default();
// SAFETY: MSG lives across PeekMessageW and dispatch on this STA.
while unsafe { PeekMessageW(&mut message, null_mut(), 0, 0, PM_REMOVE) } != 0 {
@@ -346,10 +385,10 @@ struct ProxyV5Vtable {
set_buffered_update_interval: unsafe extern "system" fn(*mut c_void, i32, i32) -> HRESULT,
}
struct Proxy {
pub(crate) struct Proxy {
interface: NonNull<c_void>,
registrations: BTreeSet<i32>,
publisher: Publisher,
pub(crate) publisher: Publisher,
connections: Vec<Connection>,
items: BTreeMap<(i32, i32), String>,
}
@@ -383,13 +422,16 @@ impl Proxy {
Ok(proxy)
}
fn execute(&mut self, command: Command) -> Outcome {
pub(crate) fn execute(&mut self, command: Command) -> Outcome {
let object = self.interface.as_ptr();
let table = self.vtable();
let mut handle = 0;
// SAFETY: the proxy and temporary BSTRs are local to this STA.
match command {
Command::Probe => Ok(CommandResult::Done),
Command::Alarm(_) => {
unreachable!("alarm command must run through the STA alarm runtime")
}
Command::Write(request) => self.write(*request, true).map(|completion| {
CommandResult::Write(completion.expect("single write expects a completion"))
}),
+36 -2
View File
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, BTreeSet};
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::ffi::c_void;
use std::ptr::{null_mut, NonNull};
use std::sync::{
@@ -37,7 +37,7 @@ const RECORD_ID: GUID = GUID::from_u128(0xa162d9e4_9019_42f3_a087_79264007e542);
#[derive(Clone, Copy)]
pub enum EventFault {
Overflow,
AlarmDisconnected,
AlarmPoll(HRESULT),
Conversion(&'static str),
}
@@ -63,6 +63,7 @@ struct PublisherState {
last_sequence: u64,
latest_values: BTreeMap<(i32, i32), MxEvent>,
pending_write: Option<PendingWrite>,
private: BTreeMap<i32, Option<VecDeque<MxEvent>>>,
}
pub fn event_channel(
@@ -98,8 +99,28 @@ impl Publisher {
state.latest_values.remove(&(server, item));
}
pub fn private_registration(&self, server: i32, collect: bool) {
self.state
.lock()
.unwrap()
.private
.insert(server, collect.then(VecDeque::new));
}
pub fn private_events(&self, server: i32) -> VecDeque<MxEvent> {
self.state
.lock()
.unwrap()
.private
.get_mut(&server)
.and_then(Option::as_mut)
.map(std::mem::take)
.unwrap_or_default()
}
pub fn unregister(&self, server: i32) {
let mut state = self.state.lock().unwrap();
state.private.remove(&server);
state.advised.retain(|(handle, _)| *handle != server);
state
.latest_values
@@ -161,6 +182,16 @@ impl Publisher {
if !state.advised.contains(&key) {
return;
}
if let Some(queue) = state.private.get_mut(&event.server_handle) {
if let Some(queue) = queue {
if queue.len() == self.events.max_capacity() {
self.report_fault(EventFault::Overflow);
} else {
queue.push_back(event);
}
}
return;
}
state.latest_values.insert(key, event.clone());
}
if event.family() == MxEventFamily::OnWriteComplete
@@ -173,6 +204,9 @@ impl Publisher {
let _ = pending.completion.send(event.statuses.clone());
}
}
if state.private.contains_key(&event.server_handle) {
return;
}
match self.events.try_send(event) {
Ok(()) => state.last_sequence += 1,
Err(mpsc::error::TrySendError::Full(_)) => self.report_fault(EventFault::Overflow),
+29 -71
View File
@@ -11,7 +11,7 @@ use tokio::sync::{mpsc, watch};
use tokio::time::{interval, sleep, timeout, Instant, Interval, MissedTickBehavior};
use windows_sys::Win32::Foundation::E_INVALIDARG;
use crate::alarm::{self, AlarmWorker};
use crate::alarm;
use crate::automation::int32;
use crate::mxaccess::{
Command, CommandError, CommandResult, ItemOperation, Sta, StartError, WriteAccess,
@@ -126,13 +126,7 @@ impl Options {
heartbeat_interval: env_duration("MXGATEWAY_WORKER_HEARTBEAT_SEND_INTERVAL_MS", 5000),
sta_grace: env_duration("MXGATEWAY_WORKER_HEARTBEAT_GRACE_MS", 15000),
command_ceiling: env_duration("MXGATEWAY_WORKER_HEARTBEAT_STUCK_CEILING_MS", 75000),
write_completion_wait: Duration::from_millis(
std::env::var("MXGATEWAY_WORKER_WRITE_COMPLETION_WAIT_MS")
.ok()
.and_then(|value| value.parse::<u32>().ok())
.unwrap_or(1500)
.into(),
),
write_completion_wait: crate::mxaccess::write_completion_wait(),
event_capacity: std::env::var("MXGATEWAY_EVENT_QUEUE_CAPACITY")
.ok()
.and_then(|value| value.parse().ok())
@@ -286,17 +280,7 @@ pub async fn run() -> Result<(), Error> {
// finish a control write before reading the ack.
let acknowledgement = match result {
Ok(grace) => {
let shutdown = timeout(grace, async {
let alarms = async {
match events.alarm_worker.take() {
Some(worker) => worker.shutdown(&options.session, max, grace).await,
None => Ok(0),
}
};
let (sta_result, alarm_result) = tokio::join!(sta.shutdown(), alarms);
Ok::<_, io::Error>(sta_result? + alarm_result?)
})
.await;
let shutdown = timeout(grace, sta.shutdown()).await;
let (code, message) = match shutdown {
Ok(Ok(0)) => (ProtocolStatusCode::Ok, "Graceful shutdown completed".into()),
Ok(Ok(failures)) => (
@@ -322,7 +306,6 @@ pub async fn run() -> Result<(), Error> {
.map_err(Error::from)
}
Err(error) => {
drop(events.alarm_worker.take());
let _ = timeout(Duration::from_secs(2), sta.shutdown()).await;
Err(error)
}
@@ -518,9 +501,7 @@ async fn run_command<W: AsyncWrite + Unpin>(
});
let context = ExecutorContext {
options,
max,
publisher: &events.publisher,
alarm_worker: &mut events.alarm_worker,
};
let execution = timeout(
options.command_ceiling,
@@ -608,16 +589,27 @@ async fn report_event_fault(
failure: EventFault,
) -> Error {
let (category, message, exit_code) = match failure {
EventFault::AlarmPoll(hr) => {
let message = format!(
"Alarm polling repeatedly failed (HRESULT 0x{:08X})",
hr as u32
);
fault(
writer,
session,
max,
WorkerFaultCategory::MxaccessEventConversionFailed,
Some(hr),
&message,
)
.await;
return Error(1, message);
}
EventFault::Overflow => (
WorkerFaultCategory::QueueOverflow,
"MXAccess event backlog exceeded the outbound queue capacity",
8,
),
EventFault::AlarmDisconnected => (
WorkerFaultCategory::PipeDisconnected,
"Managed alarm worker disconnected",
5,
),
EventFault::Conversion(message) => (
WorkerFaultCategory::MxaccessEventConversionFailed,
message,
@@ -630,9 +622,7 @@ async fn report_event_fault(
struct ExecutorContext<'a> {
options: &'a Options,
max: usize,
publisher: &'a Publisher,
alarm_worker: &'a mut Option<AlarmWorker>,
}
async fn execute(
@@ -642,12 +632,7 @@ async fn execute(
context: ExecutorContext<'_>,
drain: Option<DrainEventsReply>,
) -> Result<MxCommandReply, Error> {
let ExecutorContext {
options,
max,
publisher,
alarm_worker,
} = context;
let ExecutorContext { options, publisher } = context;
use mx_command::Payload;
use mx_command_reply::Payload as Reply;
use MxCommandKind as Kind;
@@ -667,42 +652,16 @@ async fn execute(
};
let kind = command.kind();
if alarm::handles(kind) {
if alarm_worker.is_none() {
if kind == Kind::UnsubscribeAlarms
&& matches!(command.payload, Some(Payload::UnsubscribeAlarms(_)))
{
return Ok(reply);
}
match AlarmWorker::start(&options.session, max, publisher.clone()).await {
Ok(worker) => *alarm_worker = Some(worker),
Err(error) => {
set_status(
&mut reply,
ProtocolStatusCode::WorkerUnavailable,
&error.to_string(),
);
reply.hresult = None;
return Ok(reply);
}
}
}
let worker = alarm_worker.as_mut().expect("alarm worker was started");
return match worker
.invoke(&options.session, max, correlation, command)
let Ok(CommandResult::Alarm(mut result)) = sta
.execute(Command::Alarm(Box::new(command)))
.await
{
Ok(reply) => Ok(reply),
Err(error) => {
publisher.report_fault(EventFault::AlarmDisconnected);
set_status(
&mut reply,
ProtocolStatusCode::WorkerUnavailable,
&error.to_string(),
);
reply.hresult = None;
Ok(reply)
}
.map_err(Error::sta)?
else {
unreachable!("STA alarm route returns an alarm reply");
};
result.session_id = reply.session_id;
result.correlation_id = reply.correlation_id;
return Ok(*result);
}
let native = match (kind, command.payload) {
(Kind::Register, Some(Payload::Register(request))) => {
@@ -962,6 +921,7 @@ async fn execute(
}
}
match sta.execute(native).await.map_err(Error::sta)? {
Ok(CommandResult::Alarm(_)) => unreachable!("data command returned an alarm reply"),
Ok(CommandResult::Handle(handle)) => {
if !matches!(kind, Kind::AuthenticateUser | Kind::ArchestraUserToId) {
reply.return_value = Some(int32(handle));
@@ -1059,7 +1019,6 @@ struct EventStream {
receiver: mpsc::Receiver<MxEvent>,
faults: watch::Receiver<Option<EventFault>>,
pending: Option<MxEvent>,
alarm_worker: Option<AlarmWorker>,
}
impl EventStream {
@@ -1070,7 +1029,6 @@ impl EventStream {
receiver,
faults,
pending: None,
alarm_worker: None,
}
}