Class: Fairy::Stdout

Inherits:
Object
  • Object
show all
Defined in:
lib/fairy/share/stdout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(peer) ⇒ Stdout

Returns a new instance of Stdout.



11
12
13
14
15
16
17
18
# File 'lib/fairy/share/stdout.rb', line 11

def initialize(peer)
  @local_stdout = $stdout
  @peer = peer
  @threads = {}
  
  @mutex = Mutex.new

end

Instance Attribute Details

#local_stdoutObject (readonly)

Returns the value of attribute local_stdout.



20
21
22
# File 'lib/fairy/share/stdout.rb', line 20

def local_stdout
  @local_stdout
end

Instance Method Details

#flushObject



47
48
49
# File 'lib/fairy/share/stdout.rb', line 47

def flush
  # do nothing
end

#replace_stdout(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fairy/share/stdout.rb', line 32

def replace_stdout(&block)
  @mutex.synchronize do
	@threads[Thread.current] = 0 unless @threads[Thread.current] 
	@threads[Thread.current] += 1
  end
  begin
	yield
  ensure
	@mutex.synchronize do
	  @threads[Thread.current] -= 1
	  @threads.delete(Thread.current) if @threads[Thread.current] == 0
	end
  end
end

#write(str) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/fairy/share/stdout.rb', line 22

def write(str)
  @mutex.synchronize do
	if @threads[Thread.current]
	  @peer.stdout_write(str)
	else
	  @local_stdout.write(str)
	end
  end
end