Search in sources :

Example 11 with RequestHeader

use of org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader in project milo by eclipse.

the class DiscoveryClient method findServers.

/**
 * Query the FindServers service at the {@code endpointUrl}.
 * <p>
 * The endpoint URL(s) for each server {@link ApplicationDescription} in the response can then be used in a
 * {@link #getEndpoints(String)} call to discover the endpoints for that server.
 *
 * @param endpointUrl the endpoint URL to find servers at.
 * @param localeIds   list of locales to use. The server should return the applicationName in the
 *                    ApplicationDescription using one of locales specified. If the server supports more than one of
 *                    the requested locales then the server shall use the locale that appears first in this list. If
 *                    the server does not support any of the requested locales it chooses an appropriate default
 *                    locale. The server chooses an appropriate default locale if this list is empty.
 * @param serverUris  list of servers to return. All known servers are returned if the list is empty.
 * @return the {@link FindServersResponse}s returned by the FindServers service.
 */
public CompletableFuture<FindServersResponse> findServers(String endpointUrl, String[] localeIds, String[] serverUris) {
    RequestHeader requestHeader = stackClient.newRequestHeader(NodeId.NULL_VALUE, stackClient.getConfig().getRequestTimeout());
    FindServersRequest request = new FindServersRequest(requestHeader, endpointUrl, localeIds, serverUris);
    return stackClient.sendRequest(request).thenApply(FindServersResponse.class::cast);
}
Also used : RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) FindServersResponse(org.eclipse.milo.opcua.stack.core.types.structured.FindServersResponse) FindServersRequest(org.eclipse.milo.opcua.stack.core.types.structured.FindServersRequest)

Example 12 with RequestHeader

use of org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader in project milo by eclipse.

the class UaStackClient method sendRequest.

/**
 * Send a {@link UaRequestMessage} to the connected server.
 * <p>
 * The {@link RequestHeader} of {@code request} must have a unique request handle. Use the
 * {@code newRequestHeader} helper functions to create headers for {@link UaRequestMessage}s.
 *
 * @param request the {@link UaRequestMessage} to send.
 * @return a {@link CompletableFuture} containing the eventual {@link UaResponseMessage} from the server.
 * @see #newRequestHeader()
 * @see #newRequestHeader(NodeId)
 * @see #newRequestHeader(NodeId, UInteger)
 */
public CompletableFuture<UaResponseMessage> sendRequest(UaRequestMessage request) {
    RequestHeader requestHeader = request.getRequestHeader();
    UInteger requestHandle = requestHeader.getRequestHandle();
    final CompletableFuture<UaResponseMessage> future = new CompletableFuture<>();
    pending.put(requestHandle, future);
    transport.sendRequest(request).whenComplete((response, ex) -> {
        pending.remove(requestHandle);
        deliverResponse(request, response, ex, future);
    });
    return future;
}
Also used : UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) CompletableFuture(java.util.concurrent.CompletableFuture) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader)

Example 13 with RequestHeader

use of org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader in project milo by eclipse.

the class ClientServerTest method testClientReconnect.

@Test
public void testClientReconnect() 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);
    client.connect().get();
    RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(0), 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.info("sending request: {}", request);
    UaResponseMessage response0 = client.sendRequest(request).get();
    logger.info("got response: {}", response0);
    logger.info("initiating a reconnect by closing channel in server...");
    server.getConnectedChannels().forEach(c -> {
        try {
            c.close().await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    logger.info("sending request: {}", request);
    try {
        UaResponseMessage response1 = client.sendRequest(request).get();
        logger.info("got response: {}", response1);
    } catch (Exception e) {
        // try again because close() above is a race condition
        UaResponseMessage response1 = client.sendRequest(request).get();
        logger.info("got response: {}", response1);
    }
    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) UaException(org.eclipse.milo.opcua.stack.core.UaException) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) Test(org.testng.annotations.Test)

Example 14 with RequestHeader

use of org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader in project milo by eclipse.

the class DataTypeTreeBuilder method readNamespaceTable.

private static CompletableFuture<NamespaceTable> readNamespaceTable(UaStackClient client, OpcUaSession session) {
    RequestHeader requestHeader = client.newRequestHeader(session.getAuthenticationToken(), client.getConfig().getRequestTimeout());
    CompletableFuture<UaResponseMessage> readFuture = client.sendRequest(new ReadRequest(requestHeader, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(Identifiers.Server_NamespaceArray, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE) }));
    return readFuture.thenApply(ReadResponse.class::cast).thenApply(response -> {
        DataValue dataValue = response.getResults()[0];
        String[] namespaceUris = (String[]) dataValue.getValue().getValue();
        NamespaceTable namespaceTable = new NamespaceTable();
        for (String namespaceUri : namespaceUris) {
            namespaceTable.addUri(namespaceUri);
        }
        return namespaceTable;
    });
}
Also used : UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) NamespaceTable(org.eclipse.milo.opcua.stack.core.NamespaceTable) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)

Example 15 with RequestHeader

use of org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader in project milo by eclipse.

the class DataTypeDictionaryReader method readNodes.

private CompletableFuture<List<DataValue>> readNodes(List<ReadValueId> readValueIds) {
    RequestHeader requestHeader = stackClient.newRequestHeader(session.getAuthenticationToken(), stackClient.getConfig().getRequestTimeout());
    ReadRequest readRequest = new ReadRequest(requestHeader, 0.0, TimestampsToReturn.Neither, readValueIds.toArray(new ReadValueId[0]));
    return stackClient.sendRequest(readRequest).thenApply(ReadResponse.class::cast).thenApply(r -> l(r.getResults()));
}
Also used : ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)

Aggregations

RequestHeader (org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader)18 ReadRequest (org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)9 ReadValueId (org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId)9 UaStackClient (org.eclipse.milo.opcua.stack.client.UaStackClient)7 CompletableFuture (java.util.concurrent.CompletableFuture)6 UaResponseMessage (org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage)6 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)5 UaException (org.eclipse.milo.opcua.stack.core.UaException)4 EndpointDescription (org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription)4 List (java.util.List)3 StatusCodes (org.eclipse.milo.opcua.stack.core.StatusCodes)3 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)3 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)3 BrowseNextRequest (org.eclipse.milo.opcua.stack.core.types.structured.BrowseNextRequest)3 Test (org.testng.annotations.Test)3 Lists (com.google.common.collect.Lists)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)2