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.quavo.osrs.network.NetworkExecutor.java

License:Open Source License

/**
 * Starts the network for a {@link Server}.
 * //from   w  w  w  .jav a2s.  co  m
 * @param server The {@link Server} to use for building the network.
 * @return <True> If the network started successfully.
 */
public static void start() {
    EventLoopGroup boss = new NioEventLoopGroup();
    EventLoopGroup worker = new NioEventLoopGroup();
    ServerBootstrap bootstrap = new ServerBootstrap();

    bootstrap.group(boss, worker);
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();

            pipeline.addLast("decoder", new ConnectionDecoder());
            pipeline.addLast("encoder", new ConnectionEncoder());
            pipeline.addLast("adapter", new NetworkMessageHandler());
        }

    });
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);

    try {
        bootstrap.bind(Constants.HOST_NAME, Constants.HOST_PORT).sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("Server successfully bootstrapped on port " + Constants.HOST_PORT + " and address "
            + Constants.HOST_NAME + ".");
}

From source file:com.rackspacecloud.blueflood.inputs.handlers.HttpMetricsIngestionServer.java

License:Apache License

/**
 * Starts the Ingest server/*from   www.j ava 2s  . c  o m*/
 *
 * @throws InterruptedException
 */
public void startServer() throws InterruptedException {

    RouteMatcher router = new RouteMatcher();
    router.get("/v1.0", new DefaultHandler());
    router.post("/v1.0/multitenant/experimental/metrics",
            new HttpMultitenantMetricsIngestionHandler(processor, timeout));
    router.post("/v1.0/:tenantId/experimental/metrics", new HttpMetricsIngestionHandler(processor, timeout));
    router.post("/v1.0/:tenantId/experimental/metrics/statsd",
            new HttpAggregatedIngestionHandler(processor, timeout));

    router.get("/v2.0", new DefaultHandler());
    router.post("/v2.0/:tenantId/ingest/multi", new HttpMultitenantMetricsIngestionHandler(processor, timeout));
    router.post("/v2.0/:tenantId/ingest", new HttpMetricsIngestionHandler(processor, timeout));
    router.post("/v2.0/:tenantId/ingest/aggregated", new HttpAggregatedIngestionHandler(processor, timeout));
    router.post("/v2.0/:tenantId/events", getHttpEventsIngestionHandler());
    router.post("/v2.0/:tenantId/ingest/aggregated/multi",
            new HttpAggregatedMultiIngestionHandler(processor, timeout));
    final RouteMatcher finalRouter = router;

    log.info("Starting metrics listener HTTP server on port {}", httpIngestPort);
    ServerBootstrap server = new ServerBootstrap();
    server.group(acceptorGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel channel) throws Exception {
                    setupPipeline(channel, finalRouter);
                }
            });

    Channel channel = server.bind(new InetSocketAddress(httpIngestHost, httpIngestPort)).sync().channel();
    allOpenChannels.add(channel);

    //register the tracker MBean for JMX/jolokia
    log.info("Registering tracker service");
    Tracker.getInstance().register();
}

From source file:com.rackspacecloud.blueflood.outputs.handlers.HttpMetricDataQueryServer.java

License:Apache License

public void startServer() throws InterruptedException {

    RouteMatcher router = new RouteMatcher();
    router.get("/v1.0", new DefaultHandler());
    router.get("/v1.0/:tenantId/experimental/views/metric_data/:metricName", new HttpRollupsQueryHandler());

    router.post("/v1.0/:tenantId/experimental/views/metric_data", new HttpMultiRollupsQueryHandler());
    router.post("/v2.0/:tenantId/views", new HttpMultiRollupsQueryHandler());

    router.get("/v2.0", new DefaultHandler());
    router.get("/v2.0/:tenantId/views/:metricName", new HttpRollupsQueryHandler());
    router.get("/v2.0/:tenantId/metrics/search", new HttpMetricsIndexHandler());
    router.get("/v2.0/:tenantId/metric_name/search", new HttpMetricTokensHandler());
    router.get("/v2.0/:tenantId/events/getEvents", new HttpEventsQueryHandler(getEventsIO()));

    router.options("/v2.0/:tenantId/views/:metricName", new HttpOptionsHandler());
    router.options("/v2.0/:tenantId/views", new HttpOptionsHandler());
    router.options("/v2.0/:tenantId/metrics/search", new HttpOptionsHandler());
    router.options("/v2.0/:tenantId/metric_name/search", new HttpOptionsHandler());
    router.options("/v2.0/:tenantId/events/getEvents", new HttpOptionsHandler());

    final RouteMatcher finalRouter = router;

    log.info("Starting metric data query server (HTTP) on port {}", this.httpQueryPort);
    ServerBootstrap server = new ServerBootstrap();
    server.group(acceptorGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override//w  w  w  .  ja va 2  s.c o  m
                public void initChannel(SocketChannel channel) throws Exception {
                    setupPipeline(channel, finalRouter);
                }
            });
    serverChannel = server.bind(new InetSocketAddress(httpQueryHost, httpQueryPort)).sync().channel();

    //register the tracker MBean for JMX/jolokia
    log.info("Registering tracker service");
    Tracker.getInstance().register();
}

