Class: MQTT::Packet::Connect
- Inherits:
-
MQTT::Packet
- Object
- MQTT::Packet
- MQTT::Packet::Connect
- Defined in:
- lib/qubitro-mqtt/packet.rb
Overview
Class representing an MQTT Connect Packet
Constant Summary collapse
- ATTR_DEFAULTS =
Default attribute values
{ :client_id => nil, :clean_session => true, :keep_alive => 15, :will_topic => nil, :will_qos => 0, :ack_timeout => 15, :will_retain => false, :will_payload => '', :device_id => nil, :device_token => nil }
Instance Attribute Summary collapse
-
#ack_timeout ⇒ Object
Returns the value of attribute ack_timeout.
-
#clean_session ⇒ Object
Set to false to keep a persistent session with the server.
-
#client_id ⇒ Object
The client identifier string.
-
#device_id ⇒ Object
The device_id for authenticating with the server.
-
#device_token ⇒ Object
The device_token for authenticating with the server.
-
#keep_alive ⇒ Object
Period the server should keep connection open for between pings.
-
#protocol_level ⇒ Object
The version number of the protocol.
-
#protocol_name ⇒ Object
The name of the protocol.
-
#will_payload ⇒ Object
The payload of the Will message.
-
#will_qos ⇒ Object
The QoS level to send the Will message as.
-
#will_retain ⇒ Object
Set to true to make the Will message retained.
-
#will_topic ⇒ Object
The topic name to send the Will message to.
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 = {}) ⇒ Connect
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 packet.
-
#protocol_version ⇒ Object
deprecated
Deprecated.
Please use #protocol_level instead
-
#protocol_version=(args) ⇒ Object
deprecated
Deprecated.
Please use #protocol_level= instead
Methods inherited from MQTT::Packet
create_from_header, #message_id, #message_id=, parse, parse_header, read, read_byte, #to_s, #type_id, #type_name, #update_attributes, #validate_flags
Constructor Details
#initialize(args = {}) ⇒ Connect
Create a new Client Connect packet
448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/qubitro-mqtt/packet.rb', line 448 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 |
Instance Attribute Details
#ack_timeout ⇒ Object
Returns the value of attribute ack_timeout.
413 414 415 |
# File 'lib/qubitro-mqtt/packet.rb', line 413 def ack_timeout @ack_timeout end |
#clean_session ⇒ Object
Set to false to keep a persistent session with the server
408 409 410 |
# File 'lib/qubitro-mqtt/packet.rb', line 408 def clean_session @clean_session end |
#client_id ⇒ Object
The client identifier string
405 406 407 |
# File 'lib/qubitro-mqtt/packet.rb', line 405 def client_id @client_id end |
#device_id ⇒ Object
The device_id for authenticating with the server
428 429 430 |
# File 'lib/qubitro-mqtt/packet.rb', line 428 def device_id @device_id end |
#device_token ⇒ Object
The device_token for authenticating with the server
431 432 433 |
# File 'lib/qubitro-mqtt/packet.rb', line 431 def device_token @device_token end |
#keep_alive ⇒ Object
Period the server should keep connection open for between pings
411 412 413 |
# File 'lib/qubitro-mqtt/packet.rb', line 411 def keep_alive @keep_alive end |
#protocol_level ⇒ Object
The version number of the protocol
402 403 404 |
# File 'lib/qubitro-mqtt/packet.rb', line 402 def protocol_level @protocol_level end |
#protocol_name ⇒ Object
The name of the protocol
399 400 401 |
# File 'lib/qubitro-mqtt/packet.rb', line 399 def protocol_name @protocol_name end |
#will_payload ⇒ Object
The payload of the Will message
425 426 427 |
# File 'lib/qubitro-mqtt/packet.rb', line 425 def will_payload @will_payload end |
#will_qos ⇒ Object
The QoS level to send the Will message as
419 420 421 |
# File 'lib/qubitro-mqtt/packet.rb', line 419 def will_qos @will_qos end |
#will_retain ⇒ Object
Set to true to make the Will message retained
422 423 424 |
# File 'lib/qubitro-mqtt/packet.rb', line 422 def will_retain @will_retain end |
#will_topic ⇒ Object
The topic name to send the Will message to
416 417 418 |
# File 'lib/qubitro-mqtt/packet.rb', line 416 def will_topic @will_topic end |
Instance Method Details
#encode_body ⇒ Object
Get serialisation of packet’s body
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/qubitro-mqtt/packet.rb', line 463 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 @device_token.nil? @connect_flags |= 0x80 unless @device_id.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(@device_id) unless @device_id.nil? body += encode_string(@device_token) unless @device_token.nil? body end |
#inspect ⇒ Object
Returns a human readable string, summarising the properties of the packet
534 535 536 537 538 539 540 541 542 |
# File 'lib/qubitro-mqtt/packet.rb', line 534 def inspect str = "\#<#{self.class}: " \ "keep_alive=#{keep_alive}" str += ', clean' if clean_session str += ", client_id='#{client_id}'" str += ", device_id='#{device_id}'" unless device_id.nil? str += ', device_token=...' unless device_token.nil? str + '>' end |
#parse_body(buffer) ⇒ Object
Parse the body (variable header and payload) of a Connect packet
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/qubitro-mqtt/packet.rb', line 501 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 @device_id = shift_string(buffer) end if ((@connect_flags & 0x40) >> 6) == 0x01 && buffer.bytesize > 0 # rubocop: disable Style/GuardClause @device_token = shift_string(buffer) end end |
#protocol_version ⇒ Object
Please use #protocol_level instead
547 548 549 |
# File 'lib/qubitro-mqtt/packet.rb', line 547 def protocol_version protocol_level end |
#protocol_version=(args) ⇒ Object
Please use #protocol_level= instead
552 553 554 |
# File 'lib/qubitro-mqtt/packet.rb', line 552 def protocol_version=(args) self.protocol_level = args end |