class MainLoop::Bus

Constants

EOL

Attributes

read[R]
write[R]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/main_loop/bus.rb, line 13
def initialize
  super()
  @read, @write = IO.pipe
  @read.sync = true
  @write.sync = true
  @buffer = ''
end

Public Instance Methods

close() click to toggle source
# File lib/main_loop/bus.rb, line 25
def close
  @write.close rescue nil
  @read.close rescue nil
end
closed?() click to toggle source
# File lib/main_loop/bus.rb, line 30
def closed?
  @write.closed? || @read.closed?
end
empty?(timeout = 0) click to toggle source
# File lib/main_loop/bus.rb, line 21
def empty?(timeout = 0)
  !wait_for_event(timeout)
end
gets(timeout) click to toggle source
# File lib/main_loop/bus.rb, line 44
def gets(timeout)
  Timeouter.loop(timeout) do |t|
    line = gets_nonblock if wait_for_event(t.left)
    return line if line
  end
end
gets_nonblock() click to toggle source
# File lib/main_loop/bus.rb, line 51
def gets_nonblock
  while (ch = @read.read_nonblock(1))
    @buffer << ch
    next if ch != MainLoop::Bus::EOL

    line = @buffer
    @buffer = ''
    return line&.strip
  end
  nil
rescue IO::WaitReadable
  nil
end
puts(str) click to toggle source
# File lib/main_loop/bus.rb, line 34
def puts(str)
  synchronize do
    @write.puts str.to_s
  end
end
wait_for_event(timeout) click to toggle source
# File lib/main_loop/bus.rb, line 40
def wait_for_event(timeout)
  IO.select([@read], [], [], timeout)
end