Search in sources :

Example 16 with RequestHeader

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

the class DiscoveryClient method getEndpoints.

/**
 * Query the GetEndpoints service at {@code endpointUrl}.
 *
 * @param endpointUrl the endpoint URL to get endpoints from.
 * @param localeIds   list of locales to use. Specifies the locale to use when returning human-readable strings.
 * @param profileUris list of Transport Profile that the returned Endpoints shall support. All Endpoints are
 *                    returned if the list is empty.
 * @return the {@link GetEndpointsResponse} returned by the GetEndpoints service.
 */
public CompletableFuture<GetEndpointsResponse> getEndpoints(String endpointUrl, String[] localeIds, String[] profileUris) {
    RequestHeader header = stackClient.newRequestHeader(NodeId.NULL_VALUE, stackClient.getConfig().getRequestTimeout());
    GetEndpointsRequest request = new GetEndpointsRequest(header, endpointUrl, localeIds, profileUris);
    return stackClient.sendRequest(request).thenApply(GetEndpointsResponse.class::cast);
}
Also used : GetEndpointsResponse(org.eclipse.milo.opcua.stack.core.types.structured.GetEndpointsResponse) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) GetEndpointsRequest(org.eclipse.milo.opcua.stack.core.types.structured.GetEndpointsRequest)

Example 17 with RequestHeader

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

the class OpcUaSubscriptionManager method sendPublishRequest.

private void sendPublishRequest(UaSession session, AtomicLong pendingCount) {
    List<SubscriptionAcknowledgement> subscriptionAcknowledgements = new ArrayList<>();
    subscriptions.values().forEach(subscription -> {
        synchronized (subscription.availableAcknowledgements) {
            subscription.availableAcknowledgements.forEach(sequenceNumber -> subscriptionAcknowledgements.add(new SubscriptionAcknowledgement(subscription.getSubscriptionId(), sequenceNumber)));
            subscription.availableAcknowledgements.clear();
        }
    });
    RequestHeader requestHeader = client.getStackClient().newRequestHeader(session.getAuthenticationToken(), getTimeoutHint());
    UInteger requestHandle = requestHeader.getRequestHandle();
    PublishRequest request = new PublishRequest(requestHeader, subscriptionAcknowledgements.toArray(new SubscriptionAcknowledgement[0]));
    if (logger.isDebugEnabled()) {
        String[] ackStrings = subscriptionAcknowledgements.stream().map(ack -> String.format("id=%s/seq=%s", ack.getSubscriptionId(), ack.getSequenceNumber())).toArray(String[]::new);
        logger.debug("Sending PublishRequest, requestHandle={}, acknowledgements={}", requestHandle, Arrays.toString(ackStrings));
    }
    client.<PublishResponse>sendRequest(request).whenComplete((response, ex) -> {
        if (response != null) {
            logger.debug("Received PublishResponse, sequenceNumber={}", response.getNotificationMessage().getSequenceNumber());
            processingQueue.submit(() -> onPublishComplete(response, pendingCount));
        } else {
            StatusCode statusCode = UaException.extract(ex).map(UaException::getStatusCode).orElse(StatusCode.BAD);
            logger.debug("Publish service failure (requestHandle={}): {}", requestHandle, statusCode, ex);
            pendingCount.getAndUpdate(p -> (p > 0) ? p - 1 : 0);
            if (statusCode.getValue() != StatusCodes.Bad_NoSubscription && statusCode.getValue() != StatusCodes.Bad_TooManyPublishRequests) {
                maybeSendPublishRequests();
            }
            UaException uax = UaException.extract(ex).orElse(new UaException(ex));
            subscriptionListeners.forEach(l -> l.onPublishFailure(uax));
        }
    });
}
Also used : Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) BiFunction(java.util.function.BiFunction) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) UaSubscriptionManager(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscriptionManager) LoggerFactory(org.slf4j.LoggerFactory) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) PublishRequest(org.eclipse.milo.opcua.stack.core.types.structured.PublishRequest) DateTime(org.eclipse.milo.opcua.stack.core.types.builtin.DateTime) 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) Map(java.util.Map) BigInteger(java.math.BigInteger) CreateSubscriptionResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionResponse) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) EventNotificationList(org.eclipse.milo.opcua.stack.core.types.structured.EventNotificationList) RepublishResponse(org.eclipse.milo.opcua.stack.core.types.structured.RepublishResponse) SubscriptionAcknowledgement(org.eclipse.milo.opcua.stack.core.types.structured.SubscriptionAcknowledgement) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) EventFieldList(org.eclipse.milo.opcua.stack.core.types.structured.EventFieldList) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) UaSession(org.eclipse.milo.opcua.sdk.client.api.UaSession) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) NotificationMessage(org.eclipse.milo.opcua.stack.core.types.structured.NotificationMessage) CompletableFuture(java.util.concurrent.CompletableFuture) DataChangeNotification(org.eclipse.milo.opcua.stack.core.types.structured.DataChangeNotification) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) UaMonitoredItem(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) FutureUtils.failedUaFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedUaFuture) SessionActivityListener(org.eclipse.milo.opcua.sdk.client.SessionActivityListener) StatusChangeNotification(org.eclipse.milo.opcua.stack.core.types.structured.StatusChangeNotification) ExecutionQueue(org.eclipse.milo.opcua.stack.core.util.ExecutionQueue) Logger(org.slf4j.Logger) MonitoredItemNotification(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemNotification) PublishResponse(org.eclipse.milo.opcua.stack.core.types.structured.PublishResponse) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) Maps(com.google.common.collect.Maps) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) ModifySubscriptionResponse(org.eclipse.milo.opcua.stack.core.types.structured.ModifySubscriptionResponse) UaException(org.eclipse.milo.opcua.stack.core.UaException) Comparator(java.util.Comparator) Collections(java.util.Collections) SubscriptionAcknowledgement(org.eclipse.milo.opcua.stack.core.types.structured.SubscriptionAcknowledgement) UaException(org.eclipse.milo.opcua.stack.core.UaException) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) PublishRequest(org.eclipse.milo.opcua.stack.core.types.structured.PublishRequest) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)

Example 18 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)

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