Search in sources :

Example 1 with MqttConnectVariableHeader

use of io.netty.handler.codec.mqtt.MqttConnectVariableHeader 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 MqttConnectVariableHeader

use of io.netty.handler.codec.mqtt.MqttConnectVariableHeader in project netty by netty.

the class MqttHeartBeatClientHandler method channelActive.

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    MqttFixedHeader connectFixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttConnectVariableHeader connectVariableHeader = new MqttConnectVariableHeader(PROTOCOL_NAME_MQTT_3_1_1, PROTOCOL_VERSION_MQTT_3_1_1, true, true, false, 0, false, false, 20, MqttProperties.NO_PROPERTIES);
    MqttConnectPayload connectPayload = new MqttConnectPayload(clientId, MqttProperties.NO_PROPERTIES, null, null, userName, password);
    MqttConnectMessage connectMessage = new MqttConnectMessage(connectFixedHeader, connectVariableHeader, connectPayload);
    ctx.writeAndFlush(connectMessage);
    System.out.println("Sent CONNECT");
}
Also used : MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttConnectPayload(io.netty.handler.codec.mqtt.MqttConnectPayload) MqttConnectVariableHeader(io.netty.handler.codec.mqtt.MqttConnectVariableHeader)

Example 3 with MqttConnectVariableHeader

use of io.netty.handler.codec.mqtt.MqttConnectVariableHeader in project rocketmq-externals by apache.

the class ConsumeMessageIntegrationTest method getConnectMessage.

private MqttConnectMessage getConnectMessage() {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader("MQTT", 4, false, false, false, MqttQoS.AT_MOST_ONCE.value(), true, true, 60);
    MqttConnectPayload payload = new MqttConnectPayload(consumerId, "test-will-topic", "the test client is down".getBytes(), null, null);
    return new MqttConnectMessage(fixedHeader, variableHeader, payload);
}
Also used : MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttConnectPayload(io.netty.handler.codec.mqtt.MqttConnectPayload) MqttConnectVariableHeader(io.netty.handler.codec.mqtt.MqttConnectVariableHeader)

Example 4 with MqttConnectVariableHeader

use of io.netty.handler.codec.mqtt.MqttConnectVariableHeader in project rocketmq-externals by apache.

the class ProduceMessageIntegrationTest method getConnectMessage.

private MqttConnectMessage getConnectMessage() {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader("MQTT", 4, false, false, false, MqttQoS.AT_MOST_ONCE.value(), true, true, 60);
    MqttConnectPayload payload = new MqttConnectPayload(producerId, "test-will-topic", "the test client is down".getBytes(), null, null);
    return new MqttConnectMessage(fixedHeader, variableHeader, payload);
}
Also used : MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttConnectPayload(io.netty.handler.codec.mqtt.MqttConnectPayload) MqttConnectVariableHeader(io.netty.handler.codec.mqtt.MqttConnectVariableHeader)

Example 5 with MqttConnectVariableHeader

use of io.netty.handler.codec.mqtt.MqttConnectVariableHeader in project rocketmq-externals by apache.

the class PubSubIntegrationTest method getConnectMessage.

private MqttConnectMessage getConnectMessage(String clientId) {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader("MQTT", 4, false, false, false, MqttQoS.AT_MOST_ONCE.value(), true, true, 60);
    MqttConnectPayload payload = new MqttConnectPayload(clientId, "test-will-topic", "the test client is down".getBytes(), null, null);
    return new MqttConnectMessage(fixedHeader, variableHeader, payload);
}
Also used : MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttConnectPayload(io.netty.handler.codec.mqtt.MqttConnectPayload) MqttConnectVariableHeader(io.netty.handler.codec.mqtt.MqttConnectVariableHeader)

Aggregations

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