Search in sources :

Example 31 with IRemoteService

use of org.eclipse.ecf.remoteservice.IRemoteService in project ecf by eclipse.

the class HelloClientComponent method bindHello.

public void bindHello(IHello proxy) {
    // First print out on console that we got a proxy instance
    System.out.println("Got proxy IHello=" + proxy);
    // Call proxy synchronously.  Note that this call may block or fail due to
    // synchronous communication with remote service
    System.out.println("STARTING remote call via proxy...");
    proxy.hello(CONSUMER_NAME + " via proxy");
    System.out.println("COMPLETED remote call via proxy");
    System.out.println();
    // this asynchronous interface to invoke methods asynchronously
    if (proxy instanceof IHelloAsync) {
        IHelloAsync helloA = (IHelloAsync) proxy;
        // Create callback for use in IHelloAsync
        IAsyncCallback callback = new IAsyncCallback<String>() {

            public void onSuccess(String result) {
                System.out.println("COMPLETED remote call with callback SUCCESS with result=" + result);
                System.out.println();
            }

            public void onFailure(Throwable t) {
                System.out.println("COMPLETED remote call with callback FAILED with exception=" + t);
                System.out.println();
            }
        };
        // Call asynchronously with callback
        System.out.println("STARTING async remote call via callback...");
        helloA.helloAsync(CONSUMER_NAME + " via async proxy with listener", callback);
        System.out.println("LOCAL async invocation complete");
        System.out.println();
        // Call asynchronously with future
        System.out.println("STARTING async remote call via future...");
        Future<String> future = helloA.helloAsync(CONSUMER_NAME + " via async proxy with future");
        System.out.println("LOCAL async future invocation complete");
        System.out.println();
        try {
            while (!future.isDone()) {
                // do some other stuff
                System.out.println("LOCAL future not yet done...so we're doing other stuff while waiting for future to be done");
                Thread.sleep(200);
            }
            // Now it's done, so this will not block
            String result = future.get();
            System.out.println("COMPLETED remote call with future SUCCEEDED with result=" + result);
            System.out.println();
        } catch (OperationCanceledException e) {
            System.out.println("COMPLETED remote call with callback CANCELLED with exception=" + e);
            System.out.println();
            e.printStackTrace();
        } catch (InterruptedException e) {
            System.out.println("COMPLETED remote call with callback INTERRUPTED with exception=" + e);
            System.out.println();
            e.printStackTrace();
        } catch (ExecutionException e) {
            System.out.println("COMPLETED remote call with callback INTERRUPTED with exception=" + e);
            System.out.println();
            e.printStackTrace();
        }
    }
    // It's also possible to get the remote service directly from the proxy
    IRemoteService remoteService = ((IRemoteServiceProxy) proxy).getRemoteService();
    Assert.isNotNull(remoteService);
    // In this case, we will make an non-blocking call and immediately get a 'future'...which is
    // a placeholder for a result of the remote computation.  This will not block.
    System.out.println("STARTING async remote call via future...");
    IFuture future = RemoteServiceHelper.futureExec(remoteService, "hello", new Object[] { CONSUMER_NAME + " future" });
    System.out.println("LOCAL async future invocation complete");
    System.out.println();
    // Client can execute arbitrary code here...
    try {
        // This blocks until communication and computation have completed successfully
        while (!future.isDone()) {
            // do some other stuff
            System.out.println("LOCAL future not yet done...so we're doing other stuff while waiting for future to be done");
            Thread.sleep(200);
        }
        // Now it's done, so this will not block
        Object result = future.get();
        System.out.println("COMPLETED remote call with future SUCCEEDED with result=" + result);
        System.out.println();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : IHelloAsync(org.eclipse.ecf.examples.remoteservices.hello.IHelloAsync) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IFuture(org.eclipse.equinox.concurrent.future.IFuture) ExecutionException(java.util.concurrent.ExecutionException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IRemoteServiceProxy(org.eclipse.ecf.remoteservice.IRemoteServiceProxy) IAsyncCallback(org.eclipse.ecf.remoteservice.IAsyncCallback) ExecutionException(java.util.concurrent.ExecutionException) IRemoteService(org.eclipse.ecf.remoteservice.IRemoteService)

Example 32 with IRemoteService

use of org.eclipse.ecf.remoteservice.IRemoteService in project ecf by eclipse.

the class RpcRemoteServiceTest method testSyncCall.

public void testSyncCall() {
    IRemoteService rpcClientService = getRemoteServiceClientContainerAdapter(container).getRemoteService(registrationEcho.getReference());
    try {
        Object result = rpcClientService.callSync(getEchoCall());
        assertNotNull(result);
        assertTrue(ECHO_TEST_DATA.equals(result));
    } catch (ECFException e) {
        fail("Could not contact the service");
    }
}
Also used : ECFException(org.eclipse.ecf.core.util.ECFException) IRemoteService(org.eclipse.ecf.remoteservice.IRemoteService)

Example 33 with IRemoteService

use of org.eclipse.ecf.remoteservice.IRemoteService in project ecf by eclipse.

the class AbstractConcatConsumerTestCase method testAsyncResult.

public void testAsyncResult() throws Exception {
    final IRemoteService service = getRemoteService(targetID, getRemoteServiceClass().getName(), getRemoteServiceFilter());
    assertNotNull(service);
    traceCallStart("callAsynchResult");
    final IFuture result = service.callAsync(createRemoteConcat("ECF AsynchResults ", "are cool"));
    Thread.sleep(ASYNC_WAITTIME);
    traceCallEnd("callAsynchResult", result);
    assertNotNull(result);
}
Also used : IRemoteService(org.eclipse.ecf.remoteservice.IRemoteService) IFuture(org.eclipse.equinox.concurrent.future.IFuture)

Example 34 with IRemoteService

use of org.eclipse.ecf.remoteservice.IRemoteService in project ecf by eclipse.

the class AbstractConcatConsumerTestCase method testGetRemoteServiceWithLocallyRegisteredService.

public void testGetRemoteServiceWithLocallyRegisteredService() throws Exception {
    String classname = getRemoteServiceClass().getName();
    // Register service locally
    registerLocalService(classname, createRemoteService(), createRemoteServiceProperties());
    // Now get remote service
    final IRemoteService service = getRemoteService(targetID, getRemoteServiceClass().getName(), getRemoteServiceFilter());
    assertNotNull(service);
    // Now get both
    IRemoteServiceReference[] references = getRemoteServiceReferences(new ID[] { rsContainer.getContainer().getID(), targetID }, classname, getRemoteServiceFilter());
    assertNotNull(references);
    assertTrue(references.length == 2);
    Thread.sleep(ASYNC_WAITTIME);
}
Also used : IRemoteServiceReference(org.eclipse.ecf.remoteservice.IRemoteServiceReference) IRemoteService(org.eclipse.ecf.remoteservice.IRemoteService)

Example 35 with IRemoteService

use of org.eclipse.ecf.remoteservice.IRemoteService in project ecf by eclipse.

the class AbstractConcatConsumerTestCase method testFireAsynch.

public void testFireAsynch() throws Exception {
    final IRemoteService service = getRemoteService(targetID, getRemoteServiceClass().getName(), getRemoteServiceFilter());
    assertNotNull(service);
    traceCallStart("fireAsynch");
    service.fireAsync(createRemoteConcat("Eclipse ", "sucks"));
    traceCallEnd("fireAsynch");
    Thread.sleep(ASYNC_WAITTIME);
}
Also used : IRemoteService(org.eclipse.ecf.remoteservice.IRemoteService)

Aggregations

IRemoteService (org.eclipse.ecf.remoteservice.IRemoteService)64 ServiceReference (org.osgi.framework.ServiceReference)11 IFuture (org.eclipse.equinox.concurrent.future.IFuture)10 ECFException (org.eclipse.ecf.core.util.ECFException)9 IRemoteServiceContainerAdapter (org.eclipse.ecf.remoteservice.IRemoteServiceContainerAdapter)9 IRemoteServiceReference (org.eclipse.ecf.remoteservice.IRemoteServiceReference)8 IRemoteCallListener (org.eclipse.ecf.remoteservice.IRemoteCallListener)6 IRemoteCallCompleteEvent (org.eclipse.ecf.remoteservice.events.IRemoteCallCompleteEvent)6 IRemoteCallEvent (org.eclipse.ecf.remoteservice.events.IRemoteCallEvent)6 IConcatService (org.eclipse.ecf.tests.remoteservice.IConcatService)6 Dictionary (java.util.Dictionary)5 IRemoteServiceProxy (org.eclipse.ecf.remoteservice.IRemoteServiceProxy)5 JSONObject (org.json.JSONObject)5 Hashtable (java.util.Hashtable)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 RemoteServiceTracker (org.eclipse.ecf.remoteservice.util.tracker.RemoteServiceTracker)4 IRemoteServiceRegistration (org.eclipse.ecf.remoteservice.IRemoteServiceRegistration)3 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutionException (org.eclipse.core.commands.ExecutionException)2 IContainerManager (org.eclipse.ecf.core.IContainerManager)2