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