Search in sources :

Example 11 with OpcUaClient

use of org.eclipse.milo.opcua.sdk.client.OpcUaClient 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);
}
Also used : X509Certificate(java.security.cert.X509Certificate) KEY_CLOSE_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_CLOSE_FUTURE) KeyPair(java.security.KeyPair) SignedSoftwareCertificate(org.eclipse.milo.opcua.stack.core.types.structured.SignedSoftwareCertificate) Arrays(java.util.Arrays) ApplicationType(org.eclipse.milo.opcua.stack.core.types.enumerated.ApplicationType) ScheduledFuture(java.util.concurrent.ScheduledFuture) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) LoggerFactory(org.slf4j.LoggerFactory) ServerState(org.eclipse.milo.opcua.stack.core.types.enumerated.ServerState) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) TransferResult(org.eclipse.milo.opcua.stack.core.types.structured.TransferResult) KEY_WAIT_TIME(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_WAIT_TIME) OpcUaSubscriptionManager(org.eclipse.milo.opcua.sdk.client.subscriptions.OpcUaSubscriptionManager) ByteBuffer(java.nio.ByteBuffer) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) UserIdentityToken(org.eclipse.milo.opcua.stack.core.types.structured.UserIdentityToken) SecurityAlgorithm(org.eclipse.milo.opcua.stack.core.security.SecurityAlgorithm) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) Unit(org.eclipse.milo.opcua.stack.core.util.Unit) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) KEY_SESSION_INITIALIZERS(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION_INITIALIZERS) CertificateUtil(org.eclipse.milo.opcua.stack.core.util.CertificateUtil) CreateSessionRequest(org.eclipse.milo.opcua.stack.core.types.structured.CreateSessionRequest) ActivateSessionRequest(org.eclipse.milo.opcua.stack.core.types.structured.ActivateSessionRequest) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) Predicate(java.util.function.Predicate) KEY_SESSION_ACTIVITY_LISTENERS(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION_ACTIVITY_LISTENERS) TransferSubscriptionsResponse(org.eclipse.milo.opcua.stack.core.types.structured.TransferSubscriptionsResponse) Streams(com.google.common.collect.Streams) Bytes(com.google.common.primitives.Bytes) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) ServiceFault(org.eclipse.milo.opcua.stack.core.types.structured.ServiceFault) KEY_WAIT_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_WAIT_FUTURE) ActivateSessionResponse(org.eclipse.milo.opcua.stack.core.types.structured.ActivateSessionResponse) List(java.util.List) Stream(java.util.stream.Stream) PrivateKey(java.security.PrivateKey) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) KEY_KEEP_ALIVE_FAILURE_COUNT(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_KEEP_ALIVE_FAILURE_COUNT) EndpointUtil(org.eclipse.milo.opcua.stack.core.util.EndpointUtil) OpcUaSession(org.eclipse.milo.opcua.sdk.client.OpcUaSession) CertificateEncodingException(java.security.cert.CertificateEncodingException) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) CloseSessionRequest(org.eclipse.milo.opcua.stack.core.types.structured.CloseSessionRequest) KEY_SESSION_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION_FUTURE) ActionContext(com.digitalpetri.strictmachine.dsl.ActionContext) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) SignedIdentityToken(org.eclipse.milo.opcua.sdk.client.api.identity.SignedIdentityToken) CompletableFuture(java.util.concurrent.CompletableFuture) CreateSessionResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateSessionResponse) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) ImmutableList(com.google.common.collect.ImmutableList) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) SignatureData(org.eclipse.milo.opcua.stack.core.types.structured.SignatureData) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) FutureUtils.complete(org.eclipse.milo.opcua.stack.core.util.FutureUtils.complete) ServiceFaultListener(org.eclipse.milo.opcua.sdk.client.api.ServiceFaultListener) SessionFuture(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.SessionFuture) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) TransferSubscriptionsRequest(org.eclipse.milo.opcua.stack.core.types.structured.TransferSubscriptionsRequest) KEY_SESSION(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) SignatureUtil(org.eclipse.milo.opcua.stack.core.util.SignatureUtil) Fsm(com.digitalpetri.strictmachine.Fsm) Logger(org.slf4j.Logger) OpcUaClientConfig(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) KEY_KEEP_ALIVE_SCHEDULED_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_KEEP_ALIVE_SCHEDULED_FUTURE) TimeUnit(java.util.concurrent.TimeUnit) NonceUtil(org.eclipse.milo.opcua.stack.core.util.NonceUtil) FsmContext(com.digitalpetri.strictmachine.FsmContext) ApplicationDescription(org.eclipse.milo.opcua.stack.core.types.structured.ApplicationDescription) FutureUtils.failedFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedFuture) UaException(org.eclipse.milo.opcua.stack.core.UaException) FsmBuilder(com.digitalpetri.strictmachine.dsl.FsmBuilder) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) FsmContext(com.digitalpetri.strictmachine.FsmContext) OpcUaSession(org.eclipse.milo.opcua.sdk.client.OpcUaSession) ServiceFault(org.eclipse.milo.opcua.stack.core.types.structured.ServiceFault) SessionFuture(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.SessionFuture) ServerState(org.eclipse.milo.opcua.stack.core.types.enumerated.ServerState) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)

