Search in sources :

Example 1 with MqttPublishVariableHeader

use of io.netty.handler.codec.mqtt.MqttPublishVariableHeader in project activemq-artemis by apache.

the class MQTTUtil method traceMessage.

public static void traceMessage(MQTTSessionState state, MqttMessage message, boolean inbound) {
    StringBuilder log = new StringBuilder("MQTT(");
    if (state != null) {
        log.append(state.getClientId());
    }
    if (inbound) {
        log.append("): IN << ");
    } else {
        log.append("): OUT >> ");
    }
    if (message.fixedHeader() != null) {
        log.append(message.fixedHeader().messageType().toString());
        if (message.variableHeader() instanceof MqttMessageIdVariableHeader) {
            log.append("(" + ((MqttMessageIdVariableHeader) message.variableHeader()).messageId() + ")");
        }
        switch(message.fixedHeader().messageType()) {
            case PUBLISH:
                MqttPublishVariableHeader publishHeader = (MqttPublishVariableHeader) message.variableHeader();
                String publishPayload = ((MqttPublishMessage) message).payload().toString(StandardCharsets.UTF_8);
                final int maxPayloadLogSize = 256;
                log.append("(" + publishHeader.packetId() + ")").append(" topic=" + publishHeader.topicName()).append(", qos=" + message.fixedHeader().qosLevel()).append(", retain=" + message.fixedHeader().isRetain()).append(", dup=" + message.fixedHeader().isDup()).append(", payload=" + (publishPayload.length() > maxPayloadLogSize ? publishPayload.substring(0, maxPayloadLogSize) : publishPayload));
                break;
            case CONNECT:
                MqttConnectVariableHeader connectHeader = (MqttConnectVariableHeader) message.variableHeader();
                MqttConnectPayload payload = ((MqttConnectMessage) message).payload();
                log.append(" protocol=(").append(connectHeader.name()).append(", ").append(connectHeader.version()).append(")").append(", hasPassword=").append(connectHeader.hasPassword()).append(", isCleanSession=").append(connectHeader.isCleanSession()).append(", keepAliveTimeSeconds=").append(connectHeader.keepAliveTimeSeconds()).append(", clientIdentifier=").append(payload.clientIdentifier()).append(", hasUserName=").append(connectHeader.hasUserName());
                if (connectHeader.hasUserName()) {
                    log.append(", userName=").append(payload.userName());
                }
                log.append(", isWillFlag=").append(connectHeader.isWillFlag());
                if (connectHeader.isWillFlag()) {
                    log.append(", willQos=").append(connectHeader.willQos()).append(", isWillRetain=").append(connectHeader.isWillRetain()).append(", willTopic=").append(payload.willTopic());
                }
                break;
            case CONNACK:
                MqttConnAckVariableHeader connackHeader = (MqttConnAckVariableHeader) message.variableHeader();
                log.append(" connectReturnCode=").append(connackHeader.connectReturnCode().byteValue()).append(", sessionPresent=").append(connackHeader.isSessionPresent());
                break;
            case SUBSCRIBE:
                for (MqttTopicSubscription sub : ((MqttSubscribeMessage) message).payload().topicSubscriptions()) {
                    log.append("\n\t" + sub.topicName() + " : " + sub.qualityOfService());
                }
                break;
            case SUBACK:
                for (Integer qos : ((MqttSubAckMessage) message).payload().grantedQoSLevels()) {
                    log.append("\n\t" + qos);
                }
                break;
            case UNSUBSCRIBE:
                for (String topic : ((MqttUnsubscribeMessage) message).payload().topics()) {
                    log.append("\n\t" + topic);
                }
                break;
        }
        logger.trace(log.toString());
    }
}
Also used : MqttMessageIdVariableHeader(io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttConnAckVariableHeader(io.netty.handler.codec.mqtt.MqttConnAckVariableHeader) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) MqttPublishVariableHeader(io.netty.handler.codec.mqtt.MqttPublishVariableHeader) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MqttConnectPayload(io.netty.handler.codec.mqtt.MqttConnectPayload) MqttConnectVariableHeader(io.netty.handler.codec.mqtt.MqttConnectVariableHeader)

Example 2 with MqttPublishVariableHeader

use of io.netty.handler.codec.mqtt.MqttPublishVariableHeader in project activemq-artemis by apache.

the class MQTTProtocolHandler method send.

protected void send(int messageId, String topicName, int qosLevel, boolean isRetain, ByteBuf payload, int deliveryCount) {
    boolean redelivery = qosLevel == 0 ? false : (deliveryCount > 0);
    MqttFixedHeader header = new MqttFixedHeader(MqttMessageType.PUBLISH, redelivery, MqttQoS.valueOf(qosLevel), isRetain, 0);
    MqttPublishVariableHeader varHeader = new MqttPublishVariableHeader(topicName, messageId);
    MqttMessage publish = new MqttPublishMessage(header, varHeader, payload);
    sendToClient(publish);
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) MqttPublishVariableHeader(io.netty.handler.codec.mqtt.MqttPublishVariableHeader)

Aggregations

MqttPublishVariableHeader (io.netty.handler.codec.mqtt.MqttPublishVariableHeader)2 MqttConnAckVariableHeader (io.netty.handler.codec.mqtt.MqttConnAckVariableHeader)1 MqttConnectMessage (io.netty.handler.codec.mqtt.MqttConnectMessage)1 MqttConnectPayload (io.netty.handler.codec.mqtt.MqttConnectPayload)1 MqttConnectVariableHeader (io.netty.handler.codec.mqtt.MqttConnectVariableHeader)1 MqttFixedHeader (io.netty.handler.codec.mqtt.MqttFixedHeader)1 MqttMessage (io.netty.handler.codec.mqtt.MqttMessage)1 MqttMessageIdVariableHeader (io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader)1 MqttPublishMessage (io.netty.handler.codec.mqtt.MqttPublishMessage)1 MqttTopicSubscription (io.netty.handler.codec.mqtt.MqttTopicSubscription)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1