Search in sources :

Example 1 with AddrPayload

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);
}
Also used : JSONObject(org.json.JSONObject) AddrPayload(neo.model.network.AddrPayload) Test(org.junit.Test)

Example 2 with AddrPayload

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);
    }
}
Also used : RemoteNodeData(neo.network.model.RemoteNodeData) NetworkAddressWithTime(neo.model.network.NetworkAddressWithTime) InetSocketAddress(java.net.InetSocketAddress) AddrPayload(neo.model.network.AddrPayload) InetAddress(java.net.InetAddress) IOException(java.io.IOException)

Aggregations

AddrPayload (neo.model.network.AddrPayload)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 NetworkAddressWithTime (neo.model.network.NetworkAddressWithTime)1 RemoteNodeData (neo.network.model.RemoteNodeData)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1