Search in sources :

Example 1 with OnChangeWithKeepAliveSubscriptionQos

use of joynr.OnChangeWithKeepAliveSubscriptionQos in project joynr by bmwcarit.

the class GpsConsumerApplication method run.

@Override
public void run() {
    DiscoveryQos discoveryQos = new DiscoveryQos();
    // As soon as the arbitration QoS is set on the proxy builder, discovery of suitable providers
    // is triggered. If the discovery process does not find matching providers within the
    // arbitration timeout duration it will be terminated and you will get an arbitration exception.
    discoveryQos.setDiscoveryTimeoutMs(10000);
    // Provider entries in the global capabilities directory are cached locally. Discovery will
    // consider entries in this cache valid if they are younger as the max age of cached
    // providers as defined in the QoS. All valid entries will be processed by the arbitrator when searching
    // for and arbitrating the "best" matching provider.
    // NOTE: Valid cache entries might prevent triggering a lookup in the global capabilities
    // directory. Therefore, not all providers registered with the global capabilities
    // directory might be taken into account during arbitration.
    discoveryQos.setCacheMaxAgeMs(Long.MAX_VALUE);
    // The discovery process outputs a list of matching providers. The arbitration strategy then
    // chooses one or more of them to be used by the proxy.
    discoveryQos.setArbitrationStrategy(ArbitrationStrategy.HighestPriority);
    // The provider will maintain at least a minimum interval idle time in milliseconds between
    // successive notifications, even if on-change notifications are enabled and the value changes more
    // often. This prevents the consumer from being flooded by updated values. The filtering happens on
    // the provider's side, thus also preventing excessive network traffic.
    int minInterval_ms = 0;
    // The provider will send notifications every maximum interval in milliseconds, even if the value didn't
    // change. It will send notifications more often if on-change notifications are enabled,
    // the value changes more often, and the minimum interval QoS does not prevent it. The maximum interval
    // can thus be seen as a sort of heart beat.
    int maxInterval_ms = 10000;
    // The provider will send notifications until the end date is reached. The consumer will not receive any
    // notifications (neither value notifications nor missed publication notifications) after
    // this date.
    long validity_ms = 60000;
    // If no notification was received within the last alert interval, a missed publication
    // notification will be raised.
    int alertAfterInterval_ms = 20000;
    // Notification messages will be sent with this time-to-live. If a notification message can not be
    // delivered within its TTL, it will be deleted from the system.
    // NOTE: If a notification message is not delivered due to an expired TTL, it might raise a
    // missed publication notification (depending on the value of the alert interval QoS).
    int publicationTtl_ms = 5000;
    OnChangeWithKeepAliveSubscriptionQos subscriptionQos = new OnChangeWithKeepAliveSubscriptionQos();
    subscriptionQos.setMinIntervalMs(minInterval_ms).setMaxIntervalMs(maxInterval_ms).setValidityMs(validity_ms);
    subscriptionQos.setAlertAfterIntervalMs(alertAfterInterval_ms).setPublicationTtlMs(publicationTtl_ms);
    ProxyBuilder<GpsProxy> proxyBuilder = runtime.getProxyBuilder(providerDomain, GpsProxy.class);
    // reading an attribute value
    gpsProxy = proxyBuilder.setMessagingQos(new MessagingQos()).setDiscoveryQos(discoveryQos).build();
    subscriptionFuture = gpsProxy.subscribeToLocation(new AttributeSubscriptionAdapter<GpsLocation>() {

        @Override
        public void onReceive(GpsLocation value) {
            LOG.info(PRINT_BORDER + "SUBSCRIPTION: location: " + value + PRINT_BORDER);
        }

        @Override
        public void onError(JoynrRuntimeException error) {
            LOG.info(PRINT_BORDER + "SUBSCRIPTION: location, publication missed " + PRINT_BORDER);
        }
    }, subscriptionQos);
}
Also used : OnChangeWithKeepAliveSubscriptionQos(joynr.OnChangeWithKeepAliveSubscriptionQos) MessagingQos(io.joynr.messaging.MessagingQos) GpsProxy(joynr.vehicle.GpsProxy) AttributeSubscriptionAdapter(io.joynr.pubsub.subscription.AttributeSubscriptionAdapter) GpsLocation(joynr.types.Localisation.GpsLocation) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) DiscoveryQos(io.joynr.arbitration.DiscoveryQos)

