Example usage for io.netty.handler.codec.mqtt MqttMessage decoderResult

List of usage examples for io.netty.handler.codec.mqtt MqttMessage decoderResult

Introduction

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

Prototype

DecoderResult decoderResult

To view the source code for io.netty.handler.codec.mqtt MqttMessage decoderResult.

Click Source Link

Usage

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

License:Apache License

/**
 * Handle the MQTT message received from the remote MQTT server
 *
 * @param msg Incoming Packet/*from  w  w  w  . j  av a  2s  .  c  o  m*/
 */
synchronized void handleMessage(Object msg) {

    // handling directly native Netty MQTT messages, some of them are translated
    // to the related Vert.x ones for polyglotization
    if (msg instanceof MqttMessage) {

        MqttMessage mqttMessage = (MqttMessage) msg;

        DecoderResult result = mqttMessage.decoderResult();
        if (result.isFailure()) {
            chctx.pipeline().fireExceptionCaught(result.cause());
            return;
        }
        if (!result.isFinished()) {
            chctx.pipeline().fireExceptionCaught(new Exception("Unfinished message"));
            return;
        }

        log.debug(String.format("Incoming packet %s", msg));
        switch (mqttMessage.fixedHeader().messageType()) {

        case CONNACK:

            io.netty.handler.codec.mqtt.MqttConnAckMessage connack = (io.netty.handler.codec.mqtt.MqttConnAckMessage) mqttMessage;

            MqttConnAckMessage mqttConnAckMessage = MqttConnAckMessage.create(
                    connack.variableHeader().connectReturnCode(), connack.variableHeader().isSessionPresent());
            handleConnack(mqttConnAckMessage);
            break;

        case PUBLISH:

            io.netty.handler.codec.mqtt.MqttPublishMessage publish = (io.netty.handler.codec.mqtt.MqttPublishMessage) mqttMessage;
            ByteBuf newBuf = VertxHandler.safeBuffer(publish.payload(), chctx.alloc());

            MqttPublishMessage mqttPublishMessage = MqttPublishMessage.create(
                    publish.variableHeader().messageId(), publish.fixedHeader().qosLevel(),
                    publish.fixedHeader().isDup(), publish.fixedHeader().isRetain(),
                    publish.variableHeader().topicName(), newBuf);
            handlePublish(mqttPublishMessage);
            break;

        case PUBACK:
            handlePuback(((MqttMessageIdVariableHeader) mqttMessage.variableHeader()).messageId());
            break;

        case PUBREC:
            handlePubrec(((MqttMessageIdVariableHeader) mqttMessage.variableHeader()).messageId());
            break;

        case PUBREL:
            handlePubrel(((MqttMessageIdVariableHeader) mqttMessage.variableHeader()).messageId());
            break;

        case PUBCOMP:
            handlePubcomp(((MqttMessageIdVariableHeader) mqttMessage.variableHeader()).messageId());
            break;

        case SUBACK:

            io.netty.handler.codec.mqtt.MqttSubAckMessage unsuback = (io.netty.handler.codec.mqtt.MqttSubAckMessage) mqttMessage;

            MqttSubAckMessage mqttSubAckMessage = MqttSubAckMessage
                    .create(unsuback.variableHeader().messageId(), unsuback.payload().grantedQoSLevels());
            handleSuback(mqttSubAckMessage);
            break;

        case UNSUBACK:
            handleUnsuback(((MqttMessageIdVariableHeader) mqttMessage.variableHeader()).messageId());
            break;

        case PINGRESP:
            handlePingresp();
            break;

        default:

            this.chctx.pipeline()
                    .fireExceptionCaught(new Exception("Wrong message type " + msg.getClass().getName()));
            break;
        }

    } else {

        this.chctx.pipeline().fireExceptionCaught(new Exception("Wrong message type"));
    }
}

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

License:Apache License

/**
 * Handle the MQTT message received by the remote MQTT client
 *
 * @param msg message to handle/* w  w  w.  j  a  v  a 2  s . co  m*/
 */
