KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
58 import java.lang.reflect.Constructor JavaDoc;
59 import java.net.ServerSocket JavaDoc;
60 import java.net.Socket JavaDoc;
61
62 import org.lateralnz.common.util.IOUtils;
63 import org.lateralnz.common.util.StringUtils;
64
65 public class Server extends Thread JavaDoc implements Constants {
66   
67   private static final String JavaDoc[] NAMES = new String JavaDoc[256];
68   private static final String JavaDoc UNKNOWN = "[UNK]";
69     
70   static {
71     for (int i = 0; i < NAMES.length; i++) {
72       NAMES[i] = UNKNOWN;
73     }
74     NAMES[0] = "NULL";
75     NAMES[1] = "echo";
76     NAMES[3] = "suppress go ahead";
77     NAMES[5] = "status";
78     NAMES[6] = "timing mark";
79     NAMES[8] = "backspace";
80     NAMES[24] = "terminal type";
81     NAMES[31] = "window size";
82     NAMES[32] = "terminal speed";
83     NAMES[33] = "remote flow control";
84     NAMES[34] = "linemode";
85     NAMES[36] = "environment variables";
86     
87     NAMES[240] = "SE";
88     NAMES[241] = "NOP";
89     NAMES[242] = "DM";
90     NAMES[243] = "BRK";
91     NAMES[244] = "IP";
92     NAMES[245] = "AO";
93     NAMES[246] = "AYT";
94     NAMES[247] = "EC";
95     NAMES[248] = "EL";
96     NAMES[249] = "GA";
97     NAMES[250] = "SB";
98     NAMES[251] = "WILL";
99     NAMES[252] = "WONT";
100     NAMES[253] = "DO";
101     NAMES[254] = "DONT";
102     NAMES[255] = "IAC";
103   }
104   
105   private static final String JavaDoc QUIT = "quit";
106   
107   
108   private boolean running = true;
109   private int port;
110   private Class JavaDoc baseHandlerClass;
111   private String JavaDoc allowFromRegex;
112   
113   public Server(int port, Class JavaDoc baseHandlerClass, String JavaDoc allowFromRegex) throws IOException {
114     super();
115     
116     this.port = port;
117     this.baseHandlerClass = baseHandlerClass;
118    
119     if (StringUtils.isEmpty(allowFromRegex)) {
120       this.allowFromRegex = ".*";
121     }
122     else {
123       this.allowFromRegex = allowFromRegex;
124     }
125     
126    
127     
128     start();
129   }
130
131   public static final String JavaDoc getCommand(int b) {
132     if (b < 0 || b > NAMES.length) {
133       return UNKNOWN;
134     }
135     else {
136       return NAMES[b];
137     }
138   }
139   
140   public static final String JavaDoc getCommandString(int b1, int b2, int b3) {
141     return getCommand(b1) + " " + getCommand(b2) + " " + getCommand(b3);
142   }
143
144   public void run() {
145     ServerSocket JavaDoc ss = null;
146     Socket JavaDoc socket;
147     try {
148       ss = new ServerSocket JavaDoc(port);
149       Constructor JavaDoc cons = baseHandlerClass.getConstructor(new Class JavaDoc[]{ Server.class, Socket JavaDoc.class });
150
151       while (running) {
152         socket = ss.accept();
153         if (!StringUtils.matches(socket.getInetAddress().getHostAddress(), allowFromRegex)) {
154           System.err.println("invalid access from " + socket.getInetAddress());
155           socket.close();
156           continue;
157         }
158         BaseHandler bh = (BaseHandler)cons.newInstance(new Object JavaDoc[]{ this, socket });
159         
160         this.yield();
161       }
162     }
163     catch (Exception JavaDoc e) {
164       e.printStackTrace();
165     }
166     finally {
167       IOUtils.close(ss);
168     }
169   }
170   
171   public void shutdown() {
172     running = false;
173     try { interrupt(); } catch (Exception JavaDoc e) { }
174   }
175
176   public static final void main(String JavaDoc[] args) throws Exception JavaDoc {
177     Server s = new Server(1111, BaseHandler.class, "");
178   }
179
180 }
Popular Tags