Class: MQTT::Packet::Subscribe

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

Overview

Class representing an MQTT Client Subscribe 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 = {}) ⇒ Subscribe

Create a new Subscribe packet



749
750
751
# File 'lib/qubitro-mqtt/packet.rb', line 749

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

Instance Attribute Details

#topicsObject

One or more topic filters to subscribe to



740
741
742
# File 'lib/qubitro-mqtt/packet.rb', line 740

def topics
  @topics
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet’s body



792
793
794
795
796
797
798
799
800
# File 'lib/qubitro-mqtt/packet.rb', line 792

def encode_body
  raise 'no topics given when serialising packet' if @topics.empty?
  body = encode_short(@id)
  topics.each do |item|
    body += encode_string(item[0])
    body += encode_bytes(item[1])
  end
  body
end

#inspectObject

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



822
823
824
825
826
827
# File 'lib/qubitro-mqtt/packet.rb', line 822

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

#parse_body(buffer) ⇒ Object

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



803
804
805
806
807
808
809
810
811
812
# File 'lib/qubitro-mqtt/packet.rb', line 803

def parse_body(buffer)
  super(buffer)
  @id = shift_short(buffer)
  @topics = []
  while buffer.bytesize > 0
    topic_name = shift_string(buffer)
    topic_qos = shift_byte(buffer)
    @topics << [topic_name, topic_qos]
  end
end

#validate_flagsObject

Check that fixed header flags are valid for this packet type

Raises:



816
817
818
819
# File 'lib/qubitro-mqtt/packet.rb', line 816

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