Class: Mfp::PingHandler
- Inherits:
-
Object
- Object
- Mfp::PingHandler
- Defined in:
- lib/mfp/ping_handler.rb
Instance Method Summary collapse
-
#initialize(idle_timeout:, ping_timeout:) ⇒ PingHandler
constructor
A new instance of PingHandler.
- #notify_pong(cookie) ⇒ Object
- #notify_received ⇒ Object
- #recv ⇒ Object
- #start(task) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(idle_timeout:, ping_timeout:) ⇒ PingHandler
Returns a new instance of PingHandler.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/mfp/ping_handler.rb', line 5 def initialize(idle_timeout:, ping_timeout:) @timer_notify = Async::Queue.new @notifications = Async::Queue.new @idle_timer = ResettableTimer.new(idle_timeout, @timer_notify, tag: :idle) @response_timer = ResettableTimer.new(ping_timeout, @timer_notify, tag: :response) @waiting_response = false @waiting_cookie = nil end |
Instance Method Details
#notify_pong(cookie) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/mfp/ping_handler.rb', line 54 def notify_pong() return if !@waiting_response || != @waiting_cookie @waiting_response = false @waiting_cookie = nil @idle_timer.reset @response_timer.reset end |
#notify_received ⇒ Object
47 48 49 50 51 52 |
# File 'lib/mfp/ping_handler.rb', line 47 def notify_received return if @waiting_response @idle_timer.reset @response_timer.reset end |
#recv ⇒ Object
16 |
# File 'lib/mfp/ping_handler.rb', line 16 def recv = @notifications.dequeue |
#start(task) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mfp/ping_handler.rb', line 18 def start(task) task.async { @idle_timer.start(task) } task.async { @response_timer.start(task) } task.async do loop do ev, tag = @timer_notify.dequeue if ev.nil? && tag.nil? break elsif tag == :response && ev == :timeout @notifications.enqueue({ kind: :timeout }) stop break elsif tag == :idle && ev == :timeout next if @waiting_response @waiting_response = true @waiting_cookie = SecureRandom.bytes(8).unpack1("Q>") @notifications.enqueue({ kind: :request, cookie: @waiting_cookie, }) @response_timer.reset else raise "Unexpected ev/tag pair: #{ev}, #{tag}" end end end end |
#stop ⇒ Object
63 64 65 66 67 68 |
# File 'lib/mfp/ping_handler.rb', line 63 def stop @idle_timer.stop @response_timer.stop @timer_notify.close @notifications.close end |