def run
@pipe = open_pipe
@running = true
while @running && IO::select([@pipe], nil, nil, nil)
message = ""
length = ""
byte = nil
reading_length = true
found_length = false
while reading_length
byte = @pipe.read_nonblock(1)
if "#{byte}" =~ /\d/
length << byte
found_length = true
elsif found_length == false
next
else
reading_length = false
end
end
length = Integer(length, 10)
type = byte
message << "#{length}#{type}"
message << @pipe.read(length)
decoded = OTNetstring.parse(message)
modified_paths = decoded["events"].map {|event| event["path"]}
case callback.arity
when 1
callback.call(modified_paths)
when 2
callback.call(modified_paths, decoded)
end
end
rescue Interrupt, IOError, Errno::EBADF
ensure
stop
end