use of com.digitalpetri.strictmachine.FsmContext in project milo by eclipse.
the class SessionFsmFactory method initialize.
private static CompletableFuture<Unit> initialize(FsmContext<State, Event> ctx, OpcUaClient client, OpcUaSession session) {
List<SessionFsm.SessionInitializer> initializers = KEY_SESSION_INITIALIZERS.get(ctx).sessionInitializers;
if (initializers.isEmpty()) {
return completedFuture(Unit.VALUE);
} else {
UaStackClient stackClient = client.getStackClient();
CompletableFuture<?>[] futures = initializers.stream().map(i -> i.initialize(stackClient, session)).toArray(CompletableFuture[]::new);
return CompletableFuture.allOf(futures).thenApply(v -> Unit.VALUE);
}
}
use of com.digitalpetri.strictmachine.FsmContext in project milo by eclipse.
the class SessionFsmFactory method createSession.
@SuppressWarnings("Duplicates")
private static CompletableFuture<CreateSessionResponse> createSession(FsmContext<State, Event> ctx, OpcUaClient client) {
UaStackClient stackClient = client.getStackClient();
EndpointDescription endpoint = stackClient.getConfig().getEndpoint();
String gatewayServerUri = endpoint.getServer().getGatewayServerUri();
String serverUri;
if (gatewayServerUri != null && !gatewayServerUri.isEmpty()) {
serverUri = endpoint.getServer().getApplicationUri();
} else {
serverUri = null;
}
ByteString clientNonce = NonceUtil.generateNonce(32);
ByteString clientCertificate = stackClient.getConfig().getCertificate().map(c -> {
try {
return ByteString.of(c.getEncoded());
} catch (CertificateEncodingException e) {
return ByteString.NULL_VALUE;
}
}).orElse(ByteString.NULL_VALUE);
ApplicationDescription clientDescription = new ApplicationDescription(client.getConfig().getApplicationUri(), client.getConfig().getProductUri(), client.getConfig().getApplicationName(), ApplicationType.Client, null, null, null);
CreateSessionRequest request = new CreateSessionRequest(client.newRequestHeader(), clientDescription, serverUri, client.getConfig().getEndpoint().getEndpointUrl(), client.getConfig().getSessionName().get(), clientNonce, clientCertificate, client.getConfig().getSessionTimeout().doubleValue(), client.getConfig().getMaxResponseMessageSize());
LOGGER.debug("[{}] Sending CreateSessionRequest...", ctx.getInstanceId());
return stackClient.sendRequest(request).thenApply(CreateSessionResponse.class::cast).thenCompose(response -> {
try {
SecurityPolicy securityPolicy = SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri());
if (securityPolicy != SecurityPolicy.None) {
if (response.getServerCertificate().isNullOrEmpty()) {
throw new UaException(StatusCodes.Bad_SecurityChecksFailed, "Certificate missing from CreateSessionResponse");
}
List<X509Certificate> serverCertificateChain = CertificateUtil.decodeCertificates(response.getServerCertificate().bytesOrEmpty());
X509Certificate serverCertificate = serverCertificateChain.get(0);
X509Certificate certificateFromEndpoint = CertificateUtil.decodeCertificate(endpoint.getServerCertificate().bytesOrEmpty());
if (!serverCertificate.equals(certificateFromEndpoint)) {
throw new UaException(StatusCodes.Bad_SecurityChecksFailed, "Certificate from CreateSessionResponse did not " + "match certificate from EndpointDescription!");
}
client.getConfig().getCertificateValidator().validateCertificateChain(serverCertificateChain, endpoint.getServer().getApplicationUri(), EndpointUtil.getHost(endpoint.getEndpointUrl()));
SignatureData serverSignature = response.getServerSignature();
byte[] dataBytes = Bytes.concat(clientCertificate.bytesOrEmpty(), clientNonce.bytesOrEmpty());
byte[] signatureBytes = serverSignature.getSignature().bytesOrEmpty();
SignatureUtil.verify(SecurityAlgorithm.fromUri(serverSignature.getAlgorithm()), serverCertificate, dataBytes, signatureBytes);
}
return completedFuture(response);
} catch (UaException e) {
return failedFuture(e);
}
});
}
use of com.digitalpetri.strictmachine.FsmContext in project milo by eclipse.
the class SessionFsmFactory method configureActiveState.
private static void configureActiveState(FsmBuilder<State, Event> fb, OpcUaClient client) {
/* Transitions */
fb.when(State.Active).on(Event.CloseSession.class).transitionTo(State.Closing);
fb.when(State.Active).on(e -> e.getClass() == Event.KeepAliveFailure.class || e.getClass() == Event.ServiceFault.class).transitionTo(State.CreatingWait);
/* External Transition Actions */
fb.onTransitionTo(State.Active).from(State.Initializing).via(Event.InitializeSuccess.class).execute(ctx -> {
Event.InitializeSuccess event = (Event.InitializeSuccess) ctx.event();
// reset the wait time
KEY_WAIT_TIME.remove(ctx);
long keepAliveInterval = client.getConfig().getKeepAliveInterval().longValue();
KEY_KEEP_ALIVE_FAILURE_COUNT.set(ctx, 0L);
ScheduledFuture<?> scheduledFuture = client.getConfig().getScheduledExecutor().scheduleWithFixedDelay(() -> ctx.fireEvent(new Event.KeepAlive(event.session)), keepAliveInterval, keepAliveInterval, TimeUnit.MILLISECONDS);
KEY_KEEP_ALIVE_SCHEDULED_FUTURE.set(ctx, scheduledFuture);
KEY_SESSION.set(ctx, event.session);
SessionFuture sessionFuture = KEY_SESSION_FUTURE.get(ctx);
client.getConfig().getExecutor().execute(() -> sessionFuture.future.complete(event.session));
});
fb.onTransitionTo(State.Active).from(State.Initializing).via(Event.InitializeSuccess.class).execute(FsmContext::processShelvedEvents);
fb.onTransitionFrom(State.Active).to(s -> s == State.Closing || s == State.CreatingWait).viaAny().execute(ctx -> {
ScheduledFuture<?> scheduledFuture = KEY_KEEP_ALIVE_SCHEDULED_FUTURE.remove(ctx);
if (scheduledFuture != null) {
scheduledFuture.cancel(false);
}
});
// onSessionActive() callbacks
fb.onTransitionTo(State.Active).from(s -> s != State.Active).viaAny().execute(ctx -> {
OpcUaSession session = KEY_SESSION.get(ctx);
SessionFsm.SessionActivityListeners sessionActivityListeners = KEY_SESSION_ACTIVITY_LISTENERS.get(ctx);
sessionActivityListeners.sessionActivityListeners.forEach(listener -> listener.onSessionActive(session));
});
// onSessionInactive() callbacks
fb.onTransitionFrom(State.Active).to(s -> s != State.Active).viaAny().execute(ctx -> {
OpcUaSession session = KEY_SESSION.get(ctx);
SessionFsm.SessionActivityListeners sessionActivityListeners = KEY_SESSION_ACTIVITY_LISTENERS.get(ctx);
sessionActivityListeners.sessionActivityListeners.forEach(listener -> listener.onSessionInactive(session));
});
/* Internal Transition Actions */
fb.onInternalTransition(State.Active).via(Event.KeepAlive.class).execute(ctx -> {
Event.KeepAlive event = (Event.KeepAlive) ctx.event();
sendKeepAlive(client, event.session).whenComplete((response, ex) -> {
if (response != null) {
DataValue[] results = response.getResults();
if (results != null && results.length > 0) {
Object value = results[0].getValue().getValue();
if (value instanceof Integer) {
ServerState state = ServerState.from((Integer) value);
LOGGER.debug("[{}] ServerState: {}", ctx.getInstanceId(), state);
}
}
KEY_KEEP_ALIVE_FAILURE_COUNT.set(ctx, 0L);
} else {
Long keepAliveFailureCount = KEY_KEEP_ALIVE_FAILURE_COUNT.get(ctx);
if (keepAliveFailureCount == null) {
keepAliveFailureCount = 1L;
} else {
keepAliveFailureCount += 1L;
}
KEY_KEEP_ALIVE_FAILURE_COUNT.set(ctx, keepAliveFailureCount);
long keepAliveFailuresAllowed = client.getConfig().getKeepAliveFailuresAllowed().longValue();
if (keepAliveFailureCount > keepAliveFailuresAllowed) {
LOGGER.warn("[{}] Keep Alive failureCount={} exceeds failuresAllowed={}", ctx.getInstanceId(), keepAliveFailureCount, keepAliveFailuresAllowed);
ctx.fireEvent(new Event.KeepAliveFailure());
} else {
LOGGER.debug("[{}] Keep Alive failureCount={}", ctx.getInstanceId(), keepAliveFailureCount, ex);
}
}
});
});
fb.onInternalTransition(State.Active).via(Event.GetSession.class).execute(SessionFsmFactory::handleGetSessionEvent);
fb.onInternalTransition(State.Active).via(Event.OpenSession.class).execute(SessionFsmFactory::handleOpenSessionEvent);
}
use of com.digitalpetri.strictmachine.FsmContext in project milo by eclipse.
the class SessionFsmFactory method transferSubscriptions.
@SuppressWarnings("Duplicates")
private static CompletableFuture<Unit> transferSubscriptions(FsmContext<State, Event> ctx, OpcUaClient client, OpcUaSession session) {
UaStackClient stackClient = client.getStackClient();
OpcUaSubscriptionManager subscriptionManager = client.getSubscriptionManager();
ImmutableList<UaSubscription> subscriptions = subscriptionManager.getSubscriptions();
if (subscriptions.isEmpty()) {
return completedFuture(Unit.VALUE);
}
CompletableFuture<Unit> transferFuture = new CompletableFuture<>();
UInteger[] subscriptionIdsArray = subscriptions.stream().map(UaSubscription::getSubscriptionId).toArray(UInteger[]::new);
TransferSubscriptionsRequest request = new TransferSubscriptionsRequest(client.newRequestHeader(session.getAuthenticationToken()), subscriptionIdsArray, true);
LOGGER.debug("[{}] Sending TransferSubscriptionsRequest...", ctx.getInstanceId());
stackClient.sendRequest(request).thenApply(TransferSubscriptionsResponse.class::cast).whenComplete((tsr, ex) -> {
if (tsr != null) {
List<TransferResult> results = l(tsr.getResults());
LOGGER.debug("[{}] TransferSubscriptions supported: {}", ctx.getInstanceId(), tsr.getResponseHeader().getServiceResult());
if (LOGGER.isDebugEnabled()) {
try {
Stream<UInteger> subscriptionIds = subscriptions.stream().map(UaSubscription::getSubscriptionId);
Stream<StatusCode> statusCodes = results.stream().map(TransferResult::getStatusCode);
// noinspection UnstableApiUsage
String[] ss = Streams.zip(subscriptionIds, statusCodes, (i, s) -> String.format("id=%s/%s", i, StatusCodes.lookup(s.getValue()).map(sa -> sa[0]).orElse(s.toString()))).toArray(String[]::new);
LOGGER.debug("[{}] TransferSubscriptions results: {}", ctx.getInstanceId(), Arrays.toString(ss));
} catch (Throwable t) {
LOGGER.error("[{}] error logging TransferSubscription results", ctx.getInstanceId(), t);
}
}
client.getConfig().getExecutor().execute(() -> {
for (int i = 0; i < results.size(); i++) {
TransferResult result = results.get(i);
if (!result.getStatusCode().isGood()) {
UaSubscription subscription = subscriptions.get(i);
subscriptionManager.transferFailed(subscription.getSubscriptionId(), result.getStatusCode());
}
}
});
transferFuture.complete(Unit.VALUE);
} else {
StatusCode statusCode = UaException.extract(ex).map(UaException::getStatusCode).orElse(StatusCode.BAD);
LOGGER.debug("[{}] TransferSubscriptions not supported: {}", ctx.getInstanceId(), statusCode);
client.getConfig().getExecutor().execute(() -> {
// because the list from getSubscriptions() above is a copy.
for (UaSubscription subscription : subscriptions) {
subscriptionManager.transferFailed(subscription.getSubscriptionId(), statusCode);
}
});
// supported but server implementations interpret the spec differently.
if (statusCode.getValue() == StatusCodes.Bad_NotImplemented || statusCode.getValue() == StatusCodes.Bad_NotSupported || statusCode.getValue() == StatusCodes.Bad_OutOfService || statusCode.getValue() == StatusCodes.Bad_ServiceUnsupported) {
// One of the expected responses; continue moving through the FSM.
transferFuture.complete(Unit.VALUE);
} else {
// An unexpected response; complete exceptionally and start over.
// Subsequent runs through the FSM will not attempt transfer because
// transferFailed() has been called for all the existing subscriptions.
// This will prevent us from getting stuck in a "loop" attempting to
// reconnect to a defective server that responds with a channel-level
// Error message to subscription transfer requests instead of an
// application-level ServiceFault.
transferFuture.completeExceptionally(ex);
}
}
});
return transferFuture;
}
use of com.digitalpetri.strictmachine.FsmContext in project milo by eclipse.
the class SessionFsmFactory method configureCreatingWaitState.
private static void configureCreatingWaitState(FsmBuilder<State, Event> fb, @SuppressWarnings("unused") OpcUaClient client) {
/* Transitions */
fb.when(State.CreatingWait).on(Event.CreatingWaitExpired.class).transitionTo(State.Creating);
fb.when(State.CreatingWait).on(Event.CloseSession.class).transitionTo(State.Inactive);
/* External Transition Actions */
fb.onTransitionTo(State.CreatingWait).from(s -> s != State.CreatingWait).viaAny().execute(FsmContext::processShelvedEvents);
fb.onTransitionTo(State.CreatingWait).from(s -> s != State.CreatingWait).viaAny().execute(ctx -> {
SessionFuture sessionFuture = new SessionFuture();
KEY_SESSION_FUTURE.set(ctx, sessionFuture);
Long waitTime = KEY_WAIT_TIME.get(ctx);
if (waitTime == null) {
waitTime = 1L;
} else {
waitTime = Math.min(MAX_WAIT_SECONDS, waitTime << 1);
}
KEY_WAIT_TIME.set(ctx, waitTime);
ScheduledFuture<?> waitFuture = client.getConfig().getScheduledExecutor().schedule(() -> ctx.fireEvent(new Event.CreatingWaitExpired()), waitTime, TimeUnit.SECONDS);
KEY_WAIT_FUTURE.set(ctx, waitFuture);
});
fb.onTransitionFrom(State.CreatingWait).to(State.Inactive).via(Event.CloseSession.class).execute(ctx -> {
ScheduledFuture<?> waitFuture = KEY_WAIT_FUTURE.remove(ctx);
if (waitFuture != null)
waitFuture.cancel(false);
KEY_WAIT_TIME.remove(ctx);
Event.CloseSession event = (Event.CloseSession) ctx.event();
client.getConfig().getExecutor().execute(() -> event.future.complete(Unit.VALUE));
});
/* Internal Transition Actions */
fb.onInternalTransition(State.CreatingWait).via(Event.GetSession.class).execute(SessionFsmFactory::handleGetSessionEvent);
fb.onInternalTransition(State.CreatingWait).via(Event.OpenSession.class).execute(SessionFsmFactory::handleOpenSessionEvent);
}
Aggregations