Search in sources :

Example 1 with UaResponseMessage

use of org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage 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 2 with UaResponseMessage

use of org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage in project milo by eclipse.

the class ClientServerTest method testClientDisconnect.

@Test
public void testClientDisconnect() 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);
    client.disconnect().get();
    assertThrows(() -> client.sendRequest(request).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) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) Test(org.testng.annotations.Test)

Example 3 with UaResponseMessage

use of org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage 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 4 with UaResponseMessage

use of org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage 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 5 with UaResponseMessage

use of org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage in project milo by eclipse.

the class OpcUaClient method maybeHandleServiceFault.

private void maybeHandleServiceFault(UaResponseMessage response, Throwable ex) {
    if (faultListeners.isEmpty())
        return;
    if (ex != null) {
        if (ex instanceof UaServiceFaultException) {
            UaServiceFaultException faultException = (UaServiceFaultException) ex;
            ServiceFault serviceFault = faultException.getServiceFault();
            logger.debug("Notifying {} ServiceFaultListeners", faultListeners.size());
            faultNotificationQueue.submit(() -> faultListeners.forEach(h -> h.onServiceFault(serviceFault)));
        } else if (ex.getCause() instanceof UaServiceFaultException) {
            UaServiceFaultException faultException = (UaServiceFaultException) ex.getCause();
            ServiceFault serviceFault = faultException.getServiceFault();
            logger.debug("Notifying {} ServiceFaultListeners", faultListeners.size());
            faultNotificationQueue.submit(() -> faultListeners.forEach(h -> h.onServiceFault(serviceFault)));
        }
    }
}
Also used : Arrays(java.util.Arrays) BrowseNextRequest(org.eclipse.milo.opcua.stack.core.types.structured.BrowseNextRequest) WriteValue(org.eclipse.milo.opcua.stack.core.types.structured.WriteValue) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) PublishRequest(org.eclipse.milo.opcua.stack.core.types.structured.PublishRequest) WriteRequest(org.eclipse.milo.opcua.stack.core.types.structured.WriteRequest) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) DataTypeManager(org.eclipse.milo.opcua.stack.core.types.DataTypeManager) WriteResponse(org.eclipse.milo.opcua.stack.core.types.structured.WriteResponse) SessionInitializer(org.eclipse.milo.opcua.sdk.client.session.SessionFsm.SessionInitializer) TranslateBrowsePathsToNodeIdsRequest(org.eclipse.milo.opcua.stack.core.types.structured.TranslateBrowsePathsToNodeIdsRequest) ModifySubscriptionRequest(org.eclipse.milo.opcua.stack.core.types.structured.ModifySubscriptionRequest) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ModifyMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.ModifyMonitoredItemsRequest) UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) AddReferencesItem(org.eclipse.milo.opcua.stack.core.types.structured.AddReferencesItem) TransferSubscriptionsResponse(org.eclipse.milo.opcua.stack.core.types.structured.TransferSubscriptionsResponse) ManifestUtil(org.eclipse.milo.opcua.stack.core.util.ManifestUtil) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) SetPublishingModeResponse(org.eclipse.milo.opcua.stack.core.types.structured.SetPublishingModeResponse) SetMonitoringModeRequest(org.eclipse.milo.opcua.stack.core.types.structured.SetMonitoringModeRequest) MonitoringMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode) SetMonitoringModeResponse(org.eclipse.milo.opcua.stack.core.types.structured.SetMonitoringModeResponse) RepublishRequest(org.eclipse.milo.opcua.stack.core.types.structured.RepublishRequest) ModifyMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.ModifyMonitoredItemsResponse) ConversionUtil.a(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.a) OpcUaClientConfigBuilder(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder) MonitoredItemModifyRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemModifyRequest) Stack(org.eclipse.milo.opcua.stack.core.Stack) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) DeleteNodesRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteNodesRequest) HistoryUpdateRequest(org.eclipse.milo.opcua.stack.core.types.structured.HistoryUpdateRequest) RegisterNodesRequest(org.eclipse.milo.opcua.stack.core.types.structured.RegisterNodesRequest) UaRequestMessage(org.eclipse.milo.opcua.stack.core.serialization.UaRequestMessage) DeleteNodesItem(org.eclipse.milo.opcua.stack.core.types.structured.DeleteNodesItem) DeleteMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteMonitoredItemsRequest) DeleteMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteMonitoredItemsResponse) TransferSubscriptionsRequest(org.eclipse.milo.opcua.stack.core.types.structured.TransferSubscriptionsRequest) DeleteReferencesItem(org.eclipse.milo.opcua.stack.core.types.structured.DeleteReferencesItem) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) RegisterNodesResponse(org.eclipse.milo.opcua.stack.core.types.structured.RegisterNodesResponse) Lists.newCopyOnWriteArrayList(com.google.common.collect.Lists.newCopyOnWriteArrayList) OpcUaClientConfig(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig) SessionFsm(org.eclipse.milo.opcua.sdk.client.session.SessionFsm) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) SetTriggeringRequest(org.eclipse.milo.opcua.stack.core.types.structured.SetTriggeringRequest) ExecutionException(java.util.concurrent.ExecutionException) DeleteReferencesResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteReferencesResponse) AddNodesResponse(org.eclipse.milo.opcua.stack.core.types.structured.AddNodesResponse) AddNodesRequest(org.eclipse.milo.opcua.stack.core.types.structured.AddNodesRequest) DiscoveryClient(org.eclipse.milo.opcua.stack.client.DiscoveryClient) Namespaces(org.eclipse.milo.opcua.stack.core.util.Namespaces) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) LoggerFactory(org.slf4j.LoggerFactory) AddNodesItem(org.eclipse.milo.opcua.stack.core.types.structured.AddNodesItem) DeleteReferencesRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteReferencesRequest) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) OpcUaSubscriptionManager(org.eclipse.milo.opcua.sdk.client.subscriptions.OpcUaSubscriptionManager) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) 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) CreateSubscriptionResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionResponse) DeleteNodesResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteNodesResponse) VariableTypeInitializer(org.eclipse.milo.opcua.sdk.client.model.VariableTypeInitializer) HistoryReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadRequest) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) Predicate(java.util.function.Predicate) BrowsePath(org.eclipse.milo.opcua.stack.core.types.structured.BrowsePath) HistoryUpdateDetails(org.eclipse.milo.opcua.stack.core.types.structured.HistoryUpdateDetails) RepublishResponse(org.eclipse.milo.opcua.stack.core.types.structured.RepublishResponse) ServiceFault(org.eclipse.milo.opcua.stack.core.types.structured.ServiceFault) Objects(java.util.Objects) SubscriptionAcknowledgement(org.eclipse.milo.opcua.stack.core.types.structured.SubscriptionAcknowledgement) List(java.util.List) UnregisterNodesResponse(org.eclipse.milo.opcua.stack.core.types.structured.UnregisterNodesResponse) AddReferencesRequest(org.eclipse.milo.opcua.stack.core.types.structured.AddReferencesRequest) AddReferencesResponse(org.eclipse.milo.opcua.stack.core.types.structured.AddReferencesResponse) TranslateBrowsePathsToNodeIdsResponse(org.eclipse.milo.opcua.stack.core.types.structured.TranslateBrowsePathsToNodeIdsResponse) Optional(java.util.Optional) DeleteSubscriptionsResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteSubscriptionsResponse) CreateSubscriptionRequest(org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionRequest) BrowseResponse(org.eclipse.milo.opcua.stack.core.types.structured.BrowseResponse) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) BrowseRequest(org.eclipse.milo.opcua.stack.core.types.structured.BrowseRequest) UnregisterNodesRequest(org.eclipse.milo.opcua.stack.core.types.structured.UnregisterNodesRequest) CallMethodRequest(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodRequest) CallResponse(org.eclipse.milo.opcua.stack.core.types.structured.CallResponse) CreateMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsResponse) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) NamespaceTable(org.eclipse.milo.opcua.stack.core.NamespaceTable) CallRequest(org.eclipse.milo.opcua.stack.core.types.structured.CallRequest) SerializationContext(org.eclipse.milo.opcua.stack.core.serialization.SerializationContext) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) SessionFsmFactory(org.eclipse.milo.opcua.sdk.client.session.SessionFsmFactory) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) ServiceFaultListener(org.eclipse.milo.opcua.sdk.client.api.ServiceFaultListener) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) BrowseNextResponse(org.eclipse.milo.opcua.stack.core.types.structured.BrowseNextResponse) DeleteSubscriptionsRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteSubscriptionsRequest) HistoryReadDetails(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadDetails) SetTriggeringResponse(org.eclipse.milo.opcua.stack.core.types.structured.SetTriggeringResponse) HistoryReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadResponse) HistoryUpdateResponse(org.eclipse.milo.opcua.stack.core.types.structured.HistoryUpdateResponse) ExecutionQueue(org.eclipse.milo.opcua.stack.core.util.ExecutionQueue) Logger(org.slf4j.Logger) PublishResponse(org.eclipse.milo.opcua.stack.core.types.structured.PublishResponse) Unsigned.ushort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ushort) ObjectTypeInitializer(org.eclipse.milo.opcua.sdk.client.model.ObjectTypeInitializer) UserTokenType(org.eclipse.milo.opcua.stack.core.types.enumerated.UserTokenType) UaServiceFaultException(org.eclipse.milo.opcua.stack.core.UaServiceFaultException) ViewDescription(org.eclipse.milo.opcua.stack.core.types.structured.ViewDescription) CreateMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsRequest) SetPublishingModeRequest(org.eclipse.milo.opcua.stack.core.types.structured.SetPublishingModeRequest) UaClient(org.eclipse.milo.opcua.sdk.client.api.UaClient) ModifySubscriptionResponse(org.eclipse.milo.opcua.stack.core.types.structured.ModifySubscriptionResponse) UaException(org.eclipse.milo.opcua.stack.core.UaException) HistoryReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.HistoryReadValueId) ServiceFault(org.eclipse.milo.opcua.stack.core.types.structured.ServiceFault) UaServiceFaultException(org.eclipse.milo.opcua.stack.core.UaServiceFaultException)

Aggregations

UaResponseMessage (org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage)9 RequestHeader (org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader)6 UaException (org.eclipse.milo.opcua.stack.core.UaException)5 ReadRequest (org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)5 ReadValueId (org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId)5 UaStackClient (org.eclipse.milo.opcua.stack.client.UaStackClient)4 EndpointDescription (org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription)4 ByteBuf (io.netty.buffer.ByteBuf)3 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 UaTransportRequest (org.eclipse.milo.opcua.stack.client.transport.UaTransportRequest)2 NamespaceTable (org.eclipse.milo.opcua.stack.core.NamespaceTable)2 UaServiceFaultException (org.eclipse.milo.opcua.stack.core.UaServiceFaultException)2 ChunkDecoder (org.eclipse.milo.opcua.stack.core.channel.ChunkDecoder)2 MessageAbortException (org.eclipse.milo.opcua.stack.core.channel.MessageAbortException)2 MessageDecodeException (org.eclipse.milo.opcua.stack.core.channel.MessageDecodeException)2 SecurityPolicy (org.eclipse.milo.opcua.stack.core.security.SecurityPolicy)2 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)2 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)2 ServiceFault (org.eclipse.milo.opcua.stack.core.types.structured.ServiceFault)2