1 /** 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * SPDX-License-Identifier: Apache-2.0. 4 */ 5 package software.amazon.awssdk.crt.mqtt5.packets; 6 7 import java.util.List; 8 9 /** 10 * Data model of an <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901033">MQTT5 CONNECT</a> packet. 11 */ 12 public class ConnectPacket { 13 14 private Long keepAliveIntervalSeconds; 15 private String clientId; 16 private String username; 17 private byte[] password; 18 private Long sessionExpiryIntervalSeconds; 19 private Boolean requestResponseInformation; 20 private Boolean requestProblemInformation; 21 private Long receiveMaximum; 22 private Long maximumPacketSizeBytes; 23 private Long willDelayIntervalSeconds; 24 private PublishPacket will; 25 private List<UserProperty> userProperties; 26 27 /** 28 * Creates a ConnectPacket instance using the provided ConnectPacketBuilder 29 */ ConnectPacket(ConnectPacketBuilder builder)30 private ConnectPacket(ConnectPacketBuilder builder) { 31 this.keepAliveIntervalSeconds = builder.keepAliveIntervalSeconds; 32 this.clientId = builder.clientId; 33 this.username = builder.username; 34 this.password = builder.password; 35 this.sessionExpiryIntervalSeconds = builder.sessionExpiryIntervalSeconds; 36 this.requestResponseInformation = builder.requestResponseInformation; 37 this.requestProblemInformation = builder.requestProblemInformation; 38 this.receiveMaximum = builder.receiveMaximum; 39 this.maximumPacketSizeBytes = builder.maximumPacketSizeBytes; 40 this.willDelayIntervalSeconds = builder.willDelayIntervalSeconds; 41 this.will = builder.will; 42 this.userProperties = builder.userProperties; 43 } 44 45 /** 46 * Returns the maximum time interval, in seconds, that is permitted to elapse between the point at which the client 47 * finishes transmitting one MQTT packet and the point it starts sending the next. The client will use 48 * PINGREQ packets to maintain this property. 49 * 50 * If the responding ConnAckPacket contains a keep alive property value, then that is the negotiated keep alive value. 51 * Otherwise, the keep alive sent by the client is the negotiated value. 52 * 53 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901045">MQTT5 Keep Alive</a> 54 * 55 * @return The maximum time interval, in seconds, that is permitted to elapse between the point at which the client 56 * finishes transmitting one MQTT packet and the point it starts sending the next. 57 */ getKeepAliveIntervalSeconds()58 public Long getKeepAliveIntervalSeconds() 59 { 60 return this.keepAliveIntervalSeconds; 61 } 62 63 /** 64 * Returns a unique string identifying the client to the server. Used to restore session state between connections. 65 * 66 * If left empty, the broker will auto-assign a unique client id. When reconnecting, the Mqtt5Client will 67 * always use the auto-assigned client id. 68 * 69 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901059">MQTT5 Client Identifier</a> 70 * 71 * @return A unique string identifying the client to the server. 72 */ getClientId()73 public String getClientId() 74 { 75 return this.clientId; 76 } 77 78 /** 79 * Returns a string value that the server may use for client authentication and authorization. 80 * 81 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901071">MQTT5 User Name</a> 82 * 83 * @return A string value that the server may use for client authentication and authorization. 84 */ getUsername()85 public String getUsername() 86 { 87 return this.username; 88 } 89 90 /** 91 * Returns opaque binary data that the server may use for client authentication and authorization. 92 * 93 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901072">MQTT5 Password</a> 94 * 95 * @return Opaque binary data that the server may use for client authentication and authorization. 96 */ getPassword()97 public byte[] getPassword() 98 { 99 return this.password; 100 } 101 102 /** 103 * Returns a time interval, in seconds, that the client requests the server to persist this connection's MQTT session state 104 * for. Has no meaning if the client has not been configured to rejoin sessions. Must be non-zero in order to 105 * successfully rejoin a session. 106 * 107 * If the responding ConnAckPacket contains a session expiry property value, then that is the negotiated session expiry 108 * value. Otherwise, the session expiry sent by the client is the negotiated value. 109 * 110 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901048">MQTT5 Session Expiry Interval</a> 111 * 112 * @return A time interval, in seconds, that the client requests the server to persist this connection's MQTT session 113 * state for. 114 */ getSessionExpiryIntervalSeconds()115 public Long getSessionExpiryIntervalSeconds() 116 { 117 return this.sessionExpiryIntervalSeconds; 118 } 119 120 /** 121 * Returns a boolean that, if true, requests that the server send response information in the subsequent ConnAckPacket. This response 122 * information may be used to set up request-response implementations over MQTT, but doing so is outside 123 * the scope of the MQTT5 spec and client. 124 * 125 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901052">MQTT5 Request Response Information</a> 126 * 127 * @return If true, requests that the server send response information in the subsequent ConnAckPacket. 128 */ getRequestResponseInformation()129 public Boolean getRequestResponseInformation() 130 { 131 return this.requestResponseInformation; 132 } 133 134 /** 135 * Returns a boolean that, if true, requests that the server send additional diagnostic information (via response string or 136 * user properties) in DisconnectPacket or ConnAckPacket from the server. 137 * 138 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901053">MQTT5 Request Problem Information</a> 139 * 140 * @return If true, requests that the server send additional diagnostic information (via response string or 141 * user properties) in DisconnectPacket or ConnAckPacket from the server. 142 */ getRequestProblemInformation()143 public Boolean getRequestProblemInformation() 144 { 145 return this.requestProblemInformation; 146 } 147 148 /** 149 * Returns the maximum number of in-flight QoS 1 and 2 messages the client is willing to handle. If 150 * omitted or null, then no limit is requested. 151 * 152 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901049">MQTT5 Receive Maximum</a> 153 * 154 * @return The maximum number of in-flight QoS 1 and 2 messages the client is willing to handle. 155 */ getReceiveMaximum()156 public Long getReceiveMaximum() 157 { 158 return this.receiveMaximum; 159 } 160 161 /** 162 * Returns the maximum packet size the client is willing to handle. If 163 * omitted or null, then no limit beyond the natural limits of MQTT packet size is requested. 164 * 165 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901050">MQTT5 Maximum Packet Size</a> 166 * 167 * @return The maximum packet size the client is willing to handle 168 */ getMaximumPacketSizeBytes()169 public Long getMaximumPacketSizeBytes() 170 { 171 return this.maximumPacketSizeBytes; 172 } 173 174 /** 175 * Returns a time interval, in seconds, that the server should wait (for a session reconnection) before sending the 176 * will message associated with the connection's session. If omitted or null, the server will send the will when the 177 * associated session is destroyed. If the session is destroyed before a will delay interval has elapsed, then 178 * the will must be sent at the time of session destruction. 179 * 180 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901062">MQTT5 Will Delay Interval</a> 181 * 182 * @return A time interval, in seconds, that the server should wait (for a session reconnection) before sending the 183 * will message associated with the connection's session. 184 */ getWillDelayIntervalSeconds()185 public Long getWillDelayIntervalSeconds() 186 { 187 return this.willDelayIntervalSeconds; 188 } 189 190 /** 191 * Returns the definition of a message to be published when the connection's session is destroyed by the server or when 192 * the will delay interval has elapsed, whichever comes first. If null, then nothing will be sent. 193 * 194 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901040">MQTT5 Will</a> 195 * 196 * @return The message to be published when the connection's session is destroyed by the server or when 197 * the will delay interval has elapsed, whichever comes first. 198 */ getWill()199 public PublishPacket getWill() 200 { 201 return this.will; 202 } 203 204 /** 205 * Returns a list of MQTT5 user properties included with the packet. 206 * 207 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901054">MQTT5 User Property</a> 208 * 209 * @return List of MQTT5 user properties included with the packet. 210 */ getUserProperties()211 public List<UserProperty> getUserProperties() 212 { 213 return this.userProperties; 214 } 215 216 /******************************************************************************* 217 * builder 218 ******************************************************************************/ 219 220 /** 221 * A class to that allows for the creation of a ConnectPacket. Set all of the settings you want in the 222 * packet and then use the build() function to get a ConnectPacket populated with the settings 223 * defined in the builder. 224 */ 225 static final public class ConnectPacketBuilder { 226 private Long keepAliveIntervalSeconds = 1200L; 227 private String clientId; 228 private String username; 229 private byte[] password; 230 private Long sessionExpiryIntervalSeconds; 231 private Boolean requestResponseInformation; 232 private Boolean requestProblemInformation; 233 private Long receiveMaximum; 234 private Long maximumPacketSizeBytes; 235 private Long willDelayIntervalSeconds; 236 private PublishPacket will = null; 237 private List<UserProperty> userProperties; 238 239 /** 240 * Sets the maximum time interval, in seconds, that is permitted to elapse between the point at which the client 241 * finishes transmitting one MQTT packet and the point it starts sending the next. The client will use 242 * PINGREQ packets to maintain this property. 243 * 244 * If the responding ConnAckPacket contains a keep alive property value, then that is the negotiated keep alive value. 245 * Otherwise, the keep alive sent by the client is the negotiated value. 246 * 247 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901045">MQTT5 Keep Alive</a> 248 * 249 * NOTE: The keepAliveIntervalSeconds HAS to be larger than the pingTimeoutMs time set in the Mqtt5ClientOptions. 250 * 251 * @param keepAliveInteralSeconds the maximum time interval, in seconds, that is permitted to elapse between the point 252 * at which the client finishes transmitting one MQTT packet and the point it starts sending the next. 253 * @return The ConnectPacketBuilder after setting the keep alive interval. 254 */ withKeepAliveIntervalSeconds(Long keepAliveInteralSeconds)255 public ConnectPacketBuilder withKeepAliveIntervalSeconds(Long keepAliveInteralSeconds) 256 { 257 this.keepAliveIntervalSeconds = keepAliveInteralSeconds; 258 return this; 259 } 260 261 /** 262 * Sets the unique string identifying the client to the server. Used to restore session state between connections. 263 * 264 * If left empty, the broker will auto-assign a unique client id. When reconnecting, the Mqtt5Client will 265 * always use the auto-assigned client id. 266 * 267 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901059">MQTT5 Client Identifier</a> 268 * 269 * @param clientId A unique string identifying the client to the server. 270 * @return The ConnectPacketBuilder after setting the client ID. 271 */ withClientId(String clientId)272 public ConnectPacketBuilder withClientId(String clientId) 273 { 274 this.clientId = clientId; 275 return this; 276 } 277 278 /** 279 * Sets the string value that the server may use for client authentication and authorization. 280 * 281 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901071">MQTT5 User Name</a> 282 * 283 * @param username The string value that the server may use for client authentication and authorization. 284 * @return The ConnectPacketBuilder after setting the username. 285 */ withUsername(String username)286 public ConnectPacketBuilder withUsername(String username) 287 { 288 this.username = username; 289 return this; 290 } 291 292 /** 293 * Sets the opaque binary data that the server may use for client authentication and authorization. 294 * 295 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901072">MQTT5 Password</a> 296 * 297 * @param password Opaque binary data that the server may use for client authentication and authorization. 298 * @return The ConnectPacketBuilder after setting the password. 299 */ withPassword(byte[] password)300 public ConnectPacketBuilder withPassword(byte[] password) 301 { 302 this.password = password; 303 return this; 304 } 305 306 /** 307 * Sets the time interval, in seconds, that the client requests the server to persist this connection's MQTT session state 308 * for. Has no meaning if the client has not been configured to rejoin sessions. Must be non-zero in order to 309 * successfully rejoin a session. 310 * 311 * If the responding ConnAckPacket contains a session expiry property value, then that is the negotiated session expiry 312 * value. Otherwise, the session expiry sent by the client is the negotiated value. 313 * 314 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901048">MQTT5 Session Expiry Interval</a> 315 * 316 * @param sessionExpiryIntervalSeconds A time interval, in seconds, that the client requests the server to persist this 317 * connection's MQTT session state for. 318 * @return The ConnectPacketBuilder after setting the session expiry interval. 319 */ withSessionExpiryIntervalSeconds(Long sessionExpiryIntervalSeconds)320 public ConnectPacketBuilder withSessionExpiryIntervalSeconds(Long sessionExpiryIntervalSeconds) 321 { 322 this.sessionExpiryIntervalSeconds = sessionExpiryIntervalSeconds; 323 return this; 324 } 325 326 /** 327 * Sets whether requests that the server send response information in the subsequent ConnAckPacket. This response 328 * information may be used to set up request-response implementations over MQTT, but doing so is outside 329 * the scope of the MQTT5 spec and client. 330 * 331 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901052">MQTT5 Request Response Information</a> 332 * 333 * @param requestResponseInformation If true, requests that the server send response information in the subsequent ConnAckPacket. 334 * @return The ConnectPacketBuilder after setting the request response information. 335 */ withRequestResponseInformation(Boolean requestResponseInformation)336 public ConnectPacketBuilder withRequestResponseInformation(Boolean requestResponseInformation) 337 { 338 this.requestResponseInformation = requestResponseInformation; 339 return this; 340 } 341 342 /** 343 * Sets whether requests that the server send additional diagnostic information (via response string or 344 * user properties) in DisconnectPacket or ConnAckPacket from the server. 345 * 346 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901053">MQTT5 Request Problem Information</a> 347 * 348 * @param requestProblemInformation If true, requests that the server send additional diagnostic information 349 * (via response string or user properties) in DisconnectPacket or ConnAckPacket from the server. 350 * @return The ConnectPacketBuilder after setting the request problem information. 351 */ withRequestProblemInformation(Boolean requestProblemInformation)352 public ConnectPacketBuilder withRequestProblemInformation(Boolean requestProblemInformation) 353 { 354 this.requestProblemInformation = requestProblemInformation; 355 return this; 356 } 357 358 /** 359 * Sets the maximum number of in-flight QoS 1 and 2 messages the client is willing to handle. If 360 * omitted or null, then no limit is requested. 361 * 362 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901049">MQTT5 Receive Maximum</a> 363 * 364 * @param receiveMaximum The maximum number of in-flight QoS 1 and 2 messages the client is willing to handle. 365 * @return The ConnectPacketBuilder after setting the receive maximum. 366 */ withReceiveMaximum(Long receiveMaximum)367 public ConnectPacketBuilder withReceiveMaximum(Long receiveMaximum) 368 { 369 this.receiveMaximum = receiveMaximum; 370 return this; 371 } 372 373 /** 374 * Sets the maximum packet size the client is willing to handle. If 375 * omitted or null, then no limit beyond the natural limits of MQTT packet size is requested. 376 * 377 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901050">MQTT5 Maximum Packet Size</a> 378 * 379 * @param maximumPacketSizeBytes The maximum packet size the client is willing to handle 380 * @return The ConnectPacketBuilder after setting the maximum packet size. 381 */ withMaximumPacketSizeBytes(Long maximumPacketSizeBytes)382 public ConnectPacketBuilder withMaximumPacketSizeBytes(Long maximumPacketSizeBytes) 383 { 384 this.maximumPacketSizeBytes = maximumPacketSizeBytes; 385 return this; 386 } 387 388 /** 389 * Sets the time interval, in seconds, that the server should wait (for a session reconnection) before sending the 390 * will message associated with the connection's session. If omitted or null, the server will send the will when the 391 * associated session is destroyed. If the session is destroyed before a will delay interval has elapsed, then 392 * the will must be sent at the time of session destruction. 393 * 394 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901062">MQTT5 Will Delay Interval</a> 395 * 396 * @param willDelayIntervalSeconds A time interval, in seconds, that the server should wait (for a session reconnection) 397 * before sending the will message associated with the connection's session. 398 * @return The ConnectPacketBuilder after setting the will message delay interval. 399 */ withWillDelayIntervalSeconds(Long willDelayIntervalSeconds)400 public ConnectPacketBuilder withWillDelayIntervalSeconds(Long willDelayIntervalSeconds) 401 { 402 this.willDelayIntervalSeconds = willDelayIntervalSeconds; 403 return this; 404 } 405 406 /** 407 * Sets the definition of a message to be published when the connection's session is destroyed by the server or when 408 * the will delay interval has elapsed, whichever comes first. If null, then nothing will be sent. 409 * 410 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901040">MQTT5 Will</a> 411 * 412 * @param will The message to be published when the connection's session is destroyed by the server or when 413 * the will delay interval has elapsed, whichever comes first. 414 * @return The ConnectPacketBuilder after setting the will message. 415 */ withWill(PublishPacket will)416 public ConnectPacketBuilder withWill(PublishPacket will) 417 { 418 this.will = will; 419 return this; 420 } 421 422 /** 423 * Sets the list of MQTT5 user properties included with the packet. 424 * 425 * See <a href="https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901054">MQTT5 User Property</a> 426 * 427 * @param userProperties List of MQTT5 user properties included with the packet. 428 * @return The ConnectPacketBuilder after setting the user properties. 429 */ withUserProperties(List<UserProperty> userProperties)430 public ConnectPacketBuilder withUserProperties(List<UserProperty> userProperties) 431 { 432 this.userProperties = userProperties; 433 return this; 434 } 435 436 /** 437 * Creates a new ConnectPacketBuilder so a ConnectPacket can be created. 438 */ ConnectPacketBuilder()439 public ConnectPacketBuilder() {} 440 441 /** 442 * Creates a new ConnectPacket using the settings set in the builder. 443 * @return The ConnectPacket created from the builder 444 */ build()445 public ConnectPacket build() 446 { 447 return new ConnectPacket(this); 448 } 449 } 450 } 451