Search in sources :

Example 1 with Future

use of io.joynr.proxy.Future in project joynr by bmwcarit.

the class LocalCapabilitiesDirectoryImpl method lookup.

@Override
@CheckForNull
public DiscoveryEntryWithMetaInfo lookup(String participantId, DiscoveryQos discoveryQos) {
    final Future<DiscoveryEntryWithMetaInfo> lookupFuture = new Future<>();
    lookup(participantId, discoveryQos, new CapabilityCallback() {

        @Override
        public void processCapabilityReceived(DiscoveryEntryWithMetaInfo capability) {
            lookupFuture.onSuccess(capability);
        }

        @Override
        public void onError(Throwable e) {
            lookupFuture.onFailure(new JoynrRuntimeException(e));
        }
    });
    DiscoveryEntryWithMetaInfo retrievedCapabilitiyEntry = null;
    try {
        retrievedCapabilitiyEntry = lookupFuture.get();
    } catch (InterruptedException e1) {
        logger.error("interrupted while retrieving capability entry by participant ID", e1);
    } catch (ApplicationException e1) {
        // should not be reachable since ApplicationExceptions are not used internally
        logger.error("ApplicationException while retrieving capability entry by participant ID", e1);
    }
    return retrievedCapabilitiyEntry;
}
Also used : ApplicationException(joynr.exceptions.ApplicationException) Future(io.joynr.proxy.Future) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) CheckForNull(javax.annotation.CheckForNull)

Example 2 with Future

use of io.joynr.proxy.Future in project joynr by bmwcarit.

the class LocalDiscoveryAggregator method lookup.

@Override
public Future<DiscoveryEntryWithMetaInfo[]> lookup(final Callback<DiscoveryEntryWithMetaInfo[]> callback, String[] domains, String interfaceName, DiscoveryQos discoveryQos) {
    final Set<DiscoveryEntryWithMetaInfo> discoveryEntries = new HashSet<>();
    Set<String> missingDomains = new HashSet<>();
    for (String domain : domains) {
        if (provisionedDiscoveryEntries.containsKey(domain + interfaceName)) {
            DiscoveryEntryWithMetaInfo discoveryEntry = provisionedDiscoveryEntries.get(domain + interfaceName);
            discoveryEntries.add(discoveryEntry);
        } else {
            missingDomains.add(domain);
        }
    }
    logger.trace("Found locally provisioned discovery entries: {}", discoveryEntries);
    final Future<DiscoveryEntryWithMetaInfo[]> discoveryEntryFuture = new Future<>();
    if (!missingDomains.isEmpty()) {
        logger.trace("Did not find entries for the following domains: {}", missingDomains);
        Callback<DiscoveryEntryWithMetaInfo[]> newCallback = new Callback<DiscoveryEntryWithMetaInfo[]>() {

            @Override
            public void onFailure(JoynrRuntimeException error) {
                callback.onFailure(error);
                discoveryEntryFuture.onFailure(error);
            }

            @Override
            public void onSuccess(DiscoveryEntryWithMetaInfo[] entries) {
                assert entries != null : "Entries must not be null.";
                logger.trace("Globally found entries for missing domains: {}", Arrays.toString(entries));
                Collections.addAll(discoveryEntries, entries);
                resolveDiscoveryEntriesFutureWithEntries(discoveryEntryFuture, discoveryEntries, callback);
            }
        };
        String[] missingDomainsArray = new String[missingDomains.size()];
        missingDomains.toArray(missingDomainsArray);
        getDiscoveryProxy(discoveryQos.getDiscoveryTimeout()).lookup(newCallback, missingDomainsArray, interfaceName, discoveryQos);
    } else {
        resolveDiscoveryEntriesFutureWithEntries(discoveryEntryFuture, discoveryEntries, callback);
    }
    return discoveryEntryFuture;
}
Also used : Callback(io.joynr.proxy.Callback) Future(io.joynr.proxy.Future) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) HashSet(java.util.HashSet)

Example 3 with Future

use of io.joynr.proxy.Future in project joynr by bmwcarit.

