Search in sources :

Example 21 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project carbondata by apache.

the class DictionaryClient method startClient.

/**
   * start dictionary client
   *
   * @param address
   * @param port
   */
public void startClient(String address, int port) {
    LOGGER.audit("Starting client on " + address + " " + port);
    long start = System.currentTimeMillis();
    // Create an Event with 1 thread.
    workerGroup = new NioEventLoopGroup(1);
    Bootstrap clientBootstrap = new Bootstrap();
    clientBootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            // Based on length provided at header, it collects all packets
            pipeline.addLast("LengthDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 2, 0, 2));
            pipeline.addLast("DictionaryClientHandler", dictionaryClientHandler);
        }
    });
    clientBootstrap.connect(new InetSocketAddress(address, port));
    LOGGER.info("Dictionary client Started, Total time spent : " + (System.currentTimeMillis() - start));
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 22 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup 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)

Example 23 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project apn-proxy by apn-proxy.

the class ApnProxyServer method start.

public void start() {
    int bossThreadCount = ApnProxyConfig.getConfig().getBossThreadCount();
    int workerThreadCount = ApnProxyConfig.getConfig().getWorkerThreadCount();
    int port = ApnProxyConfig.getConfig().getPort();
    LoggerUtil.info(logger, "ApnProxy Server Listen on: " + port);
    ServerBootstrap serverBootStrap = new ServerBootstrap();
    serverBootStrap.childOption(ChannelOption.ALLOCATOR, UnpooledByteBufAllocator.DEFAULT);
    bossGroup = new NioEventLoopGroup(bossThreadCount);
    workerGroup = new NioEventLoopGroup(workerThreadCount);
    try {
        serverBootStrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port).childHandler(new ApnProxyServerChannelInitializer());
        serverBootStrap.bind().sync().channel().closeFuture().sync();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        logger.error("showdown the server");
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 24 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project apn-proxy by apn-proxy.

the class HttpServer method main.

public static void main(String[] args) {
    ServerBootstrap serverBootStrap = new ServerBootstrap();
    serverBootStrap.childOption(ChannelOption.ALLOCATOR, UnpooledByteBufAllocator.DEFAULT);
    try {
        serverBootStrap.group(new NioEventLoopGroup(1), new NioEventLoopGroup(2)).channel(NioServerSocketChannel.class).localAddress(5000).childHandler(new HttpServerChannelInitializer());
        serverBootStrap.bind().sync().channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
    }
}
Also used : ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 25 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project netty by netty.

the class SslHandlerTest method testAlertProducedAndSend.

private void testAlertProducedAndSend(SslProvider provider) throws Exception {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    final SslContext sslServerCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(provider).trustManager(new SimpleTrustManagerFactory() {

        @Override
        protected void engineInit(KeyStore keyStore) {
        }

        @Override
        protected void engineInit(ManagerFactoryParameters managerFactoryParameters) {
        }

        @Override
        protected TrustManager[] engineGetTrustManagers() {
            return new TrustManager[] { new X509TrustManager() {

                @Override
                public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    // Fail verification which should produce an alert that is send back to the client.
                    throw new CertificateException();
                }

                @Override
                public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
                // NOOP
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return EmptyArrays.EMPTY_X509_CERTIFICATES;
                }
            } };
        }
    }).clientAuth(ClientAuth.REQUIRE).build();
    final SslContext sslClientCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).keyManager(new File(getClass().getResource("test.crt").getFile()), new File(getClass().getResource("test_unencrypted.pem").getFile())).sslProvider(provider).build();
    NioEventLoopGroup group = new NioEventLoopGroup();
    Channel sc = null;
    Channel cc = null;
    try {
        final Promise<Void> promise = group.next().newPromise();
        sc = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(sslServerCtx.newHandler(ch.alloc()));
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        // Just trigger a close
                        ctx.close();
                    }
                });
            }
        }).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
        cc = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(sslClientCtx.newHandler(ch.alloc()));
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        if (cause.getCause() instanceof SSLException) {
                            // We received the alert and so produce an SSLException.
                            promise.setSuccess(null);
                        }
                    }
                });
            }
        }).connect(sc.localAddress()).syncUninterruptibly().channel();
        promise.syncUninterruptibly();
    } finally {
        if (cc != null) {
            cc.close().syncUninterruptibly();
        }
        if (sc != null) {
            sc.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
        ReferenceCountUtil.release(sslServerCtx);
        ReferenceCountUtil.release(sslClientCtx);
    }
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) InetSocketAddress(java.net.InetSocketAddress) CertificateException(java.security.cert.CertificateException) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SSLException(javax.net.ssl.SSLException) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) SimpleTrustManagerFactory(io.netty.handler.ssl.util.SimpleTrustManagerFactory) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) IllegalReferenceCountException(io.netty.util.IllegalReferenceCountException) CodecException(io.netty.handler.codec.CodecException) SSLProtocolException(javax.net.ssl.SSLProtocolException) DecoderException(io.netty.handler.codec.DecoderException) SSLException(javax.net.ssl.SSLException) ClosedChannelException(java.nio.channels.ClosedChannelException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedMessageTypeException(io.netty.handler.codec.UnsupportedMessageTypeException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) X509TrustManager(javax.net.ssl.X509TrustManager) File(java.io.File) ManagerFactoryParameters(javax.net.ssl.ManagerFactoryParameters) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Aggregations

NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)521 EventLoopGroup (io.netty.channel.EventLoopGroup)256 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)226 Bootstrap (io.netty.bootstrap.Bootstrap)184 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)178 SocketChannel (io.netty.channel.socket.SocketChannel)169 ChannelFuture (io.netty.channel.ChannelFuture)157 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)157 Channel (io.netty.channel.Channel)138 InetSocketAddress (java.net.InetSocketAddress)118 LoggingHandler (io.netty.handler.logging.LoggingHandler)87 ChannelPipeline (io.netty.channel.ChannelPipeline)84 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)73 SslContext (io.netty.handler.ssl.SslContext)64 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)49 IOException (java.io.IOException)49 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)47 ByteBuf (io.netty.buffer.ByteBuf)44 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)43 ChannelInitializer (io.netty.channel.ChannelInitializer)41