use of neo.model.network.exception.MessageFormatException in project neo-java by coranos.
the class RemoteNodeControllerRunnable method run.
/**
* the run method.
*/
@Override
public void run() {
LOG.debug("STARTED RemoteNodeControllerRunnable run {}", data.getHostAddress());
final long startTimeMs = System.currentTimeMillis();
final LocalNodeData localNodeData = localControllerNode.getLocalNodeData();
final long readTimeOut = localNodeData.getRpcServerTimeoutMillis();
final long magic = localNodeData.getMagic();
final int localPort = localNodeData.getTcpPort();
final int nonce = localNodeData.getNonce();
final Block maxStartHeightBlock = localNodeData.getBlockDb().getHeaderOfBlockWithMaxIndex();
final long startHeight;
if (maxStartHeightBlock == null) {
startHeight = 0;
} else {
startHeight = maxStartHeightBlock.getIndexAsLong();
}
data.getSendQueue().add(new Message(magic, CommandEnum.VERSION, PayloadUtil.getVersionPayload(localPort, nonce, startHeight).toByteArray()));
data.getSendQueue().add(new Message(magic, CommandEnum.VERACK));
try {
try (SocketWrapper s = localNodeData.getSocketFactory().newSocketWrapper()) {
s.setSoTimeout(2000);
s.connect(data.getTcpAddressAndPort(), 2000);
try (OutputStream out = s.getOutputStream();
InputStream in = s.getInputStream()) {
data.setGoodPeer(true);
while (data.isGoodPeer()) {
sendMessages(out);
out.flush();
try {
Thread.sleep(data.getSleepIntervalMs());
} catch (final InterruptedException e) {
LOG.debug("InterruptedException[1], stopping. {}", e.getMessage());
data.setGoodPeer(false);
}
if (data.isGoodPeer()) {
recieveMessages(readTimeOut, magic, in);
}
final long currTimeMs = System.currentTimeMillis();
final long recycleTimeMs = startTimeMs + data.getRecycleIntervalMs();
if (recycleTimeMs < currTimeMs) {
LOG.debug("recycling remote node {}", data.getHostAddress());
data.setGoodPeer(false);
}
if (data.isGoodPeer()) {
try {
Thread.sleep(data.getSleepIntervalMs());
} catch (final InterruptedException e) {
LOG.debug("InterruptedException[2], stopping. {}", e.getMessage());
data.setGoodPeer(false);
}
}
}
}
} catch (final SocketTimeoutException e) {
LOG.trace("SocketTimeoutException[2] from {}, closing peer", data.getHostAddress());
LOG.trace("SocketTimeoutException[2]", e);
data.setGoodPeer(false);
} catch (final ConnectException e) {
LOG.trace("ConnectException from {}, closing peer", data.getHostAddress());
LOG.trace("ConnectException", e);
data.setGoodPeer(false);
} catch (final MessageFormatException e) {
LOG.trace("MessageFormatException from {}, closing peer", data.getHostAddress());
LOG.trace("MessageFormatException", e);
data.setGoodPeer(false);
} catch (final SocketException e) {
if (e.getMessage().equals("Broken pipe (Write failed)")) {
LOG.trace("SocketException from {}, broken pipe, closing peer", data.getHostAddress());
} else if (e.getMessage().equals("Operation timed out (Read failed)")) {
LOG.trace("SocketException from {}, timeout, closing peer", data.getHostAddress());
} else if (e.getMessage().equals("Connection reset")) {
LOG.trace("SocketException from {}, connection reset, closing peer", data.getHostAddress());
} else if (e.getMessage().equals("Network is unreachable (connect failed)")) {
LOG.trace("SocketException from {}, unreachable network, closing peer", data.getHostAddress());
} else if (e.getMessage().equals("Protocol wrong type for socket (Write failed)")) {
LOG.trace("SocketException from {}, wrong protocol, closing peer", data.getHostAddress());
} else {
LOG.error("SocketException from {}, closing peer", data.getHostAddress());
LOG.error("SocketException", e);
}
data.setGoodPeer(false);
} catch (final ClosedChannelException e) {
LOG.trace("ClosedChannelException from {}, closing peer", data.getHostAddress());
data.setGoodPeer(false);
}
} catch (final Exception e) {
LOG.error("error", e);
LOG.debug("FAILURE RemoteNodeControllerRunnable run");
localControllerNode.onSocketClose(RemoteNodeControllerRunnable.this);
return;
}
localControllerNode.onSocketClose(RemoteNodeControllerRunnable.this);
LOG.debug("SUCCESS RemoteNodeControllerRunnable run");
}
Aggregations