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.flysoloing.learning.network.netty.http.helloworld.HttpHelloWorldServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }//ww w.  j a va 2s. c o m
    p.addLast(new HttpServerCodec());
    p.addLast(new HttpHelloWorldServerHandler());
}

From source file:com.flysoloing.learning.network.netty.http2.helloworld.multiplex.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *///from  ww  w.ja v a 2  s.c o m
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();

    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null,
                    new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:com.flysoloing.learning.network.netty.redis.RedisClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {// w w w  .  ja  v a2s  .  c o m
        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();
                p.addLast(new RedisDecoder());
                p.addLast(new RedisBulkStringAggregator());
                p.addLast(new RedisArrayAggregator());
                p.addLast(new RedisEncoder());
                p.addLast(new RedisClientHandler());
            }
        });

        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();

        // Read commands from the stdin.
        System.out.println("Enter Redis commands (quit to end)");
        ChannelFuture lastWriteFuture = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (;;) {
            final String input = in.readLine();
            final String line = input != null ? input.trim() : null;
            if (line == null || "quit".equalsIgnoreCase(line)) { // EOF or "quit"
                ch.close().sync();
                break;
            } else if (line.isEmpty()) { // skip `enter` or `enter` with spaces.
                continue;
            }
            // Sends the received line to the server.
            lastWriteFuture = ch.writeAndFlush(line);
            lastWriteFuture.addListener(new GenericFutureListener<ChannelFuture>() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        System.err.print("write failed: ");
                        future.cause().printStackTrace(System.err);
                    }
                }
            });
        }

        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.flysoloing.learning.network.netty.spdy.server.SpdyOrHttpHandler.java

License:Apache License

private static void configureSpdy(ChannelHandlerContext ctx, SpdyVersion version) throws Exception {
    ChannelPipeline p = ctx.pipeline();
    p.addLast(new SpdyFrameCodec(version));
    p.addLast(new SpdySessionHandler(version, true));
    p.addLast(new SpdyHttpEncoder(version));
    p.addLast(new SpdyHttpDecoder(version, MAX_CONTENT_LENGTH));
    p.addLast(new SpdyHttpResponseStreamIdHandler());
    p.addLast(new SpdyServerHandler());
}

From source file:com.flysoloing.learning.network.netty.spdy.server.SpdyOrHttpHandler.java

License:Apache License

private static void configureHttp1(ChannelHandlerContext ctx) throws Exception {
    ChannelPipeline p = ctx.pipeline();
    p.addLast(new HttpServerCodec());
    p.addLast(new HttpObjectAggregator(MAX_CONTENT_LENGTH));
    p.addLast(new SpdyServerHandler());
}

From source file:com.flysoloing.learning.network.netty.spdy.server.SpdyServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    p.addLast(sslCtx.newHandler(ch.alloc()));
    // Negotiates with the browser if SPDY or HTTP is going to be used
    p.addLast(new SpdyOrHttpHandler());
}

From source file:com.flysoloing.learning.network.netty.telnet.TelnetServerInitializer.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()));
    }//  ww  w .  ja  va  2s  .  c  o  m

    // Add the text line codec combination first,
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    // the encoder and decoder are static as these are sharable
    pipeline.addLast(DECODER);
    pipeline.addLast(ENCODER);

    // and then business logic.
    pipeline.addLast(SERVER_HANDLER);
}

From source file:com.gdut.dongjun.service.net_server.initializer.impl.LowVoltageServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {

    super.initChannel(ch);
    ChannelPipeline p = ch.pipeline();
    p.addLast(new Decoder());
    p.addLast(new Encoder());
    p.addLast(dataReceiver);/* ww  w  .j  av a 2 s. c  o m*/
    // ********************************************************
    // inbound
    // ????
    // ********************************************************

}

From source file:com.gdut.dongjun.service.net_server.initializer.ServerInitializer.java

License:Apache License

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

    ChannelPipeline p = ch.pipeline();
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));// SSL
    }//from  w  ww. ja v a2  s  .co  m
}

From source file:com.gdut.Netty_testing.dongjun.client.ClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc(), Client.HOST, Client.PORT));
    }//from   w w w.j a  v a2 s. c  o m

    p.addLast(new Decoder());
    p.addLast(new Encoder());

    p.addLast(new ClientInBoundHandler());
}