Class: Rookout::ComWs::Pinger

Inherits:
Object
  • Object
show all
Defined in:
lib/rookout/com_ws/pinger.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, interval, timeout) ⇒ Pinger

Returns a new instance of Pinger.



6
7
8
9
10
11
12
13
# File 'lib/rookout/com_ws/pinger.rb', line 6

def initialize connection, interval, timeout
  @interval = interval
  @timeout = timeout
  @connection = connection

  @last_pong = Time.now
  @last_ping = Time.now
end

Instance Method Details

#repeatObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rookout/com_ws/pinger.rb', line 15

def repeat
  loop do
    if Time.now - @last_ping > @interval
      Logger.instance.debug "Sending Ping"
      @last_ping = Time.now
      begin
        @connection.ping Time.now.to_s do
          Logger.instance.debug "Got Ping reply"
          @last_pong = Time.now
        end
      rescue RuntimeError, Errno::EPIPE
        Logger.instance.debug "Failed to send ping"
        break
      end
    end

    if Time.now - @last_pong > @timeout
      Logger.instance.debug "Ping timeout"
      break
    end

    yield
  end
end