Search in sources :

Example 6 with RemoteOSGiException

use of ch.ethz.iks.r_osgi.RemoteOSGiException in project ecf by eclipse.

the class ChannelEndpointImpl method sendAndWait.

/**
 * send a message and wait for the result.
 *
 * @param msg
 *            the message.
 * @return the result message.
 */
private RemoteOSGiMessage sendAndWait(final RemoteOSGiMessage msg) {
    if (msg.getXID() == 0) {
        msg.setXID(RemoteOSGiServiceImpl.nextXid());
    }
    final Integer xid = new Integer(msg.getXID());
    final WaitingCallback blocking = new WaitingCallback();
    synchronized (callbacks) {
        callbacks.put(xid, blocking);
    }
    send(msg);
    // wait for the reply
    synchronized (blocking) {
        final long timeout = System.currentTimeMillis() + TIMEOUT;
        RemoteOSGiMessage result = blocking.getResult();
        try {
            while (result == null && networkChannel != null && System.currentTimeMillis() < timeout) {
                blocking.wait(TIMEOUT);
                result = blocking.getResult();
            }
        } catch (InterruptedException ie) {
            throw new RemoteOSGiException("Interrupted while waiting for callback", // $NON-NLS-1$
            ie);
        }
        if (result != null) {
            return result;
        } else if (networkChannel == null) {
            // $NON-NLS-1$
            throw new RemoteOSGiException("Channel is closed");
        } else {
            throw new RemoteOSGiException(// $NON-NLS-1$
            "Method Invocation failed, timeout exceeded.");
        }
    }
}
Also used : RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) RemoteOSGiMessage(ch.ethz.iks.r_osgi.messages.RemoteOSGiMessage)

Example 7 with RemoteOSGiException

use of ch.ethz.iks.r_osgi.RemoteOSGiException in project ecf by eclipse.

the class ChannelEndpointImpl method asyncRemoteCall.

