Class: MQTT::Packet::Unsubscribe

Inherits:
MQTT::Packet show all
Defined in:
lib/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



901
902
903
# File 'lib/mqtt/packet.rb', line 901

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

Instance Attribute Details

#topicsObject

One or more topic paths to unsubscribe from



892
893
894
# File 'lib/mqtt/packet.rb', line 892

def topics
  @topics
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet’s body



911
912
913
914
915
916
# File 'lib/mqtt/packet.rb', line 911

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



933
934
935
936
937
938
# File 'lib/mqtt/packet.rb', line 933

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



919
920
921
922
923
# File 'lib/mqtt/packet.rb', line 919

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:



927
928
929
930
# File 'lib/mqtt/packet.rb', line 927

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