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:org.emsg.smart_connector.netty.acceptor.NettyAcceptor.java

License:Open Source License

private void initializePlainTCPTransport(final NettyMQTTHandler handler, IConfig props) throws IOException {
    final SmartConnectorIdleTimeoutHandler timeoutHandler = new SmartConnectorIdleTimeoutHandler();
    String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME);
    int port = Integer.parseInt(props.getProperty(BrokerConstants.PORT_PROPERTY_NAME));
    initFactory(host, port, new PipelineInitializer() {
        @Override/*from ww w.j  ava  2  s .  c  o  m*/
        void init(ChannelPipeline pipeline) {
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:org.emsg.smart_connector.netty.acceptor.NettyAcceptor.java

License:Open Source License

private void initializeWebSocketTransport(final NettyMQTTHandler handler, IConfig props) throws IOException {
    String webSocketPortProp = props.getProperty(BrokerConstants.WEB_SOCKET_PORT_PROPERTY_NAME);
    if (webSocketPortProp == null) {
        //Do nothing no WebSocket configured
        LOG.info("WebSocket is disabled");
        return;/*from w ww.  j av  a  2 s  . c om*/
    }
    int port = Integer.parseInt(webSocketPortProp);

    final SmartConnectorIdleTimeoutHandler timeoutHandler = new SmartConnectorIdleTimeoutHandler();

    String host = props.getProperty(BrokerConstants.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("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
            pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:org.emsg.smart_connector.netty.acceptor.NettyAcceptor.java

License:Open Source License

private void initializeSSLTCPTransport(final NettyMQTTHandler handler, IConfig props,
        final SSLContext sslContext) throws IOException {
    String sslPortProp = props.getProperty(BrokerConstants.SSL_PORT_PROPERTY_NAME);
    if (sslPortProp == null) {
        //Do nothing no SSL configured
        LOG.info("SSL is disabled");
        return;//from   w  w  w  .  ja v  a  2s .c  o  m
    }

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

    final SmartConnectorIdleTimeoutHandler timeoutHandler = new SmartConnectorIdleTimeoutHandler();
    String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME);
    initFactory(host, sslPort, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) throws Exception {
            pipeline.addLast("ssl", createSslHandler(sslContext));
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:org.emsg.smart_connector.netty.acceptor.NettyAcceptor.java

License:Open Source License

private void initializeWSSTransport(final NettyMQTTHandler handler, IConfig props, final SSLContext sslContext)
        throws IOException {
    String sslPortProp = props.getProperty(BrokerConstants.WSS_PORT_PROPERTY_NAME);
    if (sslPortProp == null) {
        //Do nothing no SSL configured
        LOG.info("SSL is disabled");
        return;/*  w w w  . j a  va 2  s  .c o  m*/
    }
    int sslPort = Integer.parseInt(sslPortProp);
    final SmartConnectorIdleTimeoutHandler timeoutHandler = new SmartConnectorIdleTimeoutHandler();
    String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME);
    initFactory(host, sslPort, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) throws Exception {
            pipeline.addLast("ssl", createSslHandler(sslContext));
            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("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
            pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:org.jmqtt.broker.acceptor.NettyAcceptor.java

License:Open Source License

private void initializePlainTCPTransport(final NettyMQTTHandler handler, AcceptorProperties props)
        throws IOException {
    final IdleTimeoutHandler timeoutHandler = new IdleTimeoutHandler();
    String host = props.getHost();
    boolean mqttEnable = props.getMqttEnable();
    if (!mqttEnable) {
        LOG.info("tcp MQTT is disabled");
        return;/*from   w  w w.  j a  v  a2s  . c o m*/
    }
    int port = props.getMqttPort();
    initFactory(host, port, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) {
            pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, props.getConnectTimeout()));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            //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:org.jmqtt.broker.acceptor.NettyAcceptor.java

License:Open Source License

private void initializeWebSocketTransport(final NettyMQTTHandler handler, AcceptorProperties props)
        throws IOException {
    if (!props.getWsEnable()) {
        //Do nothing no WebSocket configured
        LOG.info("WebSocket is disabled");
        return;//from   w  w  w.j a  v a2s .  c o m
    }
    int port = props.getWsPort();

    final IdleTimeoutHandler timeoutHandler = new IdleTimeoutHandler();

    String host = props.getHost();
    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_SUBPROTOCOL_CSV_LIST));
            pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
            pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
            pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, props.getConnectTimeout()));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            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:org.jmqtt.broker.acceptor.NettyAcceptor.java

License:Open Source License

private void initializeSSLTCPTransport(final NettyMQTTHandler handler, AcceptorProperties props,
        final SSLContext sslContext) throws IOException {
    if (props.getMqttsEnable()) {
        //Do nothing no SSL configured
        LOG.info("SSL MQTT is disabled");
        return;/* w  ww  . jav  a2s .  c  o  m*/
    }

    int sslPort = props.getMqttsPort();
    LOG.info("Starting SSL on port {}", sslPort);

    final IdleTimeoutHandler timeoutHandler = new IdleTimeoutHandler();
    String host = props.getHost();
    final boolean needsClientAuth = props.getNeedsClientAuth();
    initFactory(host, sslPort, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) throws Exception {
            pipeline.addLast("ssl", createSslHandler(sslContext, needsClientAuth));
            pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, props.getConnectTimeout()));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            //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:org.jmqtt.broker.acceptor.NettyAcceptor.java

License:Open Source License

private void initializeWSSTransport(final NettyMQTTHandler handler, AcceptorProperties props,
        final SSLContext sslContext) throws IOException {
    if (!props.getWssEnable()) {
        //Do nothing no SSL configured
        LOG.info("SSL websocket is disabled");
        return;//from   w  ww .j  a  v  a 2 s  . c o m
    }
    int sslPort = props.getWssPort();
    final IdleTimeoutHandler timeoutHandler = new IdleTimeoutHandler();
    String host = props.getHost();
    final boolean needsClientAuth = props.getNeedsClientAuth();
    initFactory(host, sslPort, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) throws Exception {
            pipeline.addLast("ssl", createSslHandler(sslContext, needsClientAuth));
            pipeline.addLast("httpEncoder", new HttpResponseEncoder());
            pipeline.addLast("httpDecoder", new HttpRequestDecoder());
            pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
            pipeline.addLast("webSocketHandler",
                    new WebSocketServerProtocolHandler("/mqtt", MQTT_SUBPROTOCOL_CSV_LIST));
            pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
            pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
            pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, props.getConnectTimeout()));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            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:org.legacy.network.Pipeline.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addFirst("channel-handler", new NetworkHandler());
}

From source file:org.msgpack.rpc.loop.netty.NettyTcpClientTransport.java

License:Apache License

NettyTcpClientTransport(TcpClientConfig config, Session session, NettyEventLoop loop,
        Class<? extends RpcMessageHandler> rpcHandlerClass) {
    super(config, session);

    try {/*from   ww w. j  av a  2s.  com*/
        handler = rpcHandlerClass.getConstructor(Session.class).newInstance(session);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    //      handler = new RpcMessageHandlerEx(session);
    eventLoop = loop;
    bootstrap = new Bootstrap().group(group);
    bootstrap.channel(NioSocketChannel.class);
    final NettyTcpClientTransport trans = this;
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            if (isSocks) {
                p.addFirst("socks-handler", new SocksProxyHandler(session, trans));
                p.addFirst("socks-encode", new SocksMessageEncoder());
                p.addFirst("socks-decode", new SocksInitResponseDecoder());
            }
            p.addLast("msgpack-decode-stream", new MessagePackStreamDecoder(eventLoop.getMessagePack()));
            p.addLast("msgpack-encode", new MessagePackEncoder(eventLoop.getMessagePack()));
            p.addLast("message", new MessageHandler(handler, trans));
        }

    });
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
}