Search in sources :

Example 1 with NetworkMessageBody

use of io.nuls.network.protocol.message.NetworkMessageBody in project nuls by nuls-io.

the class VersionMessageHandler method process.

@Override
public NetworkEventResult process(BaseMessage message, Node node) {
    VersionMessage versionMessage = (VersionMessage) message;
    NetworkMessageBody body = versionMessage.getMsgBody();
    // Log.info("receive a version message : {}", body);
    if (body.getBestBlockHeight() < 0) {
        // node.setStatus(Node.BAD);
        nodeManager.removeNode(node.getId());
        return null;
    }
    node.setBestBlockHeight(body.getBestBlockHeight());
    node.setBestBlockHash(body.getBestBlockHash());
    node.setTimeOffset((TimeService.currentTimeMillis() - node.getLastTime()) / 2);
    return null;
}
Also used : NetworkMessageBody(io.nuls.network.protocol.message.NetworkMessageBody) VersionMessage(io.nuls.network.protocol.message.VersionMessage)

Example 2 with NetworkMessageBody

use of io.nuls.network.protocol.message.NetworkMessageBody in project nuls by nuls-io.

the class NodeManager method sendHandshakeMessage.

private void sendHandshakeMessage(Node node, int type) {
    NetworkMessageBody body = new NetworkMessageBody(type, networkParam.getPort(), NulsContext.getInstance().getBestHeight(), NulsContext.getInstance().getBestBlock().getHeader().getHash(), node.getIp());
    broadcastHandler.broadcastToNode(new HandshakeMessage(body), node, true);
}
Also used : NetworkMessageBody(io.nuls.network.protocol.message.NetworkMessageBody) HandshakeMessage(io.nuls.network.protocol.message.HandshakeMessage)

Example 3 with NetworkMessageBody

use of io.nuls.network.protocol.message.NetworkMessageBody in project nuls by nuls-io.

the class HandshakeMessageHandler method process.

@Override
public NetworkEventResult process(BaseMessage message, Node node) {
    HandshakeMessage handshakeMessage = (HandshakeMessage) message;
    NetworkMessageBody body = handshakeMessage.getMsgBody();
    // Log.info("receive message from node {}, message : {}", node.getId(), body);
    node.setBestBlockHash(body.getBestBlockHash());
    node.setBestBlockHeight(body.getBestBlockHeight());
    node.setExternalIp(body.getNodeIp());
    node.setRemoteVersion(body.getVersion());
    node.setConnectStatus(NodeConnectStatusEnum.AVAILABLE);
    return null;
}
Also used : HandshakeMessage(io.nuls.network.protocol.message.HandshakeMessage) NetworkMessageBody(io.nuls.network.protocol.message.NetworkMessageBody)

Example 4 with NetworkMessageBody

use of io.nuls.network.protocol.message.NetworkMessageBody in project nuls by nuls-io.

the class GetVersionMessageHandler method process.

@Override
public NetworkEventResult process(BaseMessage message, Node node) {
    GetVersionMessage getVersionMessage = (GetVersionMessage) message;
    NetworkMessageBody body = getVersionMessage.getMsgBody();
    if (body.getBestBlockHeight() < 0) {
        // node.setStatus(Node.BAD);
        nodeManager.removeNode(node.getId());
        return null;
    }
    node.setBestBlockHeight(body.getBestBlockHeight());
    node.setBestBlockHash(body.getBestBlockHash());
    NetworkMessageBody myVersionBody = new NetworkMessageBody(NetworkConstant.HANDSHAKE_CLIENT_TYPE, networkParam.getPort(), NulsContext.getInstance().getBestHeight(), NulsContext.getInstance().getBestBlock().getHeader().getHash());
    return new NetworkEventResult(true, new VersionMessage(myVersionBody));
}
Also used : NetworkMessageBody(io.nuls.network.protocol.message.NetworkMessageBody) VersionMessage(io.nuls.network.protocol.message.VersionMessage) GetVersionMessage(io.nuls.network.protocol.message.GetVersionMessage) GetVersionMessage(io.nuls.network.protocol.message.GetVersionMessage) NetworkEventResult(io.nuls.network.model.NetworkEventResult)

Example 5 with NetworkMessageBody

use of io.nuls.network.protocol.message.NetworkMessageBody in project nuls by nuls-io.

the class GetNodeVersionTask method process.

private void process() {
    Collection<Node> connectedNodes = nodeManager.getAvailableNodes();
    if (connectedNodes == null) {
        return;
    }
    NetworkMessageBody body = new NetworkMessageBody(NetworkConstant.HANDSHAKE_CLIENT_TYPE, networkParam.getPort(), NulsContext.getInstance().getBestHeight(), NulsContext.getInstance().getBestBlock().getHeader().getHash());
    GetVersionMessage getVersionMessage = new GetVersionMessage(body);
    Iterator<Node> it = connectedNodes.iterator();
    while (it.hasNext()) {
        Node node = it.next();
        if (node.getType() == Node.OUT) {
            if (!checkIsSurvive(node)) {
                it.remove();
                continue;
            }
            node.setLastTime(TimeService.currentTimeMillis());
            broadcastHandler.broadcastToNode(getVersionMessage, node, true);
        }
    }
}
Also used : NetworkMessageBody(io.nuls.network.protocol.message.NetworkMessageBody) GetVersionMessage(io.nuls.network.protocol.message.GetVersionMessage) Node(io.nuls.network.model.Node)

Aggregations

NetworkMessageBody (io.nuls.network.protocol.message.NetworkMessageBody)5 GetVersionMessage (io.nuls.network.protocol.message.GetVersionMessage)2 HandshakeMessage (io.nuls.network.protocol.message.HandshakeMessage)2 VersionMessage (io.nuls.network.protocol.message.VersionMessage)2 NetworkEventResult (io.nuls.network.model.NetworkEventResult)1 Node (io.nuls.network.model.Node)1