Class: EventMachine::MQTT::Connection
- Inherits:
-
Connection
- Object
- Connection
- EventMachine::MQTT::Connection
- Defined in:
- lib/em/mqtt/connection.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#last_received ⇒ Object
readonly
Returns the value of attribute last_received.
-
#last_sent ⇒ Object
readonly
Returns the value of attribute last_sent.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
-
#connected? ⇒ Boolean
Checks whether a connection is full established.
- #post_init ⇒ Object
-
#process_packet(packet) ⇒ Object
The function needs to be sub-classed.
- #receive_data(data) ⇒ Object
- #send_packet(packet) ⇒ Object
Instance Attribute Details
#last_received ⇒ Object (readonly)
Returns the value of attribute last_received.
6 7 8 |
# File 'lib/em/mqtt/connection.rb', line 6 def last_received @last_received end |
#last_sent ⇒ Object (readonly)
Returns the value of attribute last_sent.
5 6 7 |
# File 'lib/em/mqtt/connection.rb', line 5 def last_sent @last_sent end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
4 5 6 |
# File 'lib/em/mqtt/connection.rb', line 4 def state @state end |
Instance Method Details
#connected? ⇒ Boolean
Checks whether a connection is full established
17 18 19 |
# File 'lib/em/mqtt/connection.rb', line 17 def connected? state == :connected end |
#post_init ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/em/mqtt/connection.rb', line 8 def post_init @state = :connecting @last_sent = 0 @last_received = 0 @packet = nil @data = '' end |
#process_packet(packet) ⇒ Object
The function needs to be sub-classed
44 45 |
# File 'lib/em/mqtt/connection.rb', line 44 def process_packet(packet) end |
#receive_data(data) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/em/mqtt/connection.rb', line 21 def receive_data(data) @data << data # FIXME: limit maximum data / packet size # Are we at the start of a new packet? if @packet.nil? and @data.length >= 2 @packet = MQTT::Packet.parse_header(@data) end # Do we have the the full packet body now? if @packet and @data.length >= @packet.body_length @packet.parse_body( @data.slice!(0...@packet.body_length) ) @last_received = Time.now process_packet(@packet) @packet = nil receive_data '' end end |
#send_packet(packet) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/em/mqtt/connection.rb', line 47 def send_packet(packet) # FIXME: Throw exception if we aren't connected? #unless packet.class == MQTT::Packet::Connect # raise MQTT::NotConnectedException if not connected? #end send_data(packet.to_s) @last_sent = Time.now end |