Example 2 with OnChangeWithKeepAliveSubscriptionQos

use of joynr.OnChangeWithKeepAliveSubscriptionQos in project joynr by bmwcarit.

the class MyRadioConsumerApplication method run.

@SuppressWarnings("checkstyle:methodlength")
@Override
public void run() {
    DiscoveryQos discoveryQos = new DiscoveryQos();
    // As soon as the arbitration QoS is set on the proxy builder, discovery of suitable providers
    // is triggered. If the discovery process does not find matching providers within the
    // arbitration timeout duration it will be terminated and you will get an arbitration exception.
    discoveryQos.setDiscoveryTimeoutMs(10000);
    discoveryQos.setDiscoveryScope(discoveryScope);
    // Provider entries in the global capabilities directory are cached locally. Discovery will
    // consider entries in this cache valid if they are younger as the max age of cached
    // providers as defined in the QoS. All valid entries will be processed by the arbitrator when searching
    // for and arbitrating the "best" matching provider.
    // NOTE: Valid cache entries might prevent triggering a lookup in the global capabilities
    // directory. Therefore, not all providers registered with the global capabilities
    // directory might be taken into account during arbitration.
    discoveryQos.setCacheMaxAgeMs(Long.MAX_VALUE);
    // The discovery process outputs a list of matching providers. The arbitration strategy then
    // chooses one or more of them to be used by the proxy.
    discoveryQos.setArbitrationStrategy(ArbitrationStrategy.HighestPriority);
    // The provider will maintain at least a minimum interval idle time in milliseconds between
    // successive notifications, even if on-change notifications are enabled and the value changes more
    // often. This prevents the consumer from being flooded by updated values. The filtering happens on
    // the provider's side, thus also preventing excessive network traffic.
    int minInterval_ms = 0;
    // The provider will send notifications every maximum interval in milliseconds, even if the value didn't
    // change. It will send notifications more often if on-change notifications are enabled,
    // the value changes more often, and the minimum interval QoS does not prevent it. The maximum interval
    // can thus be seen as a sort of heart beat.
    int maxInterval_ms = 10000;
    // The provider will send notifications until the end date is reached. The consumer will not receive any
    // notifications (neither value notifications nor missed publication notifications) after
    // this date.
    long validityMs = 60000;
    // If no notification was received within the last alert interval, a missed publication
    // notification will be raised.
    int alertAfterInterval_ms = 20000;
    // Notification messages will be sent with this time-to-live. If a notification message can not be
    // delivered within its TTL, it will be deleted from the system.
    // NOTE: If a notification message is not delivered due to an expired TTL, it might raise a
    // missed publication notification (depending on the value of the alert interval QoS).
    int publicationTtl_ms = 5000;
    OnChangeWithKeepAliveSubscriptionQos subscriptionQos = new OnChangeWithKeepAliveSubscriptionQos();
    subscriptionQos.setMinIntervalMs(minInterval_ms).setMaxIntervalMs(maxInterval_ms).setValidityMs(validityMs);
    subscriptionQos.setAlertAfterIntervalMs(alertAfterInterval_ms).setPublicationTtlMs(publicationTtl_ms);
    ProxyBuilder<RadioProxy> proxyBuilder = runtime.getProxyBuilder(providerDomain, RadioProxy.class);
    try {
        // getting an attribute
        radioProxy = proxyBuilder.setMessagingQos(new MessagingQos()).setDiscoveryQos(discoveryQos).build();
        RadioStation currentStation = radioProxy.getCurrentStation();
        LOG.info(PRINT_BORDER + "ATTRIBUTE GET: current station: " + currentStation + PRINT_BORDER);
        // subscribe to an attribute
        subscriptionFutureCurrentStation = radioProxy.subscribeToCurrentStation(new AttributeSubscriptionAdapter<RadioStation>() {

            @Override
            public void onReceive(RadioStation value) {
                LOG.info(PRINT_BORDER + "ATTRIBUTE SUBSCRIPTION: current station: " + value + PRINT_BORDER);
            }

            @Override
            public void onError(JoynrRuntimeException error) {
                LOG.info(PRINT_BORDER + "ATTRIBUTE SUBSCRIPTION: " + error + PRINT_BORDER);
            }
        }, subscriptionQos);
        // broadcast subscription
        // The provider will send a notification whenever the value changes.
        MulticastSubscriptionQos weakSignalBroadcastSubscriptionQos;
        // The consumer will be subscribed to the multicast until the end date is reached, after which the
        // consumer will be automatically unsubscribed, and will not receive any further notifications
        // this date.
        long wsbValidityMs = 60 * 1000;
        weakSignalBroadcastSubscriptionQos = new MulticastSubscriptionQos();
        weakSignalBroadcastSubscriptionQos.setValidityMs(wsbValidityMs);
        weakSignalFuture = subscribeToWeakSignal(weakSignalBroadcastSubscriptionQos);
        // susbcribe to weak signal with partition "GERMANY"
        weakSignalWithPartitionFuture = subscribeToWeakSignal(weakSignalBroadcastSubscriptionQos, "GERMANY");
        // selective broadcast subscription
        OnChangeSubscriptionQos newStationDiscoveredBroadcastSubscriptionQos;
        int nsdbMinIntervalMs = 2 * 1000;
        long nsdbValidityMs = 180 * 1000;
        int nsdbPublicationTtlMs = 5 * 1000;
        newStationDiscoveredBroadcastSubscriptionQos = new OnChangeSubscriptionQos();
        newStationDiscoveredBroadcastSubscriptionQos.setMinIntervalMs(nsdbMinIntervalMs).setValidityMs(nsdbValidityMs).setPublicationTtlMs(nsdbPublicationTtlMs);
        NewStationDiscoveredBroadcastFilterParameters newStationDiscoveredBroadcastFilterParams = new NewStationDiscoveredBroadcastFilterParameters();
        newStationDiscoveredBroadcastFilterParams.setHasTrafficService("true");
        // Munich
        GeoPosition positionOfInterest = new GeoPosition(48.1351250, 11.5819810);
        String positionOfInterestJson = null;
        try {
            positionOfInterestJson = objectMapper.writeValueAsString(positionOfInterest);
        } catch (JsonProcessingException e1) {
            LOG.error("Unable to write position of interest filter parameter to JSON", e1);
        }
        newStationDiscoveredBroadcastFilterParams.setPositionOfInterest(positionOfInterestJson);
        // 200 km
        newStationDiscoveredBroadcastFilterParams.setRadiusOfInterestArea("200000");
        radioProxy.subscribeToNewStationDiscoveredBroadcast(new RadioBroadcastInterface.NewStationDiscoveredBroadcastAdapter() {

            @Override
            public void onReceive(RadioStation discoveredStation, GeoPosition geoPosition) {
                LOG.info(PRINT_BORDER + "BROADCAST SUBSCRIPTION: new station discovered: " + discoveredStation + " at " + geoPosition + PRINT_BORDER);
            }
        }, newStationDiscoveredBroadcastSubscriptionQos, newStationDiscoveredBroadcastFilterParams);
        boolean success;
        try {
            // add favorite radio station
            RadioStation favoriteStation = new RadioStation("99.3 The Fox Rocks", false, Country.CANADA);
            success = radioProxy.addFavoriteStation(favoriteStation);
            LOG.info(PRINT_BORDER + "METHOD: added favorite station: " + favoriteStation + ": " + success + PRINT_BORDER);
            success = radioProxy.addFavoriteStation(favoriteStation);
        } catch (ApplicationException exception) {
            AddFavoriteStationErrorEnum error = exception.getError();
            switch(error) {
                case DUPLICATE_RADIOSTATION:
                    LOG.info(PRINT_BORDER + "METHOD: addFavoriteStation failed with the following excpected error: " + error);
                    break;
                default:
                    LOG.error(PRINT_BORDER + "METHOD: addFavoriteStation failed with an unexpected error: " + error);
                    break;
            }
        }
        try {
            // add favorite radio station
            RadioStation favoriteStation = new RadioStation("", false, Country.GERMANY);
            success = radioProxy.addFavoriteStation(favoriteStation);
            LOG.info(PRINT_BORDER + "METHOD: addFavoriteStation completed unexpected with the following output: " + success);
        } catch (ApplicationException exception) {
            String errorName = exception.getError().name();
            LOG.info(PRINT_BORDER + "METHOD: addFavoriteStation failed with the following unexpected ApplicationExcecption: " + errorName);
        } catch (ProviderRuntimeException exception) {
            String errorName = exception.getMessage();
            String expectation = errorName.equals(MyRadioProvider.MISSING_NAME) ? "expected" : "unexpected";
            LOG.info(PRINT_BORDER + "METHOD: addFavoriteStation failed with the following " + expectation + " exception: " + errorName);
        }
        // shuffle the stations
        radioProxy.shuffleStations();
        currentStation = radioProxy.getCurrentStation();
        LOG.info(PRINT_BORDER + "The current radio station after shuffling is: " + currentStation + PRINT_BORDER);
        // add favorite radio station async
        RadioStation radioStation = new RadioStation("99.4 AFN", false, Country.GERMANY);
        Future<Boolean> future = radioProxy.addFavoriteStation(new CallbackWithModeledError<Boolean, AddFavoriteStationErrorEnum>() {

            @Override
            public void onSuccess(Boolean result) {
                LOG.info(PRINT_BORDER + "ASYNC METHOD: added favorite station: callback onSuccess" + PRINT_BORDER);
            }

            @Override
            public void onFailure(JoynrRuntimeException error) {
                LOG.info(PRINT_BORDER + "ASYNC METHOD: added favorite station: callback onFailure: " + error.getMessage() + PRINT_BORDER);
            }

            @Override
            public void onFailure(AddFavoriteStationErrorEnum errorEnum) {
                switch(errorEnum) {
                    case DUPLICATE_RADIOSTATION:
                        LOG.info(PRINT_BORDER + "ASYNC METHOD: added favorite station failed: Duplicate Station!" + PRINT_BORDER);
                        break;
                    default:
                        LOG.error(PRINT_BORDER + "ASYNC METHOD: added favorite station failed: unknown errorEnum:" + errorEnum + PRINT_BORDER);
                        break;
                }
                LOG.info(PRINT_BORDER + "ASYNC METHOD: added favorite station: callback onFailure: " + errorEnum + PRINT_BORDER);
            }
        }, radioStation);
        try {
            long timeoutInMilliseconds = 8000;
            Boolean reply = future.get(timeoutInMilliseconds);
            LOG.info(PRINT_BORDER + "ASYNC METHOD: added favorite station: " + radioStation + ": " + reply + PRINT_BORDER);
        } catch (InterruptedException | JoynrRuntimeException | ApplicationException e) {
            LOG.info(PRINT_BORDER + "ASYNC METHOD: added favorite station: " + radioStation + ": " + e.getClass().getSimpleName() + "!");
        }
        ConsoleReader console;
        try {
            console = new ConsoleReader();
            int key;
            while ((key = console.readCharacter()) != 'q') {
                switch(key) {
                    case 's':
                        radioProxy.shuffleStations();
                        LOG.info("called shuffleStations");
                        break;
                    case 'm':
                        GetLocationOfCurrentStationReturned locationOfCurrentStation = radioProxy.getLocationOfCurrentStation();
                        LOG.info("called getLocationOfCurrentStation. country: " + locationOfCurrentStation.country + ", location: " + locationOfCurrentStation.location);
                        break;
                    default:
                        LOG.info("\n\nUSAGE press\n" + " q\tto quit\n" + " s\tto shuffle stations\n");
                        break;
                }
            }
        } catch (IOException e) {
            LOG.error("error reading input from console", e);
        }
    } catch (DiscoveryException e) {
        LOG.error("No provider found", e);
    } catch (JoynrCommunicationException e) {
        LOG.error("The message was not sent: ", e);
    }
}
Also used : OnChangeWithKeepAliveSubscriptionQos(joynr.OnChangeWithKeepAliveSubscriptionQos) AttributeSubscriptionAdapter(io.joynr.pubsub.subscription.AttributeSubscriptionAdapter) OnChangeSubscriptionQos(joynr.OnChangeSubscriptionQos) GetLocationOfCurrentStationReturned(joynr.vehicle.RadioSync.GetLocationOfCurrentStationReturned) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) RadioProxy(joynr.vehicle.RadioProxy) RadioStation(joynr.vehicle.RadioStation) MulticastSubscriptionQos(joynr.MulticastSubscriptionQos) RadioBroadcastInterface(joynr.vehicle.RadioBroadcastInterface) MessagingQos(io.joynr.messaging.MessagingQos) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DiscoveryException(io.joynr.exceptions.DiscoveryException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) ConsoleReader(jline.console.ConsoleReader) IOException(java.io.IOException) JoynrCommunicationException(io.joynr.exceptions.JoynrCommunicationException) DiscoveryQos(io.joynr.arbitration.DiscoveryQos) AddFavoriteStationErrorEnum(joynr.vehicle.Radio.AddFavoriteStationErrorEnum) NewStationDiscoveredBroadcastFilterParameters(joynr.vehicle.RadioBroadcastInterface.NewStationDiscoveredBroadcastFilterParameters) ApplicationException(joynr.exceptions.ApplicationException) GeoPosition(joynr.vehicle.GeoPosition) SuppressWarnings(edu.umd.cs.findbugs.annotations.SuppressWarnings)

