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.l2jmobius.gameserver.network.telnet.TelnetServer.java

License:Open Source License

public void init() {
    addHandler(new ITelnetCommand() {
        @Override//from  w ww .  jav  a  2  s .c  o  m
        public String getCommand() {
            return "help";
        }

        @Override
        public String getUsage() {
            return "help [command]";
        }

        @Override
        public String handle(ChannelHandlerContext ctx, String[] args) {
            if (args.length == 0) {
                final StringBuilder sb = new StringBuilder("Available commands:" + Config.EOL);
                for (ITelnetCommand cmd : TelnetServer.getInstance().getCommands()) {
                    sb.append(cmd.getCommand() + Config.EOL);
                }
                return sb.toString();
            }
            final ITelnetCommand cmd = TelnetServer.getInstance().getCommand(args[0]);
            if (cmd == null) {
                return "Unknown command." + Config.EOL;
            }
            return "Usage:" + Config.EOL + cmd.getUsage() + Config.EOL;
        }
    });

    try {
        final InetSocketAddress socket = Config.TELNET_HOSTNAME.equals("*")
                ? new InetSocketAddress(Config.TELNET_PORT)
                : new InetSocketAddress(Config.TELNET_HOSTNAME, Config.TELNET_PORT);
        //@formatter:off
        new ServerBootstrap().group(_workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_KEEPALIVE, true).childHandler(new TelnetServerInitializer())
                .bind(socket);
        //@formatter:on
        LOGGER.info(getClass().getSimpleName() + ": Listening on " + Config.TELNET_HOSTNAME + ":"
                + Config.TELNET_PORT);
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, e.getMessage(), e);
    }
}

From source file:com.lambdaworks.redis.server.MockTcpServer.java

License:Apache License

public void initialize(int port) throws InterruptedException {

    bossGroup = Resources.bossGroup;
    workerGroup = Resources.workerGroup;

    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override/*from   www .j  ava2 s  .c o m*/
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    // p.addLast(new LoggingHandler(LogLevel.INFO));

                    for (Supplier<? extends ChannelHandler> handler : handlers) {
                        p.addLast(handler.get());
                    }
                }
            });

    // Start the server.
    ChannelFuture f = b.bind(port).sync();

    channel = f.channel();
}

From source file:com.lambdaworks.redis.server.RandomResponseServer.java

License:Apache License

public void initialize(int port) throws InterruptedException {

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

    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override//from w ww. j av  a2s  .c  o m
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    // p.addLast(new LoggingHandler(LogLevel.INFO));
                    p.addLast(new RandomServerHandler());
                }
            });

    // Start the server.
    ChannelFuture f = b.bind(port).sync();

    channel = f.channel();
}

From source file:com.lampard.netty4.frame.fault.TimeServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO//from   www  .  j a v  a2  s  . com
    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 ChildChannelHandler());

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

        // ???
        f.channel().closeFuture().sync();

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

From source file:com.lampard.netty4.protocol.http.fileServer.HttpFileServer.java

License:Apache License

public void run(final int port, final String url) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from  w  ww  . j a va2  s  . co  m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); // ??
                        ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));// ???requestresponse
                        ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());//??
                        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());//?
                        ch.pipeline().addLast("fileServerHandler", new HttpFileServerHandler(url));// 
                    }
                });
        ChannelFuture future = b.bind("192.168.1.102", port).sync();
        System.out.println(
                "HTTP??? : " + "http://192.168.1.102:" + port + url);
        future.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.lampard.netty4.protocol.netty.server.NettyServer.java

License:Apache License

public void bind() throws Exception {
    // ??NIO/*from  w  ww .  j a v a2  s  .co m*/
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    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 IOException {
                    ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4));
                    ch.pipeline().addLast(new NettyMessageEncoder());
                    ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50));
                    ch.pipeline().addLast(new LoginAuthRespHandler());
                    ch.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler());
                }
            });

    // ???
    b.bind(NettyConstant.REMOTEIP, NettyConstant.PORT).sync();
    LOG.info("Netty server start ok : " + (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT));
}

From source file:com.lb.mysession.server.TestPro.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//  w  w  w. j a v a 2s.  c om
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ProtobufServerInitializer());

        Channel ch = b.bind(port).sync().channel();
        System.out.println("Web socket server started at port " + port + '.');
        System.out.println("Open your browser and navigate to http://localhost:" + port + '/');

        ch.closeFuture().sync();

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

From source file:com.lb.netty.protoc.SubReqServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO//from  www .j  a  va2s.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, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) {
                        ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                        // protobuffer
                        ch.pipeline().addLast(
                                new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance()));
                        ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                        ch.pipeline().addLast(new ProtobufEncoder());
                        ch.pipeline().addLast(new SubReqServerHandler());
                    }
                });
        // ???
        ChannelFuture f = b.bind(port).sync();
        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.leadtone.riders.server.RidersWebSocketServer.java

License:Apache License

public void start() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  w w w .  ja v  a  2  s.co m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(serverInitializer);

        Channel ch = b.bind(port).sync().channel();
        log.info("Web socket server started at port " + port + '.');
        log.info("Open your browser and navigate to http://localhost:" + port + '/');

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

From source file:com.liferay.nativity.control.findersync.FSNativityControlImpl.java

License:Open Source License

@Override
public boolean connect() {
    if (_connected) {
        return true;
    }//from ww w . jav  a  2  s. com

    _childEventLoopGroup = new NioEventLoopGroup();
    _parentEventLoopGroup = new NioEventLoopGroup();

    try {
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        serverBootstrap.group(_parentEventLoopGroup, _childEventLoopGroup);

        serverBootstrap.channel(NioServerSocketChannel.class);

        ChannelInitializer channelInitializer = new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {

                DelimiterBasedFrameDecoder messageDecoder = new DelimiterBasedFrameDecoder(Integer.MAX_VALUE,
                        Delimiters.lineDelimiter());

                FinderSyncChannelHandler finderSyncChannelHandler = new FinderSyncChannelHandler();

                socketChannel.pipeline().addLast(messageDecoder, finderSyncChannelHandler);
            }
        };

        serverBootstrap.childHandler(channelInitializer);

        serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture channelFuture = serverBootstrap.bind(0).sync();

        InetSocketAddress inetSocketAddress = (InetSocketAddress) channelFuture.channel().localAddress();

        _writePortToFile(inetSocketAddress.getPort());
    } catch (Exception e) {
        _logger.error(e.getMessage(), e);

        _connected = false;

        return false;
    }

    _connected = true;

    return true;
}