Search in sources :

Example 36 with ServerBootstrap

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

the class FileServer method main.

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc()));
                }
                p.addLast(new StringEncoder(CharsetUtil.UTF_8), new LineBasedFrameDecoder(8192), new StringDecoder(CharsetUtil.UTF_8), new ChunkedWriteHandler(), new FileServerHandler());
            }
        });
        // Start the server.
        ChannelFuture f = b.bind(PORT).sync();
        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) StringEncoder(io.netty.handler.codec.string.StringEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 37 with ServerBootstrap

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

the class DiscardServer method main.

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc()));
                }
                p.addLast(new DiscardServerHandler());
            }
        });
        // Bind and start to accept incoming connections.
        ChannelFuture f = b.bind(PORT).sync();
        // Wait until the server socket is closed.
        // In this example, this does not happen, but you can do that to gracefully
        // shut down your server.
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) SslContext(io.netty.handler.ssl.SslContext)

Example 38 with ServerBootstrap

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

the class EchoServer method main.

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc()));
                }
                //p.addLast(new LoggingHandler(LogLevel.INFO));
                p.addLast(new EchoServerHandler());
            }
        });
        // Start the server.
        ChannelFuture f = b.bind(PORT).sync();
        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 39 with ServerBootstrap

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

the class DefaultChannelPipelineTest method setUp.

private void setUp(final ChannelHandler... handlers) throws Exception {
    final AtomicReference<Channel> peerRef = new AtomicReference<Channel>();
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(group).channel(LocalServerChannel.class);
    sb.childHandler(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            peerRef.set(ctx.channel());
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ReferenceCountUtil.release(msg);
        }
    });
    ChannelFuture bindFuture = sb.bind(LocalAddress.ANY).sync();
    Bootstrap b = new Bootstrap();
    b.group(group).channel(LocalChannel.class);
    b.handler(new ChannelInitializer<LocalChannel>() {

        @Override
        protected void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(handlers);
        }
    });
    self = b.connect(bindFuture.channel().localAddress()).sync().channel();
    peer = peerRef.get();
    bindFuture.channel().close().sync();
}
Also used : LocalChannel(io.netty.channel.local.LocalChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) OioSocketChannel(io.netty.channel.socket.oio.OioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 40 with ServerBootstrap

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

the class AbstractEventLoopTest method testReregister.

/**
     * Test for https://github.com/netty/netty/issues/803
     */
@Test
public void testReregister() {
    EventLoopGroup group = newEventLoopGroup();
    EventLoopGroup group2 = newEventLoopGroup();
    final EventExecutorGroup eventExecutorGroup = new DefaultEventExecutorGroup(2);
    ServerBootstrap bootstrap = new ServerBootstrap();
    ChannelFuture future = bootstrap.channel(newChannel()).group(group).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
        }
    }).handler(new ChannelInitializer<ServerSocketChannel>() {

        @Override
        public void initChannel(ServerSocketChannel ch) throws Exception {
            ch.pipeline().addLast(new TestChannelHandler());
            ch.pipeline().addLast(eventExecutorGroup, new TestChannelHandler2());
        }
    }).bind(0).awaitUninterruptibly();
    EventExecutor executor = future.channel().pipeline().context(TestChannelHandler2.class).executor();
    EventExecutor executor1 = future.channel().pipeline().context(TestChannelHandler.class).executor();
    future.channel().deregister().awaitUninterruptibly();
    Channel channel = group2.register(future.channel()).awaitUninterruptibly().channel();
    EventExecutor executorNew = channel.pipeline().context(TestChannelHandler.class).executor();
    assertNotSame(executor1, executorNew);
    assertSame(executor, future.channel().pipeline().context(TestChannelHandler2.class).executor());
}
Also used : DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EventExecutor(io.netty.util.concurrent.EventExecutor) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) Test(org.junit.Test)

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