Search in sources :

Example 76 with ChannelInitializer

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

the class OcspTest method newServerHandler.

private static ChannelHandler newServerHandler(final SslContext context, final byte[] response, final ChannelHandler handler) {
    return new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            SslHandler sslHandler = context.newHandler(ch.alloc());
            if (response != null) {
                ReferenceCountedOpenSslEngine engine = (ReferenceCountedOpenSslEngine) sslHandler.engine();
                engine.setOcspResponse(response);
            }
            pipeline.addLast(sslHandler);
            if (handler != null) {
                pipeline.addLast(handler);
            }
        }
    };
}
Also used : ReferenceCountedOpenSslEngine(io.netty.handler.ssl.ReferenceCountedOpenSslEngine) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Channel(io.netty.channel.Channel) ChannelInitializer(io.netty.channel.ChannelInitializer) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 77 with ChannelInitializer

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

the class LocalEcho method main.

public static void main(String[] args) throws Exception {
    // Address to bind on / connect to.
    final LocalAddress addr = new LocalAddress(PORT);
    EventLoopGroup serverGroup = new DefaultEventLoopGroup();
    // NIO event loops are also OK
    EventLoopGroup clientGroup = new NioEventLoopGroup();
    try {
        // Note that we can use any event loop to ensure certain local channels
        // are handled by the same event loop thread which drives a certain socket channel
        // to reduce the communication latency between socket channels and local channels.
        ServerBootstrap sb = new ServerBootstrap();
        sb.group(serverGroup).channel(LocalServerChannel.class).handler(new ChannelInitializer<LocalServerChannel>() {

            @Override
            public void initChannel(LocalServerChannel ch) throws Exception {
                ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
            }
        }).childHandler(new ChannelInitializer<LocalChannel>() {

            @Override
            public void initChannel(LocalChannel ch) throws Exception {
                ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new LocalEchoServerHandler());
            }
        });
        Bootstrap cb = new Bootstrap();
        cb.group(clientGroup).channel(LocalChannel.class).handler(new ChannelInitializer<LocalChannel>() {

            @Override
            public void initChannel(LocalChannel ch) throws Exception {
                ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new LocalEchoClientHandler());
            }
        });
        // Start the server.
        sb.bind(addr).sync();
        // Start the client.
        Channel ch = cb.connect(addr).sync().channel();
        // Read commands from the stdin.
        System.out.println("Enter text (quit to end)");
        ChannelFuture lastWriteFuture = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (; ; ) {
            String line = in.readLine();
            if (line == null || "quit".equalsIgnoreCase(line)) {
                break;
            }
            // Sends the received line to the server.
            lastWriteFuture = ch.writeAndFlush(line);
        }
        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.awaitUninterruptibly();
        }
    } finally {
        serverGroup.shutdownGracefully();
        clientGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LoggingHandler(io.netty.handler.logging.LoggingHandler) LocalAddress(io.netty.channel.local.LocalAddress) InputStreamReader(java.io.InputStreamReader) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) BufferedReader(java.io.BufferedReader) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 78 with ChannelInitializer

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

the class OcspClientExample method newClientHandler.

