1 55 package org.lateralnz.common.telnet; 56 57 import java.net.Socket ; 58 import java.io.InputStream ; 59 import java.io.IOException ; 60 import java.io.OutputStream ; 61 62 import org.lateralnz.common.util.IOUtils; 63 64 public class BaseHandler extends Thread implements Constants { 65 private boolean running = true; 66 67 private Socket sock; 68 protected InputStream in; 69 protected OutputStream out; 70 private boolean echo = true; 71 protected StringBuffer line = new StringBuffer (); 72 private int linepos = 0; 73 74 private BaseHandler() { } 75 76 public BaseHandler(Server parent, Socket sock) throws IOException { 77 this.sock = sock; 78 this.in = sock.getInputStream(); 79 this.out = sock.getOutputStream(); 80 81 write(new int[]{ IAC, WILL, ECHO }); 82 write(new int[]{ IAC, WONT, LINEMODE }); 84 write(new int[]{ IAC, DONT, LINEMODE }); 85 write(new int[]{ IAC, WILL, SUPPRESS_GO_AHEAD }); 86 write(new int[]{ IAC, DO, SUPPRESS_GO_AHEAD }); 87 out.flush(); 90 91 start(); 92 } 93 94 protected boolean deleteChar() throws IOException { 95 if (line.length() > 0) { 96 out.write(8); 97 out.write(32); 98 out.write(8); 99 out.flush(); 100 line.deleteCharAt(line.length()-1); 101 return true; 102 } 103 else { 104 return false; 105 } 106 } 107 108 protected void sendCursor(int count, int dir) throws IOException { 109 for (int i = 0; i < count; i++) { 110 if (dir == ESC_LEFT) { 111 write(new int[]{ ESCAPE, (int)'[', (int)'D' }); 112 } 113 else { 114 write(new int[]{ ESCAPE, (int)'[', (int)'C' }); 115 } 116 } 117 } 118 119 120 public void setEcho(boolean echo) { 121 this.echo = echo; 122 } 123 124 public void write(int[] bytes) throws IOException { 125 for (int i = 0; i < bytes.length; i++) { 126 out.write(bytes[i]); 127 } 128 } 129 130 134 private void getEscapeSequence() throws IOException { 135 int esc = -1; 136 int mod = -1; 137 138 char c = (char)in.read(); 139 char c2; 140 char c3; 141 switch (c) { 142 case '[': 143 c2 = (char)in.read(); 144 if (c2 >= '0' && c2 <= '9') { 145 c3 = (char)in.read(); 146 } 147 else if (c2 == 'A') { 148 esc = ESC_UP; 149 mod = 1; 150 } 151 else if (c2 == 'B') { 152 esc = ESC_DOWN; 153 mod = 1; 154 } 155 else if (c2 == 'D') { 156 esc = ESC_LEFT; 157 mod = 1; 158 } 159 else if (c2 == 'C') { 160 esc = ESC_RIGHT; 161 mod = 1; 162 } 163 break; 164 case '(': 165 in.read(); 166 break; 167 case '?': 168 c2 = (char)in.read(); 169 if (c2 == '2' || c2 == '6') { 170 in.read(); 171 } 172 break; 173 case 'c': 174 case 'D': 175 case 'E': 176 case 'M': 177 case '7': 178 case '8': 179 case '=': 180 case '>': 181 case 'G': 182 case 'H': 183 case 'I': 184 case 'J': 185 case 'N': 186 case 'O': 187 case 'Z': 188 } 189 handleEscape(esc, mod); 190 } 191 192 public void run() { 193 try { 194 while (running) { 195 int i = in.read(); 196 if (i == -1) { 197 running = false; 198 break; 199 } 200 201 if (i == 255) { 203 System.out.println(Server.getCommandString(i, in.read(), in.read())); 204 continue; 205 } 206 207 if (i < 32 && handleControlChar(i)) { 208 continue; 209 } 210 else if (i == 27) { 212 getEscapeSequence(); 213 continue; 214 } 215 else if (i == 127 || i == 8) { 217 if (line.length() > 0) { 218 deleteChar(); 219 } 220 continue; 221 } 222 223 if (echo) { 224 out.write(i); 225 } 226 line.append((char)i); 227 228 this.yield(); 229 } 230 } 231 catch (IOException ioe) { 232 ioe.printStackTrace(); 233 } 234 finally { 235 IOUtils.close(out); 236 IOUtils.close(in); 237 IOUtils.close(sock); 238 } 239 } 240 241 public boolean handleControlChar(int c) throws IOException { 242 switch (c) { 243 case 0: 244 return true; 245 case 9: 246 out.write((int)' '); 247 out.write((int)' '); 248 line.append(" "); 249 return true; 250 case 10: 251 case 13: 252 out.write(10); 253 out.write(13); 254 handleInput(line.toString()); 255 line.delete(0, line.length()); 256 return true; 257 case 4: 258 case 24: 259 System.out.println("received eot"); 260 running = false; 261 return true; 262 } 263 return false; 264 } 265 266 public boolean handleEscape(int esc, int mod) throws IOException { 267 return false; 268 } 269 270 public boolean handleInput(String line) throws IOException { 271 System.out.println(line); 272 return false; 273 } 274 } | Popular Tags |