}
private Message invokeOperation(Message msg) {
OtpSelf self = null;
OtpPeer other = null;
OtpConnection connection = null;
try {
self = new OtpSelf(getClientNodeName());
if (binding.hasCookie()) {
self.setCookie(binding.getCookie());
}
other = new OtpPeer(binding.getNode());
connection = self.connect(other);
Method jmethod = ((JavaOperation) msg.getOperation())
.getJavaMethod();
OtpErlangList params = TypeHelpersProxy.toErlangAsList(msg
.getBody(), jmethod.getParameterAnnotations());
OtpErlangTuple message = MessageHelper.rpcMessage(self.pid(), self
.createRef(), binding.getModule(), msg.getOperation()
.getName(), params);
connection.send(MessageHelper.RPC_MBOX, message);
OtpErlangObject rpcResponse = null;
if (binding.hasTimeout()) {
rpcResponse = connection.receive(binding.getTimeout());
} else {
rpcResponse = connection.receive();
}
OtpErlangObject result = ((OtpErlangTuple) rpcResponse)
.elementAt(1);
if (MessageHelper.isfunctionUndefMessage(result)) {
// TODO: externalize message?
Exception e = new ErlangException("No '" + binding.getModule()
+ ":" + msg.getOperation().getName()
+ "' operation defined on remote '" + binding.getNode()
+ "' node.");
reportProblem(msg, e);
msg.setBody(null);
} else if (msg.getOperation().getOutputType() != null) {
jmethod.getAnnotations();
msg.setBody(TypeHelpersProxy.toJava(result, msg.getOperation()
.getOutputType().getPhysical(), jmethod
.getAnnotations()));
}
} catch (OtpAuthException e) {
// TODO: externalize message?
ErlangException ee = new ErlangException(
"Problem while authenticating client - check your cookie",
e);
msg.setBody(null);
reportProblem(msg, ee);
} catch (InterruptedException e) {
// TODO: externalize message?
ErlangException ee = new ErlangException(
"Timeout while receiving RPC reply", e);
msg.setBody(null);
reportProblem(msg, ee);
} catch (Exception e) {
reportProblem(msg, e);
} finally {
if (connection != null) {
connection.close();
}
}
return msg;
}