Change log level, add stopped flag, move connection listener inside success handler

This commit is contained in:
Manfred Karrer 2016-02-22 23:58:42 +01:00
parent 03b5a03048
commit 2775c0a55d
5 changed files with 48 additions and 28 deletions

View File

@ -33,7 +33,6 @@ public class Broadcaster implements ConnectionListener, PeerManager.Listener {
private PeerManager peerManager;
private final Set<Listener> listeners = new CopyOnWriteArraySet<>();
private boolean stopped = false;
private final IntegerProperty numOfBroadcasts = new SimpleIntegerProperty(0);

View File

@ -96,7 +96,8 @@ public class RequestDataHandler implements MessageListener {
connection.addMessageListener(RequestDataHandler.this);
log.trace("Send " + getDataRequest + " to " + nodeAddress + " succeeded.");
} else {
log.warn("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call.");
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call." +
"Might be caused by an previous timeout.");
}
}
@ -110,7 +111,8 @@ public class RequestDataHandler implements MessageListener {
log.info(errorMessage);
handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_FAILURE);
} else {
log.warn("We have stopped already. We ignore that networkNode.sendMessage.onFailure call.");
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onFailure call. " +
"Might be caused by an previous timeout.");
}
}
});
@ -123,7 +125,8 @@ public class RequestDataHandler implements MessageListener {
log.info(errorMessage + " / RequestDataHandler=" + RequestDataHandler.this);
handleFault(errorMessage, nodeAddress, CloseConnectionReason.SEND_MSG_TIMEOUT);
} else {
log.warn("We have stopped already. We ignore that timeoutTimer.run call.");
log.trace("We have stopped already. We ignore that timeoutTimer.run call. " +
"Might be caused by an previous networkNode.sendMessage.onFailure.");
}
},
10);

View File

@ -69,14 +69,18 @@ class KeepAliveHandler implements MessageListener {
public void sendPing(Connection connection) {
Log.traceCall("connection=" + connection + " / this=" + this);
if (!stopped) {
this.connection = connection;
connection.addMessageListener(this);
Ping ping = new Ping(nonce);
SettableFuture<Connection> future = networkNode.sendMessage(connection, ping);
Futures.addCallback(future, new FutureCallback<Connection>() {
@Override
public void onSuccess(Connection connection) {
log.trace("Send " + ping + " to " + connection + " succeeded.");
if (!stopped) {
log.trace("Send " + ping + " to " + connection + " succeeded.");
KeepAliveHandler.this.connection = connection;
connection.addMessageListener(KeepAliveHandler.this);
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call.");
}
}
@Override
@ -91,12 +95,12 @@ class KeepAliveHandler implements MessageListener {
peerManager.handleConnectionFault(connection);
listener.onFault(errorMessage);
} else {
log.warn("We have stopped already. We ignore that sendPing.onFailure call.");
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onFailure call.");
}
}
});
} else {
log.warn("We have stopped already. We ignore that sendPing call.");
log.trace("We have stopped already. We ignore that sendPing call.");
}
}
@ -120,7 +124,7 @@ class KeepAliveHandler implements MessageListener {
nonce, pong.requestNonce);
}
} else {
log.warn("We have stopped already. We ignore that onMessage call.");
log.trace("We have stopped already. We ignore that onMessage call.");
}
}
}

View File

