Search in sources :

Example 1 with GetVersionEvent

use of io.nuls.network.message.entity.GetVersionEvent in project nuls by nuls-io.

the class NodeDiscoverHandler method run.

/**
 * do ping/pong and ask versionMessage
 */
@Override
public void run() {
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while (running) {
        count++;
        List<Node> nodeList = new ArrayList<>(nodesManager.getNodes().values());
        StringBuilder str = new StringBuilder();
        for (Node node : nodeList) {
            if (node.getStatus() == 2) {
                str.append(",");
                str.append(node.getIp());
            }
        }
        if (str.toString().length() == 0) {
            str.append(",");
        }
        Log.info("nodes:" + str.toString().substring(1));
        GetVersionEvent event = new GetVersionEvent(network.getExternalPort());
        GetNodesIpEvent ipEvent = new GetNodesIpEvent();
        for (Node node : nodeList) {
            if (node.isAlive()) {
                broadcaster.broadcastToNode(event, node, true);
                if (count == 10) {
                    broadcaster.broadcastToNode(ipEvent, node, true);
                }
            }
        }
        long now = TimeService.currentTimeMillis();
        if (count == 10) {
            count = 0;
            List<String> list = new ArrayList<>();
            for (Map.Entry<String, Long> entry : NetworkContext.ipMap.entrySet()) {
                if (now - entry.getValue() > DateUtil.MINUTE_TIME * 2) {
                    list.add(entry.getKey());
                }
            }
            for (String ip : list) {
                NetworkContext.ipMap.remove(ip);
            }
        }
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            Log.error(e);
        }
    }
}
Also used : GetVersionEvent(io.nuls.network.message.entity.GetVersionEvent) Node(io.nuls.network.entity.Node) GetNodesIpEvent(io.nuls.network.message.entity.GetNodesIpEvent)

Example 2 with GetVersionEvent

use of io.nuls.network.message.entity.GetVersionEvent in project nuls by nuls-io.

the class GetVersionEventHandler method process.

@Override
public NetworkEventResult process(BaseEvent networkEvent, Node node) {
    GetVersionEvent event = (GetVersionEvent) networkEvent;
    // String key = event.getHeader().getEventType() + "-" + node.getId();
    // if (cacheService.existEvent(key)) {
    // Log.info("----------GetVersionEventHandler  cacheService  existEvent--------");
    // getNetworkService().removeNode(node.getId());
    // return null;
    // }
    // cacheService.putEvent(key, event, true);
    Block block = NulsContext.getInstance().getBestBlock();
    while (block == null) {
        try {
            Thread.sleep(1000);
            block = NulsContext.getInstance().getBestBlock();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    VersionEvent replyMessage = new VersionEvent(getNetworkService().getNetworkParam().getExternalPort(), block.getHeader().getHeight(), block.getHeader().getHash().getDigestHex());
    node.setPort(event.getExternalPort());
    return new NetworkEventResult(true, replyMessage);
}
Also used : GetVersionEvent(io.nuls.network.message.entity.GetVersionEvent) VersionEvent(io.nuls.network.message.entity.VersionEvent) GetVersionEvent(io.nuls.network.message.entity.GetVersionEvent) Block(io.nuls.core.chain.entity.Block) NetworkEventResult(io.nuls.network.message.NetworkEventResult)

Aggregations

GetVersionEvent (io.nuls.network.message.entity.GetVersionEvent)2 Block (io.nuls.core.chain.entity.Block)1 Node (io.nuls.network.entity.Node)1 NetworkEventResult (io.nuls.network.message.NetworkEventResult)1 GetNodesIpEvent (io.nuls.network.message.entity.GetNodesIpEvent)1 VersionEvent (io.nuls.network.message.entity.VersionEvent)1