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