Class: ScripTTY::Apps::ReplayApp

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptty/apps/replay_app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ ReplayApp

Returns a new instance of ReplayApp.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scriptty/apps/replay_app.rb', line 33

def initialize(argv)
  @client_connection = nil
  @server_connection = nil
  @output_file = nil
  @options = parse_options(argv)
  @console_password = ""   # TODO SECURITY FIXME
  @attached_consoles = []
  @log_stringio = StringIO.new
  @log = Logger.new(@log_stringio)
  @net = ScripTTY::Net::EventLoop.new
  @input_transcript = []
  @last_command = nil
end

Instance Attribute Details

#termObject (readonly)

Returns the value of attribute term.



31
32
33
# File 'lib/scriptty/apps/replay_app.rb', line 31

def term
  @term
end

Instance Method Details

#detach_console(console) ⇒ Object



47
48
49
# File 'lib/scriptty/apps/replay_app.rb', line 47

def detach_console(console)
  @attached_consoles.delete(console)
end

#exitObject

Instruct the event loop to exit.

Can be invoked by another thread.



102
103
104
# File 'lib/scriptty/apps/replay_app.rb', line 102

def exit
  @net.exit
end

#handle_console_command_entered(cmd) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/scriptty/apps/replay_app.rb', line 81

def handle_console_command_entered(cmd)
  case cmd
  when /^$/   # repeat last command
    if @last_command
      handle_console_command_entered(@last_command)
    else
      log.warn("No previous command entered")
    end
    return nil
  when /^(\d*)(n|next)$/i    # replay next step
    count = $1 ? $1.to_i : 1
    count.times { replay_next }
  else
    log.warn("Unknown console command: #{cmd}")
  end
  @last_command = cmd
end

#log_messagesObject



51
52
53
# File 'lib/scriptty/apps/replay_app.rb', line 51

def log_messages
  ([""]*10 + @log_stringio.string.split("\n"))[-10..-1].map{|line| line.sub(/^.*?\]/, '')}
end

#mainObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/scriptty/apps/replay_app.rb', line 55

def main
  @output_file = Util::Transcript::Writer.new(File.open(@options[:output], "w")) if @options[:output]
  @net.on_accept(@options[:console_addrs] || [], :multiple => true) do |conn|
    p = ScripTTY::Net::PasswordPrompt.new(conn, "Console password: ")
    p.authenticate { |password| password == @console_password }
    p.on_fail { conn.write("Authentiation failed.\r\n") { conn.close } }
    p.on_success {
      @attached_consoles << ScripTTY::Net::Console.new(conn, self)
      @attached_consoles.each { |c| c.refresh! }
    }
  end
  @net.on_accept(@options[:listen_addrs], :multiple => true) do |conn|
    @output_file.client_open(*conn.remote_address) if @output_file
    @client_connection = conn
    @client_connection.on_receive_bytes { |bytes| handle_client_receive_bytes(bytes) }
    @client_connection.on_close { handle_client_close ; @client_connection = nil }
    handle_client_connected
  end
  @net.main
ensure
  if @output_file
    @output_file.close
    @output_file = nil
  end
end