[#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
This commit is contained in:
Francisco A. Lozano 2015-07-29 14:00:18 +02:00 committed by Norman Maurer
parent a7713069a1
commit a91e648505
4 changed files with 11 additions and 5 deletions

View File

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

View File

@ -236,11 +236,11 @@ public class MqttDecoder extends ReplayingDecoder<DecoderState> {
}
private static Result<MqttConnAckVariableHeader> 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>(mqttConnAckVariableHeader, numberOfBytesConsumed);
}

View File

@ -192,7 +192,7 @@ public class MqttEncoder extends MessageToMessageEncoder<MqttMessage> {
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;

View File

@ -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);
}