Example usage for io.netty.channel ChannelPipeline addLast

List of usage examples for io.netty.channel ChannelPipeline addLast

Introduction

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

Prototype

ChannelPipeline addLast(ChannelHandler... handlers);

Source Link

Document

Inserts ChannelHandler s at the last position of this pipeline.

Usage

From source file:com.linecorp.armeria.server.ServerInitializer.java

License:Apache License

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

    if (port.protocol().isTls()) {
        p.addLast(new SniHandler(sslContexts));
        configureHttps(p);//w  ww .  j a va2s  .co  m
    } else {
        configureHttp(p);
    }
}

From source file:com.linecorp.armeria.server.ServerInitializer.java

License:Apache License

private void configureRequestCountingHandlers(ChannelPipeline p) {
    if (config.idleTimeoutMillis() > 0) {
        p.addLast(new HttpServerIdleTimeoutHandler(config.idleTimeoutMillis()));
    }//from w ww . j a  va2s .  com
    gracefulShutdownHandler.ifPresent(h -> {
        h.reset();
        p.addLast(h);
    });
}

From source file:com.lm.WebSocketServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }/* w  w w  . j  a  v a2s . c  om*/
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketLoggingHandler());
    pipeline.addLast(new WebSocketServerHandler());
}

From source file:com.look.netty.demo.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;// w ww .jav  a  2s.c  o  m
        } 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, false, 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), 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.ltln.modules.ni.omc.system.simulator.AlmClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    Constants.init();//from  www .ja v a 2  s  .c o  m
    final SslContext sslCtx;
    if (SSL) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    final EventExecutorGroup handlerGroup = new DefaultEventExecutorGroup(1);
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                        }
                        p.addLast(new AlarmMsgDecoder(8192, 7, 2, 0, 0, false));
                        p.addLast(new AlarmMsgEncoder());
                        p.addLast(handlerGroup, new AlarmClientHandler());
                    }
                });

        // 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.lufs.preresearch.nettyrouter.playground.HttpHelloWorldServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws NoSuchMethodException, IllegalAccessException {
    ChannelPipeline p = ch.pipeline();
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }/*  www.jav a  2  s. c o m*/

    p.addLast(new HttpServerCodec());
    p.addLast(new HttpServerExpectContinueHandler());
    p.addLast("httpObjectAggregator", new HttpObjectAggregator(Integer.MAX_VALUE));
    p.addLast(new HttpHelloWorldServerHandler(RouteConfig.newMHRouter()));
    p.addLast(new BadClientSilencer());
}

From source file:com.lunex.inputprocessor.testdemo.HttpSnoopClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();

    // Enable HTTPS if necessary.
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }/*  w  ww  .  j a v  a 2 s. co  m*/

    p.addLast(new HttpClientCodec());

    // Remove the following line if you don't want automatic content
    // decompression.
    p.addLast(new HttpContentDecompressor());

    // Uncomment the following line if you don't want to handle
    // HttpContents.
    // p.addLast(new HttpObjectAggregator(1048576));

    p.addLast(new HttpSnoopClientHandler());
}

From source file:com.lxd.client.link.ClientInitalizer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ///< ??//  w  ww  .  ja v  a2s  . c o m
    ChannelPipeline pipeline = ch.pipeline();

    ///< Protobuf  ?
    pipeline.addLast(new ProtobufVarint32FrameDecoder())
            ///<  Protobuf  ??
            .addLast(new ProtobufDecoder(Msg.Msg_.getDefaultInstance()))
            ///<  Protobuf ??
            .addLast(new ProtobufVarint32LengthFieldPrepender())
            ///<  Protobuf ??
            .addLast(new ProtobufEncoder())
            ///<  ??
            .addLast(new ClientHandler());

}

From source file:com.lxd.server.link.ServerInitalizer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ///< ??//from   w w  w  . ja  v a2  s. c  om
    ChannelPipeline pipeline = ch.pipeline();

    ///< Protobuf  ?
    pipeline.addLast(new ProtobufVarint32FrameDecoder())
            ///<  Protobuf  ??
            .addLast(new ProtobufDecoder(Msg.Msg_.getDefaultInstance()))
            ///<  Protobuf ??
            .addLast(new ProtobufVarint32LengthFieldPrepender())
            ///<  Protobuf ??
            .addLast(new ProtobufEncoder())
            ///<  ??
            .addLast(new ServerHandler());

}

From source file:com.lxz.talk.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 ? "http" : uri.getScheme();
    final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
    final int port;
    if (uri.getPort() == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;//  w  w w  .ja  va 2s.com
        } else if ("https".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) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } 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, false, 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), 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();
    }
}