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 : /// An Mqtt message that is used to initiate a connection to a message broker.
11 : class MqttConnectMessage extends MqttMessage {
12 : /// Initializes a new instance of the MqttConnectMessage class.
13 : /// Only called via the MqttMessage.create operation during processing of an Mqtt message stream.
14 4 : MqttConnectMessage() {
15 12 : this.header = new MqttHeader().asType(MqttMessageType.connect);
16 8 : this.variableHeader = new MqttConnectVariableHeader();
17 12 : this.payload = new MqttConnectPayload(this.variableHeader);
18 : }
19 :
20 : /// Initializes a new instance of the <see cref="MqttConnectMessage" /> class.
21 : MqttConnectMessage.fromByteBuffer(MqttHeader header,
22 4 : MqttByteBuffer messageStream) {
23 4 : this.header = header;
24 4 : readFrom(messageStream);
25 : }
26 :
27 : /// Sets the name of the protocol to use.
28 : MqttConnectMessage withProtocolName(String protocolName) {
29 2 : this.variableHeader.protocolName = protocolName;
30 : return this;
31 : }
32 :
33 : /// Sets the protocol version. (Defaults to v3, the only protcol version supported)
34 : MqttConnectMessage withProtocolVersion(int protocolVersion) {
35 2 : this.variableHeader.protocolVersion = protocolVersion;
36 : return this;
37 : }
38 :
39 : /// Sets the startClean flag so that the broker drops any messages that were previously destined for us.
40 : MqttConnectMessage startClean() {
41 6 : this.variableHeader.connectFlags.cleanStart = true;
42 : return this;
43 : }
44 :
45 : /// Sets the keep alive period
46 : MqttConnectMessage keepAliveFor(int keepAliveSeconds) {
47 4 : this.variableHeader.keepAlive = keepAliveSeconds;
48 : return this;
49 : }
50 :
51 : /// Sets the Will flag of the variable header
52 : MqttConnectMessage will() {
53 3 : this.variableHeader.connectFlags.willFlag = true;
54 : return this;
55 : }
56 :
57 : /// Sets the WillQos of the connect flag.
58 : MqttConnectMessage withWillQos(MqttQos qos) {
59 3 : this.variableHeader.connectFlags.willQos = qos;
60 : return this;
61 : }
62 :
63 : /// Sets the WillRetain flag of the Connection Flags
64 : MqttConnectMessage withWillRetain() {
65 3 : this.variableHeader.connectFlags.willRetain = true;
66 : return this;
67 : }
68 :
69 : /// Sets the client identifier of the message.
70 : MqttConnectMessage withClientIdentifier(String clientIdentifier) {
71 8 : this.payload.clientIdentifier = clientIdentifier;
72 : return this;
73 : }
74 :
75 : /// Sets the will message.
76 : MqttConnectMessage withWillMessage(String willMessage) {
77 2 : this.payload.willMessage = willMessage;
78 : return this;
79 : }
80 :
81 : /// Sets the Will Topic
82 : MqttConnectMessage withWillTopic(String willTopic) {
83 2 : this.payload.willTopic = willTopic;
84 : return this;
85 : }
86 :
87 : /// Sets the authentication
88 : MqttConnectMessage authenticateAs(String username, String password) {
89 : if (username != null) {
90 4 : this.variableHeader.connectFlags.usernameFlag = username.isNotEmpty;
91 2 : this.payload.username = username;
92 : }
93 : if (password != null) {
94 4 : this.variableHeader.connectFlags.passwordFlag = password.isNotEmpty;
95 2 : this.payload.password = password;
96 : }
97 : return this;
98 : }
99 :
100 : /// The variable header contents. Contains extended metadata about the message
101 : MqttConnectVariableHeader variableHeader;
102 :
103 : /// The payload of the Mqtt Message.
104 : MqttConnectPayload payload;
105 :
106 : /// Writes the message to the supplied stream.
107 : void writeTo(MqttByteBuffer messageStream) {
108 8 : this.header.writeTo(
109 20 : variableHeader.getWriteLength() + payload.getWriteLength(),
110 : messageStream);
111 8 : this.variableHeader.writeTo(messageStream);
112 8 : this.payload.writeTo(messageStream);
113 : }
114 :
115 : /// Reads a message from the supplied stream.
116 : void readFrom(MqttByteBuffer messageStream) {
117 4 : this.variableHeader =
118 4 : new MqttConnectVariableHeader.fromByteBuffer(messageStream);
119 8 : this.payload = new MqttConnectPayload.fromByteBuffer(
120 4 : this.variableHeader, messageStream);
121 : }
122 :
123 : String toString() {
124 4 : final StringBuffer sb = new StringBuffer();
125 8 : sb.write(super.toString());
126 12 : sb.writeln(variableHeader.toString());
127 12 : sb.writeln(payload.toString());
128 4 : return sb.toString();
129 : }
130 : }
|