Class: EMDirWatcher::Platform::Windows::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/em-dir-watcher/platform/windows.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, inclusions, exclusions, &handler) ⇒ Watcher

Returns a new instance of Watcher.


26
27
28
29
30
31
32
33
34
35
# File 'lib/em-dir-watcher/platform/windows.rb', line 26

def initialize path, inclusions, exclusions, &handler
    @path = path
    @handler = handler
    @active = true
    @ready_to_use = false
    @ready_to_use_handlers = []

    start_server
    setup_listener
end

Instance Method Details

#killObject


77
78
79
80
81
82
83
# File 'lib/em-dir-watcher/platform/windows.rb', line 77

def kill
    if @io
        Process.kill 9, @io.pid
        Process.waitpid @io.pid
        @io = nil
    end
end

#listener_diedObject


95
96
97
98
99
100
101
102
# File 'lib/em-dir-watcher/platform/windows.rb', line 95

def listener_died
    kill  # waitpid to cleanup zombie
    if @active
        EM.next_tick do
            setup_listener
        end
    end
end

#path_changed(path) ⇒ Object


91
92
93
# File 'lib/em-dir-watcher/platform/windows.rb', line 91

def path_changed path
    @handler.call path, true
end

#ready_to_use!Object


45
46
47
48
49
50
51
52
53
# File 'lib/em-dir-watcher/platform/windows.rb', line 45

def ready_to_use!
    return if @ready_to_use
    @ready_to_use = true
    # give the child process additional 100ms to start watching loop
    EM.add_timer 0.1 do
        @ready_to_use_handlers.each { |handler| handler.call() }
        @ready_to_use_handlers = nil
    end
end

#ready_to_use?Boolean

Returns:

  • (Boolean)

55
# File 'lib/em-dir-watcher/platform/windows.rb', line 55

def ready_to_use?; @ready_to_use; end

#setup_listenerObject


85
86
87
88
89
# File 'lib/em-dir-watcher/platform/windows.rb', line 85

def setup_listener
    path_to_script = File.join(File.dirname(__FILE__), 'windows', 'monitor.rb')
    path_to_ruby_exe = 'ruby'
    @io = IO.popen(path_to_ruby_exe + " " + path_to_script + " #{@server_port} " + File.expand_path(@path))
end

#start_serverObject


57
58
59
60
61
62
63
# File 'lib/em-dir-watcher/platform/windows.rb', line 57

def start_server
    @server = EM.start_server '127.0.0.1', 0, TcpHandler do |server|
        server.watcher = self
        ready_to_use!
    end
    @server_port, _ = Socket.unpack_sockaddr_in(EM::get_sockname(@server))
end

#stopObject


72
73
74
75
# File 'lib/em-dir-watcher/platform/windows.rb', line 72

def stop
    @active = false
    kill
end

#stop_serverObject


65
66
67
68
69
70
# File 'lib/em-dir-watcher/platform/windows.rb', line 65

def stop_server
    if @server
        EM.stop_server @server
        @server = nil
    end
end

#when_ready_to_use(&ready_to_use_handler) ⇒ Object


37
38
39
40
41
42
43
# File 'lib/em-dir-watcher/platform/windows.rb', line 37

def when_ready_to_use &ready_to_use_handler
  if @ready_to_use_handlers.nil?
      ready_to_use_handler.call()
  else
      @ready_to_use_handlers << ready_to_use_handler
  end
end