Line data Source code
1 : /*
2 : * Package : mqtt_client
3 : * Author : S. Hamblett <steve.hamblett@linux.com>
4 : * Date : 31/05/2017
5 : * Copyright : S.Hamblett
6 : */
7 :
8 : part of mqtt_client;
9 :
10 : /// Enumeration used by subclasses to tell the variable header what should be read from the underlying stream.
11 : enum readWriteFlags {
12 : none,
13 : protocolName,
14 : protocolVersion,
15 : connectFlags,
16 : keepAlive,
17 : returnCode,
18 : topicName,
19 : messageIdentifier
20 : }
21 :
22 : /// Represents the base class for the Variable Header portion of some MQTT Messages.
23 : class MqttVariableHeader {
24 : /// The length, in bytes, consumed by the variable header.
25 : int length = 0;
26 :
27 : String protocolName = "";
28 : int protocolVersion = 0;
29 : MqttConnectFlags connectFlags;
30 :
31 : /// Defines the maximum allowable lag, in seconds, between expected messages.
32 : /// The spec indicates that clients won't be disconnected until KeepAlive + 1/2 KeepAlive time period
33 : /// elapses.
34 : int keepAlive = 0;
35 :
36 : MqttConnectReturnCode returnCode = MqttConnectReturnCode.brokerUnavailable;
37 : String topicName = "";
38 : int messageIdentifier = 0;
39 :
40 : /// Initializes a new instance of the MqttVariableHeader class.
41 6 : MqttVariableHeader() {
42 12 : this.protocolName = Protocol.name;
43 12 : this.protocolVersion = Protocol.version;
44 12 : this.connectFlags = new MqttConnectFlags();
45 : }
46 :
47 : /// Initializes a new instance of the MqttVariableHeader class, populating it with data from a stream.
48 4 : MqttVariableHeader.fromByteBuffer(MqttByteBuffer headerStream) {
49 4 : readFrom(headerStream);
50 : }
51 :
52 : /// Creates a variable header from the specified header stream.
53 : /// A subclass can override this method to do completely custom read operations
54 : /// if required.
55 : void readFrom(MqttByteBuffer variableHeaderStream) {
56 1 : readProtocolName(variableHeaderStream);
57 1 : readProtocolVersion(variableHeaderStream);
58 1 : readConnectFlags(variableHeaderStream);
59 1 : readKeepAlive(variableHeaderStream);
60 1 : readReturnCode(variableHeaderStream);
61 1 : readTopicName(variableHeaderStream);
62 1 : readMessageIdentifier(variableHeaderStream);
63 : }
64 :
65 : /// Writes the variable header to the supplied stream.
66 : /// A subclass can override this method to do completely custom write operations
67 : /// if required.
68 : void writeTo(MqttByteBuffer variableHeaderStream) {
69 1 : writeProtocolName(variableHeaderStream);
70 1 : writeProtocolVersion(variableHeaderStream);
71 1 : writeConnectFlags(variableHeaderStream);
72 1 : writeKeepAlive(variableHeaderStream);
73 1 : writeReturnCode(variableHeaderStream);
74 1 : writeTopicName(variableHeaderStream);
75 1 : writeMessageIdentifier(variableHeaderStream);
76 : }
77 :
78 : /// Gets the length of the write data when WriteTo will be called.
79 : /// A subclass that overrides writeTo must also overwrite this method.
80 : int getWriteLength() {
81 : int headerLength = 0;
82 1 : final MqttEncoding enc = new MqttEncoding();
83 3 : headerLength += enc.getByteCount(protocolName);
84 1 : headerLength += 1; // protocolVersion
85 2 : headerLength += MqttConnectFlags.getWriteLength();
86 1 : headerLength += 2; // keepAlive
87 1 : headerLength += 1; // returnCode
88 4 : headerLength += enc.getByteCount(topicName.toString());
89 1 : headerLength += 2; // MessageIdentifier
90 : return headerLength;
91 : }
92 :
93 : /// Write functions
94 :
95 : void writeProtocolName(MqttByteBuffer stream) {
96 8 : MqttByteBuffer.writeMqttString(stream, protocolName);
97 : }
98 :
99 : void writeProtocolVersion(MqttByteBuffer stream) {
100 8 : stream.writeByte(protocolVersion);
101 : }
102 :
103 : void writeKeepAlive(MqttByteBuffer stream) {
104 8 : stream.writeShort(keepAlive);
105 : }
106 :
107 : void writeReturnCode(MqttByteBuffer stream) {
108 12 : stream.writeByte(returnCode.index);
109 : }
110 :
111 : void writeTopicName(MqttByteBuffer stream) {
112 6 : MqttByteBuffer.writeMqttString(stream, topicName.toString());
113 : }
114 :
115 : void writeMessageIdentifier(MqttByteBuffer stream) {
116 4 : stream.writeShort(messageIdentifier);
117 : }
118 :
119 : void writeConnectFlags(MqttByteBuffer stream) {
120 8 : connectFlags.writeTo(stream);
121 : }
122 :
123 : /// Read functions
124 :
125 : void readProtocolName(MqttByteBuffer stream) {
126 8 : protocolName = MqttByteBuffer.readMqttString(stream);
127 20 : length += protocolName.length + 2; // 2 for length short at front of string
128 : }
129 :
130 : void readProtocolVersion(MqttByteBuffer stream) {
131 8 : protocolVersion = stream.readByte();
132 8 : length++;
133 : }
134 :
135 : void readKeepAlive(MqttByteBuffer stream) {
136 8 : keepAlive = stream.readShort();
137 8 : length += 2;
138 : }
139 :
140 : void readReturnCode(MqttByteBuffer stream) {
141 12 : returnCode = MqttConnectReturnCode.values[stream.readByte()];
142 8 : length++;
143 : }
144 :
145 : void readTopicName(MqttByteBuffer stream) {
146 4 : topicName = MqttByteBuffer.readMqttString(stream);
147 10 : length += topicName.length + 2; // 2 for length short at front of string.
148 : }
149 :
150 : void readMessageIdentifier(MqttByteBuffer stream) {
151 4 : messageIdentifier = stream.readShort();
152 4 : length += 2;
153 : }
154 :
155 : void readConnectFlags(MqttByteBuffer stream) {
156 8 : connectFlags = new MqttConnectFlags.fromByteBuffer(stream);
157 8 : length += 1;
158 : }
159 : }
|