Search in sources :

Example 1 with Message

use of neo.model.network.Message in project neo-java by coranos.

the class TestUtil method sendAsynchCommand.

public static void sendAsynchCommand(final OutputStream out, final CommandEnum command, final Optional<ByteArraySerializable> payload, final JSONObject error) throws IOException {
    final byte[] payloadBa;
    if (payload.isPresent()) {
        payloadBa = payload.get().toByteArray();
    } else {
        payloadBa = new byte[0];
    }
    final Message requestMessage = new Message(TestUtil.MAIN_NET_MAGIC, command, payloadBa);
    final byte[] requestBa = requestMessage.toByteArray();
    LOG.info(">>>:{}", Hex.encodeHexString(requestBa));
    out.write(requestBa);
}
Also used : Message(neo.model.network.Message)

Example 2 with Message

use of neo.model.network.Message in project neo-java by coranos.

the class MessageUtil method sendGetHeaders.

/**
 * send a message to get header data.
 *
 * @param remoteNodeData
 *            the remote node data to use.
 * @param localNodeData
 *            the local node data to use.
 * @param hash
 *            the hash to use.
 */
public static void sendGetHeaders(final RemoteNodeData remoteNodeData, final LocalNodeData localNodeData, final UInt256 hash) {
    LOG.debug("sendGetHeaders requesting hash:{};", hash);
    remoteNodeData.send(new Message(localNodeData.getMagic(), CommandEnum.GETHEADERS, new GetBlocksPayload(hash, null).toByteArray()));
}
Also used : Message(neo.model.network.Message) GetBlocksPayload(neo.model.network.GetBlocksPayload)

Example 3 with Message

use of neo.model.network.Message in project neo-java by coranos.

the class MessageUtil method sendGetData.

/**
 * send a message to get block data.
 *
 * @param remoteNodeData
 *            the remote node data to use.
 * @param localNodeData
 *            the local node data to use.
 * @param type
 *            the inventory type.
 * @param hashs
 *            the hashes to use.
 */
public static void sendGetData(final RemoteNodeData remoteNodeData, final LocalNodeData localNodeData, final InventoryType type, final UInt256... hashs) {
    if (type.equals(InventoryType.BLOCK)) {
        boolean hasDuplicates = false;
        for (final UInt256 hash : hashs) {
            if (localNodeData.getBlockDb().containsBlockWithHash(hash)) {
                hasDuplicates = true;
                LOG.debug("sendGetData requesting duplicate block hash:{}", hash);
            }
        }
        if (hasDuplicates) {
            MapUtil.increment(LocalNodeData.API_CALL_MAP, DUPLICATE_OUT_BLOCK);
        }
    }
    remoteNodeData.send(new Message(localNodeData.getMagic(), CommandEnum.GETDATA, new InvPayload(type, hashs).toByteArray()));
}
Also used : Message(neo.model.network.Message) InvPayload(neo.model.network.InvPayload) UInt256(neo.model.bytes.UInt256)

Example 4 with Message

use of neo.model.network.Message 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)

Example 5 with Message

use of neo.model.network.Message in project neo-java by coranos.

the class TestUtil method sendAsynchCommand.

public static Message sendAsynchCommand(final String host, final int port, final CommandEnum command, final Optional<ByteArraySerializable> payload, final JSONObject error) throws IOException {
    final byte[] payloadBa;
    if (payload.isPresent()) {
        payloadBa = payload.get().toByteArray();
    } else {
        payloadBa = new byte[0];
    }
    final Message requestMessage = new Message(TestUtil.MAIN_NET_MAGIC, command, payloadBa);
    final byte[] requestBa = requestMessage.toByteArray();
    LOG.info(">>>:{}", Hex.encodeHexString(requestBa));
    final byte[] responseBa = sendAsynch(host, port, requestBa, error);
    LOG.info("<<<:{}", Hex.encodeHexString(responseBa));
    if (responseBa.length == 0) {
        return null;
    }
    final ByteBuffer bb = ByteBuffer.wrap(responseBa);
    final Message responseMessage = new Message(bb);
    if (responseMessage.magic != MAIN_NET_MAGIC) {
        throw new RuntimeException("response magic was " + responseMessage.magic + " expected " + MAIN_NET_MAGIC);
    }
    return responseMessage;
}
Also used : Message(neo.model.network.Message) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Message (neo.model.network.Message)9 IOException (java.io.IOException)3 InterruptedIOException (java.io.InterruptedIOException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 ByteBuffer (java.nio.ByteBuffer)2 InvPayload (neo.model.network.InvPayload)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ConnectException (java.net.ConnectException)1 SocketException (java.net.SocketException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 UInt256 (neo.model.bytes.UInt256)1 Block (neo.model.core.Block)1 Transaction (neo.model.core.Transaction)1 GetBlocksPayload (neo.model.network.GetBlocksPayload)1 MessageFormatException (neo.model.network.exception.MessageFormatException)1 LocalNodeData (neo.network.model.LocalNodeData)1 SocketWrapper (neo.network.model.socket.SocketWrapper)1