class IO::Dispatcher
Constants
- VERSION
Public Class Methods
new()
click to toggle source
# File lib/io-dispatcher.rb, line 5 def initialize @io_handlers = [{}, {}, {}] @timeout = nil end
Public Instance Methods
add_io_handler(input: nil, output: nil, exception: nil, handler: nil, &block)
click to toggle source
# File lib/io-dispatcher.rb, line 10 def add_io_handler(input: nil, output: nil, exception: nil, handler: nil, &block) mode, io = if input [0, input] elsif output [1, output] elsif exception [2, exception] end @io_handlers[mode][io] = handler || block end
remove_io_handler(input: nil, output: nil, exception: nil)
click to toggle source
# File lib/io-dispatcher.rb, line 21 def remove_io_handler(input: nil, output: nil, exception: nil) mode, io = if input [0, input] elsif output [1, output] elsif exception [2, exception] end @io_handlers[mode].delete(io) end
run()
click to toggle source
# File lib/io-dispatcher.rb, line 37 def run loop do if (ready = IO.select(*@io_handlers.map(&:keys), @timeout)) ready.each_with_index do |mode, i| mode.each { |io| @io_handlers[i][io].call(io) } end else @timeout_handler.call if @timeout_handler end end end
set_timeout_handler(timeout, &block)
click to toggle source
# File lib/io-dispatcher.rb, line 32 def set_timeout_handler(timeout, &block) @timeout = timeout @timeout_handler = block end