Class: MqttRails::Packet::Connect

Inherits:
Base
  • Object
show all
Defined in:
lib/mqtt_rails/packet/connect.rb

Constant Summary collapse

ATTR_DEFAULTS =

Default attribute values

{
  :client_id     => nil,
  :clean_session => true,
  :keep_alive    => 15,
  :will_topic    => nil,
  :will_qos      => 0,
  :will_retain   => false,
  :will_payload  => '',
  :username      => nil,
  :password      => nil,
}

Instance Attribute Summary collapse

Attributes inherited from Base

#body_length, #flags, #id, #version

Instance Method Summary collapse

Methods inherited from Base

create_from_header, parse, parse_header, read, #to_s, #type_id, #type_name, #update_attributes, #validate_flags

Constructor Details

#initialize(args = {}) ⇒ Connect

Create a new Client Connect packet



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mqtt_rails/packet/connect.rb', line 70

def initialize(args={})
  super(ATTR_DEFAULTS.merge(args))

  if version == '3.1.0' || version == '3.1'
    self.protocol_name ||= 'MQIsdp'
    self.protocol_level ||= 0x03
  elsif version == '3.1.1'
    self.protocol_name ||= 'MQTT'
    self.protocol_level ||= 0x04
  else
    raise MqttRails::PacketFormatException.new(
            "Unsupported protocol version: #{version}")
  end
end

Instance Attribute Details

#clean_sessionObject

Set to false to keep a persistent session with the server



33
34
35
# File 'lib/mqtt_rails/packet/connect.rb', line 33

def clean_session
  @clean_session
end

#client_idObject

The client identifier string



30
31
32
# File 'lib/mqtt_rails/packet/connect.rb', line 30

def client_id
  @client_id
end

#keep_aliveObject

Period the server should keep connection open for between pings



36
37
38
# File 'lib/mqtt_rails/packet/connect.rb', line 36

def keep_alive
  @keep_alive
end

#passwordObject

The password for authenticating with the server



54
55
56
# File 'lib/mqtt_rails/packet/connect.rb', line 54

def password
  @password
end

#protocol_levelObject

The version number of the protocol



27
28
29
# File 'lib/mqtt_rails/packet/connect.rb', line 27

def protocol_level
  @protocol_level
end

#protocol_nameObject

The name of the protocol



24
25
26
# File 'lib/mqtt_rails/packet/connect.rb', line 24

def protocol_name
  @protocol_name
end

#usernameObject

The username for authenticating with the server



51
52
53
# File 'lib/mqtt_rails/packet/connect.rb', line 51

def username
  @username
end

#will_payloadObject

The payload of the Will message



48
49
50
# File 'lib/mqtt_rails/packet/connect.rb', line 48

def will_payload
  @will_payload
end

#will_qosObject

The QoS level to send the Will message as



42
43
44
# File 'lib/mqtt_rails/packet/connect.rb', line 42

def will_qos
  @will_qos
end

#will_retainObject

Set to true to make the Will message retained



45
46
47
# File 'lib/mqtt_rails/packet/connect.rb', line 45

def will_retain
  @will_retain
end

#will_topicObject

The topic name to send the Will message to



39
40
41
# File 'lib/mqtt_rails/packet/connect.rb', line 39

def will_topic
  @will_topic
end

Instance Method Details

#check_versionObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mqtt_rails/packet/connect.rb', line 109

def check_version
  if @version == '3.1.0'
    if @client_id.nil? || @client_id.bytesize < 1
      raise MqttRails::PacketFormatException.new(
              "Client identifier too short while serialising packet")
    elsif @client_id.bytesize > 23
      raise MqttRails::PacketFormatException.new(
              "Client identifier too long when serialising packet")
    end
  end
end

#encode_bodyObject

Get serialisation of packet’s body



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mqtt_rails/packet/connect.rb', line 86

def encode_body
  body = ''
  check_version
  body += encode_string(@protocol_name)
  body += encode_bytes(@protocol_level.to_i)
  if @keep_alive < 0
    raise MqttRails::PacketFormatException.new(
            "Invalid keep-alive value: cannot be less than 0")
  end

  body += encode_flags(@connect_flags)
  body += encode_short(@keep_alive)
  body += encode_string(@client_id)
  unless will_topic.nil?
    body += encode_string(@will_topic)
    # The MQTT v3.1 specification says that the payload is a UTF-8 string
    body += encode_string(@will_payload)
  end
  body += encode_string(@username) unless @username.nil?
  body += encode_string(@password) unless @password.nil?
  body
end

#encode_flags(flags) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mqtt_rails/packet/connect.rb', line 121

def encode_flags(flags)
  # Set the Connect flags
  flags = 0
  flags |= 0x02 if @clean_session
  flags |= 0x04 unless @will_topic.nil?
  flags |= ((@will_qos & 0x03) << 3)
  flags |= 0x20 if @will_retain
  flags |= 0x40 unless @password.nil?
  flags |= 0x80 unless @username.nil?
  encode_bytes(flags)
end

#inspectObject

Returns a human readable string, summarising the properties of the packet



172
173
174
175
176
177
178
179
180
# File 'lib/mqtt_rails/packet/connect.rb', line 172

def inspect
  str = "\#<#{self.class}: "
  str += "keep_alive=#{keep_alive}"
  str += ", clean" if clean_session
  str += ", client_id='#{client_id}'"
  str += ", username='#{username}'" unless username.nil?
  str += ", password=..." unless password.nil?
  str += ">"
end

#parse_body(buffer) ⇒ Object

Parse the body (variable header and payload) of a Connect packet



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mqtt_rails/packet/connect.rb', line 134

def parse_body(buffer)
  super(buffer)
  @protocol_name = shift_string(buffer)
  @protocol_level = shift_byte(buffer).to_i
  if @protocol_name == 'MQIsdp' && @protocol_level == 3
    @version = '3.1.0'
  elsif @protocol_name == 'MQTT' && @protocol_level == 4
    @version = '3.1.1'
  else
    raise MqttRails::PacketFormatException.new(
            "Unsupported protocol: #{@protocol_name}/#{@protocol_level}")
  end

  @connect_flags = shift_byte(buffer)
  @keep_alive = shift_short(buffer)
  @client_id = shift_string(buffer)
  parse_connect_flags(@connect_flag, buffer)
end

#parse_connect_flags(flags, buffer) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/mqtt_rails/packet/connect.rb', line 153

def parse_connect_flags(flags, buffer)
  @clean_session = ((@connect_flags & 0x02) >> 1) == 0x01
  if ((flags & 0x04) >> 2) == 0x01
    # Last Will and Testament
    @will_qos = ((flags & 0x18) >> 3)
    @will_retain = ((flags & 0x20) >> 5) == 0x01
    @will_topic = shift_string(buffer)
    # The MQTT v3.1 specification says that the payload is a UTF-8 string
    @will_payload = shift_string(buffer)
  end
  if ((@connect_flags & 0x80) >> 7) == 0x01 && buffer.bytesize > 0
    @username = shift_string(buffer)
  end
  if ((@connect_flags & 0x40) >> 6) == 0x01 && buffer.bytesize > 0
    @password = shift_string(buffer)
  end
end