Use connection for onMessage listeners if possible

This commit is contained in:
Manfred Karrer 2016-02-22 23:34:52 +01:00
parent 77511a43f5
commit 03b5a03048
2 changed files with 13 additions and 7 deletions

View File

@ -52,6 +52,8 @@ public class RequestDataHandler implements MessageListener {
private Timer timeoutTimer; private Timer timeoutTimer;
private final int nonce = new Random().nextInt(); private final int nonce = new Random().nextInt();
private boolean stopped; private boolean stopped;
private Connection connection;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Constructor // Constructor
@ -63,8 +65,6 @@ public class RequestDataHandler implements MessageListener {
this.dataStorage = dataStorage; this.dataStorage = dataStorage;
this.peerManager = peerManager; this.peerManager = peerManager;
this.listener = listener; this.listener = listener;
networkNode.addMessageListener(this);
} }
public void cancel() { public void cancel() {
@ -90,8 +90,14 @@ public class RequestDataHandler implements MessageListener {
SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getDataRequest); SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getDataRequest);
Futures.addCallback(future, new FutureCallback<Connection>() { Futures.addCallback(future, new FutureCallback<Connection>() {
@Override @Override
public void onSuccess(@Nullable Connection connection) { public void onSuccess(Connection connection) {
if (!stopped) {
RequestDataHandler.this.connection = connection;
connection.addMessageListener(RequestDataHandler.this);
log.trace("Send " + getDataRequest + " to " + nodeAddress + " succeeded."); log.trace("Send " + getDataRequest + " to " + nodeAddress + " succeeded.");
} else {
log.warn("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call.");
}
} }
@Override @Override
@ -104,7 +110,7 @@ public class RequestDataHandler implements MessageListener {
log.info(errorMessage); log.info(errorMessage);
handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_FAILURE); handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_FAILURE);
} else { } else {
log.warn("We have stopped already. We ignore that requestData.onFailure call."); log.warn("We have stopped already. We ignore that networkNode.sendMessage.onFailure call.");
} }
} }
}); });
@ -176,7 +182,8 @@ public class RequestDataHandler implements MessageListener {
private void cleanup() { private void cleanup() {
Log.traceCall(); Log.traceCall();
stopped = true; stopped = true;
networkNode.removeMessageListener(this); if (connection != null)
connection.removeMessageListener(this);
stopTimeoutTimer(); stopTimeoutTimer();
} }

View File

@ -85,7 +85,6 @@ class PeerExchangeHandler implements MessageListener {
@Override @Override
public void onSuccess(Connection connection) { public void onSuccess(Connection connection) {
if (!stopped) { if (!stopped) {
if (!connection.getPeersNodeAddressOptional().isPresent()) { if (!connection.getPeersNodeAddressOptional().isPresent()) {
connection.setPeersNodeAddress(nodeAddress); connection.setPeersNodeAddress(nodeAddress);
log.warn("sendGetPeersRequest: !connection.getPeersNodeAddressOptional().isPresent()"); log.warn("sendGetPeersRequest: !connection.getPeersNodeAddressOptional().isPresent()");