Search in sources :

Example 1 with MqttTopicSubscription

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

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

the class MQTTSubscriptionManager method start.

synchronized void start() throws Exception {
    for (MqttTopicSubscription subscription : session.getSessionState().getSubscriptions()) {
        String coreAddress = MQTTUtil.convertMQTTAddressFilterToCore(subscription.topicName(), session.getWildcardConfiguration());
        Queue q = createQueueForSubscription(coreAddress, subscription.qualityOfService().value());
        createConsumerForSubscriptionQueue(q, subscription.topicName(), subscription.qualityOfService().value());
    }
}
Also used : MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Queue(org.apache.activemq.artemis.core.server.Queue)

Example 3 with MqttTopicSubscription

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

the class MQTTSessionState method addSubscription.

boolean addSubscription(MqttTopicSubscription subscription, WildcardConfiguration wildcardConfiguration) {
    // synchronized to prevent race with removeSubscription
    synchronized (subscriptions) {
        addressMessageMap.putIfAbsent(MQTTUtil.convertMQTTAddressFilterToCore(subscription.topicName(), wildcardConfiguration), new ConcurrentHashMap<Long, Integer>());
        MqttTopicSubscription existingSubscription = subscriptions.get(subscription.topicName());
        if (existingSubscription != null) {
            if (subscription.qualityOfService().value() > existingSubscription.qualityOfService().value()) {
                subscriptions.put(subscription.topicName(), subscription);
                return true;
            }
        } else {
            subscriptions.put(subscription.topicName(), subscription);
            return true;
        }
    }
    return false;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription)

Example 4 with MqttTopicSubscription

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

the class MqttSubscribeMessageHandler method handleMessage.

/**
 * handle the SUBSCRIBE message from the client
 * <ol>
 * <li>validate the topic filters in each subscription</li>
 * <li>set actual qos of each filter</li>
 * <li>get the topics matching given filters</li>
 * <li>check the client authorization of each topic</li>
 * <li>generate SUBACK message which includes the subscription result for each TopicFilter</li>
 * <li>send SUBACK message to the client</li>
 * </ol>
 *
 * @param message the message wrapping MqttSubscriptionMessage
 * @return
 */
@Override
public void handleMessage(Message message) {
    Client client = message.getClient();
    MqttSubscribeMessage subscribeMessage = (MqttSubscribeMessage) message.getPayload();
    List<MqttTopicSubscription> topicSubscriptions = subscribeMessage.payload().topicSubscriptions();
    List<Integer> grantQoss = new ArrayList<>();
    topicSubscriptions.forEach(s -> {
        String topic = s.topicName();
        int actualQos = MessageUtil.actualQos(s.qualityOfService().value());
        grantQoss.add(actualQos);
        subscriptionStore.append(topic, Subscription.Builder.newBuilder().client((MqttClient) client).qos(actualQos).build());
    });
    MqttSubAckMessage subackMessage = MessageUtil.getMqttSubackMessage(subscribeMessage, new MqttSubAckPayload(grantQoss));
    client.getCtx().writeAndFlush(subackMessage);
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) MqttSubAckMessage(io.netty.handler.codec.mqtt.MqttSubAckMessage) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) ArrayList(java.util.ArrayList) Client(org.apache.rocketmq.iot.connection.client.Client) MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MqttSubAckPayload(io.netty.handler.codec.mqtt.MqttSubAckPayload)

Example 5 with MqttTopicSubscription

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

the class MqttSubscribeMessageHandlerTest method getMqttSubscribeMessage.

private MqttSubscribeMessage getMqttSubscribeMessage() {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(1);
    List<MqttTopicSubscription> subscriptions = new ArrayList<>();
    subscriptions.add(new MqttTopicSubscription("topic1", MqttQoS.AT_MOST_ONCE));
    subscriptions.add(new MqttTopicSubscription("topic2", MqttQoS.AT_LEAST_ONCE));
    MqttSubscribePayload payload = new MqttSubscribePayload(subscriptions);
    return new MqttSubscribeMessage(fixedHeader, variableHeader, payload);
}
Also used : MqttMessageIdVariableHeader(io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) ArrayList(java.util.ArrayList) MqttSubscribePayload(io.netty.handler.codec.mqtt.MqttSubscribePayload)

Aggregations

MqttTopicSubscription (io.netty.handler.codec.mqtt.MqttTopicSubscription)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)3 MqttClient (org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 MqttMessageIdVariableHeader (io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader)2 MqttSubscribeMessage (io.netty.handler.codec.mqtt.MqttSubscribeMessage)2 ArrayList (java.util.ArrayList)2 Queue (org.apache.activemq.artemis.core.server.Queue)2 ClientManagerImpl (org.apache.rocketmq.iot.connection.client.impl.ClientManagerImpl)2 MessageDispatcher (org.apache.rocketmq.iot.protocol.mqtt.handler.MessageDispatcher)2 MqttConnectMessageHandler (org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttConnectMessageHandler)2 MqttMessageForwarder (org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttMessageForwarder)2 MqttSubscribeMessageHandler (org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttSubscribeMessageHandler)2 InMemorySubscriptionStore (org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore)2 Before (org.junit.Before)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 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