Skip to content

Commit

Permalink
Improved incoming TopicAliasMapping #175
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Jul 6, 2018
1 parent e79e082 commit 2e49eb2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/main/java/org/mqttbee/mqtt/MqttClientConnectionData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<MqttTopicImpl> topicAliasMapping;
private final int maximumPacketSize;
private final int subscriptionIdentifierMaximum;
private final Mqtt5EnhancedAuthProvider enhancedAuthProvider;
Expand All @@ -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
Expand Down Expand Up @@ -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<MqttTopicImpl> getTopicAliasMapping() {
return topicAliasMapping;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<MqttTopicImpl> 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) {
Expand Down

0 comments on commit 2e49eb2

Please sign in to comment.