Class: MQTT::Packet::Publish
- Inherits:
-
MQTT::Packet
- Object
- MQTT::Packet
- MQTT::Packet::Publish
- Defined in:
- lib/mqtt/packet.rb
Overview
Class representing an MQTT Publish message
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 MQTT::Packet
#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 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 = {}) ⇒ Publish
Create a new Publish packet
318 319 320 |
# File 'lib/mqtt/packet.rb', line 318 def initialize(args={}) super(ATTR_DEFAULTS.merge(args)) end |
Instance Attribute Details
#duplicate ⇒ Object
Duplicate delivery flag
297 298 299 |
# File 'lib/mqtt/packet.rb', line 297 def duplicate @duplicate end |
#payload ⇒ Object
The data to be published
309 310 311 |
# File 'lib/mqtt/packet.rb', line 309 def payload @payload end |
#qos ⇒ Object
Quality of Service level (0, 1, 2)
303 304 305 |
# File 'lib/mqtt/packet.rb', line 303 def qos @qos end |
#retain ⇒ Object
Retain flag
300 301 302 |
# File 'lib/mqtt/packet.rb', line 300 def retain @retain end |
#topic ⇒ Object
The topic name to publish to
306 307 308 |
# File 'lib/mqtt/packet.rb', line 306 def topic @topic end |
Instance Method Details
#encode_body ⇒ Object
Get serialisation of packet’s body
364 365 366 367 368 369 370 371 372 373 |
# File 'lib/mqtt/packet.rb', line 364 def encode_body body = '' if @topic.nil? or @topic.to_s.empty? raise "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
395 396 397 398 399 400 401 402 403 |
# File 'lib/mqtt/packet.rb', line 395 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
376 377 378 379 380 381 |
# File 'lib/mqtt/packet.rb', line 376 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
385 386 387 388 389 390 391 392 |
# File 'lib/mqtt/packet.rb', line 385 def validate_flags if qos == 3 raise ProtocolException.new("Invalid packet: QoS value of 3 is not allowed") end if qos == 0 and duplicate raise ProtocolException.new("Invalid packet: DUP cannot be set for QoS 0") end end |