Search in sources :

Example 6 with Message

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

the class TestUtil method readAsynchCommand.

public static Message readAsynchCommand(final InputStream in, final JSONObject error) throws IOException {
    final byte[] responseBa = readFully(in);
    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)

Example 7 with Message

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

the class RemoteNodeControllerRunnable method getMessageOrTimeOut.

/**
 * returns the message, or returns null if there's a SocketTimeoutException.
 *
 * @param readTimeOut
 *            teh read time out.
 * @param in
 *            the input stream to read.
 *
 * @return the message, or returns null if there's a SocketTimeoutException.
 * @throws IOException
 *             if an error occurs.
 */
private Message getMessageOrTimeOut(final long readTimeOut, final InputStream in) throws IOException {
    Message messageRecieved;
    try {
        messageRecieved = new Message(readTimeOut, in);
    } catch (final SocketTimeoutException e) {
        LOG.trace("SocketTimeoutException[1] from {}, closing peer", data.getHostAddress());
        LOG.trace("SocketTimeoutException[1]", e);
        messageRecieved = null;
    } catch (final InterruptedIOException e) {
        LOG.trace("InterruptedIOException from {}, closing peer", data.getHostAddress());
        LOG.trace("InterruptedIOException", e);
        data.setGoodPeer(false);
        messageRecieved = null;
    } catch (final IOException e) {
        if (e.getMessage() == null) {
            throw new RuntimeException(e);
        } else if (e.getMessage().equals("Connection reset by peer")) {
            LOG.trace("IOException[1] from {}, \"{}\" closing peer", e.getMessage(), data.getHostAddress());
            LOG.trace("IOException[1]", e);
            data.setGoodPeer(false);
            messageRecieved = null;
        } else if (e.getMessage().equals("Operation timed out")) {
            messageRecieved = null;
        } else {
            throw new RuntimeException(e);
        }
    } catch (final RuntimeException e) {
        LOG.trace("RuntimeException from {}, closing peer", data.getHostAddress());
        data.setGoodPeer(false);
        messageRecieved = null;
        throw e;
    }
    return messageRecieved;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) SocketTimeoutException(java.net.SocketTimeoutException) Message(neo.model.network.Message) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 8 with Message

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

the class RemoteNodeControllerRunnable method recieveMessages.

/**
 * recieve messages.
 *
 * @param readTimeOut
 *            the read timeout.
 * @param magic
 *            the magic number to check for valid messages.
 * @param in
 *            the input stream.
 * @throws IOException
 *             if an error occurs.
 */
private void recieveMessages(final long readTimeOut, final long magic, final InputStream in) throws IOException {
    Message messageRecieved = getMessageOrTimeOut(readTimeOut, in);
    while (messageRecieved != null) {
        if (messageRecieved.magic != magic) {
            LOG.debug(" magic was {} expected {} closing peer.", messageRecieved.magic, magic);
            data.setGoodPeer(false);
        } else {
            MapUtil.increment(LocalNodeData.API_CALL_MAP, RemoteNodeData.IN_BYTES, messageRecieved.getPayloadByteArray().length + 24);
            if (messageRecieved.commandEnum != null) {
                final long apiCallCount;
                final String apiCallRoot = "in-" + messageRecieved.commandEnum.name().toLowerCase();
                if (messageRecieved.commandEnum.equals(CommandEnum.TX)) {
                    final Transaction tx = messageRecieved.getPayload(Transaction.class);
                    final String apiCall = apiCallRoot + DASH + tx.type.name().toLowerCase();
                    apiCallCount = MapUtil.increment(LocalNodeData.API_CALL_MAP, apiCall);
                } else if (messageRecieved.commandEnum.equals(CommandEnum.INV)) {
                    final InvPayload payload = messageRecieved.getPayload(InvPayload.class);
                    final String apiCall = apiCallRoot + DASH + payload.getType().name().toLowerCase();
                    final String apiCallHash = apiCallRoot + DASH + payload.getType().name().toLowerCase() + "-hashes";
                    apiCallCount = MapUtil.increment(LocalNodeData.API_CALL_MAP, apiCall);
                    MapUtil.increment(LocalNodeData.API_CALL_MAP, apiCallHash, payload.getHashes().size());
                } else {
                    apiCallCount = MapUtil.increment(LocalNodeData.API_CALL_MAP, apiCallRoot);
                }
                LOG.debug("response from {}:{} {}", data.getHostAddress(), messageRecieved.command, apiCallCount);
            }
            localControllerNode.onMessage(RemoteNodeControllerRunnable.this, messageRecieved);
        }
        if (!data.isGoodPeer()) {
            return;
        }
        messageRecieved = getMessageOrTimeOut(readTimeOut, in);
    }
}
Also used : Message(neo.model.network.Message) Transaction(neo.model.core.Transaction) InvPayload(neo.model.network.InvPayload)

Example 9 with Message

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

the class RemoteNodeControllerRunnable method sendMessages.

/**
 * send messages.
 *
 * @param out
 *            the output stream to use.
 * @throws IOException
 *             if an error occurs.
 */
private void sendMessages(final OutputStream out) throws IOException {
    Message messageToSend = data.getSendQueue().poll();
    while (messageToSend != null) {
        if (!data.isGoodPeer()) {
            return;
        }
        final byte[] outBa = messageToSend.toByteArray();
        try {
            out.write(outBa);
        } catch (final SocketTimeoutException e) {
            LOG.trace("SocketTimeoutException[3] from {}, closing peer", data.getHostAddress());
            LOG.trace("SocketTimeoutException[3]", e);
            data.setGoodPeer(false);
            return;
        } catch (final IOException e) {
            if (e.getMessage().equals("Broken pipe")) {
                LOG.trace("IOException[2] from {}, \"{}\" closing peer", e.getMessage(), data.getHostAddress());
                LOG.trace("IOException[2]", e);
                data.setGoodPeer(false);
                return;
            } else {
                throw new RuntimeException(e);
            }
        }
        if (messageToSend.commandEnum != null) {
            final long apiCallCount;
            apiCallCount = MapUtil.increment(LocalNodeData.API_CALL_MAP, "out-" + messageToSend.commandEnum.name().toLowerCase());
            MapUtil.increment(LocalNodeData.API_CALL_MAP, RemoteNodeData.OUT_BYTES, outBa.length);
            LOG.debug("request to {}:{} {}", data.getHostAddress(), messageToSend.command, apiCallCount);
        }
        messageToSend = data.getSendQueue().poll();
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) Message(neo.model.network.Message) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

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