public void operationComplete(FuturePeerConnection future) throws Exception {
if (fpc.isSuccess()) {
final PeerConnection peerConnection = fpc.peerConnection();
if (peerConnection != null) {
// create the necessary messages
final Message setUpMessage = createSetupMessage(relayPeerAddress, unreachablePeerAddress);
// send the message to the relay so it forwards it to the unreachable peer
FutureResponse futureResponse = RelayUtils.send(peerConnection, peer.peerBean(),
peer.connectionBean(), config, setUpMessage);
// wait for the unreachable peer to answer
futureResponse.addListener(new BaseFutureAdapter<FutureResponse>() {
@Override
public void operationComplete(FutureResponse future) throws Exception {
// get the PeerConnection which is cached in the PeerBean object
final PeerConnection openPeerConnection = peer.peerBean().peerConnection(unreachablePeerAddress.peerId());
if (openPeerConnection != null && openPeerConnection.isOpen()) {
futureDone.done(openPeerConnection);
} else {
LOG.error("The reverse connection to the unreachable peer failed.");
handleFail(futureDone, "No reverse connection could be established");
}
// can close the connection to the relay peer
peerConnection.close();
}
});
} else {
handleFail(futureDone, "The PeerConnection was null!");
}
} else {
handleFail(futureDone, "no channel could be established");
}
}
private void handleFail(final FutureDone<PeerConnection> futureDone, final String failMessage) {
LOG.error(failMessage);
futureDone.failed(failMessage);
}
// this message is sent to the relay peer to initiate the rcon setup
private Message createSetupMessage(final PeerAddress relayPeerAddress, final PeerAddress unreachablePeerAddress) {
Message setUpMessage = new Message();
setUpMessage.version(MESSAGE_VERSION);
setUpMessage.sender(peer.peerAddress());
setUpMessage.recipient(relayPeerAddress.changePeerId(unreachablePeerAddress.peerId()));
setUpMessage.command(RPC.Commands.RCON.getNr());
setUpMessage.type(Type.REQUEST_1);
// setUpMessage.keepAlive(true);
return setUpMessage;
}
});
return futureDone;