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 an MQTT Publish Message, used for publishing telemetry data along a live MQTT stream.
11 : class MqttPublishMessage extends MqttMessage {
12 : /// The variable header contents. Contains extended metadata about the message
13 : MqttPublishVariableHeader variableHeader;
14 :
15 : /// Gets or sets the payload of the Mqtt Message.
16 : MqttPublishPayload payload;
17 :
18 : /// Initializes a new instance of the MqttPublishMessage class.
19 4 : MqttPublishMessage() {
20 12 : this.header = new MqttHeader().asType(MqttMessageType.publish);
21 12 : this.variableHeader = new MqttPublishVariableHeader(this.header);
22 8 : this.payload = new MqttPublishPayload();
23 : }
24 :
25 : /// Initializes a new instance of the MqttPublishMessage class.
26 : MqttPublishMessage.fromByteBuffer(MqttHeader header,
27 2 : MqttByteBuffer messageStream) {
28 2 : this.header = header;
29 2 : readFrom(messageStream);
30 : }
31 :
32 : /// Reads a message from the supplied stream.
33 : void readFrom(MqttByteBuffer messageStream) {
34 2 : super.readFrom(messageStream);
35 4 : this.variableHeader = new MqttPublishVariableHeader.fromByteBuffer(
36 2 : this.header, messageStream);
37 4 : this.payload = new MqttPublishPayload.fromByteBuffer(
38 4 : this.header, this.variableHeader, messageStream);
39 : }
40 :
41 : /// Writes the message to the supplied stream.
42 : void writeTo(MqttByteBuffer messageStream) {
43 4 : final int variableHeaderLength = variableHeader.getWriteLength();
44 4 : final int payloadLength = payload.getWriteLength();
45 6 : this.header.writeTo(variableHeaderLength + payloadLength, messageStream);
46 4 : this.variableHeader.writeTo(messageStream);
47 4 : this.payload.writeTo(messageStream);
48 : }
49 :
50 : /// Sets the topic to publish data to.
51 : MqttPublishMessage toTopic(String topicName) {
52 8 : this.variableHeader.topicName = topicName;
53 : return this;
54 : }
55 :
56 : /// Appends data to publish to the end of the current message payload.
57 : MqttPublishMessage publishData(typed.Uint8Buffer data) {
58 12 : this.payload.message.addAll(data);
59 : return this;
60 : }
61 :
62 : /// Sets the message identifier of the message.
63 : MqttPublishMessage withMessageIdentifier(int messageIdentifier) {
64 8 : this.variableHeader.messageIdentifier = messageIdentifier;
65 : return this;
66 : }
67 :
68 : /// Sets the Qos of the published message.
69 : MqttPublishMessage withQos(MqttQos qos) {
70 8 : this.header.withQos(qos);
71 : return this;
72 : }
73 :
74 : /// Removes the current published data.
75 : MqttPublishMessage clearPublishData() {
76 3 : this.payload.message.clear();
77 : return this;
78 : }
79 :
80 : String toString() {
81 3 : final StringBuffer sb = new StringBuffer();
82 6 : sb.write(super.toString());
83 9 : sb.writeln(variableHeader.toString());
84 9 : sb.writeln(payload.toString());
85 3 : return sb.toString();
86 : }
87 : }
|