Class: MQTT::SN::Packet
- Inherits:
-
Object
- Object
- MQTT::SN::Packet
- Defined in:
- lib/mqtt/sn/packet.rb
Overview
Class representing a MQTT::SN Packet Performs binary encoding and decoding of headers
Direct Known Subclasses
Advertise, Connack, Connect, Disconnect, Gwinfo, Pingreq, Pingresp, Puback, Pubcomp, Publish, Pubrec, Pubrel, Regack, Register, Searchgw, Suback, Subscribe, Unsuback, Unsubscribe, Willmsg, Willmsgreq, Willmsgresp, Willmsgupd, Willtopic, Willtopicreq, Willtopicresp, Willtopicupd
Defined Under Namespace
Classes: Advertise, Connack, Connect, Disconnect, Gwinfo, Pingreq, Pingresp, Puback, Pubcomp, Publish, Pubrec, Pubrel, Regack, Register, Searchgw, Suback, Subscribe, Unsuback, Unsubscribe, Willmsg, Willmsgreq, Willmsgresp, Willmsgupd, Willtopic, Willtopicreq, Willtopicresp, Willtopicupd
Constant Summary collapse
- DEFAULTS =
{}
Instance Attribute Summary collapse
-
#clean_session ⇒ Object
When true, subscriptions are deleted after disconnect.
-
#duplicate ⇒ Object
Duplicate delivery flag.
-
#qos ⇒ Object
Quality of Service level.
-
#request_will ⇒ Object
Request that gateway prompts for Will.
-
#retain ⇒ Object
Retain flag.
-
#topic_id_type ⇒ Object
One of :normal, :predefined or :short.
Class Method Summary collapse
-
.parse(buffer) ⇒ Object
Parse buffer into new packet object.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Packet
constructor
Create a new empty packet.
- #parse_body(buffer) ⇒ Object
-
#to_s ⇒ Object
Serialise the packet.
-
#type_id ⇒ Object
Get the identifer for this packet type.
- #update_attributes(attr = {}) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Packet
Create a new empty packet
41 42 43 |
# File 'lib/mqtt/sn/packet.rb', line 41 def initialize(args = {}) update_attributes(self.class::DEFAULTS.merge(args)) end |
Instance Attribute Details
#clean_session ⇒ Object
When true, subscriptions are deleted after disconnect
12 13 14 |
# File 'lib/mqtt/sn/packet.rb', line 12 def clean_session @clean_session end |
#duplicate ⇒ Object
Duplicate delivery flag
8 9 10 |
# File 'lib/mqtt/sn/packet.rb', line 8 def duplicate @duplicate end |
#qos ⇒ Object
Quality of Service level
9 10 11 |
# File 'lib/mqtt/sn/packet.rb', line 9 def qos @qos end |
#request_will ⇒ Object
Request that gateway prompts for Will
11 12 13 |
# File 'lib/mqtt/sn/packet.rb', line 11 def request_will @request_will end |
#retain ⇒ Object
Retain flag
10 11 12 |
# File 'lib/mqtt/sn/packet.rb', line 10 def retain @retain end |
#topic_id_type ⇒ Object
One of :normal, :predefined or :short
13 14 15 |
# File 'lib/mqtt/sn/packet.rb', line 13 def topic_id_type @topic_id_type end |
Class Method Details
.parse(buffer) ⇒ Object
Parse buffer into new packet object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mqtt/sn/packet.rb', line 18 def self.parse(buffer) # Parse the fixed header (length and type) length, type_id, body = buffer.unpack('CCa*') length, type_id, body = buffer.unpack('xnCa*') if length == 1 # Double-check the length if buffer.length != length raise ProtocolException, 'Length of packet is not the same as the length header' end packet_class = PACKET_TYPES[type_id] if packet_class.nil? raise ProtocolException, "Invalid packet type identifier: #{type_id}" end # Create a new packet object packet = packet_class.new packet.parse_body(body) packet end |
Instance Method Details
#parse_body(buffer) ⇒ Object
75 |
# File 'lib/mqtt/sn/packet.rb', line 75 def parse_body(buffer); end |
#to_s ⇒ Object
Serialise the packet
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mqtt/sn/packet.rb', line 60 def to_s # Get the packet's variable header and payload body = encode_body # Build up the body length field bytes body_length = body.length raise 'MQTT-SN Packet is too big, maximum packet body size is 65531' if body_length > 65_531 if body_length > 253 [0x01, body_length + 4, type_id].pack('CnC') + body else [body_length + 2, type_id].pack('CC') + body end end |
#type_id ⇒ Object
Get the identifer for this packet type
52 53 54 55 56 57 |
# File 'lib/mqtt/sn/packet.rb', line 52 def type_id PACKET_TYPES.each_pair do |key, value| return key if self.class == value end raise "Invalid packet type: #{self.class}" end |
#update_attributes(attr = {}) ⇒ Object
45 46 47 48 49 |
# File 'lib/mqtt/sn/packet.rb', line 45 def update_attributes(attr = {}) attr.each_pair do |k, v| send("#{k}=", v) end end |