class Nitra::Channel

Constants

ProtocolInvalidError

Attributes

raise_epipe_on_write_error[RW]
rd[R]
wr[R]

Public Class Methods

new(rd, wr) click to toggle source
# File lib/nitra/channel.rb, line 10
def initialize(rd, wr)
  @rd = rd
  @wr = wr
end
pipe() click to toggle source
# File lib/nitra/channel.rb, line 15
def self.pipe
  c_rd, s_wr = IO.pipe
  s_rd, c_wr = IO.pipe
  [new(c_rd, c_wr), new(s_rd, s_wr)]
end
read_select(channels) click to toggle source
# File lib/nitra/channel.rb, line 21
def self.read_select(channels)
  fds = IO.select(channels.collect(&:rd))
  fds.first.collect do |fd|
    channels.detect {|c| c.rd == fd}
  end
end

Public Instance Methods

close() click to toggle source
# File lib/nitra/channel.rb, line 28
def close
  rd.close
  wr.close
end
read() click to toggle source
# File lib/nitra/channel.rb, line 33
def read
  return unless line = rd.gets
  if result = line.strip.match(/\ANITRA,(\d+)\z/)
    data = rd.read(result[1].to_i)
    YAML.load(data)
  else
    raise ProtocolInvalidError, "Expected nitra length line, got #{line.inspect}"
  end
end
write(data) click to toggle source
# File lib/nitra/channel.rb, line 43
def write(data)
  encoded = YAML.dump(data)
  wr.write("NITRA,#{encoded.bytesize}\n#{encoded}")
  wr.flush
rescue Errno::EPIPE
  raise if raise_epipe_on_write_error
end