Updated publish message parto to conform to 3.1.1 specification

This commit is contained in:
selva.andre
2014-07-31 00:08:41 +02:00
parent 482ffe0c65
commit d4ef81a7ef
9 changed files with 160 additions and 18 deletions
@@ -18,6 +18,8 @@ package org.dna.mqtt.moquette.parser.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.util.AttributeMap;
import java.util.List;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
import org.dna.mqtt.moquette.proto.messages.PublishMessage;
@@ -34,6 +36,11 @@ class PublishDecoder extends DemuxDecoder {
@Override
void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
decode((AttributeMap)ctx, in, out);
}
// @Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws Exception {
LOG.info("decode invoked with buffer {}", in);
in.resetReaderIndex();
int startPos = in.readerIndex();
@@ -45,6 +52,18 @@ class PublishDecoder extends DemuxDecoder {
in.resetReaderIndex();
return;
}
if (Utils.isMQTT3_1_1(ctx)) {
if (message.getQos() == AbstractMessage.QOSType.MOST_ONE && message.isDupFlag()) {
//bad protocol, if QoS=0 => DUP = 0
throw new CorruptedFrameException("Received a PUBLISH with QoS=0 & DUP = 1, MQTT 3.1.1 violation");
}
if (message.getQos() == AbstractMessage.QOSType.RESERVED) {
throw new CorruptedFrameException("Received a PUBLISH with QoS flags setted 10 b11, MQTT 3.1.1 violation");
}
}
int remainingLength = message.getRemainingLength();
//Topic name
@@ -53,6 +72,10 @@ class PublishDecoder extends DemuxDecoder {
in.resetReaderIndex();
return;
}
if (topic.contains("+") || topic.contains("#")) {
throw new CorruptedFrameException("Received a PUBLISH with topic containting wild card chars, topic: " + topic);
}
message.setTopicName(topic);
if (message.getQos() == AbstractMessage.QOSType.LEAST_ONE ||
@@ -18,6 +18,8 @@ package org.dna.mqtt.moquette.parser.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.util.Attribute;
import io.netty.util.AttributeMap;
import java.io.UnsupportedEncodingException;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
import org.slf4j.LoggerFactory;
@@ -174,4 +176,13 @@ public class Utils {
flags |= ((message.getQos().ordinal() & 0x03) << 1);
return flags;
}
static boolean isMQTT3_1_1(AttributeMap attrsMap) {
Attribute<Integer> versionAttr = attrsMap.attr(MQTTDecoder.PROTOCOL_VERSION);
Integer protocolVersion = versionAttr.get();
if (protocolVersion == null) {
return true;
}
return protocolVersion == VERSION_3_1_1;
}
}