Example 12 with OpcUaClient

use of org.eclipse.milo.opcua.sdk.client.OpcUaClient 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;
}
Also used : X509Certificate(java.security.cert.X509Certificate) KEY_CLOSE_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_CLOSE_FUTURE) KeyPair(java.security.KeyPair) SignedSoftwareCertificate(org.eclipse.milo.opcua.stack.core.types.structured.SignedSoftwareCertificate) Arrays(java.util.Arrays) ApplicationType(org.eclipse.milo.opcua.stack.core.types.enumerated.ApplicationType) ScheduledFuture(java.util.concurrent.ScheduledFuture) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) LoggerFactory(org.slf4j.LoggerFactory) ServerState(org.eclipse.milo.opcua.stack.core.types.enumerated.ServerState) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) TransferResult(org.eclipse.milo.opcua.stack.core.types.structured.TransferResult) KEY_WAIT_TIME(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_WAIT_TIME) OpcUaSubscriptionManager(org.eclipse.milo.opcua.sdk.client.subscriptions.OpcUaSubscriptionManager) ByteBuffer(java.nio.ByteBuffer) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) UserIdentityToken(org.eclipse.milo.opcua.stack.core.types.structured.UserIdentityToken) SecurityAlgorithm(org.eclipse.milo.opcua.stack.core.security.SecurityAlgorithm) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) Unit(org.eclipse.milo.opcua.stack.core.util.Unit) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) KEY_SESSION_INITIALIZERS(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION_INITIALIZERS) CertificateUtil(org.eclipse.milo.opcua.stack.core.util.CertificateUtil) CreateSessionRequest(org.eclipse.milo.opcua.stack.core.types.structured.CreateSessionRequest) ActivateSessionRequest(org.eclipse.milo.opcua.stack.core.types.structured.ActivateSessionRequest) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) Predicate(java.util.function.Predicate) KEY_SESSION_ACTIVITY_LISTENERS(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION_ACTIVITY_LISTENERS) TransferSubscriptionsResponse(org.eclipse.milo.opcua.stack.core.types.structured.TransferSubscriptionsResponse) Streams(com.google.common.collect.Streams) Bytes(com.google.common.primitives.Bytes) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) ServiceFault(org.eclipse.milo.opcua.stack.core.types.structured.ServiceFault) KEY_WAIT_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_WAIT_FUTURE) ActivateSessionResponse(org.eclipse.milo.opcua.stack.core.types.structured.ActivateSessionResponse) List(java.util.List) Stream(java.util.stream.Stream) PrivateKey(java.security.PrivateKey) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) KEY_KEEP_ALIVE_FAILURE_COUNT(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_KEEP_ALIVE_FAILURE_COUNT) EndpointUtil(org.eclipse.milo.opcua.stack.core.util.EndpointUtil) OpcUaSession(org.eclipse.milo.opcua.sdk.client.OpcUaSession) CertificateEncodingException(java.security.cert.CertificateEncodingException) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) CloseSessionRequest(org.eclipse.milo.opcua.stack.core.types.structured.CloseSessionRequest) KEY_SESSION_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION_FUTURE) ActionContext(com.digitalpetri.strictmachine.dsl.ActionContext) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) SignedIdentityToken(org.eclipse.milo.opcua.sdk.client.api.identity.SignedIdentityToken) CompletableFuture(java.util.concurrent.CompletableFuture) CreateSessionResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateSessionResponse) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) ImmutableList(com.google.common.collect.ImmutableList) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) SignatureData(org.eclipse.milo.opcua.stack.core.types.structured.SignatureData) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) FutureUtils.complete(org.eclipse.milo.opcua.stack.core.util.FutureUtils.complete) ServiceFaultListener(org.eclipse.milo.opcua.sdk.client.api.ServiceFaultListener) SessionFuture(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.SessionFuture) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) TransferSubscriptionsRequest(org.eclipse.milo.opcua.stack.core.types.structured.TransferSubscriptionsRequest) KEY_SESSION(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) SignatureUtil(org.eclipse.milo.opcua.stack.core.util.SignatureUtil) Fsm(com.digitalpetri.strictmachine.Fsm) Logger(org.slf4j.Logger) OpcUaClientConfig(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) KEY_KEEP_ALIVE_SCHEDULED_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_KEEP_ALIVE_SCHEDULED_FUTURE) TimeUnit(java.util.concurrent.TimeUnit) NonceUtil(org.eclipse.milo.opcua.stack.core.util.NonceUtil) FsmContext(com.digitalpetri.strictmachine.FsmContext) ApplicationDescription(org.eclipse.milo.opcua.stack.core.types.structured.ApplicationDescription) FutureUtils.failedFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedFuture) UaException(org.eclipse.milo.opcua.stack.core.UaException) FsmBuilder(com.digitalpetri.strictmachine.dsl.FsmBuilder) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) TransferSubscriptionsRequest(org.eclipse.milo.opcua.stack.core.types.structured.TransferSubscriptionsRequest) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) Unit(org.eclipse.milo.opcua.stack.core.util.Unit) TimeUnit(java.util.concurrent.TimeUnit) TransferResult(org.eclipse.milo.opcua.stack.core.types.structured.TransferResult) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) CompletableFuture(java.util.concurrent.CompletableFuture) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) OpcUaSubscriptionManager(org.eclipse.milo.opcua.sdk.client.subscriptions.OpcUaSubscriptionManager)

