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 Subscribe Message.
11 : class MqttSubscribeMessage extends MqttMessage {
12 : /// Gets or sets the variable header contents. Contains extended metadata about the message
13 : MqttSubscribeVariableHeader variableHeader;
14 :
15 : /// Gets or sets the payload of the Mqtt Message.
16 : MqttSubscribePayload payload;
17 :
18 : String _lastTopic;
19 :
20 : /// Initializes a new instance of the MqttSubscribeMessage class.
21 2 : MqttSubscribeMessage() {
22 6 : this.header = new MqttHeader().asType(MqttMessageType.subscribe);
23 4 : this.variableHeader = new MqttSubscribeVariableHeader();
24 4 : this.payload = new MqttSubscribePayload();
25 : }
26 :
27 : /// Initializes a new instance of the MqttSubscribeMessage class.
28 : MqttSubscribeMessage.fromByteBuffer(MqttHeader header,
29 1 : MqttByteBuffer messageStream) {
30 1 : this.header = header;
31 1 : readFrom(messageStream);
32 : }
33 :
34 : /// Writes the message to the supplied stream.
35 : void writeTo(MqttByteBuffer messageStream) {
36 2 : this.header.writeTo(
37 5 : this.variableHeader.getWriteLength() + this.payload.getWriteLength(),
38 : messageStream);
39 2 : this.variableHeader.writeTo(messageStream);
40 2 : this.payload.writeTo(messageStream);
41 : }
42 :
43 : /// Reads a message from the supplied stream.
44 : void readFrom(MqttByteBuffer messageStream) {
45 1 : this.variableHeader =
46 1 : new MqttSubscribeVariableHeader.fromByteBuffer(messageStream);
47 2 : this.payload = new MqttSubscribePayload.fromByteBuffer(
48 2 : header, variableHeader, messageStream);
49 : }
50 :
51 : /// Adds a new subscription topic with the AtMostOnce Qos Level. If you want to change the
52 : /// Qos level follow this call with a call to AtTopic(MqttQos)
53 : MqttSubscribeMessage toTopic(String topic) {
54 2 : _lastTopic = topic;
55 4 : this.payload.addSubscription(topic, MqttQos.atMostOnce);
56 : return this;
57 : }
58 :
59 : /// Sets the Qos level of the last topic added to the subscription list via a call to ToTopic(string)
60 : MqttSubscribeMessage atQos(MqttQos qos) {
61 8 : if (this.payload.subscriptions.containsKey(_lastTopic)) {
62 8 : this.payload.subscriptions[_lastTopic] = qos;
63 : }
64 : return this;
65 : }
66 :
67 : /// Sets the message identifier on the subscribe message.
68 : MqttSubscribeMessage withMessageIdentifier(int messageIdentifier) {
69 4 : this.variableHeader.messageIdentifier = messageIdentifier;
70 : return this;
71 : }
72 :
73 : /// Sets the message up to request acknowledgement from the broker for each topic subscription.
74 : MqttSubscribeMessage expectAcknowledgement() {
75 2 : this.header.withQos(MqttQos.atLeastOnce);
76 : return this;
77 : }
78 :
79 : /// Sets the duplicate flag for the message to indicate its a duplicate of a previous message type
80 : /// with the same message identifier.
81 : MqttSubscribeMessage isDuplicate() {
82 2 : this.header.isDuplicate();
83 : return this;
84 : }
85 :
86 : String toString() {
87 2 : final StringBuffer sb = new StringBuffer();
88 4 : sb.write(super.toString());
89 6 : sb.writeln(variableHeader.toString());
90 6 : sb.writeln(payload.toString());
91 2 : return sb.toString();
92 : }
93 : }
|