Example 3 with OnChangeWithKeepAliveSubscriptionQos

use of joynr.OnChangeWithKeepAliveSubscriptionQos in project joynr by bmwcarit.

the class AbstractSubscriptionEnd2EndTest method testOnChangeWithKeepAliveSubscriptionSendsKeepAlive.

@Ignore
@Test
public void testOnChangeWithKeepAliveSubscriptionSendsKeepAlive() throws InterruptedException, ApplicationException {
    Semaphore onReceiveSemaphore = new Semaphore(0);
    AttributeSubscriptionListener<Integer> integerListener = prepareOnReceiveListenerMock(onReceiveSemaphore);
    // NOTE: 50 is the minimum minInterval supported
    long minInterval_ms = 50;
    // get an interval update after 200 ms
    long maxInterval_ms = 200;
    int numberExpectedKeepAlives = 3;
    // the subscription duration is a little longer so that it does not expire exactly as the last keep alive is to be sent
    long subscriptionDuration = maxInterval_ms * numberExpectedKeepAlives + (maxInterval_ms / 2);
    // longer than next interval
    OnChangeWithKeepAliveSubscriptionQos subscriptionQos = new OnChangeWithKeepAliveSubscriptionQos();
    subscriptionQos.setMinIntervalMs(minInterval_ms);
    subscriptionQos.setMaxIntervalMs(maxInterval_ms);
    subscriptionQos.setValidityMs(subscriptionDuration);
    subscriptionQos.setAlertAfterIntervalMs(0);
    // publications don't live
    subscriptionQos.setPublicationTtlMs(maxInterval_ms);
    Future<String> subscriptionId = proxy.subscribeToTestAttribute(integerListener, subscriptionQos);
    getSubscriptionTestsPublisher().waitForAttributeSubscription("testAttribute");
    // when subscribing, we automatically get 1 publication. Expect the
    // starting-publication
    verify(integerListener, times(0)).onError(null);
    assertTrue(onReceiveSemaphore.tryAcquire(1000, TimeUnit.MILLISECONDS));
    for (int i = 1; i <= numberExpectedKeepAlives; i++) {
        assertTrue(onReceiveSemaphore.tryAcquire(maxInterval_ms + EXPECTED_LATENCY_MS, TimeUnit.MILLISECONDS));
    }
    proxy.unsubscribeFromTestAttribute(subscriptionId.get(FUTURE_SUBSCRIPTION_ID_TIMEOUTMS));
    getSubscriptionTestsPublisher().waitForAttributeUnsubscription("testAttribute");
}
Also used : OnChangeWithKeepAliveSubscriptionQos(joynr.OnChangeWithKeepAliveSubscriptionQos) Semaphore(java.util.concurrent.Semaphore) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with OnChangeWithKeepAliveSubscriptionQos

