Search in sources :

Example 6 with Node

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

the class ClientChannelHandler method channelUnregistered.

@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    super.channelUnregistered(ctx);
    Attribute<Node> nodeAttribute = ctx.channel().attr(NodeAttributeKey.NODE_KEY);
    Node node = nodeAttribute.get();
    if (node != null && node.getDisconnectListener() != null) {
        node.getDisconnectListener().action();
    }
}
Also used : Node(io.nuls.network.model.Node)

Example 7 with Node

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

the class ClientChannelHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    if (!(cause instanceof IOException) && !(cause instanceof TooLongFrameException)) {
        Attribute<Node> nodeAttribute = ctx.channel().attr(NodeAttributeKey.NODE_KEY);
        Node node = nodeAttribute.get();
        Log.error("----------------nodeId:" + node.getId());
        Log.error(cause);
    }
    ctx.close();
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Node(io.nuls.network.model.Node) IOException(java.io.IOException)

Example 8 with Node

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

the class ServerChannelHandler method channelRead0.

/**
 * 继承SimpleChannelInboundHandler后,只需要重新channelRead0方法,msg会自动释放
 * @param ctx
 * @param msg
 * @throws Exception
 */
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    SocketChannel channel = (SocketChannel) ctx.channel();
    String nodeId = IpUtil.getNodeId(channel.remoteAddress());
    Attribute<Node> nodeAttribute = channel.attr(AttributeKey.valueOf("node-" + nodeId));
    Node node = nodeAttribute.get();
    ByteBuf buf = (ByteBuf) msg;
    messageProcessor.processor(buf, node);
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.model.Node) ByteBuf(io.netty.buffer.ByteBuf)

Example 9 with Node

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

the class NodeManager method canConnectIn.

/**
 * 服务器被动连接规则
 * 1. 不超过最大被动连接数
 * 2. 如果自己已经主动连接了对方,不接受对方的被动连接
 * 3. 相同的IP的被动连接不超过10次
 *
 * @param ip
 * @param port
 * @return boolean
 */
private boolean canConnectIn(String ip, int port) {
    int size = nodesContainer.getConnectedCount(Node.IN);
    if (size >= networkParam.getMaxInCount()) {
        return false;
    }
    Map<String, Node> connectedNodes = nodesContainer.getConnectedNodes();
    int sameIpCount = 0;
    for (Node node : connectedNodes.values()) {
        // if(ip.equals(node.getIp()) && (node.getPort().intValue() == port || node.getType() == Node.OUT)) {
        if (ip.equals(node.getIp()) && node.getType() == Node.OUT) {
            return false;
        }
        if (ip.equals(node.getIp())) {
            sameIpCount++;
        }
        if (sameIpCount >= NetworkConstant.SAME_IP_MAX_COUNT) {
            return false;
        }
    }
    return true;
}
Also used : Node(io.nuls.network.model.Node)

Example 10 with Node

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

the class NodeManager method getSeedNodes.

public List<Node> getSeedNodes() {
    List<Node> seedList = new ArrayList<>();
    for (String seedId : networkParam.getSeedIpList()) {
        try {
            String[] ipPort = seedId.split(":");
            String ip = ipPort[0];
            int port = Integer.parseInt(ipPort[1]);
            Node node = new Node(ip, port, Node.OUT);
            node.setSeedNode(true);
            node.setStatus(NodeStatusEnum.CONNECTABLE);
            seedList.add(node);
        } catch (Exception e) {
            Log.warn("the seed config is warn of {}", seedId);
        }
    }
    return seedList;
}
Also used : Node(io.nuls.network.model.Node)

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