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
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 =
{}.freeze
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
38 39 40 |
# File 'lib/mqtt/sn/packet.rb', line 38 def initialize(args = {}) update_attributes(self.class::DEFAULTS.merge(args)) end |
Instance Attribute Details
#clean_session ⇒ Object
When true, subscriptions are deleted after disconnect
13 14 15 |
# File 'lib/mqtt/sn/packet.rb', line 13 def clean_session @clean_session end |
#duplicate ⇒ Object
Duplicate delivery flag
9 10 11 |
# File 'lib/mqtt/sn/packet.rb', line 9 def duplicate @duplicate end |
#qos ⇒ Object
Quality of Service level
10 11 12 |
# File 'lib/mqtt/sn/packet.rb', line 10 def qos @qos end |
#request_will ⇒ Object
Request that gateway prompts for Will
12 13 14 |
# File 'lib/mqtt/sn/packet.rb', line 12 def request_will @request_will end |
#retain ⇒ Object
Retain flag
11 12 13 |
# File 'lib/mqtt/sn/packet.rb', line 11 def retain @retain end |
#topic_id_type ⇒ Object
One of :normal, :predefined or :short
14 15 16 |
# File 'lib/mqtt/sn/packet.rb', line 14 def topic_id_type @topic_id_type end |
Class Method Details
.parse(buffer) ⇒ Object
Parse buffer into new packet object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mqtt/sn/packet.rb', line 19 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 raise ProtocolException, "Length of packet is not the same as the length header" if buffer.length != length packet_class = PACKET_TYPES[type_id] raise ProtocolException, "Invalid packet type identifier: #{type_id}" if packet_class.nil? # Create a new packet object packet = packet_class.new packet.parse_body(body) packet end |
Instance Method Details
#parse_body(buffer) ⇒ Object
72 |
# File 'lib/mqtt/sn/packet.rb', line 72 def parse_body(buffer); end |
#to_s ⇒ Object
Serialise the packet
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mqtt/sn/packet.rb', line 57 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
49 50 51 52 53 54 |
# File 'lib/mqtt/sn/packet.rb', line 49 def type_id PACKET_TYPES.each_pair do |key, value| return key if instance_of?(value) end raise "Invalid packet type: #{self.class}" end |
#update_attributes(attr = {}) ⇒ Object
42 43 44 45 46 |
# File 'lib/mqtt/sn/packet.rb', line 42 def update_attributes(attr = {}) attr.each_pair do |k, v| send("#{k}=", v) end end |