Search in sources :

Example 46 with ServerBootstrap

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

the class Http2Server method main.

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        SslProvider provider = OpenSsl.isAlpnSupported() ? SslProvider.OPENSSL : SslProvider.JDK;
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(provider).ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE).applicationProtocolConfig(new ApplicationProtocolConfig(Protocol.ALPN, // NO_ADVERTISE is currently the only mode supported by both OpenSsl and JDK providers.
        SelectorFailureBehavior.NO_ADVERTISE, // ACCEPT is currently the only mode supported by both OpenSsl and JDK providers.
        SelectedListenerFailureBehavior.ACCEPT, ApplicationProtocolNames.HTTP_2, ApplicationProtocolNames.HTTP_1_1)).build();
    } else {
        sslCtx = null;
    }
    // Configure the server.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new Http2ServerInitializer(sslCtx));
        Channel ch = b.bind(PORT).sync().channel();
        System.err.println("Open your HTTP/2-enabled web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:" + PORT + '/');
        ch.closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) SslProvider(io.netty.handler.ssl.SslProvider) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SslContext(io.netty.handler.ssl.SslContext) ApplicationProtocolConfig(io.netty.handler.ssl.ApplicationProtocolConfig)

Example 47 with ServerBootstrap

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

the class WebSocketServer 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 WebSocketServerInitializer(sslCtx));
        Channel ch = b.bind(PORT).sync().channel();
        System.out.println("Open your web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:" + PORT + '/');
        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Channel(io.netty.channel.Channel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SslContext(io.netty.handler.ssl.SslContext)

Example 48 with ServerBootstrap

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

the class SocksServer method main.

public static void main(String[] args) throws Exception {
    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 SocksServerInitializer());
        b.bind(PORT).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 49 with ServerBootstrap

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

the class Http2Server method start.

public ChannelFuture start() throws Exception {
    final SslContext sslCtx = configureTLS();
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(group).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), new Http2OrHttpHandler());
        }
    });
    Channel ch = b.bind(PORT).sync().channel();
    return ch.closeFuture();
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) SslContext(io.netty.handler.ssl.SslContext)

Example 50 with ServerBootstrap

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

the class LocalChannelTest method testWriteFailsFastOnClosedChannel.

@Test
public void testWriteFailsFastOnClosedChannel() throws Exception {
    Bootstrap cb = new Bootstrap();
    ServerBootstrap sb = new ServerBootstrap();
    cb.group(group1).channel(LocalChannel.class).handler(new TestHandler());
    sb.group(group2).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<LocalChannel>() {

        @Override
        public void initChannel(LocalChannel ch) throws Exception {
            ch.pipeline().addLast(new TestHandler());
        }
    });
    Channel sc = null;
    Channel cc = null;
    try {
        // Start server
        sc = sb.bind(TEST_ADDRESS).sync().channel();
        // Connect to the server
        cc = cb.connect(sc.localAddress()).sync().channel();
        // Close the channel and write something.
        cc.close().sync();
        try {
            cc.writeAndFlush(new Object()).sync();
            fail("must raise a ClosedChannelException");
        } catch (Exception e) {
            assertThat(e, is(instanceOf(ClosedChannelException.class)));
            // the ClosedChannelException has been created by AbstractUnsafe rather than transport implementations.
            if (e.getStackTrace().length > 0) {
                assertThat(e.getStackTrace()[0].getClassName(), is(AbstractChannel.class.getName() + "$AbstractUnsafe"));
                e.printStackTrace();
            }
        }
    } finally {
        closeChannel(cc);
        closeChannel(sc);
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) AbstractChannel(io.netty.channel.AbstractChannel) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) 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