Search in sources :

Example 1 with Unit

use of org.eclipse.milo.opcua.stack.core.util.Unit in project milo by eclipse.

the class DataTypeTreeBuilder method addChildren.

private static CompletableFuture<Unit> addChildren(Tree<DataTypeTree.DataType> tree, UaStackClient client, OpcUaSession session, NamespaceTable namespaceTable) {
    CompletableFuture<List<ReferenceDescription>> subtypes = browseSafe(client, session, new BrowseDescription(tree.getValue().getNodeId(), BrowseDirection.Forward, Identifiers.HasSubtype, false, uint(NodeClass.DataType.getValue()), uint(BrowseResultMask.All.getValue())));
    CompletableFuture<List<DataTypeTree.DataType>> dataTypesFuture = subtypes.thenCompose(references -> {
        Stream<CompletableFuture<DataTypeTree.DataType>> dataTypeFutures = references.stream().map(dataTypeReference -> {
            NodeId dataTypeId = dataTypeReference.getNodeId().toNodeId(namespaceTable).orElse(NodeId.NULL_VALUE);
            CompletableFuture<List<ReferenceDescription>> encodings = browseSafe(client, session, new BrowseDescription(dataTypeId, BrowseDirection.Forward, Identifiers.HasEncoding, false, uint(NodeClass.Object.getValue()), uint(BrowseResultMask.All.getValue())));
            return encodings.thenApply(encodingReferences -> {
                NodeId binaryEncodingId = null;
                NodeId xmlEncodingId = null;
                for (ReferenceDescription r : encodingReferences) {
                    if (r.getBrowseName().equals(OpcUaDefaultBinaryEncoding.ENCODING_NAME)) {
                        binaryEncodingId = r.getNodeId().toNodeId(namespaceTable).orElse(null);
                    } else if (r.getBrowseName().equals(OpcUaDefaultXmlEncoding.ENCODING_NAME)) {
                        xmlEncodingId = r.getNodeId().toNodeId(namespaceTable).orElse(null);
                    }
                }
                return new DataTypeTree.DataType(dataTypeReference.getBrowseName(), dataTypeId, binaryEncodingId, xmlEncodingId);
            });
        });
        return FutureUtils.sequence(dataTypeFutures);
    });
    return dataTypesFuture.thenCompose(dataTypes -> {
        Stream<CompletableFuture<Unit>> futures = dataTypes.stream().map(tree::addChild).map(childNode -> addChildren(childNode, client, session, namespaceTable));
        return FutureUtils.sequence(futures);
    }).thenApply(v -> Unit.VALUE);
}
Also used : OpcUaDefaultBinaryEncoding(org.eclipse.milo.opcua.stack.core.types.OpcUaDefaultBinaryEncoding) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) CompletableFuture(java.util.concurrent.CompletableFuture) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) Tree(org.eclipse.milo.opcua.stack.core.util.Tree) NamespaceTable(org.eclipse.milo.opcua.stack.core.NamespaceTable) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) Unit(org.eclipse.milo.opcua.stack.core.util.Unit) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) OpcUaDefaultXmlEncoding(org.eclipse.milo.opcua.stack.core.types.OpcUaDefaultXmlEncoding) DataTypeTree(org.eclipse.milo.opcua.sdk.core.DataTypeTree) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ReferenceDescription(org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription) BrowseDirection(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) BrowseResultMask(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseResultMask) Stream(java.util.stream.Stream) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) FutureUtils(org.eclipse.milo.opcua.stack.core.util.FutureUtils) UaException(org.eclipse.milo.opcua.stack.core.UaException) Collections(java.util.Collections) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) Unit(org.eclipse.milo.opcua.stack.core.util.Unit) CompletableFuture(java.util.concurrent.CompletableFuture) DataTypeTree(org.eclipse.milo.opcua.sdk.core.DataTypeTree) ReferenceDescription(org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) List(java.util.List) Stream(java.util.stream.Stream) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription)

Example 2 with Unit

use of org.eclipse.milo.opcua.stack.core.util.Unit in project milo by eclipse.

the class ServerChannelManager method bind.