private static ChannelInitializer<Channel> newClientHandler(final ReferenceCountedOpenSslContext context, final String host, final Promise<FullHttpResponse> promise) {
    return new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            SslHandler sslHandler = context.newHandler(ch.alloc());
            ReferenceCountedOpenSslEngine engine = (ReferenceCountedOpenSslEngine) sslHandler.engine();
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(sslHandler);
            pipeline.addLast(new ExampleOcspClientHandler(engine));
            pipeline.addLast(new HttpClientCodec());
            pipeline.addLast(new HttpObjectAggregator(1024 * 1024));
            pipeline.addLast(new HttpClientHandler(host, promise));
        }

        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            if (!promise.isDone()) {
                promise.tryFailure(new IllegalStateException("Connection closed and Promise was not done."));
            }
            ctx.fireChannelInactive();
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            if (!promise.tryFailure(cause)) {
                ctx.fireExceptionCaught(cause);
            }
        }
    };
}
Also used : ReferenceCountedOpenSslEngine(io.netty.handler.ssl.ReferenceCountedOpenSslEngine) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInitializer(io.netty.channel.ChannelInitializer) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) SslHandler(io.netty.handler.ssl.SslHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 79 with ChannelInitializer

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

the class EpollSocketChannelBenchmark method setup.

@Setup
public void setup() throws Exception {
    group = new EpollEventLoopGroup(1);
    // add an arbitrary timeout to make the timer reschedule
    future = group.schedule(new Runnable() {

        @Override
        public void run() {
            throw new AssertionError();
        }
    }, 5, TimeUnit.MINUTES);
    serverChan = new ServerBootstrap().channel(EpollServerSocketChannel.class).group(group).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) {
            ch.pipeline().addLast(new ChannelDuplexHandler() {

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) {
                    if (msg instanceof ByteBuf) {
                        ctx.writeAndFlush(msg, ctx.voidPromise());
                    } else {
                        throw new AssertionError();
                    }
                }
            });
        }
    }).bind(0).sync().channel();
    chan = new Bootstrap().channel(EpollSocketChannel.class).handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) {
            ch.pipeline().addLast(new ChannelDuplexHandler() {

                private ChannelPromise lastWritePromise;

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) {
                    if (msg instanceof ByteBuf) {
                        ByteBuf buf = (ByteBuf) msg;
                        try {
                            if (buf.readableBytes() == 1) {
                                lastWritePromise.trySuccess();
                                lastWritePromise = null;
                            } else {
                                throw new AssertionError();
                            }
                        } finally {
                            buf.release();
                        }
                    } else {
                        throw new AssertionError();
                    }
                }

                @Override
                public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                    if (lastWritePromise != null) {
                        throw new IllegalStateException();
                    }
                    lastWritePromise = promise;
                    super.write(ctx, msg, ctx.voidPromise());
                }
            });
        }
    }).group(group).connect(serverChan.localAddress()).sync().channel();
    abyte = chan.alloc().directBuffer(1);
    abyte.writeByte('a');
}
Also used : Channel(io.netty.channel.Channel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) Setup(org.openjdk.jmh.annotations.Setup)

Example 80 with ChannelInitializer

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

the class Http2MultiplexCodecBuilderTest method setUp.

@BeforeEach
public void setUp() throws InterruptedException {
    final CountDownLatch serverChannelLatch = new CountDownLatch(1);
    LocalAddress serverAddress = new LocalAddress(getClass().getName());
    serverLastInboundHandler = new SharableLastInboundHandler();
    ServerBootstrap sb = new ServerBootstrap().channel(LocalServerChannel.class).group(group).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            serverConnectedChannel = ch;
            ch.pipeline().addLast(new Http2MultiplexCodecBuilder(true, new ChannelInitializer<Channel>() {

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

                        private boolean writable;

                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            writable |= ctx.channel().isWritable();
                            super.channelActive(ctx);
                        }

                        @Override
                        public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
                            writable |= ctx.channel().isWritable();
                            super.channelWritabilityChanged(ctx);
                        }

                        @Override
                        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                            assertTrue(writable);
                            super.channelInactive(ctx);
                        }
                    });
                    ch.pipeline().addLast(serverLastInboundHandler);
                }
            }).build());
            serverChannelLatch.countDown();
        }
    });
    serverChannel = sb.bind(serverAddress).sync().channel();
    Bootstrap cb = new Bootstrap().channel(LocalChannel.class).group(group).handler(new Http2MultiplexCodecBuilder(false, new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            fail("Should not be called for outbound streams");
        }
    }).build());
    clientChannel = cb.connect(serverAddress).sync().channel();
    assertTrue(serverChannelLatch.await(5, SECONDS));
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ChannelInitializer (io.netty.channel.ChannelInitializer)83 Channel (io.netty.channel.Channel)58 Bootstrap (io.netty.bootstrap.Bootstrap)42 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)39 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)36 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)32 InetSocketAddress (java.net.InetSocketAddress)32 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)31 ChannelFuture (io.netty.channel.ChannelFuture)29 EventLoopGroup (io.netty.channel.EventLoopGroup)26 ChannelPipeline (io.netty.channel.ChannelPipeline)25 LocalServerChannel (io.netty.channel.local.LocalServerChannel)22 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)21 LocalChannel (io.netty.channel.local.LocalChannel)20 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)17 SslHandler (io.netty.handler.ssl.SslHandler)17 SocketChannel (io.netty.channel.socket.SocketChannel)16 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)14 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)12 CountDownLatch (java.util.concurrent.CountDownLatch)12