Class: MqttRails::Packet::Subscribe

Inherits:
Base
  • Object
show all
Defined in:
lib/mqtt_rails/packet/subscribe.rb

Constant Summary collapse

ATTR_DEFAULTS =

Default attribute values

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

Instance Attribute Summary collapse

Attributes inherited from Base

#body_length, #flags, #id, #version

Instance Method Summary collapse

Methods inherited from Base

create_from_header, parse, parse_header, read, #to_s, #type_id, #type_name, #update_attributes

Constructor Details

#initialize(args = {}) ⇒ Subscribe

Create a new Subscribe packet



33
34
35
# File 'lib/mqtt_rails/packet/subscribe.rb', line 33

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

Instance Attribute Details

#topicsObject

One or more topic filters to subscribe to



24
25
26
# File 'lib/mqtt_rails/packet/subscribe.rb', line 24

def topics
  @topics
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet’s body



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mqtt_rails/packet/subscribe.rb', line 81

def encode_body
  if @topics.empty?
    raise MqttRails::PacketFormatException.new(
            "No topics given when serialising packet")
  end
  body = encode_short(@id)
  topics.each do |item|
    body += encode_string(item[0])
    body += encode_bytes(item[1])
  end
  return body
end

#inspectObject

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



116
117
118
119
120
121
# File 'lib/mqtt_rails/packet/subscribe.rb', line 116

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



95
96
97
98
99
100
101
102
103
104
# File 'lib/mqtt_rails/packet/subscribe.rb', line 95

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



108
109
110
111
112
113
# File 'lib/mqtt_rails/packet/subscribe.rb', line 108

def validate_flags
  if @flags != [false, true, false, false]
    raise MqttRails::PacketFormatException.new(
            "Invalid flags in SUBSCRIBE packet header")
  end
end