Example usage for io.netty.bootstrap ServerBootstrap group

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

Introduction

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

Prototype

public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) 

Source Link

Document

Set the EventLoopGroup for the parent (acceptor) and the child (client).

Usage

From source file:de.mxro.thrd.netty4.tests.http.HttpHelloWorldServer.java

License:Apache License

public Future<Void> run() throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from   w ww .  jav a2s  .c o  m
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new HttpHelloWorldServerInitializer());

        Channel ch = b.bind(port).sync().channel();

        return ch.closeFuture();
        // ch.closeFuture().sync();
    } finally {

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

From source file:de.pvsnp.chat.api.connector.ChatServer.java

public void connect() {
    EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
    EventLoopGroup workerGroup = new NioEventLoopGroup(); // (1)

    try {//from  w  ww .  ja  v a 2s .  c  o  m
        ServerBootstrap bootstrap = new ServerBootstrap(); // (2)
        bootstrap.group(bossGroup, workerGroup); // (3)
        bootstrap.channel(NioServerSocketChannel.class);// (4)
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { // 5
            @Override
            protected void initChannel(SocketChannel channel) throws Exception {
                System.out.println(
                        "Ein Computer hat sich verbunden. IP: " + channel.remoteAddress().getHostName()); // (6)
                channel.pipeline().addLast(new StringEncoder(Charset.forName("UTF-8")), // (2)
                        new LineBasedFrameDecoder(1024), // (3)
                        new StringDecoder(Charset.forName("UTF-8")), // (2)
                        new EchoServerHandler());
                c.add(channel);
                sendMassage(channel);
            }
        });// (7)
        bootstrap.childOption(ChannelOption.SO_KEEPALIVE, keepalive); // (8)
        ChannelFuture future = bootstrap.bind(port).sync(); // (9)
        System.out.println("Server gestartet!");
        future.channel().closeFuture().sync(); // (10)
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        bossGroup.shutdownGracefully(); // (11)
        workerGroup.shutdownGracefully(); // (11)
    }
}

From source file:de.saxsys.synchronizefx.netty.base.server.NettyBasicServer.java

License:Open Source License

@Override
public void start() throws SynchronizeFXException {
    this.connectionAccptorGroup = new NioEventLoopGroup();
    this.clientConnectionGroup = new NioEventLoopGroup();

    BasicChannelInitializerServer channelInitializer = createChannelInitializer();
    channelInitializer.setTopologyCallback(callback);

    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(connectionAccptorGroup, clientConnectionGroup).channel(NioServerSocketChannel.class)
            .childHandler(channelInitializer).childOption(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.SO_KEEPALIVE, true);

    bootstrap.bind(port).syncUninterruptibly();
}

From source file:de.xatc.server.nettybootstrap.atc.ATCServerBootstrap.java

public void initServer() throws java.net.BindException {

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

    try {//from w w  w. j  a va2  s .c o  m
        ServerBootstrap b = new ServerBootstrap(); // (2)
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3)
                .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ObjectEncoder());
                        ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                        ch.pipeline().addLast(new ATCServerHandler());
                    }
                }).option(ChannelOption.SO_BACKLOG, ServerConfig.getMaxConnectionsAllowed()) // (5)
                .childOption(ChannelOption.SO_KEEPALIVE, ServerConfig.isKeepConnectionsAlive()); // (6)

        LOG.info("Listening for Data on " + ServerConfig.getAtcIP() + ":" + ServerConfig.getAtcPort());
        channelFuture = b.bind(ServerConfig.getAtcIP(), ServerConfig.getAtcPort()).sync(); // (7)

    } catch (InterruptedException ex) {
        LOG.error(ex.getLocalizedMessage());
        ex.printStackTrace(System.err);
    } catch (Exception ex) {
        System.err.println("Exceptoin detected. Exit");
        System.exit(-1);
    }

}

From source file:de.xatc.server.nettybootstrap.pilot.DataServerBootstrap.java

public void initServer() {

    bossGroup = new NioEventLoopGroup();
    workerGroup = new NioEventLoopGroup();
    try {/*from  w w w. j a v  a 2  s . com*/
        ServerBootstrap b = new ServerBootstrap(); // (2)

        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3)
                .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ObjectEncoder());
                        ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                        ch.pipeline().addLast(new DataServerHandler());

                    }
                }).option(ChannelOption.SO_BACKLOG, ServerConfig.getMaxConnectionsAllowed()) // (5)
                .childOption(ChannelOption.SO_KEEPALIVE, ServerConfig.isKeepConnectionsAlive()); // (6)

        LOG.info("Listening for Data on " + ServerConfig.getDataIP() + ":" + ServerConfig.getDataPort());
        channelFuture = b.bind(ServerConfig.getDataIP(), ServerConfig.getDataPort()).sync(); // (7)

    } catch (InterruptedException ex) {
        LOG.error(ex.getLocalizedMessage());
        ex.printStackTrace(System.err);
    } catch (Exception ex) {
        System.err.println(" exception caught. Could not startup. Exiting" + ex.getLocalizedMessage());
        System.exit(-1);
    }

}

From source file:deathcap.wsmc.web.WebThread.java

License:Apache License

@Override
public void run() {
    // First ping the server and save the response, it'll be useful later for Forge
    // https://github.com/deathcap/wsmc/issues/40
    try {/* ww w .  ja  v a  2  s.  c  om*/
        PingStatus pingStatus = new PingStatus(this.mcAddress, this.mcPort);
        this.pingResponseText = pingStatus.ping();
        this.pingResponse = PingStatus.parse(this.pingResponseText);

        System.out.println("ping description=" + this.pingResponse.description + ", type="
                + (this.pingResponse.modinfo != null ? this.pingResponse.modinfo.type : "(no modinfo)"));
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(
                new ServerHandler(this, this.mcAddress, this.mcPort, this.users, this.filter, this.verbose));

        SocketAddress socketAddress;

        if (this.wsAddress == null || this.wsAddress.equals("")) {
            socketAddress = new InetSocketAddress((InetAddress) null, this.wsPort);
        } else {
            socketAddress = new InetSocketAddress(this.wsAddress, this.wsPort);
        }

        Channel channel = bootstrap.bind(socketAddress).sync().channel();

        channel.closeFuture().sync();
    } catch (InterruptedException e) {
        interrupt();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:demo.netty.discard.DiscardServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from w  ww.j  ava  2 s .  c  om*/
        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();
    }
}

From source file:demo.netty.stickypacket.correct.linebased.TimeServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO// w ww. j a  va 2s. c o m
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel arg0) throws Exception {
                        arg0.pipeline().addLast(new LineBasedFrameDecoder(1024));
                        arg0.pipeline().addLast(new StringDecoder());
                        arg0.pipeline().addLast(new TimeServerHandler());
                    }
                });
        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:demo.netty.stickypacket.err.TimeServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO//from w  ww .  j  a  v a 2 s  .c o m
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel arg0) throws Exception {
                        arg0.pipeline().addLast(new TimeServerHandler());
                    }

                });
        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:dpfmanager.shell.modules.server.core.HttpServer.java

License:Open Source License

public void start() throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from  w  ww.  ja v a  2  s  . com*/
        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);
        b.channel(NioServerSocketChannel.class);
        b.handler(new LoggingHandler(LogLevel.INFO));
        b.childHandler(new HttpServerInitializer(sslCtx, context));

        Channel ch = b.bind(PORT).sync().channel();

        context.send(BasicConfig.MODULE_MESSAGE, new LogMessage(getClass(), Level.DEBUG,
                DPFManagerProperties.getBundle().getString("startedServer").replace("%1", getServerUri()),
                true));

        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}