Class: Hijack::OutputReceiver

Inherits:
Object
  • Object
show all
Defined in:
lib/hijack/output_receiver.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote) ⇒ OutputReceiver

Returns a new instance of OutputReceiver.



38
39
40
# File 'lib/hijack/output_receiver.rb', line 38

def initialize(remote)
  @remote = remote
end

Class Method Details

.muteObject



26
27
28
# File 'lib/hijack/output_receiver.rb', line 26

def self.mute
  Process.kill(Signal.list["USR1"], @pid) if started?
end

.mute_momentarilyObject



34
35
36
# File 'lib/hijack/output_receiver.rb', line 34

def self.mute_momentarily
  Process.kill(Signal.list["HUP"], @pid) if started?
end

.pidObject



3
4
5
# File 'lib/hijack/output_receiver.rb', line 3

def self.pid
  @pid
end

.start(remote) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/hijack/output_receiver.rb', line 7

def self.start(remote)
  @started = true
  @instance = new(remote)
  @pid = fork do
    @instance.start
  end
end

.started?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/hijack/output_receiver.rb', line 15

def self.started?
  @started
end

.stopObject



19
20
21
22
23
24
# File 'lib/hijack/output_receiver.rb', line 19

def self.stop
  if started?
    Process.kill("KILL", @pid)
    Process.waitpid(@pid)
  end
end

.unmuteObject



30
31
32
# File 'lib/hijack/output_receiver.rb', line 30

def self.unmute
  Process.kill(Signal.list["USR2"], @pid) if started?
end

Instance Method Details

#get_io(io) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/hijack/output_receiver.rb', line 83

def get_io(io)
  case io.to_s
  when 'stdout'
    $stdout
  when 'stderr'
    $stderr
  else
    raise "get_io cannot handle '#{io}'"
  end
end

#muteObject



55
56
57
58
# File 'lib/hijack/output_receiver.rb', line 55

def mute
  @mute = true
  @explicit_mute = true
end

#mute_momentarilyObject



65
66
67
68
69
70
71
# File 'lib/hijack/output_receiver.rb', line 65

def mute_momentarily
  @mute = true
  Thread.new do 
    sleep 3
    @mute = false unless @explicit_mute
  end
end

#puts(io, str) ⇒ Object



78
79
80
81
# File 'lib/hijack/output_receiver.rb', line 78

def puts(io, str)
  return if @mute
  get_io(io).puts(str)
end

#setup_signal_trapsObject



49
50
51
52
53
# File 'lib/hijack/output_receiver.rb', line 49

def setup_signal_traps
  Signal.trap("USR1") { mute }
  Signal.trap("USR2") { unmute }
  Signal.trap("HUP")  { mute_momentarily }
end

#startObject



42
43
44
45
46
47
# File 'lib/hijack/output_receiver.rb', line 42

def start
  setup_signal_traps
  DRb.start_service(Hijack.socket_for(Process.pid), self)
  @remote.evaluate("OutputCopier.start(#{Process.pid})")
  DRb.thread.join
end

#unmuteObject



60
61
62
63
# File 'lib/hijack/output_receiver.rb', line 60

def unmute
  @mute = false
  @explicit_mute = false
end

#write(io, str) ⇒ Object



73
74
75
76
# File 'lib/hijack/output_receiver.rb', line 73

def write(io, str)
  return if @mute
  get_io(io).write(str)
end