synchronized void handleMessage(Object msg) {

    // handling directly native Netty MQTT messages because we don't need to
    // expose them at higher level (so no need for polyglotization)
    if (msg instanceof io.netty.handler.codec.mqtt.MqttMessage) {

        io.netty.handler.codec.mqtt.MqttMessage mqttMessage = (io.netty.handler.codec.mqtt.MqttMessage) msg;

        DecoderResult result = mqttMessage.decoderResult();
        if (result.isFailure()) {
            channel.pipeline().fireExceptionCaught(result.cause());
            return;
        }
        if (!result.isFinished()) {
            channel.pipeline().fireExceptionCaught(new Exception("Unfinished message"));
            return;
        }

        switch (mqttMessage.fixedHeader().messageType()) {

        case CONNECT:
            handleConnect((MqttConnectMessage) msg);
            break;

        case PUBACK:

            io.netty.handler.codec.mqtt.MqttPubAckMessage mqttPubackMessage = (io.netty.handler.codec.mqtt.MqttPubAckMessage) mqttMessage;
            this.handlePuback(mqttPubackMessage.variableHeader().messageId());
            break;

        case PUBREC:

            int pubrecMessageId = ((io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) mqttMessage
                    .variableHeader()).messageId();
            this.handlePubrec(pubrecMessageId);
            break;

        case PUBREL:

            int pubrelMessageId = ((io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) mqttMessage
                    .variableHeader()).messageId();
            this.handlePubrel(pubrelMessageId);
            break;

        case PUBCOMP:

            int pubcompMessageId = ((io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) mqttMessage
                    .variableHeader()).messageId();
            this.handlePubcomp(pubcompMessageId);
            break;

        case PINGREQ:

            this.handlePingreq();
            break;

        case DISCONNECT:

            this.handleDisconnect();
            break;

        default:

            this.channel.pipeline()
                    .fireExceptionCaught(new Exception("Wrong message type " + msg.getClass().getName()));
            break;

        }

        // handling mapped Vert.x MQTT messages (from Netty ones) because they'll be provided
        // to the higher layer (so need for ployglotization)
    } else {

        if (msg instanceof MqttSubscribeMessage) {

            this.handleSubscribe((MqttSubscribeMessage) msg);

        } else if (msg instanceof MqttUnsubscribeMessage) {

            this.handleUnsubscribe((MqttUnsubscribeMessage) msg);

        } else if (msg instanceof MqttPublishMessage) {

            this.handlePublish((MqttPublishMessage) msg);

        } else {

            this.channel.pipeline().fireExceptionCaught(new Exception("Wrong message type"));
        }
    }
}

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

License:Apache License

/**
 * Handle the MQTT message received by the remote MQTT client
 *
 * @param msg message to handle//from   ww w  .j av a 2  s  .  co m
 */
synchronized void handleMessage(Object msg) {

    // handling directly native Netty MQTT messages, some of them are translated
    // to the related Vert.x ones for polyglotization
    if (msg instanceof io.netty.handler.codec.mqtt.MqttMessage) {

        io.netty.handler.codec.mqtt.MqttMessage mqttMessage = (io.netty.handler.codec.mqtt.MqttMessage) msg;

        DecoderResult result = mqttMessage.decoderResult();
        if (result.isFailure()) {
            chctx.pipeline().fireExceptionCaught(result.cause());
            return;
        }
        if (!result.isFinished()) {
            chctx.pipeline().fireExceptionCaught(new Exception("Unfinished message"));
            return;
        }

        switch (mqttMessage.fixedHeader().messageType()) {

        case CONNECT:
            handleConnect((MqttConnectMessage) msg);
            break;

        case SUBSCRIBE:

            io.netty.handler.codec.mqtt.MqttSubscribeMessage subscribe = (io.netty.handler.codec.mqtt.MqttSubscribeMessage) mqttMessage;

            MqttSubscribeMessage mqttSubscribeMessage = MqttSubscribeMessage
                    .create(subscribe.variableHeader().messageId(), subscribe.payload().topicSubscriptions());
            this.handleSubscribe(mqttSubscribeMessage);
            break;

        case UNSUBSCRIBE:

            io.netty.handler.codec.mqtt.MqttUnsubscribeMessage unsubscribe = (io.netty.handler.codec.mqtt.MqttUnsubscribeMessage) mqttMessage;

            MqttUnsubscribeMessage mqttUnsubscribeMessage = MqttUnsubscribeMessage
                    .create(unsubscribe.variableHeader().messageId(), unsubscribe.payload().topics());
            this.handleUnsubscribe(mqttUnsubscribeMessage);
            break;

        case PUBLISH:

            io.netty.handler.codec.mqtt.MqttPublishMessage publish = (io.netty.handler.codec.mqtt.MqttPublishMessage) mqttMessage;
            ByteBuf newBuf = VertxHandler.safeBuffer(publish.payload(), this.chctx.alloc());

            MqttPublishMessage mqttPublishMessage = MqttPublishMessage.create(
                    publish.variableHeader().messageId(), publish.fixedHeader().qosLevel(),
                    publish.fixedHeader().isDup(), publish.fixedHeader().isRetain(),
                    publish.variableHeader().topicName(), newBuf);
            this.handlePublish(mqttPublishMessage);
            break;

        case PUBACK:

            io.netty.handler.codec.mqtt.MqttPubAckMessage mqttPubackMessage = (io.netty.handler.codec.mqtt.MqttPubAckMessage) mqttMessage;
            this.handlePuback(mqttPubackMessage.variableHeader().messageId());
            break;

        case PUBREC:

            int pubrecMessageId = ((io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) mqttMessage
                    .variableHeader()).messageId();
            this.handlePubrec(pubrecMessageId);
            break;

        case PUBREL:

            int pubrelMessageId = ((io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) mqttMessage
                    .variableHeader()).messageId();
            this.handlePubrel(pubrelMessageId);
            break;

        case PUBCOMP:

            int pubcompMessageId = ((io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) mqttMessage
                    .variableHeader()).messageId();
            this.handlePubcomp(pubcompMessageId);
            break;

        case PINGREQ:

            this.handlePingreq();
            break;

        case DISCONNECT:

            this.handleDisconnect();
            break;

        default:

            this.chctx.fireExceptionCaught(new Exception("Wrong message type " + msg.getClass().getName()));
            break;

        }

    } else {

        this.chctx.fireExceptionCaught(new Exception("Wrong message type"));
    }
}

