Class: MqttRails::Packet::Publish
- Defined in:
- lib/mqtt_rails/packet/publish.rb
Constant Summary collapse
- ATTR_DEFAULTS =
Default attribute values
{ :topic => nil, :payload => '' }
Instance Attribute Summary collapse
-
#duplicate ⇒ Object
Duplicate delivery flag.
-
#payload ⇒ Object
The data to be published.
-
#qos ⇒ Object
Quality of Service level (0, 1, 2).
-
#retain ⇒ Object
Retain flag.
-
#topic ⇒ Object
The topic name to publish to.
Attributes inherited from Base
#body_length, #flags, #id, #version
Instance Method Summary collapse
-
#encode_body ⇒ Object
Get serialisation of packet’s body.
-
#initialize(args = {}) ⇒ Publish
constructor
Create a new Publish packet.
-
#inspect ⇒ Object
Returns a human readable string, summarising the properties of the packet.
-
#parse_body(buffer) ⇒ Object
Parse the body (variable header and payload) of a Publish packet.
-
#validate_flags ⇒ Object
Check that fixed header flags are valid for this packet type.
Methods inherited from Base
create_from_header, parse, parse_header, read, #to_s, #type_id, #type_name, #update_attributes
Constructor Details
#initialize(args = {}) ⇒ Publish
Create a new Publish packet
45 46 47 |
# File 'lib/mqtt_rails/packet/publish.rb', line 45 def initialize(args={}) super(ATTR_DEFAULTS.merge(args)) end |
Instance Attribute Details
#duplicate ⇒ Object
Duplicate delivery flag
24 25 26 |
# File 'lib/mqtt_rails/packet/publish.rb', line 24 def duplicate @duplicate end |
#payload ⇒ Object
The data to be published
36 37 38 |
# File 'lib/mqtt_rails/packet/publish.rb', line 36 def payload @payload end |
#qos ⇒ Object
Quality of Service level (0, 1, 2)
30 31 32 |
# File 'lib/mqtt_rails/packet/publish.rb', line 30 def qos @qos end |
#retain ⇒ Object
Retain flag
27 28 29 |
# File 'lib/mqtt_rails/packet/publish.rb', line 27 def retain @retain end |
#topic ⇒ Object
The topic name to publish to
33 34 35 |
# File 'lib/mqtt_rails/packet/publish.rb', line 33 def topic @topic end |
Instance Method Details
#encode_body ⇒ Object
Get serialisation of packet’s body
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mqtt_rails/packet/publish.rb', line 92 def encode_body body = '' if @topic.nil? || @topic.to_s.empty? raise MqttRails::PacketFormatException.new( "Invalid topic name when serialising packet") end body += encode_string(@topic) body += encode_short(@id) unless qos == 0 body += payload.to_s.dup.force_encoding('ASCII-8BIT') return body end |
#inspect ⇒ Object
Returns a human readable string, summarising the properties of the packet
126 127 128 129 130 131 132 133 134 |
# File 'lib/mqtt_rails/packet/publish.rb', line 126 def inspect "\#<#{self.class}: " + "d#{duplicate ? '1' : '0'}, " + "q#{qos}, " + "r#{retain ? '1' : '0'}, " + "m#{id}, " + "'#{topic}', " + "#{inspect_payload}>" end |
#parse_body(buffer) ⇒ Object
Parse the body (variable header and payload) of a Publish packet
105 106 107 108 109 110 |
# File 'lib/mqtt_rails/packet/publish.rb', line 105 def parse_body(buffer) super(buffer) @topic = shift_string(buffer) @id = shift_short(buffer) unless qos == 0 @payload = buffer end |
#validate_flags ⇒ Object
Check that fixed header flags are valid for this packet type
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/mqtt_rails/packet/publish.rb', line 114 def validate_flags if qos == 3 raise MqttRails::PacketFormatException.new( "Invalid packet: QoS value of 3 is not allowed") end if qos == 0 && duplicate raise MqttRails::PacketFormatException.new( "Invalid packet: DUP cannot be set for QoS 0") end end |