Example usage for io.netty.handler.codec.mqtt MqttDecoder MqttDecoder

List of usage examples for io.netty.handler.codec.mqtt MqttDecoder MqttDecoder

Introduction

In this page you can find the example usage for io.netty.handler.codec.mqtt MqttDecoder MqttDecoder.

Prototype

public MqttDecoder(int maxBytesInMessage) 

Source Link

Usage

From source file:io.vertx.mqtt.impl.MqttClientImpl.java

License:Apache License

private void initChannel(ChannelPipeline pipeline) {

    // add into pipeline netty's (en/de)coder
    pipeline.addBefore("handler", "mqttEncoder", MqttEncoder.INSTANCE);

    if (this.options.getMaxMessageSize() > 0) {
        pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder(this.options.getMaxMessageSize()));
    } else {/*from   w ww .j av  a 2s . c om*/
        // max message size not set, so the default from Netty MQTT codec is used
        pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder());
    }

    if (this.options.isAutoKeepAlive() && this.options.getKeepAliveTimeSeconds() != 0) {

        pipeline.addBefore("handler", "idle",
                new IdleStateHandler(0, this.options.getKeepAliveTimeSeconds(), 0));
        pipeline.addBefore("handler", "keepAliveHandler", new ChannelDuplexHandler() {

            @Override
            public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

                if (evt instanceof IdleStateEvent) {
                    IdleStateEvent e = (IdleStateEvent) evt;
                    if (e.state() == IdleState.WRITER_IDLE) {
                        ping();
                    }
                }
            }
        });
    }
}

From source file:io.vertx.mqtt.impl.MqttServerImpl.java

License:Apache License

private void initChannel(ChannelPipeline pipeline) {

    pipeline.addBefore("handler", "mqttEncoder", MqttEncoder.INSTANCE);
    if (this.options.getMaxMessageSize() > 0) {
        pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder(this.options.getMaxMessageSize()));
    } else {//from  ww w.  j  a v  a2s.  c  o m
        // max message size not set, so the default from Netty MQTT codec is used
        pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder());
    }

    // adding the idle state handler for timeout on CONNECT packet
    pipeline.addBefore("handler", "idle", new IdleStateHandler(this.options.timeoutOnConnect(), 0, 0));
    pipeline.addBefore("handler", "timeoutOnConnect", new ChannelDuplexHandler() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

            if (evt instanceof IdleStateEvent) {
                IdleStateEvent e = (IdleStateEvent) evt;
                if (e.state() == IdleState.READER_IDLE) {
                    // as MQTT 3.1.1 describes, if no packet is sent after a "reasonable" time (here CONNECT timeout)
                    // the connection is closed
                    ctx.channel().close();
                }
            }
        }
    });
}

From source file:org.apache.activemq.artemis.core.protocol.mqtt.MQTTProtocolManager.java

License:Apache License

@Override
public void addChannelHandlers(ChannelPipeline pipeline) {
    pipeline.addLast(MqttEncoder.INSTANCE);
    pipeline.addLast(new MqttDecoder(MQTTUtil.MAX_MESSAGE_SIZE));

    pipeline.addLast(new MQTTProtocolHandler(server, this));
}

From source file:org.thingsboard.server.transport.mqtt.MqttTransportServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    SslHandler sslHandler = null;/*from w  ww .  j av a2s  .  co  m*/
    if (context.getSslHandlerProvider() != null) {
        sslHandler = context.getSslHandlerProvider().getSslHandler();
        pipeline.addLast(sslHandler);
        context.setSslHandler(sslHandler);
    }
    pipeline.addLast("decoder", new MqttDecoder(context.getMaxPayloadSize()));
    pipeline.addLast("encoder", MqttEncoder.INSTANCE);

    MqttTransportHandler handler = new MqttTransportHandler(context);

    pipeline.addLast(handler);
    ch.closeFuture().addListener(handler);
}