Search in sources :

Example 56 with SocketChannel

use of io.netty.channel.socket.SocketChannel in project java by wavefrontHQ.

the class StreamIngester method run.

public void run() {
    activeListeners.inc();
    // Configure the server.
    ServerBootstrap b = new ServerBootstrap();
    EventLoopGroup parentGroup;
    EventLoopGroup childGroup;
    Class<? extends ServerChannel> socketChannelClass;
    if (Epoll.isAvailable()) {
        logger.fine("Using native socket transport for port " + listeningPort);
        parentGroup = new EpollEventLoopGroup(1);
        childGroup = new EpollEventLoopGroup();
        socketChannelClass = EpollServerSocketChannel.class;
    } else {
        logger.fine("Using NIO socket transport for port " + listeningPort);
        parentGroup = new NioEventLoopGroup(1);
        childGroup = new NioEventLoopGroup();
        socketChannelClass = NioServerSocketChannel.class;
    }
    try {
        b.group(parentGroup, childGroup).channel(socketChannelClass).option(ChannelOption.SO_BACKLOG, 1024).localAddress(listeningPort).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("frame decoder", frameDecoderFactory.getDecoder());
                pipeline.addLast("byte array decoder", new ByteArrayDecoder());
                pipeline.addLast(commandHandler);
            }
        });
        if (parentChannelOptions != null) {
            for (Map.Entry<ChannelOption<?>, ?> entry : parentChannelOptions.entrySet()) {
                b.option((ChannelOption<Object>) entry.getKey(), entry.getValue());
            }
        }
        if (childChannelOptions != null) {
            for (Map.Entry<ChannelOption<?>, ?> entry : childChannelOptions.entrySet()) {
                b.childOption((ChannelOption<Object>) entry.getKey(), entry.getValue());
            }
        }
        // Start the server.
        ChannelFuture f = b.bind().sync();
        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } catch (final InterruptedException e) {
        logger.log(Level.WARNING, "Interrupted");
        parentGroup.shutdownGracefully();
        childGroup.shutdownGracefully();
        logger.info("Listener on port " + String.valueOf(listeningPort) + " shut down");
    } catch (Exception e) {
        // ChannelFuture throws undeclared checked exceptions, so we need to handle it
        if (e instanceof BindException) {
            logger.severe("Unable to start listener - port " + String.valueOf(listeningPort) + " is already in use!");
        } else {
            logger.log(Level.SEVERE, "StreamIngester exception: ", e);
        }
    } finally {
        activeListeners.dec();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ChannelOption(io.netty.channel.ChannelOption) BindException(java.net.BindException) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) BindException(java.net.BindException) ChannelPipeline(io.netty.channel.ChannelPipeline) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) Map(java.util.Map) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ByteArrayDecoder(io.netty.handler.codec.bytes.ByteArrayDecoder)

Example 57 with SocketChannel

use of io.netty.channel.socket.SocketChannel 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 58 with SocketChannel

use of 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 59 with SocketChannel

use of 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 60 with SocketChannel

use of 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)

Aggregations

SocketChannel (io.netty.channel.socket.SocketChannel)235 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)137 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)101 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)94 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)94 Bootstrap (io.netty.bootstrap.Bootstrap)89 ChannelPipeline (io.netty.channel.ChannelPipeline)79 EventLoopGroup (io.netty.channel.EventLoopGroup)79 ChannelFuture (io.netty.channel.ChannelFuture)77 InetSocketAddress (java.net.InetSocketAddress)43 LoggingHandler (io.netty.handler.logging.LoggingHandler)42 Channel (io.netty.channel.Channel)35 IOException (java.io.IOException)33 SslContext (io.netty.handler.ssl.SslContext)30 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)29 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)29 ByteBuf (io.netty.buffer.ByteBuf)27 StringDecoder (io.netty.handler.codec.string.StringDecoder)23 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)18 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)15