Line data Source code
1 : /*
2 : * Package : mqtt_client
3 : * Author : S. Hamblett <steve.hamblett@linux.com>
4 : * Date : 19/06/2017
5 : * Copyright : S.Hamblett
6 : */
7 :
8 : part of mqtt_client;
9 :
10 : /// Class that contains details related to an MQTT Subscribe Ack messages payload
11 : class MqttSubscribeAckPayload extends MqttPayload {
12 : MqttVariableHeader variableHeader;
13 : MqttHeader header;
14 :
15 : /// The collection of Qos grants, Key is the topic, Value is the qos
16 : List<MqttQos> qosGrants = new List<MqttQos>();
17 :
18 : /// Initializes a new instance of the MqttSubscribeAckPayload class.
19 2 : MqttSubscribeAckPayload();
20 :
21 : /// Initializes a new instance of the MqttSubscribeAckPayload class.
22 : MqttSubscribeAckPayload.fromByteBuffer(MqttHeader header,
23 : MqttSubscribeAckVariableHeader variableHeader,
24 1 : MqttByteBuffer payloadStream) {
25 1 : this.header = header;
26 1 : this.variableHeader = variableHeader;
27 1 : readFrom(payloadStream);
28 : }
29 :
30 : /// Writes the payload to the supplied stream.
31 : void writeTo(MqttByteBuffer payloadStream) {
32 2 : for (MqttQos value in qosGrants) {
33 2 : payloadStream.writeByte(value.index);
34 : }
35 : }
36 :
37 : /// Creates a payload from the specified header stream.
38 : void readFrom(MqttByteBuffer payloadStream) {
39 : int payloadBytesRead = 0;
40 5 : final int payloadLength = header.messageSize - variableHeader.length;
41 : // Read the qos grants from the message payload
42 1 : while (payloadBytesRead < payloadLength) {
43 2 : final MqttQos granted = MqttQos.values[payloadStream.readByte()];
44 1 : payloadBytesRead++;
45 1 : addGrant(granted);
46 : }
47 : }
48 :
49 : /// Gets the length of the payload in bytes when written to a stream.
50 : int getWriteLength() {
51 2 : return qosGrants.length;
52 : }
53 :
54 : /// Adds a new QosGrant to the collection of QosGrants
55 : void addGrant(MqttQos grantedQos) {
56 4 : qosGrants.add(grantedQos);
57 : }
58 :
59 : /// Clears the grants.
60 : void clearGrants() {
61 2 : qosGrants.clear();
62 : }
63 :
64 : String toString() {
65 1 : final StringBuffer sb = new StringBuffer();
66 4 : sb.writeln("Payload: Qos grants [{${qosGrants.length}}]");
67 2 : for (MqttQos value in qosGrants) {
68 2 : sb.writeln("{{ Grant={$value} }}");
69 : }
70 1 : return sb.toString();
71 : }
72 : }
|