the class LocalDiscoveryTest method testMixedLocalCachedRemoteDiscoveryEntries.

@SuppressWarnings("unchecked")
@Test
public void testMixedLocalCachedRemoteDiscoveryEntries() {
    String testDomain = "testDomain";
    String remoteDomain = "remoteDomain";
    Set<String> testDomains = new HashSet<>();
    testDomains.add(testDomain);
    testDomains.add(remoteDomain);
    String interfaceName = testProxy.INTERFACE_NAME;
    final Collection<DiscoveryEntry> localDiscoveryEntries = new HashSet<>();
    final Collection<DiscoveryEntry> cachedDiscoveryEntries = new HashSet<>();
    final List<GlobalDiscoveryEntry> remoteDiscoveryEntries = new ArrayList<>();
    DiscoveryEntry discoveryEntry = new DiscoveryEntry(VersionUtil.getVersionFromAnnotation(testProxy.class), testDomain, interfaceName, "participantIdLocal", new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 100000, "publicKeyId");
    DiscoveryEntry cachedDiscoveryEntry = new DiscoveryEntry(VersionUtil.getVersionFromAnnotation(testProxy.class), testDomain, interfaceName, "participantIdCached", new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 100000, "publicKeyId");
    GlobalDiscoveryEntry remoteDiscoveryEntry = CapabilityUtils.discoveryEntry2GlobalDiscoveryEntry(new DiscoveryEntry(VersionUtil.getVersionFromAnnotation(testProxy.class), remoteDomain, interfaceName, "participantIdRemote", new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 100000, "publicKeyId"), new MqttAddress());
    localDiscoveryEntries.add(discoveryEntry);
    cachedDiscoveryEntries.add(cachedDiscoveryEntry);
    remoteDiscoveryEntries.add(remoteDiscoveryEntry);
    Set<DiscoveryEntryWithMetaInfo> discoveryEntriesWithMetaInfo = CapabilityUtils.convertToDiscoveryEntryWithMetaInfoSet(true, localDiscoveryEntries);
    discoveryEntriesWithMetaInfo.add(CapabilityUtils.convertToDiscoveryEntryWithMetaInfo(false, cachedDiscoveryEntry));
    discoveryEntriesWithMetaInfo.add(CapabilityUtils.convertToDiscoveryEntryWithMetaInfo(false, remoteDiscoveryEntry));
    when(localDiscoveryEntryStoreMock.lookup(any(String[].class), eq(interfaceName))).thenReturn(localDiscoveryEntries);
    when(globalDiscoveryEntryCacheMock.lookup(any(String[].class), eq(interfaceName), anyLong())).thenReturn(cachedDiscoveryEntries);
    Mockito.doAnswer(new Answer<Object>() {

        @SuppressWarnings("rawtypes")
        @Override
        public List<GlobalDiscoveryEntry> answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            assert (arguments[0] instanceof Callback);
            ((Callback) arguments[0]).resolve((Object) remoteDiscoveryEntries);
            return null;
        }
    }).when(globalCapabilitiesDirectoryClientMock).lookup(any(Callback.class), any(String[].class), eq(interfaceName), anyLong());
    ProxyBuilder<testProxy> proxyBuilder = runtime.getProxyBuilder(testDomains, testProxy.class);
    final Future<Void> future = new Future<Void>();
    ArbitrationStrategyFunction arbitrationStrategyFunction = new ArbitrationStrategyFunction() {

        @Override
        public Set<DiscoveryEntryWithMetaInfo> select(Map<String, String> parameters, Collection<DiscoveryEntryWithMetaInfo> capabilities) {
            return Sets.newHashSet(capabilities);
        }
    };
    DiscoveryQos discoveryQos = new DiscoveryQos(30000, arbitrationStrategyFunction, 0, DiscoveryScope.LOCAL_AND_GLOBAL);
    proxyBuilder.setDiscoveryQos(discoveryQos).build(new ProxyCreatedCallback<testProxy>() {

        @Override
        public void onProxyCreationFinished(testProxy result) {
            future.onSuccess(null);
        }

        @Override
        public void onProxyCreationError(JoynrRuntimeException error) {
            future.onFailure(error);
        }
    });
    try {
        future.get(5000);
        verify(joynrMessagingConnectorFactoryMock).create(anyString(), eq(discoveryEntriesWithMetaInfo), any(MessagingQos.class));
    } catch (Exception e) {
        Assert.fail("Unexpected exception from ProxyCreatedCallback: " + e);
    }
}
Also used : GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) MessagingQos(io.joynr.messaging.MessagingQos) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MqttAddress(joynr.system.RoutingTypes.MqttAddress) DiscoveryEntry(joynr.types.DiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) joynr.tests.testProxy(joynr.tests.testProxy) DiscoveryQos(io.joynr.arbitration.DiscoveryQos) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Callback(io.joynr.proxy.Callback) ProxyCreatedCallback(io.joynr.proxy.ProxyBuilder.ProxyCreatedCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Future(io.joynr.proxy.Future) Collection(java.util.Collection) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) ArbitrationStrategyFunction(io.joynr.arbitration.ArbitrationStrategyFunction) Map(java.util.Map) ProviderQos(joynr.types.ProviderQos) Test(org.junit.Test)

