Example usage for io.netty.channel ChannelPipeline addFirst

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

Introduction

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

Prototype

ChannelPipeline addFirst(EventExecutorGroup group, ChannelHandler... handlers);

Source Link

Document

Inserts ChannelHandler s at the first position of this pipeline.

Usage

From source file:com.baidu.jprotobuf.pbrpc.transport.RpcClientPipelineinitializer.java

License:Apache License

/**
 * @brief ??/*from w ww.  ja  v a  2  s.  c o  m*/
 * @return ChannelPipeline
 * @throws Exception
 * @author songhuiqing
 * @date 2013/03/07 11:14:53
 */
@Override
protected void initChannel(Channel ch) throws Exception {
    LOG.log(Level.FINEST, "begin process RPC server response to client handler");
    ChannelPipeline channelPipe = ch.pipeline();
    int idleTimeout = this.rpcClient.getRpcClientOptions().getIdleTimeout();
    channelPipe.addFirst(RPC_CHANNEL_STATE_AWARE_HANDLER,
            new IdleStateHandler(idleTimeout, idleTimeout, idleTimeout));
    channelPipe.addFirst(RPC_CHANNEL_IDLE_HANDLER, new RpcServerChannelIdleHandler());

    // check if need to compress for data and attachment
    channelPipe.addFirst(COMPRESS, new RpcDataPackageCompressHandler());
    // encode RpcDataPackage to byte array
    channelPipe.addFirst(CLIENT_ENCODER,
            new RpcDataPackageEncoder(rpcClient.getRpcClientOptions().getChunkSize()));

    // receive data from server
    // receive byte array to encode to RpcDataPackage
    channelPipe.addLast(CLIENT_DECODER, new RpcDataPackageDecoder(-1));
    // do uncompress handle
    channelPipe.addLast(UNCOMPRESS, new RpcDataPackageUnCompressHandler());
    // do client handler
    channelPipe.addLast(CLIENT_HANDLER, new RpcClientServiceHandler(rpcClient));

}

From source file:com.baidu.jprotobuf.pbrpc.transport.RpcServerPipelineInitializer.java

License:Apache License

@Override
protected void initChannel(Channel ch) throws Exception {
    LOG.log(Level.FINE, "begin process RPC server handler");
    ChannelPipeline channelPipe = ch.pipeline();
    // receive request data
    channelPipe.addLast(RPC_CHANNEL_STATE_AWARE_HANDLER,
            new IdleStateHandler(this.rpcServerOptions.getKeepAliveTime(),
                    this.rpcServerOptions.getKeepAliveTime(), this.rpcServerOptions.getKeepAliveTime()));

    channelPipe.addLast(RPC_CHANNEL_IDLE_HANDLER, new RpcServerChannelIdleHandler());

    RpcDataPackageDecoder rpcDataPackageDecoder = new RpcDataPackageDecoder(
            this.rpcServerOptions.getChunkPackageTimeout());
    rpcDataPackageDecoderList.add(rpcDataPackageDecoder);
    // receive byte array to encode to RpcDataPackage
    channelPipe.addLast(DECODER, rpcDataPackageDecoder);
    // do uncompress handle
    channelPipe.addLast(UNCOMPRESS, new RpcDataPackageUnCompressHandler());
    // to process RPC service handler of request object RpcDataPackage and
    // return new RpcDataPackage
    channelPipe.addLast(RPC_SERVER_HANDLER, new RpcServiceHandler(this.rpcServiceRegistry));

    // response back
    // check if need to compress for data and attachment
    channelPipe.addFirst(COMPRESS, new RpcDataPackageCompressHandler());
    // encode RpcDataPackage to byte array
    channelPipe.addFirst(SERVER_DATA_PACK, new RpcDataPackageEncoder());

}

From source file:com.friz.update.network.UpdateSessionContext.java

License:Open Source License

public void writeSuccess(int status) {
    channel.writeAndFlush(new UpdateResponseEvent(status)).addListener(new ChannelFutureListener() {
        @Override/*from  www .  j a  v a2s.  c o m*/
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                ChannelPipeline p = future.channel().pipeline();
                p.remove(UpdateInitDecoder.class);
                p.remove(UpdateInitEncoder.class);

                p.addFirst(XorEncoder.class.getName(), new XorEncoder());
                p.addFirst(UpdateEncoder.class.getName(), new UpdateEncoder());
                p.addFirst(UpdateDecoder.class.getName(), new UpdateDecoder());

                handshakeComplete = true;
            }
        }
    });
}

