Class: MQTT::Packet::Unsubscribe

Inherits:
MQTT::Packet show all
Defined in:
lib/qubitro-mqtt/packet.rb

Overview

Class representing an MQTT Client Unsubscribe packet

Constant Summary collapse

ATTR_DEFAULTS =

Default attribute values

{
  :topics => [],
  :flags => [false, true, false, false]
}

Instance Attribute Summary collapse

Attributes inherited from MQTT::Packet

#body_length, #flags, #id, #version

Instance Method Summary collapse

Methods inherited from MQTT::Packet

create_from_header, #message_id, #message_id=, parse, parse_header, read, read_byte, #to_s, #type_id, #type_name, #update_attributes

Constructor Details

#initialize(args = {}) ⇒ Unsubscribe

Create a new Unsubscribe packet



904
905
906
# File 'lib/qubitro-mqtt/packet.rb', line 904

def initialize(args = {})
  super(ATTR_DEFAULTS.merge(args))
end

Instance Attribute Details

#topicsObject

One or more topic paths to unsubscribe from



895
896
897
# File 'lib/qubitro-mqtt/packet.rb', line 895

def topics
  @topics
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet’s body



914
915
916
917
918
919
# File 'lib/qubitro-mqtt/packet.rb', line 914

def encode_body
  raise 'no topics given when serialising packet' if @topics.empty?
  body = encode_short(@id)
  topics.each { |topic| body += encode_string(topic) }
  body
end

#inspectObject

Returns a human readable string, summarising the properties of the packet



936
937
938
939
940
941
# File 'lib/qubitro-mqtt/packet.rb', line 936

def inspect
  "\#<#{self.class}: 0x%2.2X, %s>" % [
    id,
    topics.map { |t| "'#{t}'" }.join(', ')
  ]
end

#parse_body(buffer) ⇒ Object

Parse the body (variable header and payload) of a packet



922
923
924
925
926
# File 'lib/qubitro-mqtt/packet.rb', line 922

def parse_body(buffer)
  super(buffer)
  @id = shift_short(buffer)
  @topics << shift_string(buffer) while buffer.bytesize > 0
end

#validate_flagsObject

Check that fixed header flags are valid for this packet type

Raises:



930
931
932
933
# File 'lib/qubitro-mqtt/packet.rb', line 930

def validate_flags
  return if @flags == [false, true, false, false]
  raise ProtocolException, 'Invalid flags in UNSUBSCRIBE packet header'
end