use of neo.model.network.AddrPayload in project neo-java by coranos.
the class TestPayload method test001AddrPayload.
/**
* tests the AddrPayload.
*/
@Test
public void test001AddrPayload() {
final String payloadJsonStrRaw = TestUtil.getJsonTestResourceAsString(TEST_PACKAGE, getClass().getSimpleName(), "test001AddrPayloadHex");
final String payloadJsonStr = TestUtil.fromHexJsonObject(new JSONObject(payloadJsonStrRaw));
final byte[] payloadBa = ModelUtil.decodeHex(payloadJsonStr);
final AddrPayload payload = new AddrPayload(ByteBuffer.wrap(payloadBa));
Assert.assertNotNull("AddrPayload should not be null", payload);
Assert.assertEquals("AddrPayload size", 1, payload.getAddressList().size());
Assert.assertEquals("AddrPayload[0] ToString", "00000000000000000000000000000000:0", payload.getAddressList().get(0).toString());
final String expectedStr = new JSONObject(TestUtil.getJsonTestResourceAsString(TEST_PACKAGE, getClass().getSimpleName(), "test001AddrPayload")).toString();
final String actualStr = payload.toString();
Assert.assertEquals(TestUtil.RESPONSES_MUST_MATCH, expectedStr, actualStr);
final String expectedHexStr = new JSONObject(payloadJsonStrRaw).toString(2);
final String actualHexStr = TestUtil.toHexJsonObject(ModelUtil.toHexString(payload.toByteArray())).toString(2);
Assert.assertEquals(TestUtil.RESPONSES_MUST_MATCH, expectedHexStr, actualHexStr);
}
use of neo.model.network.AddrPayload in project neo-java by coranos.
the class LocalControllerNode method onAddr.
/**
* handles the "addr" message.
*
* @param peer
* the peer taht sent the message.
* @param message
* the message.
*/
private void onAddr(final RemoteNodeControllerRunnable peer, final Message message) {
if (stopped) {
return;
}
try {
final AddrPayload addrPayload = (AddrPayload) message.payload;
if (addrPayload == null) {
return;
}
for (final NetworkAddressWithTime nawt : addrPayload.getAddressList()) {
final byte[] addressBa = nawt.address.getBytesCopy();
ArrayUtils.reverse(addressBa);
final int port = nawt.port.toReverseBytesPositiveBigInteger().intValue();
final InetAddress address = InetAddress.getByAddress(addressBa);
if (LOG.isTraceEnabled()) {
LOG.trace("address:{};port:{};", address, port);
}
final InetSocketAddress addressAndPort = new InetSocketAddress(address, port);
final RemoteNodeData data = new RemoteNodeData(remoteNodeConfig);
synchronized (RemoteNodeData.class) {
data.setConnectionPhase(NodeConnectionPhaseEnum.UNKNOWN);
}
data.setTcpAddressAndPort(addressAndPort);
addRemoteNodeDataToPool(data);
}
synchronized (peerDataSet) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} onAddr response:{}, count {}, peerDataSet {}", peer.getData().getTcpAddressAndPortString(), message.command, addrPayload.getAddressList().size(), peerDataSet.size());
}
}
} catch (final Exception e) {
LOG.error("error in onAddr", e);
}
}
Aggregations