Class: Protocol::MQTT::Packet::Suback

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

Overview

SUBACK (ยง3.9).

Variable header: packet_id (u16) + v5 property block. Payload: one reason code byte per filter from the originating SUBSCRIBE, in the same order.

In v3.1.1 codes are: 0x00..0x02 granted QoS, 0x80 failure. In v5 the full reason-code taxonomy applies.

Constant Summary collapse

TYPE_ID =
SUBACK

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(packet_id:, reason_codes:, properties: {}) ⇒ Suback

Returns a new instance of Suback.

Raises:

  • (ArgumentError)


24
25
26
27
28
29
# File 'lib/protocol/mqtt/packet/suback.rb', line 24

def initialize(packet_id:, reason_codes:, properties: {})
  raise ArgumentError, "SUBACK requires at least one reason code" if reason_codes.empty?
  @packet_id = packet_id
  @reason_codes = reason_codes
  @properties = properties
end

Instance Attribute Details

#packet_idObject (readonly)

Returns the value of attribute packet_id.



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

def packet_id
  @packet_id
end

#propertiesObject (readonly)

Returns the value of attribute properties.



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

def properties
  @properties
end

#reason_codesObject (readonly)

Returns the value of attribute reason_codes.



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

def reason_codes
  @reason_codes
end

Class Method Details

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

Raises:



41
42
43
44
45
46
47
48
# File 'lib/protocol/mqtt/packet/suback.rb', line 41

def self.decode_body(reader, flags:, version:)
  packet_id = reader.read_u16
  properties = version == 5 ? Property.decode(reader) : {}
  reason_codes = []
  reason_codes << reader.read_u8 while !reader.eof?
  raise MalformedPacket, "SUBACK must carry at least one reason code" if reason_codes.empty?
  new(packet_id: packet_id, reason_codes: reason_codes, properties: properties)
end

Instance Method Details

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



51
52
53
54
55
56
# File 'lib/protocol/mqtt/packet/suback.rb', line 51

def ==(other)
  other.is_a?(Suback) &&
    other.packet_id == @packet_id &&
    other.reason_codes == @reason_codes &&
    other.properties == @properties
end

#encode_body(version) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/protocol/mqtt/packet/suback.rb', line 32

def encode_body(version)
  w = Codec::Writer.new
  w.write_u16(@packet_id)
  w.write(Property.encode(@properties)) if version == 5
  @reason_codes.each { |rc| w.write_u8(rc) }
  w.bytes
end

#hashObject



60
61
62
# File 'lib/protocol/mqtt/packet/suback.rb', line 60

def hash
  [self.class, @packet_id, @reason_codes, @properties].hash
end