Example usage for io.netty.channel ChannelInitializer ChannelInitializer

List of usage examples for io.netty.channel ChannelInitializer ChannelInitializer

Introduction

In this page you can find the example usage for io.netty.channel ChannelInitializer ChannelInitializer.

Prototype

ChannelInitializer

Source Link

Usage

From source file:com.cmz.http.websocketx.client.WebSocketClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    URI uri = new URI(URL);
    String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
    final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
    final int port;
    if (uri.getPort() == -1) {
        if ("ws".equalsIgnoreCase(scheme)) {
            port = 80;/*ww  w. ja  v  a2 s .c  om*/
        } else if ("wss".equalsIgnoreCase(scheme)) {
            port = 443;
        } else {
            port = -1;
        }
    } else {
        port = uri.getPort();
    }

    if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
        System.err.println("Only WS(S) is supported.");
        return;
    }

    final boolean ssl = "wss".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    EventLoopGroup group = new NioEventLoopGroup();
    try {
        // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
        // If you change it to V00, ping is not supported and remember to change
        // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
        final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory
                .newHandshaker(uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()));

        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
                }
                p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192),
                        WebSocketClientCompressionHandler.INSTANCE, handler);
            }
        });

        Channel ch = b.connect(uri.getHost(), port).sync().channel();
        handler.handshakeFuture().sync();

        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String msg = console.readLine();
            if (msg == null) {
                break;
            } else if ("bye".equals(msg.toLowerCase())) {
                ch.writeAndFlush(new CloseWebSocketFrame());
                ch.closeFuture().sync();
                break;
            } else if ("ping".equals(msg.toLowerCase())) {
                WebSocketFrame frame = new PingWebSocketFrame(
                        Unpooled.wrappedBuffer(new byte[] { 8, 1, 8, 1 }));
                ch.writeAndFlush(frame);
            } else {
                WebSocketFrame frame = new TextWebSocketFrame(msg);
                ch.writeAndFlush(frame);
            }
        }
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.cmz.rxtx.RxtxClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new OioEventLoopGroup();
    try {//from ww w  . ja  v  a 2s. c  o m
        Bootstrap b = new Bootstrap();
        b.group(group).channel(RxtxChannel.class).handler(new ChannelInitializer<RxtxChannel>() {
            @Override
            public void initChannel(RxtxChannel ch) throws Exception {
                ch.pipeline().addLast(new LineBasedFrameDecoder(32768), new StringEncoder(),
                        new StringDecoder(), new RxtxClientHandler());
            }
        });

        ChannelFuture f = b.connect(new RxtxDeviceAddress(PORT)).sync();

        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.cmz.sctp.multihoming.SctpMultiHomingEchoClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {//from  w w  w  .j  av a  2s  .  c o m
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSctpChannel.class).option(SctpChannelOption.SCTP_NODELAY, true)
                .handler(new ChannelInitializer<SctpChannel>() {
                    @Override
                    public void initChannel(SctpChannel ch) throws Exception {
                        ch.pipeline().addLast(
                                //                             new LoggingHandler(LogLevel.INFO),
                                new SctpEchoClientHandler());
                    }
                });

        InetSocketAddress localAddress = SocketUtils.socketAddress(CLIENT_PRIMARY_HOST, CLIENT_PORT);
        InetAddress localSecondaryAddress = SocketUtils.addressByName(CLIENT_SECONDARY_HOST);

        InetSocketAddress remoteAddress = SocketUtils.socketAddress(SERVER_REMOTE_HOST, SERVER_REMOTE_PORT);

        // Bind the client channel.
        ChannelFuture bindFuture = b.bind(localAddress).sync();

        // Get the underlying sctp channel
        SctpChannel channel = (SctpChannel) bindFuture.channel();

        // Bind the secondary address.
        // Please note that, bindAddress in the client channel should be done before connecting if you have not
        // enable Dynamic Address Configuration. See net.sctp.addip_enable kernel param
        channel.bindAddress(localSecondaryAddress).sync();

        // Finish connect
        ChannelFuture connectFuture = channel.connect(remoteAddress).sync();

        // Wait until the connection is closed.
        connectFuture.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}

