Search in sources :

Example 1 with RemoteOSGiMessage

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

the class ChannelEndpointImpl method receivedMessage.

/**
 * process a recieved message. Called by the channel.
 *
 * @param msg
 *            the received message.
 * @see ch.ethz.iks.r_osgi.channels.ChannelEndpoint#receivedMessage(ch.ethz.iks.r_osgi.messages.RemoteOSGiMessage)
 * @category ChannelEndpoint
 */
public void receivedMessage(final RemoteOSGiMessage msg) {
    if (msg == null) {
        dispose();
        return;
    }
    final Integer xid = new Integer(msg.getXID());
    final WaitingCallback callback;
    synchronized (callbacks) {
        callback = (WaitingCallback) callbacks.remove(xid);
    }
    if (callback != null) {
        callback.result(msg);
        return;
    } else {
        final Runnable r = new Runnable() {

            public void run() {
                final RemoteOSGiMessage reply = handleMessage(msg);
                if (reply != null) {
                    try {
                        trace("reply(msg=" + reply + ";remoteAddress=" + networkChannel.getRemoteAddress() + ")");
                        networkChannel.sendMessage(reply);
                    } catch (final NotSerializableException nse) {
                        throw new RemoteOSGiException(// $NON-NLS-1$
                        "Error sending " + reply, nse);
                    } catch (NullPointerException npe) {
                    // channel got closed
                    } catch (final IOException e) {
                        dispose();
                    }
                }
            }
        };
        synchronized (workQueue) {
            workQueue.add(r);
            workQueue.notify();
        }
    }
}
Also used : NotSerializableException(java.io.NotSerializableException) RemoteOSGiException(ch.ethz.iks.r_osgi.RemoteOSGiException) RemoteOSGiMessage(ch.ethz.iks.r_osgi.messages.RemoteOSGiMessage) IOException(java.io.IOException)

Example 2 with RemoteOSGiMessage

use of ch.ethz.iks.r_osgi.messages.RemoteOSGiMessage 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 3 with RemoteOSGiMessage

use of ch.ethz.iks.r_osgi.messages.RemoteOSGiMessage 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)

Aggregations

RemoteOSGiException (ch.ethz.iks.r_osgi.RemoteOSGiException)3 RemoteOSGiMessage (ch.ethz.iks.r_osgi.messages.RemoteOSGiMessage)3 ChannelEndpoint (ch.ethz.iks.r_osgi.channels.ChannelEndpoint)1 RemoteCallMessage (ch.ethz.iks.r_osgi.messages.RemoteCallMessage)1 RemoteCallResultMessage (ch.ethz.iks.r_osgi.messages.RemoteCallResultMessage)1 InputStreamHandle (ch.ethz.iks.r_osgi.streams.InputStreamHandle)1 OutputStreamHandle (ch.ethz.iks.r_osgi.streams.OutputStreamHandle)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 NotSerializableException (java.io.NotSerializableException)1 OutputStream (java.io.OutputStream)1