import com.google.common.base.Preconditions;
import java.net.InetSocketAddress;
import java.util.Date;
+import java.util.Objects;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
*/
public class SystemNotificationsListenerImpl implements SystemNotificationsListener {
- private final ConnectionContext connectionContext;
private static final Logger LOG = LoggerFactory.getLogger(SystemNotificationsListenerImpl.class);
+ private static final Xid ECHO_XID = new Xid(0L);
+
+ private final ConnectionContext connectionContext;
@VisibleForTesting
static final long MAX_ECHO_REPLY_TIMEOUT = 2000;
private final long echoReplyTimeout;
@Override
public void onDisconnectEvent(final DisconnectEvent notification) {
LOG.info("ConnectionEvent: Connection closed by device, Device:{}, NodeId:{}",
- connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getNodeId());
+ connectionContext.getConnectionAdapter().getRemoteAddress(), connectionContext.getSafeNodeIdForLOG());
connectionContext.onConnectionClosed();
}
connectionContext.changeStateToTimeouting();
EchoInputBuilder builder = new EchoInputBuilder();
builder.setVersion(features.getVersion());
- Xid xid = new Xid(0L);
- builder.setXid(xid.getValue());
+ builder.setXid(ECHO_XID.getValue());
Future<RpcResult<EchoOutput>> echoReplyFuture = connectionContext.getConnectionAdapter().echo(builder.build());
try {
RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
- if (echoReplyValue.isSuccessful()) {
+ if (echoReplyValue.isSuccessful() &&
+ Objects.equals(echoReplyValue.getResult().getXid(), ECHO_XID.getValue())) {
connectionContext.changeStateToWorking();
shouldBeDisconnected = false;
} else {
if (shouldBeDisconnected) {
if (LOG.isInfoEnabled()) {
LOG.info("ConnectionEvent:Closing connection as device is idle. Echo sent at {}. Device:{}, NodeId:{}",
- new Date(System.currentTimeMillis() - echoReplyTimeout), remoteAddress, connectionContext.getNodeId());
+ new Date(System.currentTimeMillis() - echoReplyTimeout), remoteAddress, connectionContext.getSafeNodeIdForLOG());
}
connectionContext.closeConnection(true);