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.github.spapageo.jannel.client.JannelClientTest.java

License:Open Source License

@Test(expected = PrematureChannelClosureException.class)
public void testIdentifyCloseChannelOnFailure() throws Exception {
    Channel channel = mock(Channel.class, Answers.RETURNS_SMART_NULLS.get());
    mockWriteHandler = mock(ChannelHandler.class);

    DefaultChannelPromise completedFuture = new DefaultChannelPromise(channel);
    completedFuture.setSuccess();//from   w  w w  . j a  va 2s .co m

    DefaultChannelPromise failedFuture = new DefaultChannelPromise(channel);
    failedFuture.setFailure(new PrematureChannelClosureException("test"));

    ChannelPipeline channelPipeline = mock(ChannelPipeline.class);
    when(channelPipeline.addLast(anyString(), any(ChannelHandler.class))).thenReturn(channelPipeline);
    when(channelPipeline.addLast(any(EventExecutorGroup.class), anyString(), any(ChannelHandler.class)))
            .thenReturn(channelPipeline);
    when(channel.pipeline()).thenReturn(channelPipeline);
    when(channel.isActive()).thenReturn(true);
    when(channel.writeAndFlush(any())).thenReturn(failedFuture);
    when(channel.close()).thenReturn(completedFuture);

    when(bootstrap.connect(anyString(), anyInt())).thenReturn(completedFuture);

    ClientSessionConfiguration configuration = new ClientSessionConfiguration();

    jannelClient.identify(configuration, null);
}

From source file:com.github.wangshuwei5.client.NettyClientInitializer.java

License:Apache License

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

    SSLEngine engine = null;/*from   w w  w . j a v  a 2  s .co  m*/
    if (SSLMODE.CA.toString().equals(tlsMode)) {
        engine = NettySslContextFactory.getClientContext(tlsMode, null,
                System.getProperty("user.dir") + "/src/main/resources/cChat.jks").createSSLEngine();
    } else if (SSLMODE.CSA.toString().equals(tlsMode)) {
        engine = NettySslContextFactory
                .getClientContext(tlsMode, System.getProperty("user.dir") + "/src/main/resources/cChat.jks",
                        System.getProperty("user.dir") + "/src/main/resources/cChat.jks")
                .createSSLEngine();

    } else {
        System.err.println("ERROR : " + tlsMode);
        System.exit(-1);
    }
    engine.setUseClientMode(true);
    pipeline.addLast("ssl", new SslHandler(engine));

    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

    ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50));
    ch.pipeline().addLast("LoginAuthHandler", new LoginAuthReqHandler());
    ch.pipeline().addLast("HeartBeatHandler", new HeartBeatReqHandler());
}

From source file:com.github.wangshuwei5.server.NettyServerInitializer.java

License:Apache License

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

    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.
    ////w  ww . j a  v  a  2 s . co m
    // Read SecureChatSslContextFactory
    // if you need client certificate authentication.

    SSLEngine engine = null;
    if (SSLMODE.CA.toString().equals(tlsMode)) {
        engine = NettySslContextFactory.getServerContext(tlsMode,
                System.getProperty("user.dir") + "/src/main/resources/sChat.jks", null).createSSLEngine();
    } else if (SSLMODE.CSA.toString().equals(tlsMode)) {
        engine = NettySslContextFactory
                .getServerContext(tlsMode, System.getProperty("user.dir") + "/src/main/resources/sChat.jks",
                        System.getProperty("user.dir") + "/src/main/resources/sChat.jks")
                .createSSLEngine();

    } else {
        System.err.println("ERROR : " + tlsMode);
        System.exit(-1);
    }
    engine.setUseClientMode(false);

    // Client auth
    if (SSLMODE.CSA.toString().equals(tlsMode))
        engine.setNeedClientAuth(true);
    pipeline.addLast("ssl", new SslHandler(engine));

    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

    ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50));
    ch.pipeline().addLast(new LoginAuthRespHandler());
    ch.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler());
}

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);// w  ww  .  j  a  va2  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;/* ww  w.  jav a2  s .  co 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;//ww w.java 2 s . c o 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;//from w w  w. java  2 s .  co  m
    }
    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.googlecode.protobuf.pro.duplex.client.DuplexTcpClientPipelineFactory.java

License:Apache License

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

    RpcSSLContext ssl = getSslContext();
    if (ssl != null) {
        p.addLast(Handler.SSL, new SslHandler(ssl.createClientEngine()));
    }//from  w  w w . j a va  2s.  co  m

    p.addLast(Handler.FRAME_DECODER, new ProtobufVarint32FrameDecoder());
    p.addLast(Handler.PROTOBUF_DECODER, new ProtobufDecoder(DuplexProtocol.WirePayload.getDefaultInstance(),
            getWirelinePayloadExtensionRegistry()));

    p.addLast(Handler.FRAME_ENCODER, new ProtobufVarint32LengthFieldPrepender());
    p.addLast(Handler.PROTOBUF_ENCODER, new ProtobufEncoder());

    // the connectResponseHandler is swapped after the client connection
    // handshake with the RpcClient for the Channel
    p.addLast(Handler.CLIENT_CONNECT, new ClientConnectResponseHandler());
}

From source file:com.googlecode.protobuf.pro.duplex.server.DuplexTcpServerPipelineFactory.java

License:Apache License

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

    if (getSslContext() != null) {
        p.addLast(Handler.SSL, new SslHandler(getSslContext().createServerEngine()));
    }//from  w w w. j  av  a 2s .c  o  m

    p.addLast(Handler.FRAME_DECODER, new ProtobufVarint32FrameDecoder());
    p.addLast(Handler.PROTOBUF_DECODER, new ProtobufDecoder(DuplexProtocol.WirePayload.getDefaultInstance(),
            getWirelinePayloadExtensionRegistry()));

    p.addLast(Handler.FRAME_ENCODER, new ProtobufVarint32LengthFieldPrepender());
    p.addLast(Handler.PROTOBUF_ENCODER, new ProtobufEncoder());

    p.addLast(Handler.SERVER_CONNECT, connectRequestHandler); // one instance shared by all channels

    if (log.isDebugEnabled()) {
        log.debug("initChannel " + ch);
    }
}

From source file:com.gxkj.demo.netty.socksproxy.DirectClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline channelPipeline = socketChannel.pipeline();
    channelPipeline.addLast(DirectClientHandler.getName(), new DirectClientHandler(promise));
}