void asyncRemoteCall(final String fragment, final String methodSignature, final Object[] args, final AsyncRemoteCallCallback callback) {
    if (networkChannel == null) {
        // $NON-NLS-1$
        throw new RemoteOSGiException("Channel is closed");
    }
    // check arguments for streams and replace with placeholder
    for (int i = 0; i < args.length; i++) {
        if (args[i] instanceof InputStream) {
            args[i] = getInputStreamPlaceholder((InputStream) args[i]);
        } else if (args[i] instanceof OutputStream) {
            args[i] = getOutputStreamPlaceholder((OutputStream) args[i]);
        }
    }
    final Integer xid = new Integer(RemoteOSGiServiceImpl.nextXid());
    synchronized (callbacks) {
        callbacks.put(xid, new AsyncCallback() {

            public void result(final RemoteOSGiMessage msg) {
                final RemoteCallResultMessage resultMsg = (RemoteCallResultMessage) msg;
                if (resultMsg.causedException()) {
                    callback.remoteCallResult(false, resultMsg.getException());
                }
                final Object result = resultMsg.getResult();
                final Object res;
                if (result instanceof InputStreamHandle) {
                    res = getInputStreamProxy((InputStreamHandle) result);
                } else if (result instanceof OutputStreamHandle) {
                    res = getOutputStreamProxy((OutputStreamHandle) result);
                } else {
                    res = result;
                }
                callback.remoteCallResult(true, res);
            }
        });
    }
    final RemoteCallMessage invokeMsg = new RemoteCallMessage();
    invokeMsg.setServiceID(fragment);
    invokeMsg.setMethodSignature(methodSignature);
    invokeMsg.setArgs(args);
    invokeMsg.setXID(xid.shortValue());
    try {
        send(invokeMsg);
    } catch (final RemoteOSGiException e) {
        callbacks.remove(xid);
        callback.remoteCallResult(false, new RemoteOSGiException(// $NON-NLS-1$
        "Method invocation of " + getRemoteAddress() + "#" + fragment + " " + methodSignature + " failed.", // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        e));
    }
}
Also used : RemoteCallResultMessage(ch.ethz.iks.r_osgi.messages.RemoteCallResultMessage) OutputStreamHandle(ch.ethz.iks.r_osgi.streams.OutputStreamHandle) RemoteCallMessage(ch.ethz.iks.r_osgi.messages.RemoteCallMessage) RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) InputStreamHandle(ch.ethz.iks.r_osgi.streams.InputStreamHandle) RemoteOSGiMessage(ch.ethz.iks.r_osgi.messages.RemoteOSGiMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ChannelEndpoint(ch.ethz.iks.r_osgi.channels.ChannelEndpoint)

Example 8 with RemoteOSGiException

use of ch.ethz.iks.r_osgi.RemoteOSGiException in project ecf by eclipse.

the class ChannelEndpointImpl method invokeMethod.

/**
 * invoke a method on the remote host. This function is used by all proxy
 * bundles.
 *
 * @param service
 *            the service uri.
 * @param methodSignature
 *            the method signature.
 * @param args
 *            the method parameter.
 * @throws Throwable
 *             can throw any exception that the original method can throw,
 *             plus RemoteOSGiException.
 * @return the result of the remote method invocation.
 * @see ch.ethz.iks.r_osgi.channels.ChannelEndpoint#invokeMethod(java.lang.String,
 *      java.lang.String, java.lang.Object[])
 * @category ChannelEndpoint
 */
public Object invokeMethod(final String service, final String methodSignature, final Object[] args) throws Throwable {
    if (networkChannel == null) {
        // $NON-NLS-1$
        throw new RemoteOSGiException("Channel is closed");
    }
    // check arguments for streams and replace with placeholder
    for (int i = 0; i < args.length; i++) {
        if (args[i] instanceof InputStream) {
            args[i] = getInputStreamPlaceholder((InputStream) args[i]);
        } else if (args[i] instanceof OutputStream) {
            args[i] = getOutputStreamPlaceholder((OutputStream) args[i]);
        }
    }
    final RemoteCallMessage invokeMsg = new RemoteCallMessage();
    invokeMsg.setServiceID(URI.create(service).getFragment());
    invokeMsg.setMethodSignature(methodSignature);
    invokeMsg.setArgs(args);
    try {
        // send the message and get a MethodResultMessage in return
        final RemoteCallResultMessage resultMsg = (RemoteCallResultMessage) sendAndWait(invokeMsg);
        if (resultMsg.causedException()) {
            throw resultMsg.getException();
        }
        final Object result = resultMsg.getResult();
        if (result instanceof InputStreamHandle) {
            return getInputStreamProxy((InputStreamHandle) result);
        } else if (result instanceof OutputStreamHandle) {
            return getOutputStreamProxy((OutputStreamHandle) result);
        } else {
            if (result != null) {
                String returnType = Type.getReturnType(methodSignature).getClassName();
                RemoteServiceReferenceImpl refImpl = getRemoteReference(URI.create(service).toString());
                if (refImpl != null && refImpl.isOSGiAsync() && AsyncReturnUtil.isAsyncType(returnType))
                    return AsyncReturnUtil.convertReturnToAsync(result, returnType);
            }
            return result;
        }
    } catch (final RemoteOSGiException e) {
        throw new RemoteOSGiException(// $NON-NLS-1$
        "Method invocation of " + service + " " + methodSignature + " failed.", // $NON-NLS-1$ //$NON-NLS-2$
        e);
    }
}
Also used : RemoteCallResultMessage(ch.ethz.iks.r_osgi.messages.RemoteCallResultMessage) OutputStreamHandle(ch.ethz.iks.r_osgi.streams.OutputStreamHandle) RemoteCallMessage(ch.ethz.iks.r_osgi.messages.RemoteCallMessage) RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) InputStreamHandle(ch.ethz.iks.r_osgi.streams.InputStreamHandle) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ChannelEndpoint(ch.ethz.iks.r_osgi.channels.ChannelEndpoint)

Example 9 with RemoteOSGiException

use of ch.ethz.iks.r_osgi.RemoteOSGiException in project ecf by eclipse.

the class ChannelEndpointImpl method getCloneBundle.

/**
 * get a clone of a remote bundle
 *
 * @param ref
 *            the remote service reference to the service for which the
 *            bundle clone is requested.
 */
void getCloneBundle(final RemoteServiceReference ref) {
    if (networkChannel == null) {
        // $NON-NLS-1$
        throw new RemoteOSGiException("Channel is closed.");
    }
    // build the RequestBundleMessage
    final RequestBundleMessage req = new RequestBundleMessage();
    req.setServiceID(ref.getURI().getFragment());
    final DeliverBundlesMessage deliv = (DeliverBundlesMessage) sendAndWait(req);
    final byte[] bundleBytes = deliv.getDependencies()[0];
    installResolveAndStartBundle(ref, new ByteArrayInputStream(bundleBytes), false);
}
Also used : RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) ByteArrayInputStream(java.io.ByteArrayInputStream) DeliverBundlesMessage(ch.ethz.iks.r_osgi.messages.DeliverBundlesMessage) RequestBundleMessage(ch.ethz.iks.r_osgi.messages.RequestBundleMessage)