Example 13 with OpcUaClient

use of org.eclipse.milo.opcua.sdk.client.OpcUaClient in project milo by eclipse.

the class SessionFsmFactory method configureClosingState.

private static void configureClosingState(FsmBuilder<State, Event> fb, OpcUaClient client) {
    /* Transitions */
    fb.when(State.Closing).on(Event.CloseSessionSuccess.class).transitionTo(State.Inactive);
    /* External Transition Actions */
    fb.onTransitionTo(State.Closing).from(State.Active).via(Event.CloseSession.class).execute(ctx -> {
        SessionFsm.CloseFuture closeFuture = new SessionFsm.CloseFuture();
        KEY_CLOSE_FUTURE.set(ctx, closeFuture);
        Event.CloseSession closeSession = (Event.CloseSession) ctx.event();
        complete(closeSession.future).with(closeFuture.future);
        OpcUaSession session = KEY_SESSION.get(ctx);
        closeSession(ctx, client, session).whenComplete((u, ex) -> {
            if (u != null) {
                LOGGER.debug("[{}] Session closed: {}", ctx.getInstanceId(), session);
            } else {
                LOGGER.debug("[{}] CloseSession failed: {}", ctx.getInstanceId(), ex.getMessage(), ex);
            }
            ctx.fireEvent(new Event.CloseSessionSuccess());
        });
    });
    fb.onTransitionFrom(State.Closing).to(State.Inactive).via(Event.CloseSessionSuccess.class).execute(ctx -> {
        SessionFsm.CloseFuture closeFuture = KEY_CLOSE_FUTURE.get(ctx);
        if (closeFuture != null) {
            client.getConfig().getExecutor().execute(() -> closeFuture.future.complete(Unit.VALUE));
        }
    });
    /* Internal Transition Actions */
    fb.onInternalTransition(State.Closing).via(Event.CloseSession.class).execute(ctx -> {
        Event.CloseSession event = (Event.CloseSession) ctx.event();
        SessionFsm.CloseFuture closeFuture = KEY_CLOSE_FUTURE.get(ctx);
        if (closeFuture != null) {
            complete(event.future).with(closeFuture.future);
        }
    });
    fb.onInternalTransition(State.Closing).via(e -> e.getClass() != Event.CloseSession.class).execute(ctx -> ctx.shelveEvent(ctx.event()));
}
Also used : X509Certificate(java.security.cert.X509Certificate) KEY_CLOSE_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_CLOSE_FUTURE) KeyPair(java.security.KeyPair) SignedSoftwareCertificate(org.eclipse.milo.opcua.stack.core.types.structured.SignedSoftwareCertificate) Arrays(java.util.Arrays) ApplicationType(org.eclipse.milo.opcua.stack.core.types.enumerated.ApplicationType) ScheduledFuture(java.util.concurrent.ScheduledFuture) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) LoggerFactory(org.slf4j.LoggerFactory) ServerState(org.eclipse.milo.opcua.stack.core.types.enumerated.ServerState) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) TransferResult(org.eclipse.milo.opcua.stack.core.types.structured.TransferResult) KEY_WAIT_TIME(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_WAIT_TIME) OpcUaSubscriptionManager(org.eclipse.milo.opcua.sdk.client.subscriptions.OpcUaSubscriptionManager) ByteBuffer(java.nio.ByteBuffer) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) UserIdentityToken(org.eclipse.milo.opcua.stack.core.types.structured.UserIdentityToken) SecurityAlgorithm(org.eclipse.milo.opcua.stack.core.security.SecurityAlgorithm) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) Unit(org.eclipse.milo.opcua.stack.core.util.Unit) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) KEY_SESSION_INITIALIZERS(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION_INITIALIZERS) CertificateUtil(org.eclipse.milo.opcua.stack.core.util.CertificateUtil) CreateSessionRequest(org.eclipse.milo.opcua.stack.core.types.structured.CreateSessionRequest) ActivateSessionRequest(org.eclipse.milo.opcua.stack.core.types.structured.ActivateSessionRequest) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) Predicate(java.util.function.Predicate) KEY_SESSION_ACTIVITY_LISTENERS(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION_ACTIVITY_LISTENERS) TransferSubscriptionsResponse(org.eclipse.milo.opcua.stack.core.types.structured.TransferSubscriptionsResponse) Streams(com.google.common.collect.Streams) Bytes(com.google.common.primitives.Bytes) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) ServiceFault(org.eclipse.milo.opcua.stack.core.types.structured.ServiceFault) KEY_WAIT_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_WAIT_FUTURE) ActivateSessionResponse(org.eclipse.milo.opcua.stack.core.types.structured.ActivateSessionResponse) List(java.util.List) Stream(java.util.stream.Stream) PrivateKey(java.security.PrivateKey) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) KEY_KEEP_ALIVE_FAILURE_COUNT(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_KEEP_ALIVE_FAILURE_COUNT) EndpointUtil(org.eclipse.milo.opcua.stack.core.util.EndpointUtil) OpcUaSession(org.eclipse.milo.opcua.sdk.client.OpcUaSession) CertificateEncodingException(java.security.cert.CertificateEncodingException) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) CloseSessionRequest(org.eclipse.milo.opcua.stack.core.types.structured.CloseSessionRequest) KEY_SESSION_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION_FUTURE) ActionContext(com.digitalpetri.strictmachine.dsl.ActionContext) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) SignedIdentityToken(org.eclipse.milo.opcua.sdk.client.api.identity.SignedIdentityToken) CompletableFuture(java.util.concurrent.CompletableFuture) CreateSessionResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateSessionResponse) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) ImmutableList(com.google.common.collect.ImmutableList) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) SignatureData(org.eclipse.milo.opcua.stack.core.types.structured.SignatureData) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) FutureUtils.complete(org.eclipse.milo.opcua.stack.core.util.FutureUtils.complete) ServiceFaultListener(org.eclipse.milo.opcua.sdk.client.api.ServiceFaultListener) SessionFuture(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.SessionFuture) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) TransferSubscriptionsRequest(org.eclipse.milo.opcua.stack.core.types.structured.TransferSubscriptionsRequest) KEY_SESSION(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_SESSION) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) SignatureUtil(org.eclipse.milo.opcua.stack.core.util.SignatureUtil) Fsm(com.digitalpetri.strictmachine.Fsm) Logger(org.slf4j.Logger) OpcUaClientConfig(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) KEY_KEEP_ALIVE_SCHEDULED_FUTURE(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.KEY_KEEP_ALIVE_SCHEDULED_FUTURE) TimeUnit(java.util.concurrent.TimeUnit) NonceUtil(org.eclipse.milo.opcua.stack.core.util.NonceUtil) FsmContext(com.digitalpetri.strictmachine.FsmContext) ApplicationDescription(org.eclipse.milo.opcua.stack.core.types.structured.ApplicationDescription) FutureUtils.failedFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedFuture) UaException(org.eclipse.milo.opcua.stack.core.UaException) FsmBuilder(com.digitalpetri.strictmachine.dsl.FsmBuilder) OpcUaSession(org.eclipse.milo.opcua.sdk.client.OpcUaSession)

