Example usage for io.netty.bootstrap ServerBootstrap ServerBootstrap

List of usage examples for io.netty.bootstrap ServerBootstrap ServerBootstrap

Introduction

In this page you can find the example usage for io.netty.bootstrap ServerBootstrap ServerBootstrap.

Prototype

public ServerBootstrap() 

Source Link

Usage

From source file:com.sangupta.swift.netty.proxy.ReverseProxyServer.java

License:Apache License

public ReverseProxyServer(SwiftServer server) {
    this.bossGroup = new NioEventLoopGroup(1);
    this.workerGroup = new NioEventLoopGroup();

    try {/*from www.  ja  va2  s .  c o  m*/
        this.serverBootstrap = new ServerBootstrap();

        this.serverBootstrap.group(this.bossGroup, this.workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ProxyInitializer(server.getProxyHost(), server.getProxyPort()))
                .childOption(ChannelOption.AUTO_READ, false);

        this.channel = this.serverBootstrap.bind(server.getListenPort()).sync().channel();

        System.out.println("Listening on port " + server.getListenPort());

        this.channel.closeFuture().sync();
    } catch (InterruptedException e) {
        // TODO: think what we can do with this
        e.printStackTrace();
    } finally {
        this.shutdownGracefully();
    }
}

From source file:com.sangupta.swift.netty.spdy.SpdyStaticFileServer.java

License:Apache License

public SpdyStaticFileServer(SwiftServer server) {
    if (server.isSpdyEnabled() && !server.isSslEnabled()) {
        throw new IllegalStateException("SPDY can only be enabled along with SSL");
    }/*ww w.j  av a 2 s  .  c o m*/

    if (server.isSslEnabled()) {
        if (server.isSelfSignedSSL()) {
            SelfSignedCertificate ssc;

            if (server.isSpdyEnabled()) {
                try {
                    ssc = new SelfSignedCertificate();
                    this.sslContext = SslContext.newServerContext(ssc.certificate(), ssc.privateKey(), null,
                            null, Arrays.asList(SelectedProtocol.SPDY_3_1.protocolName(),
                                    SelectedProtocol.HTTP_1_1.protocolName()),
                            0, 0);
                } catch (CertificateException e) {
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                } catch (SSLException e) {
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                }
            } else {
                // basic self signed cert
                try {
                    ssc = new SelfSignedCertificate();
                    this.sslContext = SslContext.newServerContext(SslProvider.JDK, ssc.certificate(),
                            ssc.privateKey());
                } catch (CertificateException e) {
                    e.printStackTrace();
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                } catch (SSLException e) {
                    e.printStackTrace();
                    throw new RuntimeException("Unable to initialize self-signed SSL certificate");
                }
            }
        }
    } else {
        this.sslContext = null;
    }

    this.bossGroup = new NioEventLoopGroup(1);
    this.workerGroup = new NioEventLoopGroup();

    try {
        this.serverBootstrap = new ServerBootstrap();
        if (server.isSpdyEnabled()) {
            this.serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024);
        }

        SpdyStaticFileServerHandler fileServerHandler = new SpdyStaticFileServerHandler(server);

        this.serverBootstrap.group(this.bossGroup, this.workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new SpdyStaticFileServerInitializer(this.sslContext, fileServerHandler));

        if (AssertUtils.isNotEmpty(server.getServerName())) {
            this.channel = this.serverBootstrap.bind(server.getServerName(), server.getListenPort()).sync()
                    .channel();
        } else {
            this.channel = this.serverBootstrap.bind(server.getListenPort()).sync().channel();
        }

        System.out.println("Listening on port " + server.getListenPort());

        this.channel.closeFuture().sync();
    } catch (InterruptedException e) {
        // TODO: think what we can do with this
    } finally {
        this.shutdownGracefully();
    }
}

From source file:com.seagate.kinetic.simulator.io.provider.nio.http.HttpTransportProvider.java

License:Open Source License

public void doInit() throws InterruptedException {

    this.port = this.lcservice.getServiceConfiguration().getPort();

    bossGroup = new NioEventLoopGroup();
    workerGroup = new NioEventLoopGroup();

    msChannelInitializer = new HttpChannelInitializer(this.lcservice);

    bootstrap = new ServerBootstrap();

    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(msChannelInitializer);

    logger.info("KineticClient http service binding on port =" + port);

    channelFuture = bootstrap.bind(port).sync();

    this.myName = "ioMessageService-" + port;

    this.myThread = new Thread(this);
    this.myThread.setName(myName);
    this.myThread.start();
}

From source file:com.seagate.kinetic.simulator.io.provider.nio.ssl.SslNioTransportProvider.java

License:Open Source License

public void doInit() throws InterruptedException {

    // if use ssl as default system property is set, only ssl is running on
    // the TCP port.
    // this is to make it possible to run ssl mode for all unit tests.
    if (this.service.getServiceConfiguration().getUseSslAsDefault()) {
        this.port = this.service.getServiceConfiguration().getPort();
    } else {/*from www.j  a v  a2  s  .co m*/
        // ssl as a separate service
        this.port = this.service.getServiceConfiguration().getSslPort();
    }

    if (SimulatorConfiguration.getNioResourceSharing()) {
        // resource sharing within the same JVM
        bossGroup = NioSharedResourceManager.getBossGroup();
        workerGroup = NioSharedResourceManager.getWorkerGroup();
    } else {
        bossGroup = this.service.getNioEventLoopGroupManager().getBossGroup();
        workerGroup = this.service.getNioEventLoopGroupManager().getWorkerGroup();
    }

    sslChannelInitializer = new SslChannelInitializer(this.service);

    bootstrap = new ServerBootstrap();

    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(sslChannelInitializer);

    logger.info("KineticClient ssl service binding on port =" + port);

    channelFuture = bootstrap.bind(port).sync();
}

