Class: MQTT::Packet::Connack

Inherits:
MQTT::Packet show all
Defined in:
lib/mqtt/packet.rb

Overview

Class representing an MQTT Connect Acknowledgment Packet

Constant Summary collapse

ATTR_DEFAULTS =

Default attribute values

{:return_code => 0x00}

Instance Attribute Summary collapse

Attributes inherited from MQTT::Packet

#body_length, #flags, #id, #version

Instance Method Summary collapse

Methods inherited from MQTT::Packet

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

Constructor Details

#initialize(args = {}) ⇒ Connack

Create a new Client Connect packet


591
592
593
594
595
# File 'lib/mqtt/packet.rb', line 591

def initialize(args={})
  # We must set flags before other attributes
  @connack_flags = [false, false, false, false, false, false, false, false]
  super(ATTR_DEFAULTS.merge(args))
end

Instance Attribute Details

#return_codeObject

The return code (defaults to 0 for connection accepted)


585
586
587
# File 'lib/mqtt/packet.rb', line 585

def return_code
  @return_code
end

#session_presentObject

Get the Session Present flag


582
583
584
# File 'lib/mqtt/packet.rb', line 582

def session_present
  @session_present
end

Instance Method Details

#encode_bodyObject

Get serialisation of packet's body


632
633
634
635
636
637
# File 'lib/mqtt/packet.rb', line 632

def encode_body
  body = ''
  body += encode_bits(@connack_flags)
  body += encode_bytes(@return_code.to_i)
  return body
end

#inspectObject

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


653
654
655
# File 'lib/mqtt/packet.rb', line 653

def inspect
  "\#<#{self.class}: 0x%2.2X>" % return_code
end

#parse_body(buffer) ⇒ Object

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


640
641
642
643
644
645
646
647
648
649
650
# File 'lib/mqtt/packet.rb', line 640

def parse_body(buffer)
  super(buffer)
  @connack_flags = shift_bits(buffer)
  unless @connack_flags[1,7] == [false, false, false, false, false, false, false]
    raise ProtocolException.new("Invalid flags in Connack variable header")
  end
  @return_code = shift_byte(buffer)
  unless buffer.empty?
    raise ProtocolException.new("Extra bytes at end of Connect Acknowledgment packet")
  end
end

#return_msgObject

Get a string message corresponding to a return code


612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/mqtt/packet.rb', line 612

def return_msg
  case return_code
    when 0x00
      "Connection Accepted"
    when 0x01
      "Connection refused: unacceptable protocol version"
    when 0x02
      "Connection refused: client identifier rejected"
    when 0x03
      "Connection refused: server unavailable"
    when 0x04
      "Connection refused: bad user name or password"
    when 0x05
      "Connection refused: not authorised"
    else
      "Connection refused: error code #{return_code}"
  end
end