From source file:com.google.cloud.pubsub.proxy.moquette.NettyAcceptor.java

License:Open Source License

private void initializePlainTcpTransport(IMessaging messaging, Properties props) throws IOException {
    final NettyMQTTHandler mqttHandler = new NettyMQTTHandler();
    final PubsubHandler handler = new PubsubHandler(pubsub, mqttHandler);
    handler.setMessaging(messaging);//from  www.  j ava  2  s. c  o  m
    String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
    int port = Integer.parseInt(props.getProperty(Constants.PORT_PROPERTY_NAME));
    initFactory(host, port, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) {
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimeoutHandler());
            //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:com.google.cloud.pubsub.proxy.moquette.NettyAcceptor.java

License:Open Source License

private void initializeWebSocketTransport(IMessaging messaging, Properties props) throws IOException {
    String webSocketPortProp = props.getProperty(Constants.WEB_SOCKET_PORT_PROPERTY_NAME);
    if (webSocketPortProp == null) {
        //Do nothing no WebSocket configured
        LOG.info("WebSocket is disabled");
        return;/*from  w  w w.j  av a  2 s . c o m*/
    }
    int port = Integer.parseInt(webSocketPortProp);

    final NettyMQTTHandler mqttHandler = new NettyMQTTHandler();
    final PubsubHandler handler = new PubsubHandler(pubsub, mqttHandler);
    handler.setMessaging(messaging);

    String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
    initFactory(host, port, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) {
            pipeline.addLast("httpEncoder", new HttpResponseEncoder());
            pipeline.addLast("httpDecoder", new HttpRequestDecoder());
            pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
            pipeline.addLast("webSocketHandler",
                    new WebSocketServerProtocolHandler("/mqtt"/*"/mqtt"*/, "mqttv3.1, mqttv3.1.1"));
            //pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler(null, "mqtt"));
            pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
            pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimeoutHandler());
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:com.google.cloud.pubsub.proxy.moquette.NettyAcceptor.java

License:Open Source License

private void initializeSslTcpTransport(IMessaging messaging, Properties props, final SslHandler sslHandler)
        throws IOException {
    String sslPortProp = props.getProperty(Constants.SSL_PORT_PROPERTY_NAME);
    if (sslPortProp == null) {
        //Do nothing no SSL configured
        LOG.info("SSL is disabled");
        return;//w  w  w  .j av  a 2 s . co m
    }

    int sslPort = Integer.parseInt(sslPortProp);
    LOG.info("Starting SSL on port {}", sslPort);

    final NettyMQTTHandler mqttHandler = new NettyMQTTHandler();
    final PubsubHandler handler = new PubsubHandler(pubsub, mqttHandler);
    handler.setMessaging(messaging);
    String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
    initFactory(host, sslPort, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) throws Exception {
            pipeline.addLast("ssl", sslHandler);
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimeoutHandler());
            //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:com.google.cloud.pubsub.proxy.moquette.NettyAcceptor.java

License:Open Source License

private void initializeWssTransport(IMessaging messaging, Properties props, final SslHandler sslHandler)
        throws IOException {
    String sslPortProp = props.getProperty(Constants.WSS_PORT_PROPERTY_NAME);
    if (sslPortProp == null) {
        //Do nothing no SSL configured
        LOG.info("SSL is disabled");
        return;//  ww w .  j  av  a  2 s . c  om
    }
    int sslPort = Integer.parseInt(sslPortProp);
    final NettyMQTTHandler mqttHandler = new NettyMQTTHandler();
    final PubsubHandler handler = new PubsubHandler(pubsub, mqttHandler);
    handler.setMessaging(messaging);
    String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
    initFactory(host, sslPort, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) throws Exception {
            pipeline.addLast("ssl", sslHandler);
            pipeline.addLast("httpEncoder", new HttpResponseEncoder());
            pipeline.addLast("httpDecoder", new HttpRequestDecoder());
            pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
            pipeline.addLast("webSocketHandler",
                    new WebSocketServerProtocolHandler("/mqtt", "mqttv3.1, mqttv3.1.1"));
            pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
            pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimeoutHandler());
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpBlobStore.java

License:Open Source License

public HttpBlobStore(URI uri, int timeoutMillis, @Nullable final Credentials creds) throws Exception {
    boolean useTls = uri.getScheme().equals("https");
    if (uri.getPort() == -1) {
        int port = useTls ? 443 : 80;
        uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), port, uri.getPath(), uri.getQuery(),
                uri.getFragment());/*from w  ww. jav  a  2s.  com*/
    }
    this.uri = uri;
    final SslContext sslCtx;
    if (useTls) {
        // OpenSsl gives us a > 2x speed improvement on fast networks, but requires netty tcnative
        // to be there which is not available on all platforms and environments.
        SslProvider sslProvider = OpenSsl.isAvailable() ? SslProvider.OPENSSL : SslProvider.JDK;
        sslCtx = SslContextBuilder.forClient().sslProvider(sslProvider).build();
    } else {
        sslCtx = null;
    }
    Bootstrap clientBootstrap = new Bootstrap().channel(NioSocketChannel.class)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeoutMillis).group(eventLoop)
            .remoteAddress(uri.getHost(), uri.getPort());
    downloadChannels = new SimpleChannelPool(clientBootstrap, new ChannelPoolHandler() {
        @Override
        public void channelReleased(Channel ch) {
            ch.pipeline().remove("read-timeout-handler");
        }

        @Override
        public void channelAcquired(Channel ch) {
            ch.pipeline().addFirst("read-timeout-handler", new ReadTimeoutHandler(timeoutMillis));
        }

        @Override
        public void channelCreated(Channel ch) {
            ChannelPipeline p = ch.pipeline();
            p.addFirst("read-timeout-handler", new ReadTimeoutHandler(timeoutMillis));
            if (sslCtx != null) {
                SSLEngine engine = sslCtx.newEngine(ch.alloc());
                engine.setUseClientMode(true);
                p.addFirst(new SslHandler(engine));
            }
            p.addLast(new HttpClientCodec());
            p.addLast(new HttpDownloadHandler(creds));
        }
    });
    uploadChannels = new SimpleChannelPool(clientBootstrap, new ChannelPoolHandler() {
        @Override
        public void channelReleased(Channel ch) {
        }

        @Override
        public void channelAcquired(Channel ch) {
        }

        @Override
        public void channelCreated(Channel ch) {
            ChannelPipeline p = ch.pipeline();
            if (sslCtx != null) {
                SSLEngine engine = sslCtx.newEngine(ch.alloc());
                engine.setUseClientMode(true);
                p.addFirst(new SslHandler(engine));
            }
            p.addLast(new HttpResponseDecoder());
            // The 10KiB limit was chosen at random. We only expect HTTP servers to respond with
            // an error message in the body and that should always be less than 10KiB.
            p.addLast(new HttpObjectAggregator(10 * 1024));
            p.addLast(new HttpRequestEncoder());
            p.addLast(new ChunkedWriteHandler());
            p.addLast(new HttpUploadHandler(creds));
        }
    });
    this.creds = creds;
}

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

License:Apache License

private void upgradeProtocol(ChannelPipeline p, String scheme) throws IOException, GeneralSecurityException {
    if (p.get(HTTP_HANDLER) != null) {
        p.remove(HTTP_HANDLER);//  w w  w .j  a  v a 2  s .  com
    }

    if (isSecure(scheme)) {
        if (p.get(SSL_HANDLER) == null) {
            p.addFirst(HTTP_HANDLER, newHttpClientCodec());
            p.addFirst(SSL_HANDLER, new SslHandler(createSSLEngine()));
        } else {
            p.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec());
        }

    } else {
        p.addFirst(HTTP_HANDLER, newHttpClientCodec());
    }
}

From source file:com.qualys.jserf.SerfClientInitializer.java

License:Apache License

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

    if (log.isDebugEnabled()) {
        pipeline.addFirst("loggingHandler", new LoggingHandler());
    }/*  w ww .  j  av  a 2 s. c o  m*/

    pipeline.addLast("reconnectHandler", new ReconnectClientHandler(channelManger));

    log.debug("Adding ByteArray Encoder");
    pipeline.addLast("bytesEncoder", new ByteArrayEncoder());

    log.debug("Adding SerfClientHandler");
    pipeline.addLast("handler", new SerfClientHandler(extractorManager, messagePack, callBacksBySequence));
}