use of joynr.OnChangeWithKeepAliveSubscriptionQos in project joynr by bmwcarit.

the class IltConsumerAttributeSubscriptionTest method callSubscribeAttributeEnumeration.

@Test
public void callSubscribeAttributeEnumeration() {
    Future<String> subscriptionIdFuture;
    String subscriptionId;
    int minIntervalMs = 0;
    int maxIntervalMs = 10000;
    long validityMs = 60000;
    int alertAfterIntervalMs = 20000;
    int publicationTtlMs = 5000;
    OnChangeWithKeepAliveSubscriptionQos subscriptionQos = new OnChangeWithKeepAliveSubscriptionQos().setMinIntervalMs(minIntervalMs).setMaxIntervalMs(maxIntervalMs).setValidityMs(validityMs).setAlertAfterIntervalMs(alertAfterIntervalMs).setPublicationTtlMs(publicationTtlMs);
    boolean result;
    LOG.info(name.getMethodName() + "");
    try {
        // must set the value before it can be retrieved again via subscription
        Enumeration enumerationArg = Enumeration.ENUM_0_VALUE_2;
        testInterfaceProxy.setAttributeEnumeration(enumerationArg);
        subscriptionIdFuture = testInterfaceProxy.subscribeToAttributeEnumeration(new AttributeSubscriptionAdapter<Enumeration>() {

            @Override
            public void onReceive(Enumeration value) {
                if (value == Enumeration.ENUM_0_VALUE_2) {
                    LOG.info(name.getMethodName() + " - callback - got publication with correct value");
                    subscribeAttributeEnumerationCallbackResult = true;
                } else {
                    subscribeAttributeEnumerationCallbackResult = false;
                    LOG.info(name.getMethodName() + " - callback - got publication with invalid value");
                }
                subscribeAttributeEnumerationCallbackDone = true;
            }

            @Override
            public void onError(JoynrRuntimeException error) {
                LOG.info(name.getMethodName() + " - callback - got unexpected exception");
                subscribeAttributeEnumerationCallbackResult = false;
                subscribeAttributeEnumerationCallbackDone = true;
            }
        }, subscriptionQos);
        subscriptionId = subscriptionIdFuture.get(10000);
        LOG.info(name.getMethodName() + " - subscription successful, subscriptionId = " + subscriptionId);
        // should have been called ahead anyway
        if (subscribeAttributeEnumerationCallbackDone == false) {
            LOG.info(name.getMethodName() + " - about to wait for a second for callback");
            Thread.sleep(1000);
            LOG.info(name.getMethodName() + " - wait for callback is over");
        } else {
            LOG.info(name.getMethodName() + " - callback already done");
        }
        if (subscribeAttributeEnumerationCallbackDone && subscribeAttributeEnumerationCallbackResult) {
            result = true;
        } else {
            fail(name.getMethodName() + " - FAILED - callback NOT done");
            result = false;
        }
        // try to unsubscribe in any case
        try {
            testInterfaceProxy.unsubscribeFromAttributeEnumeration(subscriptionId);
            LOG.info(name.getMethodName() + " - unsubscribe successful");
        } catch (Exception e) {
            fail(name.getMethodName() + " - FAILED - caught unexpected exception: " + e.getMessage());
            result = false;
        }
        if (!result) {
            LOG.info(name.getMethodName() + " - FAILED");
        } else {
            LOG.info(name.getMethodName() + " - OK");
        }
        return;
    } catch (Exception e) {
        // also catches InterruptedException from Thread.sleep() call
        fail(name.getMethodName() + " - FAILED - caught unexpected exception: " + e.getMessage());
        return;
    }
}
Also used : OnChangeWithKeepAliveSubscriptionQos(joynr.OnChangeWithKeepAliveSubscriptionQos) Enumeration(joynr.interlanguagetest.Enumeration) AttributeSubscriptionAdapter(io.joynr.pubsub.subscription.AttributeSubscriptionAdapter) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Test(org.junit.Test)

