Class: Nitra::Channel
- Inherits:
-
Object
- Object
- Nitra::Channel
- Defined in:
- lib/nitra/channel.rb
Constant Summary collapse
- ProtocolInvalidError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#raise_epipe_on_write_error ⇒ Object
Returns the value of attribute raise_epipe_on_write_error.
-
#rd ⇒ Object
readonly
Returns the value of attribute rd.
-
#wr ⇒ Object
readonly
Returns the value of attribute wr.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(rd, wr) ⇒ Channel
constructor
A new instance of Channel.
- #read ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(rd, wr) ⇒ Channel
Returns a new instance of Channel.
10 11 12 13 |
# File 'lib/nitra/channel.rb', line 10 def initialize(rd, wr) @rd = rd @wr = wr end |
Instance Attribute Details
#raise_epipe_on_write_error ⇒ Object
Returns the value of attribute raise_epipe_on_write_error.
8 9 10 |
# File 'lib/nitra/channel.rb', line 8 def raise_epipe_on_write_error @raise_epipe_on_write_error end |
#rd ⇒ Object (readonly)
Returns the value of attribute rd.
7 8 9 |
# File 'lib/nitra/channel.rb', line 7 def rd @rd end |
#wr ⇒ Object (readonly)
Returns the value of attribute wr.
7 8 9 |
# File 'lib/nitra/channel.rb', line 7 def wr @wr end |
Class Method Details
.pipe ⇒ Object
15 16 17 18 19 |
# 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) ⇒ Object
21 22 23 24 25 26 |
# 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 |
Instance Method Details
#close ⇒ Object
28 29 30 31 |
# File 'lib/nitra/channel.rb', line 28 def close rd.close wr.close end |
#read ⇒ Object
33 34 35 36 37 38 39 40 41 |
# 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) ⇒ Object
43 44 45 46 47 48 49 |
# 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 |