public CompletableFuture<Unit> bind(EndpointConfiguration endpoint) {
    CompletableFuture<Unit> future = new CompletableFuture<>();
    semaphore.acquire().thenApply(permit -> {
        future.whenComplete((u, ex) -> permit.release());
        InetSocketAddress bindAddress = new InetSocketAddress(endpoint.getBindAddress(), endpoint.getBindPort());
        if (channels.containsKey(bindAddress)) {
            return future.complete(Unit.VALUE);
        } else {
            logger.debug("binding to {}", bindAddress);
            CompletableFuture<Channel> bootstrap = bootstrap(stackServer, bindAddress, endpoint.getTransportProfile());
            return bootstrap.whenComplete((channel, ex) -> {
                if (channel != null) {
                    addresses.add(bindAddress);
                    channels.put(bindAddress, channel);
                    future.complete(Unit.VALUE);
                } else {
                    future.completeExceptionally(ex);
                }
            });
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) Unit(org.eclipse.milo.opcua.stack.core.util.Unit)

Example 3 with Unit

use of org.eclipse.milo.opcua.stack.core.util.Unit in project milo by eclipse.

the class ServerChannelManager method unbind.

public CompletableFuture<Unit> unbind(EndpointConfiguration endpoint) {
    CompletableFuture<Unit> future = new CompletableFuture<>();
    semaphore.acquire().thenAccept(permit -> {
        future.whenComplete((u, ex) -> permit.release());
        InetSocketAddress bindAddress = new InetSocketAddress(endpoint.getBindAddress(), endpoint.getBindPort());
        if (addresses.remove(bindAddress, 1) == 1) {
            logger.debug("unbinding from {}", bindAddress);
            Channel channel = channels.remove(bindAddress);
            if (channel != null) {
                channel.close();
            }
        }
        future.complete(Unit.VALUE);
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) InetSocketAddress(java.net.InetSocketAddress) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) Unit(org.eclipse.milo.opcua.stack.core.util.Unit)

Example 4 with Unit

use of org.eclipse.milo.opcua.stack.core.util.Unit in project milo by eclipse.

the class SessionFsmFactory method closeSession.

private static CompletableFuture<Unit> closeSession(FsmContext<State, Event> ctx, OpcUaClient client, OpcUaSession session) {
    CompletableFuture<Unit> closeFuture = new CompletableFuture<>();
    UaStackClient stackClient = client.getStackClient();
    RequestHeader requestHeader = stackClient.newRequestHeader(session.getAuthenticationToken(), uint(5000));
    CloseSessionRequest request = new CloseSessionRequest(requestHeader, true);
    LOGGER.debug("[{}] Sending CloseSessionRequest...", ctx.getInstanceId());
    stackClient.sendRequest(request).whenCompleteAsync((csr, ex2) -> closeFuture.complete(Unit.VALUE), client.getConfig().getExecutor());
    return closeFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) CloseSessionRequest(org.eclipse.milo.opcua.stack.core.types.structured.CloseSessionRequest) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) Unit(org.eclipse.milo.opcua.stack.core.util.Unit) TimeUnit(java.util.concurrent.TimeUnit)

Example 5 with Unit

use of org.eclipse.milo.opcua.stack.core.util.Unit 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);
    }
}
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) CompletableFuture(java.util.concurrent.CompletableFuture) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)9 Unit (org.eclipse.milo.opcua.stack.core.util.Unit)9 List (java.util.List)6 StatusCodes (org.eclipse.milo.opcua.stack.core.StatusCodes)6 UaException (org.eclipse.milo.opcua.stack.core.UaException)6 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)5 OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)5 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)5 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)5 TimestampsToReturn (org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn)5 ReadValueId (org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId)5 TimeUnit (java.util.concurrent.TimeUnit)4 RequestHeader (org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader)4 ImmutableList (com.google.common.collect.ImmutableList)3 Arrays (java.util.Arrays)3 ExecutionException (java.util.concurrent.ExecutionException)3 ScheduledFuture (java.util.concurrent.ScheduledFuture)3 Stream (java.util.stream.Stream)3 UaMonitoredItem (org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem)3 UaSubscription (org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription)3