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 : /// Implementation of the variable header for an MQTT Connect message.
11 : class MqttPublishVariableHeader extends MqttVariableHeader {
12 : /// Stores the standard header
13 : MqttHeader header;
14 :
15 : /// Initializes a new instance of the MqttPublishVariableHeader class.
16 4 : MqttPublishVariableHeader(MqttHeader header) {
17 4 : this.header = header;
18 : }
19 :
20 : /// Initializes a new instance of the MqttPublishVariableHeader class.
21 : MqttPublishVariableHeader.fromByteBuffer(MqttHeader header,
22 2 : MqttByteBuffer variableHeaderStream) {
23 2 : this.header = header;
24 2 : readFrom(variableHeaderStream);
25 : }
26 :
27 : /// Creates a variable header from the specified header stream.
28 : void readFrom(MqttByteBuffer variableHeaderStream) {
29 2 : readTopicName(variableHeaderStream);
30 6 : if (this.header.qos == MqttQos.atLeastOnce ||
31 6 : this.header.qos == MqttQos.exactlyOnce) {
32 1 : readMessageIdentifier(variableHeaderStream);
33 : }
34 : }
35 :
36 : /// Writes the variable header to the supplied stream.
37 : void writeTo(MqttByteBuffer variableHeaderStream) {
38 2 : writeTopicName(variableHeaderStream);
39 6 : if (this.header.qos == MqttQos.atLeastOnce ||
40 6 : this.header.qos == MqttQos.exactlyOnce) {
41 2 : writeMessageIdentifier(variableHeaderStream);
42 : }
43 : }
44 :
45 : /// Gets the length of the write data when WriteTo will be called.
46 : int getWriteLength() {
47 : int headerLength = 0;
48 2 : final MqttEncoding enc = new MqttEncoding();
49 6 : headerLength += enc.getByteCount(topicName);
50 6 : if (this.header.qos == MqttQos.atLeastOnce ||
51 6 : this.header.qos == MqttQos.exactlyOnce) {
52 2 : headerLength += 2;
53 : }
54 : return headerLength;
55 : }
56 :
57 : String toString() {
58 12 : return "Publish Variable Header: TopicName={$topicName}, MessageIdentifier={$messageIdentifier}, VH Length={$length}";
59 : }
60 : }
|