Example 10 with RemoteOSGiException

use of ch.ethz.iks.r_osgi.RemoteOSGiException in project ecf by eclipse.

the class RemoteOSGiServiceImpl method getRemoteServiceReference.

/**
 * @see ch.ethz.iks.r_osgi.RemoteOSGiService#getRemoteServiceReference(ch.ethz.iks.r_osgi.URI)
 */
public RemoteServiceReference getRemoteServiceReference(final URI serviceURI) {
    final String uri = getChannelURI(serviceURI);
    ChannelEndpointImpl channel = (ChannelEndpointImpl) channels.get(getChannelURI(serviceURI));
    if (channel == null) {
        try {
            connect(serviceURI);
            channel = (ChannelEndpointImpl) channels.get(uri);
        } catch (final IOException ioe) {
            // $NON-NLS-1$
            throw new RemoteOSGiException("Cannot connect to " + uri);
        }
    }
    return channel.getRemoteReference(serviceURI.toString());
}
Also used : RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) IOException(java.io.IOException)

Aggregations

RemoteOSGiException (ch.ethz.iks.r_osgi.RemoteOSGiException)13 IOException (java.io.IOException)6 ChannelEndpoint (ch.ethz.iks.r_osgi.channels.ChannelEndpoint)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)4 RemoteServiceReference (ch.ethz.iks.r_osgi.RemoteServiceReference)3 RemoteCallMessage (ch.ethz.iks.r_osgi.messages.RemoteCallMessage)3 RemoteCallResultMessage (ch.ethz.iks.r_osgi.messages.RemoteCallResultMessage)3 RemoteOSGiMessage (ch.ethz.iks.r_osgi.messages.RemoteOSGiMessage)3 InputStreamHandle (ch.ethz.iks.r_osgi.streams.InputStreamHandle)3 OutputStreamHandle (ch.ethz.iks.r_osgi.streams.OutputStreamHandle)3 OutputStream (java.io.OutputStream)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 NetworkChannelFactory (ch.ethz.iks.r_osgi.channels.NetworkChannelFactory)2 DeliverBundlesMessage (ch.ethz.iks.r_osgi.messages.DeliverBundlesMessage)2 DeliverServiceMessage (ch.ethz.iks.r_osgi.messages.DeliverServiceMessage)2 LeaseUpdateMessage (ch.ethz.iks.r_osgi.messages.LeaseUpdateMessage)2 RequestBundleMessage (ch.ethz.iks.r_osgi.messages.RequestBundleMessage)2 RequestServiceMessage (ch.ethz.iks.r_osgi.messages.RequestServiceMessage)2 ArrayList (java.util.ArrayList)2