Class: Protocol::MQTT::Packet::Connack

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

Overview

CONNACK (§3.2) — response to CONNECT.

Variable header: acknowledge_flags (u8, bit 0 = session_present) + reason/return code (u8) + v5 property block.

Constant Summary collapse

TYPE_ID =
CONNACK

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(session_present: false, reason_code: ReasonCodes::SUCCESS, properties: {}) ⇒ Connack

Returns a new instance of Connack.



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

def initialize(session_present: false, reason_code: ReasonCodes::SUCCESS, properties: {})
  @session_present = session_present
  @reason_code = reason_code
  @properties = properties
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



17
18
19
# File 'lib/protocol/mqtt/packet/connack.rb', line 17

def properties
  @properties
end

#reason_codeObject (readonly)

Returns the value of attribute reason_code.



17
18
19
# File 'lib/protocol/mqtt/packet/connack.rb', line 17

def reason_code
  @reason_code
end

#session_presentObject (readonly)

Returns the value of attribute session_present.



17
18
19
# File 'lib/protocol/mqtt/packet/connack.rb', line 17

def session_present
  @session_present
end

Class Method Details

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

Raises:



36
37
38
39
40
41
42
43
# File 'lib/protocol/mqtt/packet/connack.rb', line 36

def self.decode_body(reader, flags:, version:)
  ack_flags = reader.read_u8
  raise MalformedPacket, "reserved CONNACK flag bits must be 0" if (ack_flags & 0b11111110) != 0
  session_present = (ack_flags & 0b1) != 0
  reason_code = reader.read_u8
  properties = version == 5 ? Property.decode(reader) : {}
  new(session_present: session_present, reason_code: reason_code, properties: properties)
end

Instance Method Details

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



46
47
48
49
50
51
# File 'lib/protocol/mqtt/packet/connack.rb', line 46

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

#encode_body(version) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/protocol/mqtt/packet/connack.rb', line 27

def encode_body(version)
  w = Codec::Writer.new
  w.write_u8(@session_present ? 1 : 0)
  w.write_u8(@reason_code)
  w.write(Property.encode(@properties)) if version == 5
  w.bytes
end

#hashObject



55
56
57
# File 'lib/protocol/mqtt/packet/connack.rb', line 55

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