Skip to content

Commit f0d6038

Browse files
committed
Exposing Reason codes on various ACK messages
Signed-off-by: James Sutton <james.sutton@uk.ibm.com>
1 parent a7564f5 commit f0d6038

12 files changed

Lines changed: 108 additions & 76 deletions

File tree

org.eclipse.paho.mqttv5.client/src/main/java/org/eclipse/paho/mqttv5/client/IMqttToken.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ public interface IMqttToken {
151151
*/
152152
public int[] getGrantedQos();
153153

154+
/**
155+
* Returns a list of reason codes that were returned as a result of this token's action.
156+
* You will receive reason codes from the following MQTT actions:
157+
* <ul>
158+
* <li>CONNECT - in the corresponding CONNACK Packet.</li>
159+
* <li>PUBLISH - in the corresponding PUBACK, PUBREC, PUBCOMP, PUBREL packets</li>
160+
* <li>SUBSCRIBE - in the corresponding SUBACK Packet.</li>
161+
* <li>UNSUBSCRIBE - in the corresponding UNSUBACK Packet.</li>
162+
* <li>AUTH - in the returned AUTH Packet.</li>
163+
* </ul>
164+
* @return the reason code(s) from the response for this token's action.
165+
*
166+
*/
167+
public int[] getReasonCodes();
168+
154169
/**
155170
* @return the session present flag from a connack
156171
*/

org.eclipse.paho.mqttv5.client/src/main/java/org/eclipse/paho/mqttv5/client/MqttToken.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,9 @@ public MqttProperties getMessageProperties() {
106106
return internalTok.getWireMessage().getProperties();
107107
}
108108

109+
@Override
110+
public int[] getReasonCodes() {
111+
return internalTok.getWireMessage().getReasonCodes();
112+
}
113+
109114
}

org.eclipse.paho.mqttv5.common/src/main/java/org/eclipse/paho/mqttv5/common/packet/MqttAck.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,4 @@ public MqttAck(byte type) {
2828
protected byte getMessageInfo() {
2929
return 0;
3030
}
31-
32-
/**
33-
* @return String representation of the wire message
34-
*/
35-
@Override
36-
public String toString() {
37-
return super.toString() + " msgId " + msgId;
38-
}
3931
}

org.eclipse.paho.mqttv5.common/src/main/java/org/eclipse/paho/mqttv5/common/packet/MqttAuth.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class MqttAuth extends MqttWireMessage {
3838
MqttProperties.USER_DEFINED_PAIR_IDENTIFIER };
3939

4040
// Fields
41-
private int returnCode;
4241
private MqttProperties properties;
4342

