Class: MqttRails::Packet::Unsubscribe

Inherits:
Base
  • Object
show all
Defined in:
lib/mqtt_rails/packet/unsubscribe.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 = {}) ⇒ Unsubscribe

Create a new Unsubscribe packet



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

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

Instance Attribute Details

#topicsObject

One or more topic paths to unsubscribe from



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

def topics
  @topics
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet’s body



47
48
49
50
51
52
53
54
55
# File 'lib/mqtt_rails/packet/unsubscribe.rb', line 47

def encode_body
  if @topics.empty?
    raise MqttRails::PacketFormatException.new(
            "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



76
77
78
79
80
81
# File 'lib/mqtt_rails/packet/unsubscribe.rb', line 76

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



58
59
60
61
62
63
64
# File 'lib/mqtt_rails/packet/unsubscribe.rb', line 58

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



68
69
70
71
72
73
# File 'lib/mqtt_rails/packet/unsubscribe.rb', line 68

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