@ -45,6 +45,7 @@ class GetPeersRequestHandler {
private final PeerManager peerManager;
private final Listener listener;
private Timer timeoutTimer;
private boolean stopped;
///////////////////////////////////////////////////////////////////////////////////////////
@ -74,28 +75,40 @@ class GetPeersRequestHandler {
Futures.addCallback(future, new FutureCallback<Connection>() {
@Override
public void onSuccess(Connection connection) {
log.trace("GetPeersResponse sent successfully");
cleanup();
listener.onComplete();
if (!stopped) {
log.trace("GetPeersResponse sent successfully");
cleanup();
listener.onComplete();
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onSuccess call.");
}
}
@Override
public void onFailure(@NotNull Throwable throwable) {
String errorMessage = "Sending getPeersResponse to " + connection +
" failed. That is expected if the peer is offline. getPeersResponse=" + getPeersResponse + "." +
"Exception: " + throwable.getMessage();
log.info(errorMessage);
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_FAILURE, connection);
if (!stopped) {
String errorMessage = "Sending getPeersResponse to " + connection +
" failed. That is expected if the peer is offline. getPeersResponse=" + getPeersResponse + "." +
"Exception: " + throwable.getMessage();
log.info(errorMessage);
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_FAILURE, connection);
} else {
log.trace("We have stopped already. We ignore that networkNode.sendMessage.onFailure call.");
}
}
});
checkArgument(timeoutTimer == null, "onGetPeersRequest must not be called twice.");
timeoutTimer = UserThread.runAfter(() -> {
String errorMessage = "A timeout occurred at sending getPeersResponse:" + getPeersResponse + " on connection:" + connection;
log.info(errorMessage + " / PeerExchangeHandshake=" +
GetPeersRequestHandler.this);
log.info("timeoutTimer called. this=" + this);
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, connection);
if (!stopped) {
String errorMessage = "A timeout occurred at sending getPeersResponse:" + getPeersResponse + " on connection:" + connection;
log.info(errorMessage + " / PeerExchangeHandshake=" +
GetPeersRequestHandler.this);
log.info("timeoutTimer called. this=" + this);
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, connection);
} else {
log.trace("We have stopped already. We ignore that timeoutTimer.run call.");
}
},
TIME_OUT_SEC, TimeUnit.SECONDS);
@ -114,6 +127,7 @@ class GetPeersRequestHandler {
}
private void cleanup() {
stopped = true;
if (timeoutTimer != null) {
timeoutTimer.stop();
timeoutTimer = null;

View File

@ -94,7 +94,7 @@ class PeerExchangeHandler implements MessageListener {
connection.addMessageListener(PeerExchangeHandler.this);
log.trace("Send " + getPeersRequest + " to " + nodeAddress + " succeeded.");
} else {
log.warn("We have stopped that handler already. We ignore that sendGetPeersRequest.onSuccess call.");
log.trace("We have stopped that handler already. We ignore that sendGetPeersRequest.onSuccess call.");
}
}
@ -107,7 +107,7 @@ class PeerExchangeHandler implements MessageListener {
log.info(errorMessage);
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_FAILURE, nodeAddress);
} else {
log.warn("We have stopped that handler already. We ignore that sendGetPeersRequest.onFailure call.");
log.trace("We have stopped that handler already. We ignore that sendGetPeersRequest.onFailure call.");
}
}
});
@ -121,7 +121,7 @@ class PeerExchangeHandler implements MessageListener {
log.info("timeoutTimer called on " + this);
handleFault(errorMessage, CloseConnectionReason.SEND_MSG_TIMEOUT, nodeAddress);
} else {
log.warn("We have stopped that handler already. We ignore that timeoutTimer.run call.");
log.trace("We have stopped that handler already. We ignore that timeoutTimer.run call.");
}
},
TIME_OUT_SEC, TimeUnit.SECONDS);
@ -129,7 +129,7 @@ class PeerExchangeHandler implements MessageListener {
log.warn("My node address is still null at sendGetPeersRequest. We ignore that call.");
}
} else {
log.warn("We have stopped that handler already. We ignore that sendGetPeersRequest call.");
log.trace("We have stopped that handler already. We ignore that sendGetPeersRequest call.");
}
}
@ -157,7 +157,7 @@ class PeerExchangeHandler implements MessageListener {
nonce, getPeersResponse.requestNonce);
}
} else {
log.warn("We have stopped that handler already. We ignore that onMessage call.");
log.trace("We have stopped that handler already. We ignore that onMessage call.");
}
}
}