Class: Protocol::MQTT::Packet::Disconnect

Inherits:
Protocol::MQTT::Packet show all
Defined in:
lib/protocol/mqtt/packet/disconnect.rb

Overview

DISCONNECT (ยง3.14).

v3.1.1: empty body. v5: reason code (1 byte) + optional property block. Empty body

is equivalent to reason=NORMAL_DISCONNECTION with no props.

Constant Summary collapse

TYPE_ID =
DISCONNECT

Constants inherited from Protocol::MQTT::Packet

AUTH, CONNACK, CONNECT, DISCONNECT, PINGREQ, PINGRESP, PUBACK, PUBCOMP, PUBLISH, PUBREC, PUBREL, SUBACK, SUBSCRIBE, UNSUBACK, UNSUBSCRIBE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Protocol::MQTT::Packet

decode, decode_from_body, #encode, encode_packet, #flags_nibble, register

Constructor Details

#initialize(reason_code: ReasonCodes::NORMAL_DISCONNECTION, properties: {}) ⇒ Disconnect

Returns a new instance of Disconnect.



21
22
23
24
# File 'lib/protocol/mqtt/packet/disconnect.rb', line 21

def initialize(reason_code: ReasonCodes::NORMAL_DISCONNECTION, properties: {})
  @reason_code = reason_code
  @properties = properties
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



18
19
20
# File 'lib/protocol/mqtt/packet/disconnect.rb', line 18

def properties
  @properties
end

#reason_codeObject (readonly)

Returns the value of attribute reason_code.



18
19
20
# File 'lib/protocol/mqtt/packet/disconnect.rb', line 18

def reason_code
  @reason_code
end

Class Method Details

.decode_body(reader, flags:, version:) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/protocol/mqtt/packet/disconnect.rb', line 39

def self.decode_body(reader, flags:, version:)
  if version == 3 || reader.eof?
    return new
  end
  reason_code = reader.read_u8
  properties = reader.eof? ? {} : Property.decode(reader)
  new(reason_code: reason_code, properties: properties)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



49
50
51
52
53
# File 'lib/protocol/mqtt/packet/disconnect.rb', line 49

def ==(other)
  other.is_a?(Disconnect) &&
    other.reason_code == @reason_code &&
    other.properties == @properties
end

#encode_body(version) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/protocol/mqtt/packet/disconnect.rb', line 27

def encode_body(version)
  return "".b.freeze if version == 3
  w = Codec::Writer.new
  if @properties.empty? && @reason_code == ReasonCodes::NORMAL_DISCONNECTION
    return "".b.freeze
  end
  w.write_u8(@reason_code)
  w.write(Property.encode(@properties))
  w.bytes
end

#hashObject



57
58
59
# File 'lib/protocol/mqtt/packet/disconnect.rb', line 57

def hash
  [self.class, @reason_code, @properties].hash
end