Class: OTerm::Listener
- Inherits:
-
Object
- Object
- OTerm::Listener
- Defined in:
- lib/oterm/listener.rb
Instance Attribute Summary collapse
-
#buf ⇒ Object
Returns the value of attribute buf.
-
#con ⇒ Object
Returns the value of attribute con.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#executor ⇒ Object
Returns the value of attribute executor.
-
#history ⇒ Object
Returns the value of attribute history.
-
#hp ⇒ Object
history pointer.
-
#out ⇒ Object
Returns the value of attribute out.
-
#server ⇒ Object
Returns the value of attribute server.
Instance Method Summary collapse
- #close ⇒ Object
- #delete_char ⇒ Object
- #exec_cmd(cmd) ⇒ Object
- #history_back ⇒ Object
- #history_forward ⇒ Object
-
#initialize(server, con, executor) ⇒ Listener
constructor
A new instance of Listener.
- #insert(str) ⇒ Object
- #move_col(dif) ⇒ Object
- #process_ctrl_cmd(o) ⇒ Object
- #process_telnet_cmd(line) ⇒ Object
- #update_cmd(blen) ⇒ Object
- #vt100_cmd(line) ⇒ Object
Constructor Details
#initialize(server, con, executor) ⇒ Listener
Returns a new instance of Listener.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/oterm/listener.rb', line 15 def initialize(server, con, executor) @debug = server.debug() @server = server @con = con @executor = executor @buf = '' @kill_buf = nil @history = [] @hp = 0 @out = Output.new(con) @col = 0 @echo = false @done = false greeting = executor.greeting() @out.pl(greeting) unless greeting.nil? # initiate negotiations for single character mode and no echo @out.p(Telnet.msg(Telnet::DO, Telnet::SGA) + Telnet.msg(Telnet::DO, Telnet::ECHO)) out.prompt() until @done line = con.recv(100) begin len = line.size() break if 0 == len if @debug line.each_byte { |x| print("#{x} ") } plain = line.gsub(/./) { |c| c.ord < 32 || 127 <= c.ord ? '*' : c } puts "[#{line.size()}] #{plain}" end # determine input type (telnet command, char mode, line mode) o0 = line[0].ord() case o0 when 255 # telnet command process_telnet_cmd(line) when 27 # escape, vt100 sequence vt100_cmd(line) when 13 # new line exec_cmd(@buf) when 0..12, 14..26, 28..31, 127 # other control character process_ctrl_cmd(o0) else if 1 == len || (2 == len && "\000" == line[1]) # single char mode @hp = 0 insert(line[0]) else # line mode exec_cmd(line) end end rescue Exception => e puts "#{e.class}: #{e.}" e.backtrace.each { |bline| puts ' ' + bline } end end end |
Instance Attribute Details
#buf ⇒ Object
Returns the value of attribute buf.
9 10 11 |
# File 'lib/oterm/listener.rb', line 9 def buf @buf end |
#con ⇒ Object
Returns the value of attribute con.
7 8 9 |
# File 'lib/oterm/listener.rb', line 7 def con @con end |
#debug ⇒ Object
Returns the value of attribute debug.
13 14 15 |
# File 'lib/oterm/listener.rb', line 13 def debug @debug end |
#executor ⇒ Object
Returns the value of attribute executor.
8 9 10 |
# File 'lib/oterm/listener.rb', line 8 def executor @executor end |
#history ⇒ Object
Returns the value of attribute history.
10 11 12 |
# File 'lib/oterm/listener.rb', line 10 def history @history end |
#hp ⇒ Object
history pointer
11 12 13 |
# File 'lib/oterm/listener.rb', line 11 def hp @hp end |
#out ⇒ Object
Returns the value of attribute out.
12 13 14 |
# File 'lib/oterm/listener.rb', line 12 def out @out end |
#server ⇒ Object
Returns the value of attribute server.
6 7 8 |
# File 'lib/oterm/listener.rb', line 6 def server @server end |
Instance Method Details
#close ⇒ Object
72 73 74 |
# File 'lib/oterm/listener.rb', line 72 def close() @done = true end |
#delete_char ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/oterm/listener.rb', line 258 def delete_char() return if 0 == @col || 0 == @buf.size if @buf.size <= @col @buf.chop!() @out.p("\x08 \x08") else @buf = @buf[0...@col - 1] + @buf[@col..-1] @out.p("\r") @out.prompt() @out.p(@buf) @out.p(" \r") @out.prompt() @out.p(@buf[0...@col - 1]) end @col -= 1 end |
#exec_cmd(cmd) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/oterm/listener.rb', line 122 def exec_cmd(cmd) @hp = 0 cmd.strip!() if 0 == cmd.size() @out.pl() @out.prompt() @col = 0 return end @buf = "" @out.pl() @history << cmd if 0 < cmd.size() && (0 == @history.size() || @history[-1] != cmd) executor.execute(cmd, self) @out.prompt() @col = 0 end |
#history_back ⇒ Object
210 211 212 213 214 215 216 217 |
# File 'lib/oterm/listener.rb', line 210 def history_back() if @hp < @history.size() @hp += 1 blen = @buf.size() @buf = @history[-@hp] update_cmd(blen) end end |
#history_forward ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/oterm/listener.rb', line 219 def history_forward() if 0 < @hp && @hp <= @history.size() @hp -= 1 blen = @buf.size() if 0 == @hp @buf = '' else @buf = @history[-@hp] end update_cmd(blen) end end |
#insert(str) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/oterm/listener.rb', line 232 def insert(str) # TBD be smarter with vt100 if 0 == @col @buf = str + @buf @out.p("\r") @out.prompt() @out.p(@buf) @out.p("\r") @out.prompt() @out.p(@buf[0...str.size]) elsif @buf.size <= @col @buf << str @out.pc(str) @col = @buf.size else @buf = @buf[0...@col] + str + @buf[@col..-1] @out.p("\r") @out.prompt() @out.p(@buf) @out.p("\r") @out.prompt() @out.p(@buf[0...@col + str.size]) end @col += str.size end |
#move_col(dif) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/oterm/listener.rb', line 193 def move_col(dif) if 0 > dif while 0 > dif && 0 < @col @col -= 1 @out.p("\b") dif += 1 end else max = @buf.size while 0 < dif && @col < max @out.p(@buf[@col]) @col += 1 dif -= 1 end end end |
#process_ctrl_cmd(o) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/oterm/listener.rb', line 139 def process_ctrl_cmd(o) case o when 1 # ^a move_col(-@col) when 2 # ^b move_col(-1) when 4 # ^d @hp = 0 if @col < @buf.size @col += 1 delete_char() end when 5 # ^e move_col(@buf.size() - @col) when 6 # ^f move_col(1) when 8, 127 # backspace or delete @hp = 0 delete_char() when 9 # tab @hp = 0 @executor.tab(@buf, self) when 11 # ^k @hp = 0 if @col < @buf.size() @kill_buf = @buf[@col..-1] blen = @buf.size() @buf = @buf[0...@col] update_cmd(blen) end when 14 # ^n history_forward() when 16 # ^p history_back() when 21 # ^u @hp -= 1 @buf = '' when 25 # ^y insert(@kill_buf) end end |
#process_telnet_cmd(line) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/oterm/listener.rb', line 76 def process_telnet_cmd(line) reply = '' Telnet.parse(line).each do |v,f| case f when Telnet::ECHO case v when Telnet::WILL @echo = false reply += Telnet.msg(Telnet::WONT, f) when Telnet::WONT @echo = true reply += Telnet.msg(Telnet::WILL, f) end when Telnet::SGA case v when Telnet::WILL reply += Telnet.msg(Telnet::WILL, f) when Telnet::WONT reply += Telnet.msg(Telnet::WONT, f) end end end if 0 < reply.size @con.print(reply) else # Done negotiating with telnet. Initiate negotiation for vt100 ANSI support. @out.identify() end end |
#update_cmd(blen) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/oterm/listener.rb', line 181 def update_cmd(blen) @out.cr() @out.prompt() @out.p(@buf) # erase to end of line and come back if @buf.size() < blen dif = blen - @buf.size() @out.p(' ' * dif + "\b" * dif) end @col = @buf.size end |
#vt100_cmd(line) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/oterm/listener.rb', line 106 def vt100_cmd(line) # Possible arrow key. if 3 == line.size() && '[' == line[1] case line[2] when 'A' # up arrow history_back() when 'B' # down arrow history_forward() when 'C' # right arrow move_col(1) when 'D' # left arrow move_col(-1) end end end |