Class: Rios::Proxy

Inherits:
Object show all
Defined in:
lib/rios/proxy.rb

Constant Summary collapse

DEFAULT_COMMAND =
ENV["SHELL"]

Instance Method Summary collapse

Constructor Details

#initializeProxy

Returns a new instance of Proxy.



8
9
10
11
12
13
14
# File 'lib/rios/proxy.rb', line 8

def initialize
  @fd_master, @fd_slave = Util.openpty($stdin.fileno)
  @input_filters  = []
  @output_filters = []
  @on_finishes    = []
  @pty_lock       = Mutex.new
end

Instance Method Details

#input(string) ⇒ Object

emulate user input



36
37
38
39
40
41
# File 'lib/rios/proxy.rb', line 36

def input(string)
  @pty_lock.synchronize {
    terminal.master.syswrite(string)
  }
  nil
end

#listen(command = nil, &block) ⇒ Object

begin proxy session



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rios/proxy.rb', line 54

def listen(command = nil, &block)
  @command = command || DEFAULT_COMMAND

  in_raw_mode {
    fork {
      fork {
        do_command(block)
      }
      do_output()
    }
    Signal.trap(:CHLD) { terminal.master.close() } # TODO: Is this OK?
    do_input()
  }
end

#on_finish(&block) ⇒ Object

register handler which will be called when target program exits



30
31
32
# File 'lib/rios/proxy.rb', line 30

def on_finish(&block)
  @on_finishes.push(block)
end

#on_input(&block) ⇒ Object

register handler which will be called when user inputs characters



18
19
20
# File 'lib/rios/proxy.rb', line 18

def on_input(&block)
  @input_filters.push(block)
end

#on_output(&block) ⇒ Object

register handler which will be called when target program outputs characters



24
25
26
# File 'lib/rios/proxy.rb', line 24

def on_output(&block)
  @output_filters.push(block)
end

#output(string) ⇒ Object

output string to the stdout (usually terminal)



45
46
47
48
49
50
# File 'lib/rios/proxy.rb', line 45

def output(string)
  @pty_lock.synchronize {
    $stdout.syswrite(string)
  }
  nil
end