From 6ff66fe967ed1aa45f2e87d263463f35c959b5cf Mon Sep 17 00:00:00 2001 From: "Francisco A. Lozano" Date: Wed, 29 Jul 2015 14:00:18 +0200 Subject: [PATCH] [#4041] Added sessionPresent to MqttConnAckVariableHeader Motivation: As per http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718033 Modification: Add isSessionPresent() method and constructor to set it Result: Be able to use it with mqtt 3.1.1 --- .../handler/codec/mqtt/MqttConnAckVariableHeader.java | 8 +++++++- .../java/io/netty/handler/codec/mqtt/MqttDecoder.java | 4 ++-- .../java/io/netty/handler/codec/mqtt/MqttEncoder.java | 2 +- .../java/io/netty/handler/codec/mqtt/MqttCodecTest.java | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttConnAckVariableHeader.java b/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttConnAckVariableHeader.java index 7dfa309483..75678649c6 100644 --- a/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttConnAckVariableHeader.java +++ b/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttConnAckVariableHeader.java @@ -25,19 +25,25 @@ public class MqttConnAckVariableHeader { private final MqttConnectReturnCode connectReturnCode; - public MqttConnAckVariableHeader(MqttConnectReturnCode connectReturnCode) { + private final boolean sessionPresent; + + public MqttConnAckVariableHeader(MqttConnectReturnCode connectReturnCode, boolean sessionPresent) { this.connectReturnCode = connectReturnCode; + this.sessionPresent = sessionPresent; } public MqttConnectReturnCode connectReturnCode() { return connectReturnCode; } + public boolean isSessionPresent() { return sessionPresent; } + @Override public String toString() { return new StringBuilder(StringUtil.simpleClassName(this)) .append('[') .append("connectReturnCode=").append(connectReturnCode) + .append(", sessionPresent=").append(sessionPresent) .append(']') .toString(); } diff --git a/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttDecoder.java b/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttDecoder.java index bcb770a354..70f0a41e7b 100644 --- a/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttDecoder.java +++ b/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttDecoder.java @@ -236,11 +236,11 @@ public class MqttDecoder extends ReplayingDecoder { } private static Result decodeConnAckVariableHeader(ByteBuf buffer) { - buffer.readUnsignedByte(); // reserved byte + final boolean sessionPresent = (buffer.readUnsignedByte() & 0x01) == 0x01; byte returnCode = buffer.readByte(); final int numberOfBytesConsumed = 2; final MqttConnAckVariableHeader mqttConnAckVariableHeader = - new MqttConnAckVariableHeader(MqttConnectReturnCode.valueOf(returnCode)); + new MqttConnAckVariableHeader(MqttConnectReturnCode.valueOf(returnCode), sessionPresent); return new Result(mqttConnAckVariableHeader, numberOfBytesConsumed); } diff --git a/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttEncoder.java b/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttEncoder.java index db2d871f9d..eda2a8dde2 100644 --- a/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttEncoder.java +++ b/codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttEncoder.java @@ -192,7 +192,7 @@ public class MqttEncoder extends MessageToMessageEncoder { ByteBuf buf = byteBufAllocator.buffer(4); buf.writeByte(getFixedHeaderByte1(message.fixedHeader())); buf.writeByte(2); - buf.writeByte(0); + buf.writeByte(message.variableHeader().sessionPresent() ? 0x01 : 0x00); buf.writeByte(message.variableHeader().connectReturnCode().byteValue()); return buf; diff --git a/codec-mqtt/src/test/java/io/netty/handler/codec/mqtt/MqttCodecTest.java b/codec-mqtt/src/test/java/io/netty/handler/codec/mqtt/MqttCodecTest.java index f16602a64e..705d071bfd 100644 --- a/codec-mqtt/src/test/java/io/netty/handler/codec/mqtt/MqttCodecTest.java +++ b/codec-mqtt/src/test/java/io/netty/handler/codec/mqtt/MqttCodecTest.java @@ -289,7 +289,7 @@ public class MqttCodecTest { MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false, 0); MqttConnAckVariableHeader mqttConnAckVariableHeader = - new MqttConnAckVariableHeader(MqttConnectReturnCode.CONNECTION_ACCEPTED); + new MqttConnAckVariableHeader(MqttConnectReturnCode.CONNECTION_ACCEPTED, true); return new MqttConnAckMessage(mqttFixedHeader, mqttConnAckVariableHeader); }