Example 5 with OnChangeWithKeepAliveSubscriptionQos

use of joynr.OnChangeWithKeepAliveSubscriptionQos in project joynr by bmwcarit.

the class IltConsumerAttributeSubscriptionTest method callSubscribeAttributeWithExceptionFromGetter.

@Test
public void callSubscribeAttributeWithExceptionFromGetter() {
    Future<String> subscriptionIdFuture;
    String subscriptionId;
    int minIntervalMs = 0;
    int maxIntervalMs = 10000;
    long validityMs = 60000;
    int alertAfterIntervalMs = 20000;
    int publicationTtlMs = 5000;
    OnChangeWithKeepAliveSubscriptionQos subscriptionQos = new OnChangeWithKeepAliveSubscriptionQos().setMinIntervalMs(minIntervalMs).setMaxIntervalMs(maxIntervalMs).setValidityMs(validityMs).setAlertAfterIntervalMs(alertAfterIntervalMs).setPublicationTtlMs(publicationTtlMs);
    boolean result;
    LOG.info(name.getMethodName() + "");
    try {
        subscriptionIdFuture = testInterfaceProxy.subscribeToAttributeWithExceptionFromGetter(new AttributeSubscriptionAdapter<Boolean>() {

            @Override
            public void onReceive(Boolean value) {
                LOG.info(name.getMethodName() + " - callback - got unexpected publication");
                subscribeAttributeWithExceptionFromGetterCallbackResult = false;
                subscribeAttributeWithExceptionFromGetterCallbackDone = true;
            }

            @Override
            public void onError(JoynrRuntimeException error) {
                if (error instanceof ProviderRuntimeException) {
                    if (((ProviderRuntimeException) error).getMessage().equals("Exception from getAttributeWithExceptionFromGetter")) {
                        LOG.info(name.getMethodName() + " - callback - got expected exception " + ((JoynrRuntimeException) error).getMessage());
                        subscribeAttributeWithExceptionFromGetterCallbackResult = true;
                        subscribeAttributeWithExceptionFromGetterCallbackDone = true;
                        return;
                    }
                    LOG.info(name.getMethodName() + " - callback - caught invalid exception " + ((JoynrRuntimeException) error).getMessage());
                } else if (error instanceof JoynrRuntimeException) {
                    LOG.info(name.getMethodName() + " - callback - caught invalid exception " + ((JoynrRuntimeException) error).getMessage());
                } else {
                    LOG.info(name.getMethodName() + " - callback - caught invalid exception ");
                }
                subscribeAttributeWithExceptionFromGetterCallbackResult = false;
                subscribeAttributeWithExceptionFromGetterCallbackDone = true;
            }
        }, subscriptionQos);
        subscriptionId = subscriptionIdFuture.get(10000);
        LOG.info(name.getMethodName() + " - subscription successful, subscriptionId = " + subscriptionId);
        // should have been called ahead anyway
        if (subscribeAttributeWithExceptionFromGetterCallbackDone == false) {
            LOG.info(name.getMethodName() + " - about to wait for a second for callback");
            Thread.sleep(1000);
            LOG.info(name.getMethodName() + " - wait for callback is over");
        } else {
            LOG.info(name.getMethodName() + " - callback already done");
        }
        if (!subscribeAttributeWithExceptionFromGetterCallbackDone) {
            fail(name.getMethodName() + " - FAILED - callback did not get called in time");
            result = false;
        } else if (subscribeAttributeWithExceptionFromGetterCallbackResult) {
            LOG.info(name.getMethodName() + " - callback got called and received expected exception");
            result = true;
        } else {
            fail(name.getMethodName() + " - FAILED - callback got called but received unexpected result");
            result = false;
        }
        // try to unsubscribe in any case
        try {
            testInterfaceProxy.unsubscribeFromAttributeWithExceptionFromGetter(subscriptionId);
            LOG.info(name.getMethodName() + " - unsubscribe successful");
        } catch (Exception e) {
            fail(name.getMethodName() + " - FAILED - caught unexpected exception on unsubscribe: " + e.getMessage());
            result = false;
        }
        if (!result) {
            LOG.info(name.getMethodName() + " - FAILED");
        } else {
            LOG.info(name.getMethodName() + " - OK");
        }
        return;
    } catch (Exception e) {
        // also catches InterruptedException from Thread.sleep() call
        LOG.info(name.getMethodName() + " - caught unexpected exception");
        LOG.info(name.getMethodName() + " - FAILED");
        fail(name.getMethodName() + " - FAILED - caught unexpected exception: " + e.getMessage());
        return;
    }
}
Also used : OnChangeWithKeepAliveSubscriptionQos(joynr.OnChangeWithKeepAliveSubscriptionQos) AttributeSubscriptionAdapter(io.joynr.pubsub.subscription.AttributeSubscriptionAdapter) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Test(org.junit.Test)

Aggregations

OnChangeWithKeepAliveSubscriptionQos (joynr.OnChangeWithKeepAliveSubscriptionQos)10 Test (org.junit.Test)7 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)4 AttributeSubscriptionAdapter (io.joynr.pubsub.subscription.AttributeSubscriptionAdapter)4 Semaphore (java.util.concurrent.Semaphore)3 ProviderRuntimeException (joynr.exceptions.ProviderRuntimeException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 SuppressWarnings (edu.umd.cs.findbugs.annotations.SuppressWarnings)2 DiscoveryQos (io.joynr.arbitration.DiscoveryQos)2 MessagingQos (io.joynr.messaging.MessagingQos)2 OnChangeSubscriptionQos (joynr.OnChangeSubscriptionQos)2 Ignore (org.junit.Ignore)2 DiscoveryException (io.joynr.exceptions.DiscoveryException)1 JoynrCommunicationException (io.joynr.exceptions.JoynrCommunicationException)1 SubscriptionException (io.joynr.exceptions.SubscriptionException)1 SubscriptionQos (io.joynr.pubsub.SubscriptionQos)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ConsoleReader (jline.console.ConsoleReader)1