class EaseEngine::Watcher

Public Class Methods

new( options = {} ) click to toggle source
# File lib/ease_engine/watcher.rb, line 29
def initialize( options = {} )
  # 環境に合わせて適切なものを選択
  if ! options.key?( :backend )
    backend = :select
    if EaseEngine::Platform.mac?
      backend = :kqueue
    elsif EaseEngine::Platform.linux?
      backend = :epoll
    end
    
    options[ :backend ] = backend
  end
  
  EaseEngine::Log.inf( "Watcher #{options}" )
  @loop = Cool.io::Loop.new( options )
  @watches = {}
end

Public Instance Methods

[]( id ) click to toggle source
# File lib/ease_engine/watcher.rb, line 91
def []( id )
  @watches.key?( id ) ? @watches[ id ].io : nil
end
add( io, callbacks ) click to toggle source
# File lib/ease_engine/watcher.rb, line 55
def add( io, callbacks )
  id = io.to_i
  if @watches.key?( id )
    info = @watches[ id ]
    @watches.delete( id )
    info.detach
  end
  
  flags = ""
  flags = "r" if callbacks.key?( :on_read )
  flags = "#{flags}w" if callbacks.key?( :on_write )
  
  @watches[ id ] = Info.new( io, flags, callbacks )
  @watches[ id ].attach @loop
end
each() { |info| ... } click to toggle source
# File lib/ease_engine/watcher.rb, line 85
def each
  @watches.each{|id, info|
    yield info
  }
end
remove( io ) click to toggle source
# File lib/ease_engine/watcher.rb, line 71
def remove( io )
  id = io.to_i
  return if ! @watches.key?( id )
  
  info = @watches[ id ]
  @watches.delete( id )
  info.detach
  info.on_remove
end
size() click to toggle source
# File lib/ease_engine/watcher.rb, line 81
def size
  @watches.size
end
watch( timeout = 0 ) click to toggle source
# File lib/ease_engine/watcher.rb, line 47
def watch( timeout = 0 )
  if ! @watches.empty?
    @loop.run_once( timeout )
  elsif 0 < timeout
    sleep timeout
  end
end