Search in sources :

Example 86 with ServerBootstrap

use of io.netty.bootstrap.ServerBootstrap in project android by JetBrains.

the class LocalTestServer method start.

public void start() throws Exception {
    ServerBootstrap b = new ServerBootstrap();
    myEventLoopGroup = new OioEventLoopGroup();
    b.group(myEventLoopGroup).channel(OioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpServerCodec());
            // Note: Netty's decompressor uses jcraft jzlib, which is not exported as a library
            // p.addLast(new HttpContentDecompressor());
            // big enough to collect a full thread dump
            p.addLast(new HttpObjectAggregator(32 * 1024));
            p.addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
                    ctx.flush();
                }

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    if (!(msg instanceof FullHttpRequest)) {
                        return;
                    }
                    FullHttpResponse response = myResponseSupplier.apply((FullHttpRequest) msg);
                    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
                    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
                    ctx.write(response).addListener(ChannelFutureListener.CLOSE);
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                    ctx.write(cause.toString()).addListener(ChannelFutureListener.CLOSE);
                }
            });
        }
    });
    myChannel = b.bind(myPort).sync().channel();
}
Also used : OioServerSocketChannel(io.netty.channel.socket.oio.OioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) OioServerSocketChannel(io.netty.channel.socket.oio.OioServerSocketChannel)

Example 87 with ServerBootstrap

use of io.netty.bootstrap.ServerBootstrap in project carbondata by apache.

the class DictionaryServer method bindToPort.

/**
   * Binds dictionary server to an available port.
   *
   * @param port
   */
private void bindToPort(int port) {
    long start = System.currentTimeMillis();
    // Configure the server.
    int i = 0;
    while (i < 10) {
        int newPort = port + i;
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boss, worker);
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addLast("LengthDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 2, 0, 2));
                    pipeline.addLast("DictionaryServerHandler", dictionaryServerHandler);
                }
            });
            bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
            bootstrap.bind(newPort).sync();
            LOGGER.audit("Dictionary Server started, Time spent " + (System.currentTimeMillis() - start) + " Listening on port " + newPort);
            this.port = newPort;
            break;
        } catch (Exception e) {
            LOGGER.error(e, "Dictionary Server Failed to bind to port:");
            if (i == 9) {
                throw new RuntimeException("Dictionary Server Could not bind to any port");
            }
        }
        i++;
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 88 with ServerBootstrap

use of io.netty.bootstrap.ServerBootstrap in project asterixdb by apache.

the class HttpServer method doStart.

protected void doStart() throws InterruptedException {
    /*
         * This is a hacky way to ensure that IServlets with more specific paths are checked first.
         * For example:
         * "/path/to/resource/"
         * is checked before
         * "/path/to/"
         * which in turn is checked before
         * "/path/"
         * Note that it doesn't work for the case where multiple paths map to a single IServlet
         */
    Collections.sort(servlets, (l1, l2) -> l2.getPaths()[0].length() - l1.getPaths()[0].length());
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WRITE_BUFFER_WATER_MARK).handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(new HttpServerInitializer(this));
    channel = b.bind(port).sync().channel();
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 89 with ServerBootstrap

use of io.netty.bootstrap.ServerBootstrap in project jackrabbit-oak by apache.

the class NetworkErrorProxy method connect.

public void connect() throws Exception {
    log.info("Starting proxy with flip={}, skip={},{}", flipPosition, skipPosition, skipLength);
    ServerBootstrap b = new ServerBootstrap().group(boss, worker).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new ForwardHandler(host, outboundPort, flipPosition, skipPosition, skipLength));
        }
    });
    ChannelFuture f = b.bind(this.inboundPort);
    if (f.awaitUninterruptibly(1, TimeUnit.SECONDS)) {
        log.debug("Bound on port {}", inboundPort);
    } else {
        log.debug("Binding on port {} timed out", inboundPort);
    }
    server = f.channel();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 90 with ServerBootstrap

use of io.netty.bootstrap.ServerBootstrap in project intellij-community by JetBrains.

the class ExternalJavacManager method start.

public void start(int listenPort) {
    final ServerBootstrap bootstrap = new ServerBootstrap().group(new NioEventLoopGroup(1, SharedThreadPool.getInstance())).channel(NioServerSocketChannel.class);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true);
    final ChannelHandler compilationRequestsHandler = new CompilationRequestsHandler();
    bootstrap.childHandler(new ChannelInitializer() {

        @Override
        protected void initChannel(Channel channel) throws Exception {
            channel.pipeline().addLast(myChannelRegistrar, new ProtobufVarint32FrameDecoder(), new ProtobufDecoder(JavacRemoteProto.Message.getDefaultInstance()), new ProtobufVarint32LengthFieldPrepender(), new ProtobufEncoder(), compilationRequestsHandler);
        }
    });
    try {
        final InetAddress loopback = InetAddress.getByName(null);
        myChannelRegistrar.add(bootstrap.bind(loopback, listenPort).syncUninterruptibly().channel());
        myListenPort = listenPort;
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
}
Also used : ProtobufEncoder(io.netty.handler.codec.protobuf.ProtobufEncoder) UnknownHostException(java.net.UnknownHostException) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ProtobufDecoder(io.netty.handler.codec.protobuf.ProtobufDecoder) ProtobufVarint32LengthFieldPrepender(io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) UnknownHostException(java.net.UnknownHostException) ProtobufVarint32FrameDecoder(io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder) InetAddress(java.net.InetAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

ServerBootstrap (io.netty.bootstrap.ServerBootstrap)177 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)87 Channel (io.netty.channel.Channel)84 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)80 EventLoopGroup (io.netty.channel.EventLoopGroup)73 ChannelFuture (io.netty.channel.ChannelFuture)63 Bootstrap (io.netty.bootstrap.Bootstrap)62 Test (org.junit.Test)59 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)48 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)45 InetSocketAddress (java.net.InetSocketAddress)40 SocketChannel (io.netty.channel.socket.SocketChannel)37 LoggingHandler (io.netty.handler.logging.LoggingHandler)36 ChannelPipeline (io.netty.channel.ChannelPipeline)34 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)29 CountDownLatch (java.util.concurrent.CountDownLatch)28 ClosedChannelException (java.nio.channels.ClosedChannelException)27 LocalAddress (io.netty.channel.local.LocalAddress)25 SslContext (io.netty.handler.ssl.SslContext)24 LocalChannel (io.netty.channel.local.LocalChannel)23