class MQTT::Packet::Connect
Class representing an MQTT Connect Packet
def encode_body
def encode_body body = '' if @version == '3.1.0' raise 'Client identifier too short while serialising packet' if @client_id.nil? || @client_id.bytesize < 1 raise 'Client identifier too long when serialising packet' if @client_id.bytesize > 23 end body += encode_string(@protocol_name) body += encode_bytes(@protocol_level.to_i) if @keep_alive < 0 raise 'Invalid keep-alive value: cannot be less than 0' end # Set the Connect flags @connect_flags = 0 @connect_flags |= 0x02 if @clean_session @connect_flags |= 0x04 unless @will_topic.nil? @connect_flags |= ((@will_qos & 0x03) << 3) @connect_flags |= 0x20 if @will_retain @connect_flags |= 0x40 unless @password.nil? @connect_flags |= 0x80 unless @username.nil? body += encode_bytes(@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
def initialize(args = {})
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 ArgumentError, "Unsupported protocol version: #{version}" end end
def inspect
def inspect str = "\#<#{self.class}: " \ "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
def parse_body(buffer)
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 ProtocolException, "Unsupported protocol: #{@protocol_name}/#{@protocol_level}" end @connect_flags = shift_byte(buffer) @clean_session = ((@connect_flags & 0x02) >> 1) == 0x01 @keep_alive = shift_short(buffer) @client_id = shift_string(buffer) if ((@connect_flags & 0x04) >> 2) == 0x01 # Last Will and Testament @will_qos = ((@connect_flags & 0x18) >> 3) @will_retain = ((@connect_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 # rubocop: disable Style/GuardClause @password = shift_string(buffer) end end
def protocol_version
- Please use {#protocol_level} instead
def protocol_version protocol_level end
def protocol_version=(args)
- Please use {#protocol_level=} instead
def protocol_version=(args) self.protocol_level = args end