4443
/**
@@ -56,8 +55,8 @@ public MqttAuth(byte[] data) throws IOException, MqttException {
5655
properties = new MqttProperties(validProperties);
5756
ByteArrayInputStream bais = new ByteArrayInputStream(data);
5857
DataInputStream inputStream = new DataInputStream(bais);
59-
returnCode = inputStream.readUnsignedByte();
60-
validateReturnCode(returnCode, validReturnCodes);
58+
reasonCode = inputStream.readUnsignedByte();
59+
validateReturnCode(reasonCode, validReturnCodes);
6160
this.properties.decodeProperties(inputStream);
6261
inputStream.close();
6362
}
@@ -81,7 +80,7 @@ public MqttAuth(int returnCode, MqttProperties properties) throws MqttException
8180
}
8281
this.properties.setValidProperties(validProperties);
8382
validateReturnCode(returnCode, validReturnCodes);
84-
this.returnCode = returnCode;
83+
this.reasonCode = returnCode;
8584
}
8685

8786
@Override
@@ -91,7 +90,7 @@ protected byte[] getVariableHeader() throws MqttException {
9190
DataOutputStream outputStream = new DataOutputStream(baos);
9291

9392
// Encode the Return Code
94-
outputStream.writeByte(returnCode);
93+
outputStream.writeByte(reasonCode);
9594

9695
// Write Identifier / Value Fields
9796
byte[] identifierValueFieldsByteArray = this.properties.encodeProperties();
@@ -109,7 +108,7 @@ protected byte getMessageInfo() {
109108
}
110109

111110
public int getReturnCode() {
112-
return returnCode;
111+
return reasonCode;
113112
}
114113

115114
@Override
@@ -119,6 +118,6 @@ public MqttProperties getProperties() {
119118

120119
@Override
121120
public String toString() {
122-
return "MqttAuth [returnCode=" + returnCode + ", properties=" + properties + "]";
121+
return "MqttAuth [returnCode=" + reasonCode + ", properties=" + properties + "]";
123122
}
124123
}

org.eclipse.paho.mqttv5.common/src/main/java/org/eclipse/paho/mqttv5/common/packet/MqttConnAck.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public class MqttConnAck extends MqttAck {
5252
MqttProperties.AUTH_METHOD_IDENTIFIER, MqttProperties.AUTH_DATA_IDENTIFIER,
5353
MqttProperties.REASON_STRING_IDENTIFIER, MqttProperties.USER_DEFINED_PAIR_IDENTIFIER };
5454

55-
private int returnCode;
5655
private boolean sessionPresent;
5756
private MqttProperties properties;
5857

@@ -62,8 +61,8 @@ public MqttConnAck(byte[] variableHeader) throws IOException, MqttException {
6261
ByteArrayInputStream bais = new ByteArrayInputStream(variableHeader);
6362
DataInputStream dis = new DataInputStream(bais);
6463
sessionPresent = (dis.readUnsignedByte() & 0x01) == 0x01;
65-
returnCode = dis.readUnsignedByte();
66-
validateReturnCode(returnCode, validReturnCodes);
64+
reasonCode = dis.readUnsignedByte();
65+
validateReturnCode(reasonCode, validReturnCodes);
6766
this.properties.decodeProperties(dis);
6867
dis.close();
6968
}
@@ -78,7 +77,7 @@ public MqttConnAck(boolean sessionPresent, int returnCode, MqttProperties proper
7877
this.properties.setValidProperties(validProperties);
7978
this.sessionPresent = sessionPresent;
8079
validateReturnCode(returnCode, validReturnCodes);
81-
this.returnCode = returnCode;
80+
this.reasonCode = returnCode;
8281
}
8382

8483
@Override
@@ -95,7 +94,7 @@ protected byte[] getVariableHeader() throws MqttException {
9594
dos.write(connectAchnowledgeFlag);
9695

9796
// Encode the Connect Return Code
98-
dos.write((byte) returnCode);
97+
dos.write((byte) reasonCode);
9998

10099
// Write Identifier / Value Fields
101100
byte[] identifierValueFieldsByteArray = this.properties.encodeProperties();
@@ -130,11 +129,11 @@ public void setSessionPresent(boolean sessionPresent) {
130129
}
131130

132131
public int getReturnCode() {
133-
return returnCode;
132+
return reasonCode;
134133
}
135134

136135
public void setReturnCode(int returnCode) {
137-
this.returnCode = returnCode;
136+
this.reasonCode = returnCode;
138137
}
139138

140139
@Override
@@ -148,7 +147,7 @@ public static int[] getValidreturncodes() {
148147

149148
@Override
150149
public String toString() {
151-
return "MqttConnAck [returnCode=" + returnCode + ", sessionPresent=" + sessionPresent + ", properties="
150+
return "MqttConnAck [returnCode=" + reasonCode + ", sessionPresent=" + sessionPresent + ", properties="
152151
+ properties + "]";
153152
}
154153

org.eclipse.paho.mqttv5.common/src/main/java/org/eclipse/paho/mqttv5/common/packet/MqttPubAck.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public class MqttPubAck extends MqttAck {
3535

3636
private static final Byte[] validProperties = {MqttProperties.REASON_STRING_IDENTIFIER, MqttProperties.USER_DEFINED_PAIR_IDENTIFIER};
3737

38-
private int returnCode = MqttReturnCode.RETURN_CODE_SUCCESS;
3938
private MqttProperties properties;
4039

4140

@@ -48,8 +47,8 @@ public MqttPubAck( byte[] data) throws IOException, MqttException {
4847
msgId = dis.readUnsignedShort();
4948
long remainder = (long)data.length - counter.getCounter();
5049
if (remainder > 2) {
51-
returnCode = dis.readUnsignedByte();
52-
validateReturnCode(returnCode, validReturnCodes);
50+
reasonCode = dis.readUnsignedByte();
51+
validateReturnCode(reasonCode, validReturnCodes);
5352
}
5453
if( remainder >= 4) {
5554
this.properties.decodeProperties(dis);
@@ -59,7 +58,7 @@ public MqttPubAck( byte[] data) throws IOException, MqttException {
5958

6059
public MqttPubAck(int returnCode, int msgId, MqttProperties properties) throws MqttException {
6160
super(MqttWireMessage.MESSAGE_TYPE_PUBACK);
62-
this.returnCode = returnCode;
61+
this.reasonCode = returnCode;
6362
this.msgId = msgId;
6463
if (properties != null) {
6564
this.properties = properties;
@@ -81,10 +80,10 @@ protected byte[] getVariableHeader() throws MqttException {
8180

8281
byte[] identifierValueFieldsByteArray = this.properties.encodeProperties();
8382

84-
if (returnCode != MqttReturnCode.RETURN_CODE_SUCCESS || identifierValueFieldsByteArray.length != 0) {
83+
if (reasonCode != MqttReturnCode.RETURN_CODE_SUCCESS || identifierValueFieldsByteArray.length != 0) {
8584

8685
// Encode the Return Code
87-
outputStream.write((byte) returnCode);
86+
outputStream.write((byte) reasonCode);
8887

8988
// Write Identifier / Value Fields
9089
outputStream.write(identifierValueFieldsByteArray);
@@ -98,7 +97,7 @@ protected byte[] getVariableHeader() throws MqttException {
9897
}
9998

10099
public int getReturnCode() {
101-
return returnCode;
100+
return reasonCode;
102101
}
103102

104103
@Override
@@ -108,7 +107,7 @@ public MqttProperties getProperties() {
108107

109108
@Override
110109
public String toString() {
111-
return "MqttPubAck [returnCode=" + returnCode + ", properties=" + properties + "]";
110+
return "MqttPubAck [returnCode=" + reasonCode + ", properties=" + properties + "]";
112111
}
113112

114113
}

org.eclipse.paho.mqttv5.common/src/main/java/org/eclipse/paho/mqttv5/common/packet/MqttPubComp.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public class MqttPubComp extends MqttAck {
3434
MqttProperties.USER_DEFINED_PAIR_IDENTIFIER };
3535

3636
// Fields
37-
private int returnCode = MqttReturnCode.RETURN_CODE_SUCCESS;
3837
private MqttProperties properties;
3938

4039
public MqttPubComp(byte[] data) throws IOException, MqttException {
@@ -46,8 +45,8 @@ public MqttPubComp(byte[] data) throws IOException, MqttException {
4645
msgId = dis.readUnsignedShort();
4746
long remainder = (long) data.length - counter.getCounter();
4847
if (remainder > 2) {
49-
returnCode = dis.readUnsignedByte();
50-
validateReturnCode(returnCode, validReturnCodes);
48+
reasonCode = dis.readUnsignedByte();
49+
validateReturnCode(reasonCode, validReturnCodes);
5150
}
5251
if (remainder >= 4) {
5352
this.properties.decodeProperties(dis);
@@ -59,7 +58,7 @@ public MqttPubComp(byte[] data) throws IOException, MqttException {
5958
public MqttPubComp(int returnCode, int msgId, MqttProperties properties) throws MqttException {
6059
super(MqttWireMessage.MESSAGE_TYPE_PUBCOMP);
6160
validateReturnCode(returnCode, validReturnCodes);
62-
this.returnCode = returnCode;
61+
this.reasonCode = returnCode;
6362
this.msgId = msgId;
6463
if (properties != null) {
6564
this.properties = properties;
@@ -80,9 +79,9 @@ protected byte[] getVariableHeader() throws MqttException {
8079

8180
byte[] identifierValueFieldsByteArray = this.properties.encodeProperties();
8281

83-
if (returnCode != MqttReturnCode.RETURN_CODE_SUCCESS || identifierValueFieldsByteArray.length != 0) {
82+
if (reasonCode != MqttReturnCode.RETURN_CODE_SUCCESS || identifierValueFieldsByteArray.length != 0) {
8483
// Encode the Return Code
85-
outputStream.write((byte) returnCode);
84+
outputStream.write((byte) reasonCode);
8685

8786
// Write Identifier / Value Fields
8887
outputStream.write(identifierValueFieldsByteArray);
@@ -96,11 +95,11 @@ protected byte[] getVariableHeader() throws MqttException {
9695
}
9796

9897
public int getReturnCode() {
99-
return returnCode;
98+
return reasonCode;
10099
}
101100

102101
public void setReturnCode(int returnCode) {
103-
this.returnCode = returnCode;
102+
this.reasonCode = returnCode;
104103
}
105104

106105
@Override
@@ -110,7 +109,7 @@ public MqttProperties getProperties() {
110109

111110
@Override
112111
public String toString() {
113-
return "MqttPubComp [returnCode=" + returnCode + ", properties=" + properties + "]";
112+
return "MqttPubComp [returnCode=" + reasonCode + ", properties=" + properties + "]";
114113
}
115114

116115
}

org.eclipse.paho.mqttv5.common/src/main/java/org/eclipse/paho/mqttv5/common/packet/MqttPubRec.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public class MqttPubRec extends MqttAck {
3737
MqttProperties.USER_DEFINED_PAIR_IDENTIFIER };
3838

3939
// Fields
40-
private int returnCode = MqttReturnCode.RETURN_CODE_SUCCESS;
4140
private MqttProperties properties;
4241

4342
public MqttPubRec(byte[] data) throws IOException, MqttException {
@@ -49,8 +48,8 @@ public MqttPubRec(byte[] data) throws IOException, MqttException {
4948
msgId = dis.readUnsignedShort();
5049
long remainder = (long) data.length - counter.getCounter();
5150
if (remainder > 2) {
52-
returnCode = dis.readUnsignedByte();
53-
validateReturnCode(returnCode, validReturnCodes);
51+
reasonCode = dis.readUnsignedByte();
52+
validateReturnCode(reasonCode, validReturnCodes);
5453
}
5554
if (remainder >= 4) {
5655
this.properties.decodeProperties(dis);
@@ -61,7 +60,7 @@ public MqttPubRec(byte[] data) throws IOException, MqttException {
6160
public MqttPubRec(int returnCode, int msgId, MqttProperties properties) throws MqttException {
6261
super(MqttWireMessage.MESSAGE_TYPE_PUBREC);
6362
validateReturnCode(returnCode, validReturnCodes);
64-
this.returnCode = returnCode;
63+
this.reasonCode = returnCode;
6564
this.msgId = msgId;
6665
if (properties != null) {
6766
this.properties = properties;
@@ -82,9 +81,9 @@ protected byte[] getVariableHeader() throws MqttException {
8281

8382
byte[] identifierValueFieldsByteArray = this.properties.encodeProperties();
8483

85-
if (returnCode != MqttReturnCode.RETURN_CODE_SUCCESS || identifierValueFieldsByteArray.length != 0) {
84+
if (reasonCode != MqttReturnCode.RETURN_CODE_SUCCESS || identifierValueFieldsByteArray.length != 0) {
8685
// Encode the Return Code
87-
outputStream.write((byte) returnCode);
86+
outputStream.write((byte) reasonCode);
8887

8988
// Write Identifier / Value Fields
9089
outputStream.write(identifierValueFieldsByteArray);
@@ -98,11 +97,11 @@ protected byte[] getVariableHeader() throws MqttException {
9897
}
9998

10099
public int getReturnCode() {
101-
return returnCode;
100+
return reasonCode;
102101
}
103102

104103
public void setReturnCode(int returnCode) {
105-
this.returnCode = returnCode;
104+
this.reasonCode = returnCode;
106105
}
107106

108107
@Override
@@ -112,7 +111,7 @@ public MqttProperties getProperties() {
112111

113112
@Override
114113
public String toString() {
115-
return "MqttPubRec [returnCode=" + returnCode + ", properties=" + properties + "]";
114+
return "MqttPubRec [returnCode=" + reasonCode + ", properties=" + properties + "]";
116115
}
117116

118117
}

0 commit comments

Comments
 (0)