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(EventExecutorGroup group, ChannelHandler... handlers);

Source Link

Document

Inserts ChannelHandler s at the last position of this pipeline.

Usage

From source file:com.mstfdryl.nettychat.ChatClientInitializer.java

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

    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

    pipeline.addLast("handler", new ChatClientHandler());
}

From source file:com.mylearn.netty.sample.websocket.client.WebSocketClient.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//  w w  w  . j av  a2  s.  c  o m
        Bootstrap b = new Bootstrap();
        String protocol = uri.getScheme();
        if (!"ws".equals(protocol)) {
            throw new IllegalArgumentException("Unsupported protocol: " + protocol);
        }

        HttpHeaders customHeaders = new DefaultHttpHeaders();
        customHeaders.add("MyHeader", "MyValue");

        // 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, customHeaders));

        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("http-codec", new HttpClientCodec());
                pipeline.addLast("aggregator", new HttpObjectAggregator(8192));
                pipeline.addLast("ws-handler", handler);
            }
        });

        System.out.println("WebSocket Client connecting");
        Channel ch = b.connect(uri.getHost(), uri.getPort()).sync().channel();
        handler.handshakeFuture().sync();

        // Send 10 messages and wait for responses
        System.out.println("WebSocket Client sending message");
        for (int i = 0; i < 10; i++) {
            ch.writeAndFlush(new TextWebSocketFrame("Message #" + i));
        }

        // Ping
        System.out.println("WebSocket Client sending ping");
        ch.writeAndFlush(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));

        // Close
        System.out.println("WebSocket Client sending close");
        ch.writeAndFlush(new CloseWebSocketFrame());

        // WebSocketClientHandler will close the connection when the server
        // responds to the CloseWebSocketFrame.
        ch.closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.netiq.websockify.PortUnificationHandler.java

License:Apache License

private void enableSsl(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.getPipeline();

    Logger.getLogger(PortUnificationHandler.class.getName())
            .fine("SSL request from " + ctx.getChannel().getRemoteAddress() + ".");

    SSLEngine engine = WebsockifySslContext.getInstance(keystore, keystorePassword).getServerContext()
            .createSSLEngine();//  w  w  w .  j a v a 2  s .  c o  m
    engine.setUseClientMode(false);

    p.addLast("ssl", new SslHandler(engine));
    p.addLast("unificationA", new PortUnificationHandler(cf, resolver, SSLSetting.OFF, keystore,
            keystorePassword, webDirectory, ctx));
    p.remove(this);
}

From source file:com.netiq.websockify.PortUnificationHandler.java

License:Apache License

private void switchToWebsocketProxy(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.getPipeline();

    Logger.getLogger(PortUnificationHandler.class.getName())
            .fine("Websocket proxy request from " + ctx.getChannel().getRemoteAddress() + ".");

    p.addLast("decoder", new HttpRequestDecoder());
    p.addLast("aggregator", new HttpChunkAggregator(65536));
    p.addLast("encoder", new HttpResponseEncoder());
    p.addLast("chunkedWriter", new ChunkedWriteHandler());
    p.addLast("handler", new WebsockifyProxyHandler(cf, resolver, webDirectory));
    p.remove(this);
}

From source file:com.netiq.websockify.PortUnificationHandler.java

License:Apache License

private void switchToFlashPolicy(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.getPipeline();

    Logger.getLogger(PortUnificationHandler.class.getName())
            .fine("Flash policy request from " + ctx.getChannel().getRemoteAddress() + ".");

    p.addLast("flash", new FlashPolicyHandler());

    p.remove(this);
}

From source file:com.netiq.websockify.PortUnificationHandler.java

License:Apache License

private void switchToDirectProxy(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.getPipeline();

    Logger.getLogger(PortUnificationHandler.class.getName())
            .fine("Direct proxy request from " + ctx.getChannel().getRemoteAddress() + ".");

    p.addLast("proxy", new DirectProxyHandler(ctx.getChannel(), cf, resolver));

    p.remove(this);
}