Example 14 with OpcUaClient

use of org.eclipse.milo.opcua.sdk.client.OpcUaClient in project milo by eclipse.

the class ClientExampleRunner method run.

public void run() {
    try {
        OpcUaClient client = createClient();
        // For the sake of the examples we will create mutual trust between the client and
        // server so we can run them with security enabled by default.
        // If the client example is pointed at another server then the rejected certificate
        // will need to be moved from the security "pki/rejected" directory to the
        // "pki/trusted/certs" directory.
        // Make the example server trust the example client certificate by default.
        client.getConfig().getCertificate().ifPresent(certificate -> exampleServer.getServer().getConfig().getTrustListManager().addTrustedCertificate(certificate));
        // Make the example client trust the example server certificate by default.
        exampleServer.getServer().getConfig().getCertificateManager().getCertificates().forEach(certificate -> trustListManager.addTrustedCertificate(certificate));
        future.whenCompleteAsync((c, ex) -> {
            if (ex != null) {
                logger.error("Error running example: {}", ex.getMessage(), ex);
            }
            try {
                client.disconnect().get();
                if (serverRequired && exampleServer != null) {
                    exampleServer.shutdown().get();
                }
                Stack.releaseSharedResources();
            } catch (InterruptedException | ExecutionException e) {
                logger.error("Error disconnecting: {}", e.getMessage(), e);
            }
            try {
                Thread.sleep(1000);
                System.exit(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        try {
            clientExample.run(client, future);
            future.get(15, TimeUnit.SECONDS);
        } catch (Throwable t) {
            logger.error("Error running client example: {}", t.getMessage(), t);
            future.completeExceptionally(t);
        }
    } catch (Throwable t) {
        logger.error("Error getting client: {}", t.getMessage(), t);
        future.completeExceptionally(t);
        try {
            Thread.sleep(1000);
            System.exit(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    try {
        Thread.sleep(999_999_999);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) ExecutionException(java.util.concurrent.ExecutionException)

Example 15 with OpcUaClient

use of org.eclipse.milo.opcua.sdk.client.OpcUaClient in project JBM by numen06.

the class OpcUaTemplate method readItem.

public String readItem(String deviceId, OpcPoint point) throws Exception {
    int namespace = point.getNamespace();
    String tag = point.getTagName();
    NodeId nodeId = new NodeId(namespace, tag);
    CompletableFuture<String> value = new CompletableFuture<>();
    OpcUaClient client = getOpcUaClient(deviceId);
    log.debug("start read point(ns={};s={})", namespace, tag);
    client.connect().get();
    client.readValue(0.0, TimestampsToReturn.Both, nodeId).thenAccept(dataValue -> {
        try {
            value.complete(dataValue.getValue().getValue().toString());
        } catch (Exception e) {
            log.error("accept point(ns={};s={}) value error", namespace, tag, e);
        }
    });
    String rawValue = value.get(3, TimeUnit.SECONDS);
    log.debug("end read point(ns={};s={}) value: {}", namespace, tag, rawValue);
    return rawValue;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) OpcPoint(com.jbm.framework.opcua.attribute.OpcPoint) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException)

Aggregations

OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)21 UaException (org.eclipse.milo.opcua.stack.core.UaException)13 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)11 List (java.util.List)9 CompletableFuture (java.util.concurrent.CompletableFuture)9 ExecutionException (java.util.concurrent.ExecutionException)9 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)9 TimestampsToReturn (org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn)9 UaSubscription (org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription)8 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)8 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)8 PrivateKey (java.security.PrivateKey)7 X509Certificate (java.security.cert.X509Certificate)7 OpcUaClientConfig (org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig)7 SecurityPolicy (org.eclipse.milo.opcua.stack.core.security.SecurityPolicy)7 EndpointDescription (org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription)7 ImmutableList (com.google.common.collect.ImmutableList)6 KeyPair (java.security.KeyPair)6 Arrays (java.util.Arrays)6 TimeUnit (java.util.concurrent.TimeUnit)6