Class: Protocol::MQTT::Packet::Puback

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

Overview

PUBACK (§3.4) — QoS 1 acknowledgement.

v3.1.1: packet id only (2 bytes). v5: packet id + optional reason code + optional property block.

If remaining length == 2, reason=SUCCESS and no props.
If remaining length == 3, reason only, no props.

Constant Summary collapse

TYPE_ID =
PUBACK

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_code: ReasonCodes::SUCCESS, properties: {}) ⇒ Puback

Returns a new instance of Puback.



22
23
24
25
26
# File 'lib/protocol/mqtt/packet/puback.rb', line 22

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

Instance Attribute Details

#packet_idObject (readonly)

Returns the value of attribute packet_id.



19
20
21
# File 'lib/protocol/mqtt/packet/puback.rb', line 19

def packet_id
  @packet_id
end

#propertiesObject (readonly)

Returns the value of attribute properties.



19
20
21
# File 'lib/protocol/mqtt/packet/puback.rb', line 19

def properties
  @properties
end

#reason_codeObject (readonly)

Returns the value of attribute reason_code.



19
20
21
# File 'lib/protocol/mqtt/packet/puback.rb', line 19

def reason_code
  @reason_code
end

Class Method Details

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



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

def self.decode_body(reader, flags:, version:)
  packet_id = reader.read_u16
  if version == 5 && !reader.eof?
    reason_code = reader.read_u8
    properties = reader.eof? ? {} : Property.decode(reader)
    new(packet_id: packet_id, reason_code: reason_code, properties: properties)
  else
    new(packet_id: packet_id)
  end
end

Instance Method Details

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



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

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

#encode_body(version) ⇒ Object



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

def encode_body(version)
  w = Codec::Writer.new
  w.write_u16(@packet_id)
  if version == 5 && !(@properties.empty? && @reason_code == ReasonCodes::SUCCESS)
    w.write_u8(@reason_code)
    w.write(Property.encode(@properties)) unless @properties.empty?
  end
  w.bytes
end

#hashObject



61
62
63
# File 'lib/protocol/mqtt/packet/puback.rb', line 61

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