Example usage for io.netty.channel.socket.nio NioServerSocketChannel NioServerSocketChannel

List of usage examples for io.netty.channel.socket.nio NioServerSocketChannel NioServerSocketChannel

Introduction

In this page you can find the example usage for io.netty.channel.socket.nio NioServerSocketChannel NioServerSocketChannel.

Prototype

public NioServerSocketChannel(ServerSocketChannel channel) 

Source Link

Document

Create a new instance using the given ServerSocketChannel .

Usage

From source file:org.apache.hadoop.hdfs.server.datanode.web.DatanodeHttpServer.java

License:Apache License

public DatanodeHttpServer(final Configuration conf, final InetSocketAddress jettyAddr,
        final ServerSocketChannel externalHttpChannel) throws IOException {
    this.conf = conf;
    this.confForCreate = new Configuration(conf);
    confForCreate.set(FsPermission.UMASK_LABEL, "000");

    this.bossGroup = new NioEventLoopGroup();
    this.workerGroup = new NioEventLoopGroup();
    this.externalHttpChannel = externalHttpChannel;
    HttpConfig.Policy policy = DFSUtil.getHttpPolicy(conf);

    if (policy.isHttpEnabled()) {
        this.httpServer = new ServerBootstrap().group(bossGroup, workerGroup)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override//from   ww  w .j  a  v a 2  s  .  c  o m
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new HttpRequestDecoder(), new HttpResponseEncoder(),
                                new ChunkedWriteHandler(), new URLDispatcher(jettyAddr, conf, confForCreate));
                    }
                });
        if (externalHttpChannel == null) {
            httpServer.channel(NioServerSocketChannel.class);
        } else {
            httpServer.channelFactory(new ChannelFactory<NioServerSocketChannel>() {
                @Override
                public NioServerSocketChannel newChannel() {
                    return new NioServerSocketChannel(externalHttpChannel) {
                        // The channel has been bounded externally via JSVC,
                        // thus bind() becomes a no-op.
                        @Override
                        protected void doBind(SocketAddress localAddress) throws Exception {
                        }
                    };
                }
            });
        }
    } else {
        this.httpServer = null;
    }

    if (policy.isHttpsEnabled()) {
        this.sslFactory = new SSLFactory(SSLFactory.Mode.SERVER, conf);
        try {
            sslFactory.init();
        } catch (GeneralSecurityException e) {
            throw new IOException(e);
        }
        this.httpsServer = new ServerBootstrap().group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new SslHandler(sslFactory.createSSLEngine()), new HttpRequestDecoder(),
                                new HttpResponseEncoder(), new ChunkedWriteHandler(),
                                new URLDispatcher(jettyAddr, conf, confForCreate));
                    }
                });
    } else {
        this.httpsServer = null;
        this.sslFactory = null;
    }
}