use of neo.model.network.InvPayload in project neo-java by coranos.
the class LocalControllerNode method onInv.
/**
* does something on a "inv" message.
*
* @param peer
* the peer that sent the message.
* @param message
* the message.
*/
private void onInv(final RemoteNodeControllerRunnable peer, final Message message) {
if (stopped) {
return;
}
// TODO: figure out what to do here.
final InvPayload invp = message.getPayload(InvPayload.class);
final List<UInt256> hashes = invp.getHashes();
switch(invp.getType()) {
case BLOCK:
break;
case CONSENSUS:
break;
case TRANSACTION:
break;
}
MessageUtil.sendGetData(peer.getData(), localNodeData, invp.getType(), hashes.toArray(new UInt256[0]));
}
use of neo.model.network.InvPayload 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.InvPayload 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);
}
}
Aggregations