From 8d59667de469e31fd9b68994d2e06b5d34bd58aa Mon Sep 17 00:00:00 2001 From: Silvio Giebl Date: Fri, 6 Jul 2018 10:38:24 +0200 Subject: [PATCH] Improved incoming TopicAliasMapping #175 --- .../org/mqttbee/mqtt/MqttClientConnectionData.java | 11 +++++++---- .../mqtt/codec/decoder/mqtt5/Mqtt5PublishDecoder.java | 9 +++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/mqttbee/mqtt/MqttClientConnectionData.java b/src/main/java/org/mqttbee/mqtt/MqttClientConnectionData.java index 6ef05ae80..bbca8260f 100644 --- a/src/main/java/org/mqttbee/mqtt/MqttClientConnectionData.java +++ b/src/main/java/org/mqttbee/mqtt/MqttClientConnectionData.java @@ -26,6 +26,7 @@ import org.mqttbee.api.mqtt.mqtt5.auth.Mqtt5EnhancedAuthProvider; import org.mqttbee.mqtt.datatypes.MqttTopicImpl; import org.mqttbee.mqtt.datatypes.MqttVariableByteInteger; +import org.mqttbee.util.collections.IntMap; import java.util.Optional; @@ -37,7 +38,8 @@ public class MqttClientConnectionData implements Mqtt5ClientConnectionData, Mqtt private int keepAlive; private long sessionExpiryInterval; private final int receiveMaximum; - private final MqttTopicImpl[] topicAliasMapping; + private final int topicAliasMaximum; + private final IntMap topicAliasMapping; private final int maximumPacketSize; private final int subscriptionIdentifierMaximum; private final Mqtt5EnhancedAuthProvider enhancedAuthProvider; @@ -56,7 +58,8 @@ public MqttClientConnectionData( this.keepAlive = keepAlive; this.sessionExpiryInterval = sessionExpiryInterval; this.receiveMaximum = receiveMaximum; - this.topicAliasMapping = (topicAliasMaximum == 0) ? null : new MqttTopicImpl[topicAliasMaximum]; + this.topicAliasMaximum = topicAliasMaximum; + this.topicAliasMapping = (topicAliasMaximum == 0) ? null : IntMap.range(1, topicAliasMaximum); this.maximumPacketSize = maximumPacketSize; this.subscriptionIdentifierMaximum = MqttVariableByteInteger.FOUR_BYTES_MAX_VALUE; // TODO CONNECT + CONNACK user properties @@ -92,11 +95,11 @@ public int getReceiveMaximum() { @Override public int getTopicAliasMaximum() { - return (topicAliasMapping == null) ? 0 : topicAliasMapping.length; + return topicAliasMaximum; } @Nullable - public MqttTopicImpl[] getTopicAliasMapping() { + public IntMap getTopicAliasMapping() { return topicAliasMapping; } diff --git a/src/main/java/org/mqttbee/mqtt/codec/decoder/mqtt5/Mqtt5PublishDecoder.java b/src/main/java/org/mqttbee/mqtt/codec/decoder/mqtt5/Mqtt5PublishDecoder.java index 23e32a1d2..93af4e556 100644 --- a/src/main/java/org/mqttbee/mqtt/codec/decoder/mqtt5/Mqtt5PublishDecoder.java +++ b/src/main/java/org/mqttbee/mqtt/codec/decoder/mqtt5/Mqtt5PublishDecoder.java @@ -36,6 +36,7 @@ import org.mqttbee.mqtt.message.publish.MqttStatefulPublish; import org.mqttbee.mqtt.netty.ChannelAttributes; import org.mqttbee.util.ByteBufferUtil; +import org.mqttbee.util.collections.IntMap; import javax.inject.Inject; import javax.inject.Singleton; @@ -183,20 +184,20 @@ public MqttStatefulPublish decode( boolean isNewTopicAlias = false; if (topicAlias != DEFAULT_NO_TOPIC_ALIAS) { - final MqttTopicImpl[] topicAliasMapping = clientConnectionData.getTopicAliasMapping(); - if ((topicAliasMapping == null) || (topicAlias > topicAliasMapping.length)) { + final IntMap topicAliasMapping = clientConnectionData.getTopicAliasMapping(); + if ((topicAliasMapping == null) || (topicAlias > clientConnectionData.getTopicAliasMaximum())) { throw new MqttDecoderException( Mqtt5DisconnectReasonCode.TOPIC_ALIAS_INVALID, "topic alias must not exceed topic alias maximum"); } if (topic == null) { - topic = topicAliasMapping[topicAlias - 1]; + topic = topicAliasMapping.get(topicAlias); if (topic == null) { throw new MqttDecoderException( Mqtt5DisconnectReasonCode.TOPIC_ALIAS_INVALID, "topic alias has no mapping"); } } else { - topicAliasMapping[topicAlias - 1] = topic; + topicAliasMapping.put(topicAlias, topic); isNewTopicAlias = true; } } else if (topic == null) {