Example 4 with Future

use of io.joynr.proxy.Future in project joynr by bmwcarit.

the class LocalDiscoveryTest method testCachedGlobalDiscoveryEntries.

@Test
public void testCachedGlobalDiscoveryEntries() {
    String testDomain = "testDomain";
    String interfaceName = testProxy.INTERFACE_NAME;
    Collection<DiscoveryEntry> discoveryEntries = new HashSet<>();
    discoveryEntries.add(new DiscoveryEntry(VersionUtil.getVersionFromAnnotation(testProxy.class), testDomain, interfaceName, "participantId", new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 100000, "publicKeyId"));
    Set<DiscoveryEntryWithMetaInfo> discoveryEntriesWithMetaInfo = CapabilityUtils.convertToDiscoveryEntryWithMetaInfoSet(false, discoveryEntries);
    when(globalDiscoveryEntryCacheMock.lookup(any(String[].class), eq(interfaceName), anyLong())).thenReturn(discoveryEntries);
    ProxyBuilder<testProxy> proxyBuilder = runtime.getProxyBuilder(testDomain, testProxy.class);
    final Future<Void> future = new Future<Void>();
    DiscoveryQos discoveryQos = new DiscoveryQos();
    discoveryQos.setDiscoveryScope(DiscoveryScope.GLOBAL_ONLY);
    proxyBuilder.setDiscoveryQos(discoveryQos).build(new ProxyCreatedCallback<testProxy>() {

        @Override
        public void onProxyCreationFinished(testProxy result) {
            future.onSuccess(null);
        }

        @Override
        public void onProxyCreationError(JoynrRuntimeException error) {
            future.onFailure(error);
        }
    });
    try {
        future.get(5000);
        verify(joynrMessagingConnectorFactoryMock).create(anyString(), eq(discoveryEntriesWithMetaInfo), any(MessagingQos.class));
    } catch (Exception e) {
        Assert.fail("Unexpected exception from ProxyCreatedCallback: " + e);
    }
}
Also used : DiscoveryEntry(joynr.types.DiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) joynr.tests.testProxy(joynr.tests.testProxy) Matchers.anyString(org.mockito.Matchers.anyString) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) DiscoveryQos(io.joynr.arbitration.DiscoveryQos) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) MessagingQos(io.joynr.messaging.MessagingQos) Future(io.joynr.proxy.Future) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) ProviderQos(joynr.types.ProviderQos) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with Future

use of io.joynr.proxy.Future in project joynr by bmwcarit.

the class LocalDiscoveryTest method testRemoteGlobalDiscoveryEntries.

