Search in sources :

Example 6 with UaStackClient

use of org.eclipse.milo.opcua.stack.client.UaStackClient in project milo by eclipse.

the class ClientServerTest method testClientStateMachine.

@Test
public void testClientStateMachine() throws Exception {
    EndpointDescription endpoint = endpoints[0];
    Variant input = new Variant(42);
    logger.info("SecurityPolicy={}, MessageSecurityMode={}, input={}", SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri()), endpoint.getSecurityMode(), input);
    UaStackClient client = createClient(endpoint);
    for (int i = 0; i < 1000; i++) {
        client.connect().get();
        RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(i), uint(0), null, DEFAULT_TIMEOUT_HINT, null);
        ReadRequest request = new ReadRequest(header, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(NodeId.NULL_VALUE, AttributeId.Value.uid(), null, null) });
        logger.debug("sending request: {}", request);
        UaResponseMessage response = client.sendRequest(request).get();
        logger.debug("got response: {}", response);
        client.disconnect().get();
    }
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) Test(org.testng.annotations.Test)

Example 7 with UaStackClient

use of org.eclipse.milo.opcua.stack.client.UaStackClient in project milo by eclipse.

the class ClientServerTest method testClientServerRoundTrip_TestStack_Basic128Rsa15_SignAndEncrypt.

@Test(dataProvider = "getVariants")
public void testClientServerRoundTrip_TestStack_Basic128Rsa15_SignAndEncrypt(Variant input) throws Exception {
    EndpointDescription endpoint = endpoints[4];
    logger.info("SecurityPolicy={}, MessageSecurityMode={}, input={}", SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri()), endpoint.getSecurityMode(), input);
    UaStackClient client = createClient(endpoint);
    connectAndTest(input, client);
}
Also used : UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) Test(org.testng.annotations.Test)

Example 8 with UaStackClient

use of org.eclipse.milo.opcua.stack.client.UaStackClient in project milo by eclipse.

the class ClientServerTest method testClientServerRoundTrip_TestStack_Basic256_SignAndEncrypt.

@Test(dataProvider = "getVariants")
public void testClientServerRoundTrip_TestStack_Basic256_SignAndEncrypt(Variant input) throws Exception {
    EndpointDescription endpoint = endpoints[5];
    logger.info("SecurityPolicy={}, MessageSecurityMode={}, input={}", SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri()), endpoint.getSecurityMode(), input);
    UaStackClient client = createClient(endpoint);
    connectAndTest(input, client);
}
Also used : UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) Test(org.testng.annotations.Test)

Example 9 with UaStackClient

use of org.eclipse.milo.opcua.stack.client.UaStackClient in project milo by eclipse.

the class SessionFsmFactory method activateSession.

@SuppressWarnings("Duplicates")
private static CompletableFuture<OpcUaSession> activateSession(FsmContext<State, Event> ctx, OpcUaClient client, CreateSessionResponse csr) {
    UaStackClient stackClient = client.getStackClient();
    try {
        EndpointDescription endpoint = client.getConfig().getEndpoint();
        ByteString csrNonce = csr.getServerNonce();
        SignedIdentityToken signedIdentityToken = client.getConfig().getIdentityProvider().getIdentityToken(endpoint, csrNonce);
        UserIdentityToken userIdentityToken = signedIdentityToken.getToken();
        SignatureData userTokenSignature = signedIdentityToken.getSignature();
        ActivateSessionRequest request = new ActivateSessionRequest(client.newRequestHeader(csr.getAuthenticationToken()), buildClientSignature(client.getConfig(), csrNonce), new SignedSoftwareCertificate[0], new String[0], ExtensionObject.encode(client.getStaticSerializationContext(), userIdentityToken), userTokenSignature);
        LOGGER.debug("[{}] Sending ActivateSessionRequest...", ctx.getInstanceId());
        return stackClient.sendRequest(request).thenApply(ActivateSessionResponse.class::cast).thenCompose(asr -> {
            ByteString asrNonce = asr.getServerNonce();
            // TODO check for repeated nonce?
            OpcUaSession session = new OpcUaSession(csr.getAuthenticationToken(), csr.getSessionId(), client.getConfig().getSessionName().get(), csr.getRevisedSessionTimeout(), csr.getMaxRequestMessageSize(), csr.getServerCertificate(), csr.getServerSoftwareCertificates());
            session.setServerNonce(asrNonce);
            return completedFuture(session);
        });
    } catch (Exception ex) {
        return failedFuture(ex);
    }
}
Also used : SignatureData(org.eclipse.milo.opcua.stack.core.types.structured.SignatureData) OpcUaSession(org.eclipse.milo.opcua.sdk.client.OpcUaSession) ActivateSessionRequest(org.eclipse.milo.opcua.stack.core.types.structured.ActivateSessionRequest) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) SignedIdentityToken(org.eclipse.milo.opcua.sdk.client.api.identity.SignedIdentityToken) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) UserIdentityToken(org.eclipse.milo.opcua.stack.core.types.structured.UserIdentityToken) CertificateEncodingException(java.security.cert.CertificateEncodingException) UaException(org.eclipse.milo.opcua.stack.core.UaException)

Example 10 with UaStackClient

use of org.eclipse.milo.opcua.stack.client.UaStackClient 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)

Aggregations

UaStackClient (org.eclipse.milo.opcua.stack.client.UaStackClient)17 EndpointDescription (org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription)15 Test (org.testng.annotations.Test)11 RequestHeader (org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader)9 ReadRequest (org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)8 ReadValueId (org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId)8 UaException (org.eclipse.milo.opcua.stack.core.UaException)6 CompletableFuture (java.util.concurrent.CompletableFuture)5 CertificateEncodingException (java.security.cert.CertificateEncodingException)4 List (java.util.List)4 TimeUnit (java.util.concurrent.TimeUnit)4 Stream (java.util.stream.Stream)4 OpcUaSession (org.eclipse.milo.opcua.sdk.client.OpcUaSession)4 SignedIdentityToken (org.eclipse.milo.opcua.sdk.client.api.identity.SignedIdentityToken)4 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)4 Unit (org.eclipse.milo.opcua.stack.core.util.Unit)4 Fsm (com.digitalpetri.strictmachine.Fsm)3 FsmContext (com.digitalpetri.strictmachine.FsmContext)3 ActionContext (com.digitalpetri.strictmachine.dsl.ActionContext)3 FsmBuilder (com.digitalpetri.strictmachine.dsl.FsmBuilder)3