From source file:com.seagate.kinetic.simulator.io.provider.nio.tcp.TcpNioTransportProvider.java

License:Open Source License

public void doInit() throws InterruptedException {

    this.port = this.service.getServiceConfiguration().getPort();

    if (SimulatorConfiguration.getNioResourceSharing()) {
        // resource sharing within the same JVM
        bossGroup = NioSharedResourceManager.getBossGroup();
        workerGroup = NioSharedResourceManager.getWorkerGroup();
    } else {//from w ww. j  ava 2 s .co m
        // resource usage independent per instance
        bossGroup = this.service.getNioEventLoopGroupManager().getBossGroup();
        workerGroup = this.service.getNioEventLoopGroupManager().getWorkerGroup();
    }

    msChannelInitializer = new NioChannelInitializer(this.service);

    bootstrap = new ServerBootstrap();

    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(msChannelInitializer);

    logger.info("Kinetic nio service binding on port =" + port);

    channelFuture = bootstrap.bind(port).sync();

    logger.info("Kinetic nio service bound on port =" + port);
}

From source file:com.seagate.kinetic.simulator.io.provider.nio.udt.UdtTransportProvider.java

License:Open Source License

public void doInit() throws InterruptedException {

    this.port = this.service.getServiceConfiguration().getPort();

    final ThreadFactory acceptFactory = new NioThreadFactory("UdtAccept");

    final ThreadFactory connectFactory = new NioThreadFactory("UdtConnect");

    bossGroup = new NioEventLoopGroup(10, acceptFactory, NioUdtProvider.MESSAGE_PROVIDER);

    workerGroup = new NioEventLoopGroup(10, connectFactory, NioUdtProvider.MESSAGE_PROVIDER);

    msChannelInitializer = new UdtChannelInitializer(this.service);

    bootstrap = new ServerBootstrap();

    bootstrap.group(bossGroup, workerGroup).channelFactory(NioUdtProvider.MESSAGE_ACCEPTOR)
            .option(ChannelOption.SO_BACKLOG, 1000).option(ChannelOption.SO_REUSEADDR, true)
            .childHandler(msChannelInitializer);

    logger.info("Kinetic udt service binding on port =" + port);

    channelFuture = bootstrap.bind(port).sync();

    logger.info("Kinetic udt service bound on port =" + port);
}

From source file:com.seed.nettyechoserver.EchoServer.java

public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(1);
    try {/*from www . j a v  a 2s.co m*/
        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();
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoServerHandler());
                    }
                });

        ChannelFuture f = b.bind(PORT).sync();
        f.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.Server.java

License:Apache License

/**
 * This is passive mode server/*from   w w  w. ja  v  a2s.  co m*/
 * @param fs FTP Session Handler
 * @param host Server IP address
 * @param port Passive port no.
 */
public Server(String host, int port, int mode, String fileName) {
    InetSocketAddress inSocketAddress = new InetSocketAddress(host, port);
    try {
        ServerBootstrap bootStrap = new ServerBootstrap();
        bootStrap.group(bossGroup, workerGroup);
        bootStrap.channel(NioServerSocketChannel.class);
        bootStrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 1);
        bootStrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 1);
        bootStrap.childHandler(new MyChannelInitializer(this, mode, fileName));
        bootStrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
        bootStrap.bind(inSocketAddress);
        System.out.println("Server started");
    } catch (Exception eg) {
        eg.printStackTrace();
        stop();
    }
}

From source file:com.seventh_root.ld33.server.LD33Server.java

License:Apache License

private void start() {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from ww  w  . j a  v  a  2s  .  c  o m*/
        ServerBootstrap bootstrap = new ServerBootstrap();
        handler = new LD33ServerHandler(this);
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel channel) throws Exception {
                        channel.pipeline().addLast(new ObjectEncoder(),
                                new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                                //new LD33ClientBoundPacketEncoder(),
                                //new LD33ServerBoundPacketDecoder(LD33Server.this),
                                handler);
                    }
                });
        Channel channel = bootstrap.bind(getConfig().getInt("port", 37896)).sync().channel();
        setRunning(true);
        long beforeTime, timeDiff, sleep;
        beforeTime = currentTimeMillis();
        while (isRunning()) {
            doTick();
            timeDiff = currentTimeMillis() - beforeTime;
            sleep = DELAY - timeDiff;
            if (sleep > 0) {
                try {
                    Thread.sleep(sleep);
                } catch (InterruptedException exception) {
                    getLogger().log(SEVERE, "Thread interrupted", exception);
                }
            }
            beforeTime = currentTimeMillis();
        }
        channel.closeFuture().sync();
    } catch (InterruptedException exception) {
        getLogger().log(SEVERE, "Event loop group interrupted", exception);
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.shbxs.netty.SecureChatServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    //SelfSignedCertificate????
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    //???/*w  w  w.  j  av a  2s  .  com*/
    SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    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 SecureChatServerInitializer(sslCtx));

        b.bind(PORT).sync().channel().closeFuture().sync();
        //bind?channnel
        //sync?future future??future
        //channel futureio??channel
        //closefuture ?????future
        //

    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}