From source file:com.cmz.sctp.multihoming.SctpMultiHomingEchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from   w w  w  .  j a  v  a  2s .c o  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioSctpServerChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SctpChannel>() {
                    @Override
                    public void initChannel(SctpChannel ch) throws Exception {
                        ch.pipeline().addLast(
                                //                             new LoggingHandler(LogLevel.INFO),
                                new SctpEchoServerHandler());
                    }
                });

        InetSocketAddress localAddress = SocketUtils.socketAddress(SERVER_PRIMARY_HOST, SERVER_PORT);
        InetAddress localSecondaryAddress = SocketUtils.addressByName(SERVER_SECONDARY_HOST);

        // Bind the server to primary address.
        ChannelFuture bindFuture = b.bind(localAddress).sync();

        //Get the underlying sctp channel
        SctpServerChannel channel = (SctpServerChannel) bindFuture.channel();

        //Bind the secondary address
        ChannelFuture connectFuture = channel.bindAddress(localSecondaryAddress).sync();

        // Wait until the connection is closed.
        connectFuture.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.cmz.sctp.SctpEchoClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {//from  www.j a  v  a  2  s  .  c  om
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSctpChannel.class).option(SctpChannelOption.SCTP_NODELAY, true)
                .handler(new ChannelInitializer<SctpChannel>() {
                    @Override
                    public void initChannel(SctpChannel ch) throws Exception {
                        ch.pipeline().addLast(
                                //new LoggingHandler(LogLevel.INFO),
                                new SctpEchoClientHandler());
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}

From source file:com.cmz.sctp.SctpEchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  w  w w .  j  a  va 2s. com*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioSctpServerChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SctpChannel>() {
                    @Override
                    public void initChannel(SctpChannel ch) throws Exception {
                        ch.pipeline().addLast(
                                //new LoggingHandler(LogLevel.INFO),
                                new SctpEchoServerHandler());
                    }
                });

        // 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.cmz.stomp.StompClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {/*ww w  . j av  a2  s .  c o  m*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class);
        b.handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("decoder", new StompSubframeDecoder());
                pipeline.addLast("encoder", new StompSubframeEncoder());
                pipeline.addLast("aggregator", new StompSubframeAggregator(1048576));
                pipeline.addLast("handler", new StompClientHandler());
            }
        });

        b.connect(HOST, PORT).sync().channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.cmz.uptime.UptimeClient.java

License:Apache License

static Bootstrap configureBootstrap(Bootstrap b, EventLoopGroup g) {
    b.group(g).channel(NioSocketChannel.class).remoteAddress(HOST, PORT)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override//from  www  . ja  va 2  s .c  om
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new IdleStateHandler(READ_TIMEOUT, 0, 0), handler);
                }
            });

    return b;
}

From source file:com.codeabovelab.dm.platform.http.async.NettyRequestFactory.java

License:Apache License

private Bootstrap getBootstrap() {
    if (this.bootstrap == null) {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(this.eventLoopGroup).channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override/* ww  w. j a v  a 2 s  .  c  om*/
                    protected void initChannel(SocketChannel channel) throws Exception {
                        configureChannel(channel.config());
                        ChannelPipeline pipeline = channel.pipeline();
                        if (sslContext != null) {
                            pipeline.addLast(sslContext.newHandler(channel.alloc()));
                        }
                        pipeline.addLast(new HttpClientCodec());
                        //pipeline.addLast(new HttpObjectAggregator(maxResponseSize));
                        if (readTimeout > 0) {
                            pipeline.addLast(new ReadTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS));
                        }
                    }
                });
        this.bootstrap = bootstrap;
    }
    return this.bootstrap;
}

From source file:com.codnos.dbgp.internal.impl.DBGpEngineImpl.java

License:Apache License

private void connectToIde(EventLoopGroup workerGroup) throws InterruptedException {
    Bootstrap b = new Bootstrap();
    b.group(workerGroup);/* w  w w .ja  v a 2 s  . c om*/
    b.channel(NioSocketChannel.class);
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new DBGpInitHandler(new Init(debuggerEngine)), new DBGpCommandDecoder(),
                    new DBGpResponseEncoder(),
                    new BreakpointSetCommandHandler(debuggerEngine, argumentConfiguration),
                    new BreakpointGetCommandHandler(debuggerEngine, argumentConfiguration),
                    new BreakpointRemoveCommandHandler(debuggerEngine, argumentConfiguration),
                    new BreakpointUpdateCommandHandler(debuggerEngine, argumentConfiguration),
                    new StackDepthCommandHandler(debuggerEngine, argumentConfiguration),
                    new RunCommandHandler(debuggerEngine, statusChangeHandlerFactory, argumentConfiguration),
                    new StepOverCommandHandler(debuggerEngine, statusChangeHandlerFactory,
                            argumentConfiguration),
                    new StepIntoCommandHandler(debuggerEngine, statusChangeHandlerFactory,
                            argumentConfiguration),
                    new StepOutCommandHandler(debuggerEngine, statusChangeHandlerFactory,
                            argumentConfiguration),
                    new StackGetCommandHandler(debuggerEngine, argumentConfiguration),
                    new ContextGetCommandHandler(debuggerEngine, argumentConfiguration),
                    new StatusCommandHandler(debuggerEngine, argumentConfiguration),
                    new BreakNowCommandHandler(debuggerEngine, argumentConfiguration),
                    new EvalCommandHandler(debuggerEngine, argumentConfiguration));
        }
    });
    b.connect("localhost", port).sync();
}