use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel 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());
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel 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);
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project nuls by nuls-io.
the class ClientChannelHandler method channelActive.
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
String channelId = ctx.channel().id().asLongText();
SocketChannel channel = (SocketChannel) ctx.channel();
Node node = getNetworkService().getNode(channel.remoteAddress().getHostString());
// check node exist
if (node == null || (node != null && node.getStatus() != Node.WAIT)) {
ctx.channel().close();
return;
}
NioChannelMap.add(channelId, channel);
node.setChannelId(channelId);
node.setStatus(Node.CONNECT);
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project nuls by nuls-io.
the class ServerChannelHandler method channelRegistered.
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
SocketChannel socketChannel = (SocketChannel) ctx.channel();
String remoteIP = socketChannel.remoteAddress().getHostString();
Node node = getNetworkService().getNode(remoteIP);
if (node != null) {
if (node.getStatus() == Node.CONNECT) {
ctx.channel().close();
return;
}
// When nodes try to connect to each other but not connected, select one of the smaller IP addresses as the server
if (node.getType() == Node.OUT) {
String localIP = InetAddress.getLocalHost().getHostAddress();
boolean isLocalServer = IpUtil.judgeIsLocalServer(localIP, remoteIP);
if (!isLocalServer) {
ctx.channel().close();
return;
} else {
getNetworkService().removeNode(remoteIP);
}
}
}
NodeGroup group = getNetworkService().getNodeGroup(NetworkConstant.NETWORK_NODE_IN_GROUP);
if (group.size() > getNetworkService().getNetworkParam().maxInCount()) {
ctx.channel().close();
return;
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel in project nuls by nuls-io.
the class ServerChannelHandler method channelInactive.
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
Log.debug("----------------------server channelInactive ------------------------- ");
SocketChannel channel = (SocketChannel) ctx.channel();
String channelId = ctx.channel().id().asLongText();
NioChannelMap.remove(channelId);
Node node = getNetworkService().getNode(channel.remoteAddress().getHostString());
if (node != null && channelId.equals(node.getChannelId())) {
getNetworkService().removeNode(channel.remoteAddress().getHostString());
}
}
Aggregations