Search in sources :

Example 51 with SocketChannel

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());
        }
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node)

Example 52 with SocketChannel

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);
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 53 with SocketChannel

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);
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node)

Example 54 with SocketChannel

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;
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node) NodeGroup(io.nuls.network.entity.NodeGroup)

Example 55 with SocketChannel

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());
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node)

Aggregations

SocketChannel (io.netty.channel.socket.SocketChannel)283 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)151 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)114 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)107 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)105 Bootstrap (io.netty.bootstrap.Bootstrap)103 ChannelPipeline (io.netty.channel.ChannelPipeline)95 ChannelFuture (io.netty.channel.ChannelFuture)93 EventLoopGroup (io.netty.channel.EventLoopGroup)92 InetSocketAddress (java.net.InetSocketAddress)46 LoggingHandler (io.netty.handler.logging.LoggingHandler)45 IOException (java.io.IOException)44 Channel (io.netty.channel.Channel)42 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)36 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)35 SslContext (io.netty.handler.ssl.SslContext)34 ByteBuf (io.netty.buffer.ByteBuf)31 StringDecoder (io.netty.handler.codec.string.StringDecoder)27 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)20 ChannelInitializer (io.netty.channel.ChannelInitializer)19