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, #to_s, #type_id, #type_name, #update_attributes

Constructor Details

#initialize(args = {}) ⇒ Unsubscribe

Create a new Unsubscribe packet


942
943
944
# File 'lib/mqtt/packet.rb', line 942

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

Instance Attribute Details

#topicsObject

One or more topic paths to unsubscribe from


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

def topics
  @topics
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet's body


956
957
958
959
960
961
962
963
# File 'lib/mqtt/packet.rb', line 956

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

#inspectObject

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


983
984
985
986
987
988
# File 'lib/mqtt/packet.rb', line 983

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


966
967
968
969
970
971
972
# File 'lib/mqtt/packet.rb', line 966

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

#validate_flagsObject

Check that fixed header flags are valid for this packet type


976
977
978
979
980
# File 'lib/mqtt/packet.rb', line 976

def validate_flags
  if @flags != [false, true, false, false]
    raise ProtocolException.new("Invalid flags in UNSUBSCRIBE packet header")
  end
end