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