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;
}
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;
}
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);
}
}
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();
}
}
Aggregations