Class: MQTT::SN::Packet

Inherits:
Object
  • Object
show all
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 =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Packet

Create a new empty packet


43
44
45
# File 'lib/mqtt/sn/packet.rb', line 43

def initialize(args={})
  update_attributes(self.class::DEFAULTS.merge(args))
end

Instance Attribute Details

#clean_sessionObject

When true, subscriptions are deleted after disconnect


12
13
14
# File 'lib/mqtt/sn/packet.rb', line 12

def clean_session
  @clean_session
end

#duplicateObject

Duplicate delivery flag


8
9
10
# File 'lib/mqtt/sn/packet.rb', line 8

def duplicate
  @duplicate
end

#qosObject

Quality of Service level


9
10
11
# File 'lib/mqtt/sn/packet.rb', line 9

def qos
  @qos
end

#request_willObject

Request that gateway prompts for Will


11
12
13
# File 'lib/mqtt/sn/packet.rb', line 11

def request_will
  @request_will
end

#retainObject

Retain flag


10
11
12
# File 'lib/mqtt/sn/packet.rb', line 10

def retain
  @retain
end

#topic_id_typeObject

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
39
40
# 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*')
  if length == 1
    length,type_id,body = buffer.unpack('xnCa*')
  end

  # Double-check the length
  if buffer.length != length
    raise ProtocolException.new("Length of packet is not the same as the length header")
  end

  packet_class = PACKET_TYPES[type_id]
  if packet_class.nil?
    raise ProtocolException.new("Invalid packet type identifier: #{type_id}")
  end

  # Create a new packet object
  packet = packet_class.new
  packet.parse_body(body)

  return packet
end

Instance Method Details

#parse_body(buffer) ⇒ Object


77
78
# File 'lib/mqtt/sn/packet.rb', line 77

def parse_body(buffer)
end

#to_sObject

Serialise the packet


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mqtt/sn/packet.rb', line 62

def to_s
  # Get the packet's variable header and payload
  body = self.encode_body

  # Build up the body length field bytes
  body_length = body.length
  if body_length > 65531
    raise "MQTT-SN Packet is too big, maximum packet body size is 65531"
  elsif body_length > 253
    [0x01, body_length + 4, type_id].pack('CnC') + body
  else
    [body_length + 2, type_id].pack('CC') + body
  end
end

#type_idObject

Get the identifer for this packet type


54
55
56
57
58
59
# File 'lib/mqtt/sn/packet.rb', line 54

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


47
48
49
50
51
# File 'lib/mqtt/sn/packet.rb', line 47

def update_attributes(attr={})
  attr.each_pair do |k,v|
    send("#{k}=", v)
  end
end