Search in sources :

Example 1 with CreateSubscriptionResponse

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

the class SubscriptionManager method createSubscription.

public void createSubscription(ServiceRequest service) {
    CreateSubscriptionRequest request = (CreateSubscriptionRequest) service.getRequest();
    UInteger subscriptionId = nextSubscriptionId();
    Subscription subscription = new Subscription(this, subscriptionId, request.getRequestedPublishingInterval(), request.getRequestedMaxKeepAliveCount().longValue(), request.getRequestedLifetimeCount().longValue(), request.getMaxNotificationsPerPublish().longValue(), request.getPublishingEnabled(), request.getPriority().intValue());
    subscriptions.put(subscriptionId, subscription);
    server.getSubscriptions().put(subscriptionId, subscription);
    server.getDiagnosticsSummary().getCumulatedSubscriptionCount().increment();
    server.getEventBus().post(new SubscriptionCreatedEvent(subscription));
    subscription.setStateListener((s, ps, cs) -> {
        if (cs == State.Closed) {
            subscriptions.remove(s.getId());
            server.getSubscriptions().remove(s.getId());
            server.getEventBus().post(new SubscriptionDeletedEvent(subscription));
        }
    });
    subscription.startPublishingTimer();
    ResponseHeader header = service.createResponseHeader();
    CreateSubscriptionResponse response = new CreateSubscriptionResponse(header, subscriptionId, subscription.getPublishingInterval(), uint(subscription.getLifetimeCount()), uint(subscription.getMaxKeepAliveCount()));
    service.setResponse(response);
}
Also used : ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) CreateSubscriptionRequest(org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionRequest) CreateSubscriptionResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionResponse)

Example 2 with CreateSubscriptionResponse

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

the class OpcUaSubscriptionManager method createSubscription.

@Override
public CompletableFuture<UaSubscription> createSubscription(double requestedPublishingInterval, BiFunction<Double, UInteger, UInteger> getLifetimeCount, Function<Double, UInteger> getMaxKeepAliveCount, UInteger maxNotificationsPerPublish, boolean publishingEnabled, UByte priority) {
    UInteger requestedMaxKeepAliveCount = getMaxKeepAliveCount.apply(requestedPublishingInterval);
    UInteger requestedLifetimeCount = getLifetimeCount.apply(requestedPublishingInterval, requestedMaxKeepAliveCount);
    CompletableFuture<CreateSubscriptionResponse> future = client.createSubscription(requestedPublishingInterval, requestedLifetimeCount, requestedMaxKeepAliveCount, maxNotificationsPerPublish, publishingEnabled, priority);
    return future.thenCompose(response -> {
        OpcUaSubscription subscription = new OpcUaSubscription(client, response.getSubscriptionId(), response.getRevisedPublishingInterval(), response.getRevisedLifetimeCount(), response.getRevisedMaxKeepAliveCount(), maxNotificationsPerPublish, publishingEnabled, priority);
        subscription.setRequestedPublishingInterval(requestedPublishingInterval);
        subscription.setRequestedLifetimeCount(requestedLifetimeCount);
        subscription.setRequestedMaxKeepAliveCount(requestedMaxKeepAliveCount);
        double revisedPublishingInterval = response.getRevisedPublishingInterval();
        UInteger newMaxKeepAliveCount = getMaxKeepAliveCount.apply(revisedPublishingInterval);
        if (requestedPublishingInterval != revisedPublishingInterval && !requestedMaxKeepAliveCount.equals(newMaxKeepAliveCount)) {
            UInteger newLifetimeCount = getLifetimeCount.apply(revisedPublishingInterval, newMaxKeepAliveCount);
            CompletableFuture<ModifySubscriptionResponse> modifyFuture = client.modifySubscription(response.getSubscriptionId(), revisedPublishingInterval, newLifetimeCount, newMaxKeepAliveCount, maxNotificationsPerPublish, priority);
            return modifyFuture.thenApply(modifyResponse -> {
                subscription.setRequestedLifetimeCount(newLifetimeCount);
                subscription.setRequestedMaxKeepAliveCount(newMaxKeepAliveCount);
                subscription.setRevisedPublishingInterval(modifyResponse.getRevisedPublishingInterval());
                subscription.setRevisedLifetimeCount(modifyResponse.getRevisedLifetimeCount());
                subscription.setRevisedMaxKeepAliveCount(modifyResponse.getRevisedMaxKeepAliveCount());
                subscriptions.put(subscription.getSubscriptionId(), subscription);
                WatchdogTimer watchdogTimer = new WatchdogTimer(subscription);
                watchdogTimers.put(subscription.getSubscriptionId(), watchdogTimer);
                watchdogTimer.kick();
                maybeSendPublishRequests();
                return subscription;
            });
        } else {
            subscriptions.put(subscription.getSubscriptionId(), subscription);
            WatchdogTimer watchdogTimer = new WatchdogTimer(subscription);
            watchdogTimers.put(subscription.getSubscriptionId(), watchdogTimer);
            watchdogTimer.kick();
            maybeSendPublishRequests();
            return completedFuture(subscription);
        }
    });
}
Also used : ModifySubscriptionResponse(org.eclipse.milo.opcua.stack.core.types.structured.ModifySubscriptionResponse) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) CreateSubscriptionResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionResponse)

Aggregations

UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)2 CreateSubscriptionResponse (org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionResponse)2 CreateSubscriptionRequest (org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionRequest)1 ModifySubscriptionResponse (org.eclipse.milo.opcua.stack.core.types.structured.ModifySubscriptionResponse)1 ResponseHeader (org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader)1