Search in sources :

Example 1 with SocketWrapper

use of neo.network.model.socket.SocketWrapper 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");
}
Also used : MessageFormatException(neo.model.network.exception.MessageFormatException) SocketException(java.net.SocketException) ClosedChannelException(java.nio.channels.ClosedChannelException) LocalNodeData(neo.network.model.LocalNodeData) Message(neo.model.network.Message) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) MessageFormatException(neo.model.network.exception.MessageFormatException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) SocketTimeoutException(java.net.SocketTimeoutException) Block(neo.model.core.Block) SocketWrapper(neo.network.model.socket.SocketWrapper) ConnectException(java.net.ConnectException)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 ConnectException (java.net.ConnectException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 Block (neo.model.core.Block)1 Message (neo.model.network.Message)1 MessageFormatException (neo.model.network.exception.MessageFormatException)1 LocalNodeData (neo.network.model.LocalNodeData)1 SocketWrapper (neo.network.model.socket.SocketWrapper)1