KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > common > telnet > BaseHandler


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.common.telnet;
56
57 import java.net.Socket JavaDoc;
58 import java.io.InputStream JavaDoc;
59 import java.io.IOException JavaDoc;
60 import java.io.OutputStream JavaDoc;
61
62 import org.lateralnz.common.util.IOUtils;
63
64 public class BaseHandler extends Thread JavaDoc implements Constants {
65   private boolean running = true;
66   
67   private Socket JavaDoc sock;
68   protected InputStream JavaDoc in;
69   protected OutputStream JavaDoc out;
70   private boolean echo = true;
71   protected StringBuffer JavaDoc line = new StringBuffer JavaDoc();
72   private int linepos = 0;
73   
74   private BaseHandler() { }
75   
76   public BaseHandler(Server parent, Socket JavaDoc sock) throws IOException JavaDoc {
77     this.sock = sock;
78     this.in = sock.getInputStream();
79     this.out = sock.getOutputStream();
80
81     write(new int[]{ IAC, WILL, ECHO });
82 // out.write(new byte[]{ IAC, DONT, ECHO });
83
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.write(new byte[]{ IAC, SB, TERMINAL_TYPE });
88
// out.write(new byte[]{ IAC,SB,(byte)24,(byte)255,(byte)240});
89
out.flush();
90
91     start();
92   }
93   
94   protected boolean deleteChar() throws IOException JavaDoc {
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 JavaDoc {
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 JavaDoc {
125     for (int i = 0; i < bytes.length; i++) {
126       out.write(bytes[i]);
127     }
128   }
129   
130  /**
131   * currently makes a best guess effort to read the escape sequence
132   * but basically ignores them.
133   */

134   private void getEscapeSequence() throws IOException JavaDoc {
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         // Interpret As Command received
202
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         // escape sequence
211
else if (i == 27) {
212           getEscapeSequence();
213           continue;
214         }
215         // backspace or delete
216
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 JavaDoc 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 JavaDoc {
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 JavaDoc {
267     return false;
268   }
269   
270   public boolean handleInput(String JavaDoc line) throws IOException JavaDoc {
271     System.out.println(line);
272     return false;
273   }
274 }
Popular Tags