Module: PPipe::Reader

Includes:
Log, Timeout
Included in:
PPipe, Methods
Defined in:
lib/parallelpipes.rb

Overview

PPipe requires a method of reading pipes that will not block even if the pipe is empty, is at eof, or has not been written to yet. configure_reader dynamically defines this method, which is called read_pipe

Reader is included in PPipe. It can also be included independently in any class

Instance Attribute Summary

Attributes included from Log

#verbosity

Instance Method Summary collapse

Methods included from Log

clean_up, io=, #log, log_file, log_file=

Instance Method Details

#configure_readerObject

The best way of defining Reader#read_pipe is operating system dependent. Work out which is the best way and then define the method read_pipe to be this method.



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/parallelpipes.rb', line 967

def configure_reader
	@verbosity ||= 0  # for logging purposes
	pipes = [nil, IO.pipe]

	pipe = IO.pipe
	
	pipes[1][1].sync = true
	pipe[1].sync = true

	my_pipes = []

	pid = Kernel.fork
	
	if pid
		pipe[1].close
		my_pipes.push pipe[0]
		pipes[1][0].close
		pipes[1] = pipes[1][1]
	# 			pipes[1].puts
	# 			sleep 0.002
		log 'v8', 'sending pears'
		pipes[1].sync = true
		pipes[1].print('pears' + "\n")
	# 			cmd = [2].pack("S")
	# 			pipes[1].ioctl(5, cmd)
	# 			sleep 1
		log 'v8',  'waiting for apples...'
		method = determine_read(my_pipes[0], "apples\nare\nnice\n")
		log 'v8', method
		self.class.send(:alias_method, :read_pipe, method)
		self.class.send(:alias_method, :get_line, method + :_get_line)
		my_pipes[0].close
		pipes[1].close
		return method
	else
		pipes[1][1].close
		my_pipes.push pipes[1][0]
		pipes[1] = nil
		pipe[0].close
		pipes[0] = pipe[1]
		pipes[0].sync = true
		log 'v8', pipes.inspect
	# 			sleep 0.5
		log 'v8', 'waiting for pears'
		log 'v8', determine_read(my_pipes[0], "pears\n")
		
			
		log 'v8', 'sending apples'
		pipes[0].print("apples\nare\nnice" + "\n")
		pipes[0].flush
		exit
	end
end

#get_line(pipe, sep = $/) ⇒ Object

Get the next line from the pipe. Return nil if the pipe is empty or at eof. Blocks if the pipe is not empty but does not contain a complete line.



828
829
# File 'lib/parallelpipes.rb', line 828

def get_line(pipe, sep = $/)
end

#read_pipe(pipe) ⇒ Object

Read all the contents of the pipe. Return nil if the pipe is empty. Never, ever, ever, ever block!



823
824
# File 'lib/parallelpipes.rb', line 823

def read_pipe(pipe)
end