Example usage for io.netty.handler.codec.mqtt MqttTopicSubscription topicName

List of usage examples for io.netty.handler.codec.mqtt MqttTopicSubscription topicName

Introduction

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

Prototype

public String topicName() 

Source Link

Usage

From source file:net.anyflow.lannister.message.MessageFactory.java

License:Apache License

public static MqttSubscribeMessage subscribe(int messageId, MqttTopicSubscription... topicSubscriptions) {
    int topicNameSize = 0;
    int topicCount = topicSubscriptions.length;

    for (MqttTopicSubscription item : topicSubscriptions) {
        topicNameSize += item.topicName().getBytes(CharsetUtil.UTF_8).length;
    }// www.  j  a v  a2s  . co  m

    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE,
            false, 2 + topicNameSize + topicCount);
    MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);
    MqttSubscribePayload payload = new MqttSubscribePayload(Lists.newArrayList(topicSubscriptions));

    return new MqttSubscribeMessage(fixedHeader, variableHeader, payload);
}

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

License:Apache License

boolean addSubscription(MqttTopicSubscription subscription, WildcardConfiguration wildcardConfiguration) {
    // synchronized to prevent race with removeSubscription
    synchronized (subscriptions) {
        addressMessageMap.putIfAbsent(//from www.j a  v a  2 s .  c o  m
                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;
}

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

License:Apache License

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());
    }//from w w w .  j  a v a  2s.  c o m
}

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

License:Apache License

private void addSubscription(MqttTopicSubscription subscription) throws Exception {
    String topicName = CompositeAddress.extractAddressName(subscription.topicName());
    MqttTopicSubscription s = session.getSessionState().getSubscription(topicName);

    int qos = subscription.qualityOfService().value();

    String coreAddress = MQTTUtil.convertMQTTAddressFilterToCore(topicName, session.getWildcardConfiguration());

    session.getSessionState().addSubscription(subscription, session.getWildcardConfiguration());

    Queue q = createQueueForSubscription(coreAddress, qos);

    if (s == null) {
        createConsumerForSubscriptionQueue(q, topicName, qos);
    } else {//  www  .  j av a  2  s  . com
        consumerQoSLevels.put(consumers.get(topicName).getID(), qos);
    }
    session.getRetainMessageManager().addRetainedMessagesToQueue(q, topicName);
}

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

License:Apache License

void clean() throws Exception {
    for (MqttTopicSubscription mqttTopicSubscription : session.getSessionState().getSubscriptions()) {
        removeSubscription(mqttTopicSubscription.topicName());
    }/*w ww  .  j  av  a 2 s  .c om*/
}

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

License:Apache License

public static void traceMessage(MQTTSessionState state, MqttMessage message, boolean inbound) {
    StringBuilder log = new StringBuilder("MQTT(");

    if (state != null) {
        log.append(state.getClientId());
    }/*  www . ja va 2  s. c  o m*/

    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());
    }
}

From source file:org.thingsboard.server.transport.mqtt.MqttTransportHandler.java

License:Apache License

private void processSubscribe(ChannelHandlerContext ctx, MqttSubscribeMessage mqttMsg) {
    if (!checkConnected(ctx, mqttMsg)) {
        return;/*w  ww .  j a va2 s  .  c  om*/
    }
    log.trace("[{}] Processing subscription [{}]!", sessionId, mqttMsg.variableHeader().messageId());
    List<Integer> grantedQoSList = new ArrayList<>();
    for (MqttTopicSubscription subscription : mqttMsg.payload().topicSubscriptions()) {
        String topic = subscription.topicName();
        MqttQoS reqQoS = subscription.qualityOfService();
        try {
            switch (topic) {
            case MqttTopics.DEVICE_ATTRIBUTES_TOPIC: {
                transportService.process(sessionInfo,
                        TransportProtos.SubscribeToAttributeUpdatesMsg.newBuilder().build(), null);
                registerSubQoS(topic, grantedQoSList, reqQoS);
                break;
            }
            case MqttTopics.DEVICE_RPC_REQUESTS_SUB_TOPIC: {
                transportService.process(sessionInfo, TransportProtos.SubscribeToRPCMsg.newBuilder().build(),
                        null);
                registerSubQoS(topic, grantedQoSList, reqQoS);
                break;
            }
            case MqttTopics.DEVICE_RPC_RESPONSE_SUB_TOPIC:
            case MqttTopics.GATEWAY_ATTRIBUTES_TOPIC:
            case MqttTopics.GATEWAY_RPC_TOPIC:
            case MqttTopics.DEVICE_ATTRIBUTES_RESPONSES_TOPIC:
                registerSubQoS(topic, grantedQoSList, reqQoS);
                break;
            default:
                log.warn("[{}] Failed to subscribe to [{}][{}]", sessionId, topic, reqQoS);
                grantedQoSList.add(FAILURE.value());
                break;
            }
        } catch (Exception e) {
            log.warn("[{}] Failed to subscribe to [{}][{}]", sessionId, topic, reqQoS);
            grantedQoSList.add(FAILURE.value());
        }
    }
    ctx.writeAndFlush(createSubAckMessage(mqttMsg.variableHeader().messageId(), grantedQoSList));
}