Class ConnectPacket

java.lang.Object
org.bluezoo.gumdrop.mqtt.codec.ConnectPacket

public class ConnectPacket extends Object
MQTT CONNECT packet (type 1).

The first packet sent by the client to the server after establishing the network connection. A client MUST send a CONNECT packet as its first packet, and the server MUST treat any other packet received before CONNECT as a protocol violation.

The variable header contains the protocol name, protocol level (version), connect flags, and keep-alive interval. The payload contains, in order: the Client Identifier, Will Topic, Will Message, User Name, and Password — each present only if the corresponding connect flag is set.

Connect Flags (byte 8 of variable header)

Bit 7User Name Flag
Bit 6Password Flag
Bit 5Will Retain
Bits 4–3Will QoS
Bit 2Will Flag
Bit 1Clean Session
Bit 0Reserved (must be 0)
Author:
Chris Burdess
See Also:
  • Constructor Details

    • ConnectPacket

      public ConnectPacket()
      Creates a new CONNECT packet with default values.
  • Method Details

    • getProperties

      public MQTTProperties getProperties()
      Returns the MQTT 5.0 properties for this packet.
      Returns:
      the properties, never null
      See Also:
    • setProperties

      public void setProperties(MQTTProperties properties)
      Sets the MQTT 5.0 properties for this packet.
      Parameters:
      properties - the properties, or null for empty
    • getVersion

      public MQTTVersion getVersion()
      Returns the MQTT protocol version indicated by the Protocol Level field in the variable header.
      Returns:
      the protocol version
      See Also:
    • setVersion

      public void setVersion(MQTTVersion version)
      Sets the MQTT protocol version.
      Parameters:
      version - the protocol version
      See Also:
    • isCleanSession

      public boolean isCleanSession()
      Returns the Clean Session flag (bit 1 of the Connect Flags).

      If true, the client and server MUST discard any previous session state and start a new session. If false, the server MUST resume communication based on the stored session state (subscriptions and pending QoS 1/2 messages).

      In MQTT 5.0 this is renamed to Clean Start.

      Returns:
      true if a clean session is requested
      See Also:
    • setCleanSession

      public void setCleanSession(boolean cleanSession)
      Sets the Clean Session flag.
      Parameters:
      cleanSession - true for a clean (new) session
      See Also:
    • getKeepAlive

      public int getKeepAlive()
      Returns the Keep Alive interval in seconds.

      This is the maximum time interval between control packets sent by the client. If no other packets are sent within this period, the client MUST send a PINGREQ. The server MUST disconnect a client that has not sent a packet within 1.5 times the Keep Alive value.

      A value of 0 means keep-alive is disabled.

      Returns:
      the keep-alive interval in seconds
      See Also:
    • setKeepAlive

      public void setKeepAlive(int keepAlive)
      Sets the Keep Alive interval in seconds.
      Parameters:
      keepAlive - the keep-alive interval (0 to disable)
      See Also:
    • getClientId

      public String getClientId()
      Returns the Client Identifier.

      The Client Identifier uniquely identifies the client to the server and MUST be present in every CONNECT packet. The server uses this to identify session state. In MQTT 3.1.1, it MUST be between 1 and 23 UTF-8 encoded bytes of characters [0-9a-zA-Z], though servers MAY allow longer or richer identifiers.

      An empty Client Identifier is valid only if Clean Session is true; the server then assigns a unique identifier.

      Returns:
      the client identifier, or null/empty if to be assigned
      See Also:
    • setClientId

      public void setClientId(String clientId)
      Sets the Client Identifier.
      Parameters:
      clientId - the client identifier
      See Also:
    • isWillFlag

      public boolean isWillFlag()
      Returns the Will Flag (bit 2 of the Connect Flags).

      If true, a Will Message MUST be stored on the server and published to the Will Topic when the connection is closed unexpectedly (without a DISCONNECT packet). The Will Topic and Will Message fields in the payload MUST be present when this flag is set.

      Returns:
      true if a Will Message is specified
      See Also:
    • setWillFlag

      public void setWillFlag(boolean willFlag)
      Sets the Will Flag.
      Parameters:
      willFlag - true to include a Will Message
      See Also:
    • getWillQoS

      public QoS getWillQoS()
      Returns the Will QoS level (bits 4–3 of the Connect Flags).

      Specifies the QoS level to use when publishing the Will Message. Only meaningful when isWillFlag() is true; if the Will Flag is false, this MUST be 0.

      Returns:
      the Will QoS level, or null if not set
      See Also:
    • setWillQoS

      public void setWillQoS(QoS willQoS)
      Sets the Will QoS level.
      Parameters:
      willQoS - the QoS level for the Will Message
      See Also:
    • isWillRetain

      public boolean isWillRetain()
      Returns the Will Retain flag (bit 5 of the Connect Flags).

      If true, the Will Message MUST be published as a retained message when the will is triggered. Only meaningful when isWillFlag() is true.

      Returns:
      true if the Will Message should be retained
      See Also:
    • setWillRetain

      public void setWillRetain(boolean willRetain)
      Sets the Will Retain flag.
      Parameters:
      willRetain - true to retain the Will Message
      See Also:
    • getWillTopic

      public String getWillTopic()
      Returns the Will Topic.

      The topic to which the Will Message will be published if the client disconnects ungracefully. Present in the payload only when isWillFlag() is true.

      Returns:
      the Will Topic, or null if no will is set
      See Also:
    • setWillTopic

      public void setWillTopic(String willTopic)
      Sets the Will Topic.
      Parameters:
      willTopic - the topic for the Will Message
      See Also:
    • getWillPayload

      public byte[] getWillPayload()
      Returns the Will Message payload.

      The application message payload that the server MUST publish to the Will Topic when the will is triggered. In MQTT 3.1.1 this is limited to 65,535 bytes.

      Returns:
      the Will Message payload, or null if no will is set
      See Also:
    • setWillPayload

      public void setWillPayload(byte[] willPayload)
      Sets the Will Message payload.
      Parameters:
      willPayload - the payload for the Will Message
      See Also:
    • getWillProperties

      public MQTTProperties getWillProperties()
      Returns the Will Properties (MQTT 5.0 only).

      Will Properties appear in the CONNECT payload before the Will Topic when the Will Flag is set. They may include Will Delay Interval, Payload Format Indicator, Message Expiry Interval, Content Type, Response Topic, and Correlation Data.

      Returns:
      the Will Properties, or null if not set
      See Also:
    • setWillProperties

      public void setWillProperties(MQTTProperties willProperties)
      Sets the Will Properties (MQTT 5.0 only).
      Parameters:
      willProperties - the Will Properties
      See Also:
    • getUsername

      public String getUsername()
      Returns the User Name.

      Present in the payload if the User Name Flag (bit 7 of the Connect Flags) is set. Used for authentication; the server MAY use this for authorization as well.

      Returns:
      the user name, or null if not provided
      See Also:
    • setUsername

      public void setUsername(String username)
      Sets the User Name.
      Parameters:
      username - the user name
      See Also:
    • getPassword

      public byte[] getPassword()
      Returns the Password.

      Present in the payload if the Password Flag (bit 6 of the Connect Flags) is set. In MQTT 3.1.1, the User Name Flag MUST be set if the Password Flag is set. In MQTT 5.0, a password may be sent without a user name.

      Returns:
      the password as a byte array, or null if not provided
      See Also:
    • setPassword

      public void setPassword(byte[] password)
      Sets the Password.
      Parameters:
      password - the password as a byte array
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object