Class: EventMachine::MQTT::Connection

Inherits:
Connection
  • Object
show all
Defined in:
lib/em-mqtt/connection.rb

Direct Known Subclasses

ClientConnection, ServerConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_receivedObject (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_sentObject (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

#stateObject (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

Returns:

  • (Boolean)


17
18
19
# File 'lib/em-mqtt/connection.rb', line 17

def connected?
  state == :connected
end

#post_initObject



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



43
44
# File 'lib/em-mqtt/connection.rb', line 43

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
# 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
  end
end

#send_packet(packet) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/em-mqtt/connection.rb', line 46

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