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();
}
}
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();
}
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);
}
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;
}
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;
}
Aggregations