Example usage for io.netty.handler.codec DecoderResult isFinished

List of usage examples for io.netty.handler.codec DecoderResult isFinished

Introduction

In this page you can find the example usage for io.netty.handler.codec DecoderResult isFinished.

Prototype

public boolean isFinished() 

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//w  w w .  j a v  a  2 s . 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// ww w.j av  a 2  s. c  o 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// w w  w  . j a v  a  2  s  . 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 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"));
    }
}