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();
}
}
}
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.");
}
}
}
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));
}
}
Aggregations