Class: Selective::Ruby::Core::NamedPipe
- Inherits:
-
Object
- Object
- Selective::Ruby::Core::NamedPipe
- Defined in:
- lib/selective/ruby/core/named_pipe.rb
Instance Attribute Summary collapse
-
#read_pipe_path ⇒ Object
readonly
Returns the value of attribute read_pipe_path.
-
#write_pipe_path ⇒ Object
readonly
Returns the value of attribute write_pipe_path.
Instance Method Summary collapse
- #delete_pipes ⇒ Object
-
#initialize(read_pipe_path, write_pipe_path, skip_reset: false) ⇒ NamedPipe
constructor
A new instance of NamedPipe.
- #initialize_pipes ⇒ Object
- #read ⇒ Object
- #reset! ⇒ Object
- #write(message) ⇒ Object
Constructor Details
#initialize(read_pipe_path, write_pipe_path, skip_reset: false) ⇒ NamedPipe
Returns a new instance of NamedPipe.
7 8 9 10 11 12 13 |
# File 'lib/selective/ruby/core/named_pipe.rb', line 7 def initialize(read_pipe_path, write_pipe_path, skip_reset: false) @read_pipe_path = read_pipe_path @write_pipe_path = write_pipe_path delete_pipes unless skip_reset initialize_pipes end |
Instance Attribute Details
#read_pipe_path ⇒ Object (readonly)
Returns the value of attribute read_pipe_path.
5 6 7 |
# File 'lib/selective/ruby/core/named_pipe.rb', line 5 def read_pipe_path @read_pipe_path end |
#write_pipe_path ⇒ Object (readonly)
Returns the value of attribute write_pipe_path.
5 6 7 |
# File 'lib/selective/ruby/core/named_pipe.rb', line 5 def write_pipe_path @write_pipe_path end |
Instance Method Details
#delete_pipes ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/selective/ruby/core/named_pipe.rb', line 68 def delete_pipes # Close the pipes before deleting them read_pipe&.close write_pipe&.close # Allow threads to close before deleting pipes sleep(0.1) delete_pipe(read_pipe_path) delete_pipe(write_pipe_path) rescue Errno::EPIPE # Noop end |
#initialize_pipes ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/selective/ruby/core/named_pipe.rb', line 15 def initialize_pipes create_pipes # Open the read and write pipes in separate threads Thread.new do @read_pipe = File.open(read_pipe_path, "r") end Thread.new do @write_pipe = File.open(write_pipe_path, "w") end end |
#read ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/selective/ruby/core/named_pipe.rb', line 49 def read return unless read_pipe begin = read_pipe.gets.chomp rescue NoMethodError => e if e.name == :chomp raise ConnectionLostError else raise e end end end |
#reset! ⇒ Object
63 64 65 66 |
# File 'lib/selective/ruby/core/named_pipe.rb', line 63 def reset! delete_pipes initialize_pipes end |
#write(message) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/selective/ruby/core/named_pipe.rb', line 27 def write() return unless write_pipe chunk_size = 1024 # 1KB chunks offset = 0 begin while offset < .bytesize chunk = .byteslice(offset, chunk_size) write_pipe.write(chunk) write_pipe.flush offset += chunk_size end write_pipe.write("\n") write_pipe.flush rescue Errno::EPIPE raise ConnectionLostError end end |