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.chen.opensourceframework.netty.discard.DiscardClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//from   ww w  . j a  va 2  s  .  co m
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                }
                p.addLast(new DiscardClientHandler());
            }
        });

        // Make the connection attempt.
        ChannelFuture f = b.connect(HOST, PORT).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.chen.opensourceframework.netty.discard.DiscardServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*w ww . jav  a2 s. co  m*/
        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:com.chenyang.proxy.EchoClient.java

License:Apache License

public static void start(EventLoopGroup group) throws Exception {
    // Configure SSL.git
    final SslContext sslCtx;
    if (SSL) {//w  ww .ja  v  a2  s . c  o m
        sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
    } else {
        sslCtx = null;
    }

    // Configure the client.
    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 LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoClientHandler());
                    }
                });

        // Start the client.
        System.out.println(" clients star ");
        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.chenyang.proxy.EchoServer.java

License:Apache License

public static void start() throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from  w  w w. ja v  a 2 s. 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.
        System.out.println(" server start in port " + PORT);
        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.chenyang.proxy.http.HttpServerChannelInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel channel) throws Exception {
    ChannelPipeline pipeline = channel.pipeline();
    pipeline.addLast(new ConnectionHandler());
    pipeline.addLast("codec", new HttpServerCodec());
    pipeline.addLast(HttpPreHandler.HANDLER_NAME, preHandler);
    pipeline.addLast(HttpSchemaHandler.HANDLER_NAME, schemaHandler);
}

From source file:com.chenyang.proxy.http.HttpTunnelChannelInitializer.java

License:Apache License

/**
 * @see io.netty.channel.ChannelInitializer#initChannel(io.netty.channel.Channel)
 *//*from  www.j  a  va  2s.  c o  m*/
@Override
protected void initChannel(SocketChannel channel) throws Exception {
    com.chenyang.proxy.common.HttpRemote apnProxyRemote = uaChannel
            .attr(com.chenyang.proxy.common.HttpConnectionAttribute.ATTRIBUTE_KEY).get().getRemote();

    channel.attr(com.chenyang.proxy.common.HttpConnectionAttribute.ATTRIBUTE_KEY)
            .set(uaChannel.attr(com.chenyang.proxy.common.HttpConnectionAttribute.ATTRIBUTE_KEY).get());

    ChannelPipeline pipeline = channel.pipeline();

    // pipeline.addLast("idlestate", new IdleStateHandler(0, 0, 3,
    // TimeUnit.MINUTES));
    // pipeline.addLast("idlehandler", new ProxyIdleHandler());

    pipeline.addLast(new com.chenyang.proxy.http.HttpRelayHandler(apnProxyRemote.getRemoteAddr() + " --> UA",
            uaChannel));

}

From source file:com.chiorichan.http.ssl.SslInitializer.java

License:Mozilla Public License

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

    try {//ww w  . j a  v  a2  s.c  o  m
        p.addLast(new SniNegotiator());
    } catch (Exception e) {
        NetworkManager.shutdownHttpsServer();
        throw new IllegalStateException("The SSL engine failed to initalize", e);
    }

    p.addLast("decoder", new HttpRequestDecoder());
    p.addLast("aggregator", new HttpObjectAggregator(Integer.MAX_VALUE));
    p.addLast("encoder", new HttpResponseEncoder());
    p.addLast("deflater", new HttpContentCompressor());
    p.addLast("handler", new HttpHandler(true));

    activeChannels.add(new WeakReference<SocketChannel>(ch));
}

From source file:com.chiorichan.net.query.QueryServerInitializer.java

License:Mozilla Public License

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

    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));

    pipeline.addLast(DECODER);//from  w ww  .  j a v  a 2  s .  c  o  m
    pipeline.addLast(ENCODER);

    pipeline.addLast(SERVER_HANDLER);
}

From source file:com.chuck.netty4.websocket.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()));
    }/*from w  ww.j a  v  a  2s . c om*/
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
    pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
    pipeline.addLast(new WebSocketFrameHandler());
}

From source file:com.circle.netty.http.AsynHttpServerInitializer.java

License:Apache License

public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }//from w ww  .  j  a va  2  s . c  om
    HttpResponseEncoder httpResponseEncoder = new HttpResponseEncoder();
    pipeline.addLast("http-encoder", httpResponseEncoder);
    HttpRequestDecoder httpRequestDecoder = new HttpRequestDecoder();
    pipeline.addLast("http-decoder", httpRequestDecoder);
    HttpObjectAggregator httpObjectAggregator = new HttpObjectAggregator(AppConfig.MAX_CONTENT_LENGTH);
    pipeline.addLast("http-aggregator", httpObjectAggregator);
    ChunkedWriteHandler chunkedWriteHandler = new ChunkedWriteHandler();
    pipeline.addLast("http-chunked", chunkedWriteHandler);
    //?handler
    pipeline.addLast("http-serverHandler", new AsynHttpServerHandler(context));
}