From source file:com.nettyhttpserver.server.NettyServerInitializer.java

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();
    p.addLast("codec", new HttpServerCodec());
    p.addLast("traffic",
            new NettyChannelTrafficShapingHandler(AbstractTrafficShapingHandler.DEFAULT_CHECK_INTERVAL));
    p.addLast("handler", new NettyServerHandler());
}

From source file:com.ning.http.client.providers.netty_4.NettyAsyncHttpProvider.java

License:Apache License

void configureNetty() {
    Map<String, ChannelOption<Object>> optionMap = new HashMap<String, ChannelOption<Object>>();
    for (Field field : ChannelOption.class.getDeclaredFields()) {
        if (field.getType().isAssignableFrom(ChannelOption.class)) {
            field.setAccessible(true);//  w ww.j  av  a 2s .  c  o m
            try {
                optionMap.put(field.getName(), (ChannelOption<Object>) field.get(null));
            } catch (IllegalAccessException ex) {
                throw new Error(ex);
            }
        }
    }

    if (asyncHttpProviderConfig != null) {
        for (Entry<String, Object> entry : asyncHttpProviderConfig.propertiesSet()) {
            ChannelOption<Object> key = optionMap.get(entry.getKey());
            Object value = entry.getValue();
            plainBootstrap.option(key, value);
            webSocketBootstrap.option(key, value);
            secureBootstrap.option(key, value);
            secureWebSocketBootstrap.option(key, value);
        }
    }

    plainBootstrap.handler(createPlainPipelineFactory());
    // DefaultChannelFuture.setUseDeadLockChecker(false);

    if (asyncHttpProviderConfig != null) {
        executeConnectAsync = asyncHttpProviderConfig.isAsyncConnect();
        if (!executeConnectAsync) {
            // DefaultChannelFuture.setUseDeadLockChecker(true);
        }
    }

    webSocketBootstrap.handler(new ChannelInitializer() {
        /* @Override */
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("ws-decoder", new HttpResponseDecoder());
            pipeline.addLast("ws-encoder", new HttpRequestEncoder());
            pipeline.addLast("httpProcessor", NettyAsyncHttpProvider.this);
        }
    });
}

From source file:com.ning.http.client.providers.netty_4.NettyAsyncHttpProvider.java

License:Apache License

protected ChannelInitializer createPlainPipelineFactory() {
    return new ChannelInitializer() {

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

            pipeline.addLast(HTTP_HANDLER, newHttpClientCodec());

            if (config.getRequestCompressionLevel() > 0) {
                pipeline.addLast("deflater", new HttpContentCompressor(config.getRequestCompressionLevel()));
            }//from  w w w . j a  v  a 2  s . com

            if (config.isCompressionEnabled()) {
                pipeline.addLast("inflater", new HttpContentDecompressor());
            }
            pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
            pipeline.addLast("httpProcessor", NettyAsyncHttpProvider.this);
        }
    };
}

From source file:com.ning.http.client.providers.netty_4.NettyAsyncHttpProvider.java

License:Apache License

void constructSSLPipeline(final NettyConnectListener<?> cl) {

    secureBootstrap.handler(new ChannelInitializer() {
        /* @Override */
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();

            try {
                pipeline.addLast(SSL_HANDLER, new SslHandler(createSSLEngine()));
            } catch (Throwable ex) {
                abort(cl.future(), ex);//from   ww  w . j  a  v  a  2s . c  o  m
            }

            pipeline.addLast(HTTP_HANDLER, newHttpClientCodec());

            if (config.isCompressionEnabled()) {
                pipeline.addLast("inflater", new HttpContentDecompressor());
            }
            pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
            pipeline.addLast("httpProcessor", NettyAsyncHttpProvider.this);
        }
    });

    secureWebSocketBootstrap.handler(new ChannelInitializer() {

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

            try {
                pipeline.addLast(SSL_HANDLER, new SslHandler(createSSLEngine()));
            } catch (Throwable ex) {
                abort(cl.future(), ex);
            }

            pipeline.addLast("ws-decoder", new HttpResponseDecoder());
            pipeline.addLast("ws-encoder", new HttpRequestEncoder());
            pipeline.addLast("httpProcessor", NettyAsyncHttpProvider.this);
        }
    });
}