Class: EaseEngine::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ease_engine/watcher.rb

Defined Under Namespace

Classes: Info

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Watcher

Returns a new instance of Watcher.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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

Instance Method Details

#[](id) ⇒ Object



91
92
93
# File 'lib/ease_engine/watcher.rb', line 91

def []( id )
  @watches.key?( id ) ? @watches[ id ].io : nil
end

#add(io, callbacks) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

#eachObject



85
86
87
88
89
# File 'lib/ease_engine/watcher.rb', line 85

def each
  @watches.each{|id, info|
    yield info
  }
end

#remove(io) ⇒ Object



71
72
73
74
75
76
77
78
79
# 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

#sizeObject



81
82
83
# File 'lib/ease_engine/watcher.rb', line 81

def size
  @watches.size
end

#watch(timeout = 0) ⇒ Object



47
48
49
50
51
52
53
# 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