Search in sources :

Example 31 with Node

use of io.nuls.network.model.Node in project nuls by nuls-io.

the class ServerChannelHandler method channelUnregistered.

@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    super.channelUnregistered(ctx);
    SocketChannel channel = (SocketChannel) ctx.channel();
    String nodeId = IpUtil.getNodeId(channel.remoteAddress());
    Attribute<Node> nodeAttribute = channel.attr(AttributeKey.valueOf("node-" + nodeId));
    Node node = nodeAttribute.get();
    if (node != null && node.getDisconnectListener() != null) {
        node.getDisconnectListener().action();
    }
// channelUnregistered之前,channel就已经close了,可以调用channel.isOpen()查看状态
// channel.close();
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.model.Node)

Example 32 with Node

use of io.nuls.network.model.Node in project nuls by nuls-io.

the class NodesContainer method addNeedCheckNode.

public boolean addNeedCheckNode(Node newNode) {
    String nodeId = newNode.getId();
    Node node = uncheckNodes.get(nodeId);
    if (node != null) {
        return false;
    }
    node = canConnectNodes.get(nodeId);
    if (node != null) {
        return false;
    }
    node = connectedNodes.get(nodeId);
    if (node != null) {
        return false;
    }
    node = disconnectNodes.get(nodeId);
    if (node != null) {
        return false;
    }
    node = failNodes.get(nodeId);
    if (node != null) {
        return false;
    }
    newNode.setLastProbeTime(0L);
    uncheckNodes.put(nodeId, newNode);
    newNode.setStatus(NodeStatusEnum.UNCHECK);
    return true;
}
Also used : Node(io.nuls.network.model.Node)

Example 33 with Node

use of io.nuls.network.model.Node in project nuls by nuls-io.

the class NodeManager method nodeConnectIn.

public boolean nodeConnectIn(String ip, int port, SocketChannel channel) {
    if (!canConnectIn(ip, port)) {
        return false;
    }
    Node node = new Node(ip, port, Node.IN);
    node.setConnectStatus(NodeConnectStatusEnum.CONNECTED);
    node.setChannel(channel);
    cacheNode(node, channel);
    nodesContainer.getConnectedNodes().put(node.getId(), node);
    nodesContainer.markCanuseNodeByIp(ip, NodeStatusEnum.AVAILABLE);
    // 监听被动连接的断开
    node.setDisconnectListener(() -> {
        // Log.info("------------in node disconnect:" + node.getId());
        nodesContainer.getConnectedNodes().remove(node.getId());
        nodesContainer.markCanuseNodeByIp(ip, NodeStatusEnum.CONNECTABLE);
    });
    sendHandshakeMessage(node, NetworkConstant.HANDSHAKE_SEVER_TYPE);
    return true;
}
Also used : Node(io.nuls.network.model.Node)

Example 34 with Node

use of io.nuls.network.model.Node in project nuls by nuls-io.

the class MessageBusServiceImplTest method receiveMessage.

@Test
public void receiveMessage() {
    BlockMessage blockMessage = new BlockMessage();
    Node node = new Node("192.168.1.90", 8003, 1);
    messageBusService.receiveMessage(blockMessage, node);
}
Also used : BlockMessage(io.nuls.protocol.message.BlockMessage) Node(io.nuls.network.model.Node) Test(org.junit.Test)

Example 35 with Node

use of io.nuls.network.model.Node in project nuls by nuls-io.

the class BroadcastHandler method broadcastToList.

private BroadcastResult broadcastToList(Collection<Node> nodeList, BaseMessage message, Node excludeNode, boolean asyn, int percent) {
    BroadcastResult result = new BroadcastResult();
    try {
        int successCount = 0;
        int minCount = 5;
        // 根据百分比决定直接广播给多少个节点
        if (nodeList.size() > minCount && percent < 100) {
            int needCount = nodeList.size() * percent / 100;
            if (needCount < minCount) {
                needCount = minCount;
            }
            Set<Integer> set = new HashSet<>();
            while (true) {
                Random rand = new Random();
                int ran = rand.nextInt(nodeList.size());
                set.add(ran);
                if (set.size() == needCount + 1) {
                    break;
                }
            }
            int nodeListIndex = 0;
            Collection<Node> nodeBroadcastList = new ArrayList<>();
            for (Node node : nodeList) {
                if (set.contains(nodeListIndex)) {
                    if (excludeNode != null && node.getId().equals(excludeNode.getId())) {
                        nodeListIndex++;
                        continue;
                    }
                    nodeBroadcastList.add(node);
                    if (nodeBroadcastList.size() == needCount) {
                        break;
                    }
                }
                nodeListIndex++;
            }
            nodeList = nodeBroadcastList;
        }
        for (Node node : nodeList) {
            if (excludeNode != null && node.getId().equals(excludeNode.getId())) {
                continue;
            }
            BroadcastResult br = broadcastToNode(message, node, asyn);
            if (br.isSuccess()) {
                successCount++;
                result.getBroadcastNodes().add(node);
            } else if (br.getErrorCode().equals(NetworkErrorCode.NET_MESSAGE_ERROR)) {
                return br;
            }
        }
        if (successCount == 0) {
            return new BroadcastResult(false, NetworkErrorCode.NET_BROADCAST_FAIL);
        }
    } catch (Exception e) {
        return new BroadcastResult(false, NetworkErrorCode.NET_MESSAGE_ERROR);
    }
    result.setSuccess(true);
    result.setErrorCode(KernelErrorCode.SUCCESS);
    return result;
}
Also used : BroadcastResult(io.nuls.network.model.BroadcastResult) Node(io.nuls.network.model.Node) IOException(java.io.IOException)

Aggregations

Node (io.nuls.network.model.Node)48 Result (io.nuls.kernel.model.Result)4 NodesContainer (io.nuls.network.netty.container.NodesContainer)4 Block (io.nuls.kernel.model.Block)3 NulsDigestData (io.nuls.kernel.model.NulsDigestData)3 RpcClientResult (io.nuls.kernel.model.RpcClientResult)3 BroadcastResult (io.nuls.network.model.BroadcastResult)3 BlockMessage (io.nuls.protocol.message.BlockMessage)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 IOException (java.io.IOException)3 GET (javax.ws.rs.GET)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 Test (org.junit.Test)3 ByteBuf (io.netty.buffer.ByteBuf)2 SocketChannel (io.netty.channel.socket.SocketChannel)2 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)2 NodeMessageBody (io.nuls.network.protocol.message.NodeMessageBody)2 NodesMessage (io.nuls.network.protocol.message.NodesMessage)2