Search in sources :

Example 6 with Node

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

the class NodesManager method start.

/**
 * get nodes from database
 * connect other nodes
 * running ping/pong thread
 * running node discovery thread
 */
public void start() {
    List<Node> nodes = discoverHandler.getLocalNodes(network.maxOutCount());
    if (nodes == null || nodes.isEmpty()) {
        nodes = getSeedNodes();
    }
    for (Node node : nodes) {
        node.setType(Node.OUT);
        node.setStatus(Node.WAIT);
        addNodeToGroup(NetworkConstant.NETWORK_NODE_OUT_GROUP, node);
    }
    running = true;
    TaskManager.createAndRunThread(NulsConstant.MODULE_ID_NETWORK, "NetworkNodeManager", this);
    discoverHandler.start();
}
Also used : Node(io.nuls.network.entity.Node)

Example 7 with Node

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

the class NodesManager method removeNode.

public void removeNode(String nodeId, Integer type) {
    if (nodes.containsKey(nodeId)) {
        Node node = nodes.get(nodeId);
        if (null != type && type != node.getType()) {
            return;
        }
        // When other modules call the interface,  close channel first
        if (StringUtils.isNotBlank(node.getChannelId())) {
            SocketChannel channel = NioChannelMap.get(node.getChannelId());
            if (channel != null) {
                channel.close();
                return;
            }
        }
        node.destroy();
        for (String groupName : node.getGroupSet()) {
            removeNodeFromGroup(groupName, nodeId);
        }
        nodes.remove(nodeId);
        getNodeDao().removeNode(NodeTransferTool.toPojo(node));
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node)

Example 8 with Node

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

the class NodesManager method blackNode.

public void blackNode(String nodeId, int status) {
    if (nodes.containsKey(nodeId)) {
        Node node = nodes.get(nodeId);
        node.setStatus(status);
        getNodeDao().removeNode(NodeTransferTool.toPojo(node));
        removeNode(node.getId(), null);
    }
}
Also used : Node(io.nuls.network.entity.Node)

Example 9 with Node

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

the class ClientChannelHandler method channelInactive.

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    Log.debug("----------------------client channelInactive ------------------------- ");
    String channelId = ctx.channel().id().asLongText();
    SocketChannel channel = (SocketChannel) ctx.channel();
    NioChannelMap.remove(channelId);
    Node node = getNetworkService().getNode(channel.remoteAddress().getHostString());
    if (node != null) {
        if (node.getChannelId() == null || channelId.equals(node.getChannelId())) {
            getNetworkService().removeNode(node.getId());
        }
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node)

Example 10 with Node

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

the class ClientChannelHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException {
    SocketChannel channel = (SocketChannel) ctx.channel();
    Node node = getNetworkService().getNode(channel.remoteAddress().getHostString());
    if (node != null && node.isAlive()) {
        ByteBuf buf = (ByteBuf) msg;
        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
        buf.release();
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        buffer.put(bytes);
        getNetworkService().receiveMessage(buffer, node);
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Node (io.nuls.network.entity.Node)25 SocketChannel (io.netty.channel.socket.SocketChannel)8 NodeGroup (io.nuls.network.entity.NodeGroup)5 ArrayList (java.util.ArrayList)4 BroadcastResult (io.nuls.network.entity.BroadcastResult)3 ByteBuf (io.netty.buffer.ByteBuf)2 NulsMessage (io.nuls.core.mesasge.NulsMessage)2 NetworkEventResult (io.nuls.network.message.NetworkEventResult)2 GetNodeEvent (io.nuls.network.message.entity.GetNodeEvent)2 GetNodesIpEvent (io.nuls.network.message.entity.GetNodesIpEvent)2 NodeEvent (io.nuls.network.message.entity.NodeEvent)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Block (io.nuls.core.chain.entity.Block)1 NodePo (io.nuls.db.entity.NodePo)1 GetVersionEvent (io.nuls.network.message.entity.GetVersionEvent)1 NodesIpEvent (io.nuls.network.message.entity.NodesIpEvent)1 InfoDto (io.nuls.rpc.entity.InfoDto)1 RpcResult (io.nuls.rpc.entity.RpcResult)1 HashSet (java.util.HashSet)1