Class: MQTT::Packet::Connack
- Inherits:
-
MQTT::Packet
- Object
- MQTT::Packet
- MQTT::Packet::Connack
- 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 }.freeze
Instance Attribute Summary collapse
-
#return_code ⇒ Object
The return code (defaults to 0 for connection accepted).
Attributes inherited from MQTT::Packet
#body_length, #flags, #id, #version
Instance Method Summary collapse
-
#encode_body ⇒ Object
Get serialisation of packet’s body.
-
#initialize(args = {}) ⇒ Connack
constructor
Create a new Client Connect packet.
-
#inspect ⇒ Object
Returns a human readable string, summarising the properties of the packet.
-
#parse_body(buffer) ⇒ Object
Parse the body (variable header and payload) of a Connect Acknowledgment packet.
-
#return_msg ⇒ Object
Get a string message corresponding to a return code.
-
#session_present ⇒ Object
Get the Session Present flag.
-
#session_present=(arg) ⇒ Object
Set the Session Present flag.
Methods inherited from MQTT::Packet
create_from_header, #dup, #message_id, #message_id=, parse, parse_header, read, read_byte, #to_s, #type_id, #type_name, #update_attributes, #validate_flags
Constructor Details
#initialize(args = {}) ⇒ Connack
Create a new Client Connect packet
557 558 559 560 561 |
# File 'lib/mqtt/packet.rb', line 557 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_code ⇒ Object
The return code (defaults to 0 for connection accepted)
551 552 553 |
# File 'lib/mqtt/packet.rb', line 551 def return_code @return_code end |
Instance Method Details
#encode_body ⇒ Object
Get serialisation of packet’s body
594 595 596 597 598 599 |
# File 'lib/mqtt/packet.rb', line 594 def encode_body body = '' body += encode_bits(@connack_flags) body += encode_bytes(@return_code.to_i) body end |
#inspect ⇒ Object
Returns a human readable string, summarising the properties of the packet
617 618 619 |
# File 'lib/mqtt/packet.rb', line 617 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
602 603 604 605 606 607 608 609 610 611 612 613 614 |
# File 'lib/mqtt/packet.rb', line 602 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, 'Invalid flags in Connack variable header' end @return_code = shift_byte(buffer) return if buffer.empty? raise ProtocolException, 'Extra bytes at end of Connect Acknowledgment packet' end |
#return_msg ⇒ Object
Get a string message corresponding to a return code
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
# File 'lib/mqtt/packet.rb', line 574 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 |
#session_present ⇒ Object
Get the Session Present flag
564 565 566 |
# File 'lib/mqtt/packet.rb', line 564 def session_present @connack_flags[0] end |
#session_present=(arg) ⇒ Object
Set the Session Present flag
569 570 571 |
# File 'lib/mqtt/packet.rb', line 569 def session_present=(arg) @connack_flags[0] = arg.is_a?(Integer) ? (arg == 0x1) : arg end |