From source file:net.anyflow.lannister.packetreceiver.GenericReceiver.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, MqttMessage msg) throws Exception {
    if (msg.decoderResult().isSuccess() == false) {
        logger.error("decoding MQTT message failed : {}", msg.decoderResult().cause().getMessage());

        Session session = Session.NEXUS.get(ctx.channel().id());
        if (session != null) {
            session.dispose(true); // [MQTT-4.8.0-1]
        } else {//from w ww  .j a  va  2  s .  co m
            ctx.channel().writeAndFlush(msg).addListener(f -> {
                logger.debug("packet outgoing [{}]", msg);

                ctx.channel().disconnect().addListener(ChannelFutureListener.CLOSE).addListener(fs -> // [MQTT-3.2.2-5]
                Plugins.SELF.get(DisconnectEventListener.class)
                        .disconnected(new AbnormalDisconnectEventArgs()));
            });
        }
        return;
    } else {
        logger.debug("packet incoming [message={}]", msg.toString());

        Session session = Session.NEXUS.get(ctx.channel().id());
        if (session == null) {
            logger.error("None exist session message : {}", msg.toString());

            ctx.channel().disconnect().addListener(ChannelFutureListener.CLOSE).addListener(fs -> // [MQTT-4.8.0-1]
            Plugins.SELF.get(DisconnectEventListener.class).disconnected(new AbnormalDisconnectEventArgs()));
            return;
        }

        session.setLastIncomingTime(new Date());

        switch (msg.fixedHeader().messageType()) {
        case DISCONNECT:
            DisconnectReceiver.SHARED.handle(session);
            return;

        case PINGREQ:
            PingReqReceiver.SHARED.handle(session);
            return;

        case PUBREC:
            PubRecReceiver.SHARED.handle(session,
                    ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId());
            return;

        case PUBREL:
            PubRelReceiver.SHARED.handle(session,
                    ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId());
            return;

        case PUBCOMP:
            PubCompReceiver.SHARED.handle(session,
                    ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId());
            return;

        default:
            session.dispose(true); // [MQTT-4.8.0-1]
            return;
        }
    }
}

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

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    try {//w  w w . jav  a 2 s .  c o  m
        if (stopped) {
            disconnect(true);
            return;
        }

        MqttMessage message = (MqttMessage) msg;

        // Disconnect if Netty codec failed to decode the stream.
        if (message.decoderResult().isFailure()) {
            log.debug("Bad Message Disconnecting Client.");
            disconnect(true);
            return;
        }

        connection.dataReceived();

        MQTTUtil.logMessage(session.getState(), message, true);

        this.protocolManager.invokeIncoming(message, this.connection);

        switch (message.fixedHeader().messageType()) {
        case CONNECT:
            handleConnect((MqttConnectMessage) message, ctx);
            break;
        case PUBLISH:
            handlePublish((MqttPublishMessage) message);
            break;
        case PUBACK:
            handlePuback((MqttPubAckMessage) message);
            break;
        case PUBREC:
            handlePubrec(message);
            break;
        case PUBREL:
            handlePubrel(message);
            break;
        case PUBCOMP:
            handlePubcomp(message);
            break;
        case SUBSCRIBE:
            handleSubscribe((MqttSubscribeMessage) message);
            break;
        case UNSUBSCRIBE:
            handleUnsubscribe((MqttUnsubscribeMessage) message);
            break;
        case PINGREQ:
            handlePingreq();
            break;
        case DISCONNECT:
            disconnect(false);
            break;
        case UNSUBACK:
        case SUBACK:
        case PINGRESP:
        case CONNACK: // The server does not instantiate connections therefore any CONNACK received over a connection is an invalid control message.
        default:
            disconnect(true);
        }
    } catch (Exception e) {
        log.debug("Error processing Control Packet, Disconnecting Client", e);
        disconnect(true);
    } finally {
        ReferenceCountUtil.release(msg);
    }
}