From source file:com.relayrides.pushy.apns.MockApnsServer.java

License:Open Source License

public synchronized void start() throws InterruptedException {
    final ServerBootstrap bootstrap = new ServerBootstrap();

    bootstrap.group(this.eventLoopGroup);
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);

    final MockApnsServer server = this;

    bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

        @Override//from  w  w  w . j  av a  2 s  .com
        protected void initChannel(final SocketChannel channel) throws Exception {
            channel.pipeline().addLast("ssl", new SslHandler(SSLTestUtil.createSSLEngineForMockServer()));
            channel.pipeline().addLast("encoder", new ApnsErrorEncoder());
            channel.pipeline().addLast("decoder", new ApnsPushNotificationDecoder());
            channel.pipeline().addLast("handler", new MockApnsServerHandler(server));
        }
    });

    this.channel = bootstrap.bind(this.port).await().channel();
}

From source file:com.relayrides.pushy.apns.MockFeedbackServer.java

License:Open Source License

public synchronized void start() throws InterruptedException {
    final ServerBootstrap bootstrap = new ServerBootstrap();

    bootstrap.group(this.eventLoopGroup);
    bootstrap.channel(NioServerSocketChannel.class);

    final MockFeedbackServer server = this;
    bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

        @Override//from   ww w .ja  v  a2 s  .c o m
        protected void initChannel(final SocketChannel channel) throws Exception {
            channel.pipeline().addLast("ssl", new SslHandler(SSLTestUtil.createSSLEngineForMockServer()));
            channel.pipeline().addLast("encoder", new ExpiredTokenEncoder());
            channel.pipeline().addLast("handler", new MockFeedbackServerHandler(server));
        }
    });

    this.channel = bootstrap.bind(this.port).await().channel();
}

From source file:com.robert.NettyProject.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//  ww  w.  j  a va 2 s .c  o  m
        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("encode", new StringEncoder());
                        p.addLast("decode", new StringDecoder());
                        // 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();
    }
}

From source file:com.rr.echoserver.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    System.out.println("Writing on separate threads: " + EchoServer.THREADED);

    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from   w  w  w .j  a  va2s.  c o m*/
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } 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();
    }
}

From source file:com.rs3e.Main.java

License:Open Source License

/**
 * Initates a new gaming enviroment, here we are going to setup everything
 * needed for the server to be able to bind.
 *///  w  w w. j av  a  2  s.co m
private void initate() {
    bootstrap = new ServerBootstrap();
    bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup());
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.option(ChannelOption.SO_BACKLOG, 100);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    bootstrap.handler(new LoggingHandler(LogLevel.INFO));
    bootstrap.childHandler(new ChannelChildHandler(this));
    try {
        bootstrap.localAddress(Constants.ServerPort).bind().sync();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.sample.netty.socket.server.Server.java

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from w w  w  .  ja v  a 2s . c o  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new MessageDecoder(), new ServerHandlerInbound());
                        ch.pipeline().addLast(new MessageEncoder(), new ServerHandlerOutbound());
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        ChannelFuture f = b.bind(port).sync();
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:com.sangupta.swift.netty.http.HttpStaticFileServer.java

License:Apache License

public HttpStaticFileServer(SwiftServer server) {
    if (server.isSslEnabled()) {
        // TODO: fix this
    } else {//from w w  w . j a  v a 2 s.com
        this.sslContext = null;
    }

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

    try {
        this.serverBootstrap = new ServerBootstrap();

        HttpStaticFileServerHandler fileServerHandler = new HttpStaticFileServerHandler(
                server.getDocumentRoot());

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

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

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

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