@SuppressWarnings("unchecked")
@Test
public void testRemoteGlobalDiscoveryEntries() {
    String testDomain = "testDomain";
    String interfaceName = testProxy.INTERFACE_NAME;
    final Collection<DiscoveryEntry> discoveryEntries = new HashSet<>();
    final List<GlobalDiscoveryEntry> globalDiscoveryEntries = new ArrayList<>();
    DiscoveryEntry discoveryEntry = new DiscoveryEntry(VersionUtil.getVersionFromAnnotation(testProxy.class), testDomain, interfaceName, "participantId", new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 100000, "publicKeyId");
    discoveryEntries.add(discoveryEntry);
    globalDiscoveryEntries.add(CapabilityUtils.discoveryEntry2GlobalDiscoveryEntry(discoveryEntry, new MqttAddress()));
    when(globalDiscoveryEntryCacheMock.lookup(any(String[].class), eq(interfaceName), anyLong())).thenReturn(new HashSet<DiscoveryEntry>());
    Mockito.doAnswer(new Answer<Object>() {

        @SuppressWarnings("rawtypes")
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            assert (arguments[0] instanceof Callback);
            ((Callback) arguments[0]).resolve((Object) globalDiscoveryEntries);
            return null;
        }
    }).when(globalCapabilitiesDirectoryClientMock).lookup(any(Callback.class), any(String[].class), eq(interfaceName), anyLong());
    ProxyBuilder<testProxy> proxyBuilder = runtime.getProxyBuilder(testDomain, testProxy.class);
    final Future<Void> future = new Future<Void>();
    DiscoveryQos discoveryQos = new DiscoveryQos();
    discoveryQos.setDiscoveryScope(DiscoveryScope.GLOBAL_ONLY);
    proxyBuilder.setDiscoveryQos(discoveryQos).build(new ProxyCreatedCallback<testProxy>() {

        @Override
        public void onProxyCreationFinished(testProxy result) {
            future.onSuccess(null);
        }

        @Override
        public void onProxyCreationError(JoynrRuntimeException error) {
            future.onFailure(error);
        }
    });
    try {
        future.get(5000);
        verify(joynrMessagingConnectorFactoryMock).create(anyString(), discoveryEntryWithMetaInfoArgumentCaptor.capture(), any(MessagingQos.class));
        assertDiscoveryEntryEqualsCaptured(discoveryEntry);
    } catch (Exception e) {
        Assert.fail("Unexpected exception from ProxyCreatedCallback: " + e);
    }
}
Also used : GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) MessagingQos(io.joynr.messaging.MessagingQos) HashSet(java.util.HashSet) MqttAddress(joynr.system.RoutingTypes.MqttAddress) DiscoveryEntry(joynr.types.DiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) joynr.tests.testProxy(joynr.tests.testProxy) DiscoveryQos(io.joynr.arbitration.DiscoveryQos) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Callback(io.joynr.proxy.Callback) ProxyCreatedCallback(io.joynr.proxy.ProxyBuilder.ProxyCreatedCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Future(io.joynr.proxy.Future) ProviderQos(joynr.types.ProviderQos) Test(org.junit.Test)

Aggregations

JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)9 Future (io.joynr.proxy.Future)9 joynr.tests.testProxy (joynr.tests.testProxy)6 DiscoveryQos (io.joynr.arbitration.DiscoveryQos)5 MessagingQos (io.joynr.messaging.MessagingQos)5 HashSet (java.util.HashSet)5 Test (org.junit.Test)5 Callback (io.joynr.proxy.Callback)4 DiscoveryEntry (joynr.types.DiscoveryEntry)4 DiscoveryEntryWithMetaInfo (joynr.types.DiscoveryEntryWithMetaInfo)4 GlobalDiscoveryEntry (joynr.types.GlobalDiscoveryEntry)4 ProviderQos (joynr.types.ProviderQos)4 Matchers.anyString (org.mockito.Matchers.anyString)4 ProxyCreatedCallback (io.joynr.proxy.ProxyBuilder.ProxyCreatedCallback)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 DeferredVoid (io.joynr.provider.DeferredVoid)2 ArrayList (java.util.ArrayList)2 MqttAddress (joynr.system.RoutingTypes.MqttAddress)2 ArbitrationStrategyFunction (io.joynr.arbitration.ArbitrationStrategyFunction)1 JoynrMessageNotSentException (io.joynr.exceptions.JoynrMessageNotSentException)1