001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.server.telnet;
017:
018: import java.io.FilterInputStream;
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022:
023: public class TelnetInputStream extends FilterInputStream implements
024: TelnetCodes {
025:
026: private TelnetOption[] options = new TelnetOption[256];
027:
028: private OutputStream out = null;
029:
030: public TelnetInputStream(InputStream in, OutputStream out)
031: throws IOException {
032: super (in);
033: this .out = out;
034: negotiateOption(DONT, 1);
035: negotiateOption(DONT, 6);
036: negotiateOption(DONT, 24);
037: negotiateOption(DONT, 33);
038: negotiateOption(DONT, 34);
039: }
040:
041: public int read() throws IOException {
042: int b = super .read();
043:
044: if (b == IAC) {
045:
046: processCommand();
047:
048: b = this .read();
049: }
050:
051: return b;
052: }
053:
054: private void processCommand() throws IOException {
055:
056: print("C: IAC ");
057:
058: int command = super .read();
059:
060: switch (command) {
061: case WILL:
062: senderWillEnableOption(super .read());
063: break;
064: case DO:
065: pleaseDoEnableOption(super .read());
066: break;
067: case WONT:
068: senderWontEnableOption(super .read());
069: break;
070: case DONT:
071: pleaseDontEnableOption(super .read());
072: break;
073: default:
074: unimplementedCommand(command);
075: break;
076: }
077:
078: }
079:
080: private void unimplementedCommand(int command) {
081: println(command + ": command not found");
082: }
083:
084: private void senderWillEnableOption(int optionID)
085: throws IOException {
086:
087: println("WILL " + optionID);
088: TelnetOption option = getOption(optionID);
089:
090: if (option.hasBeenNegotiated())
091: return;
092:
093: if (option.isInNegotiation()) {
094: option.enable();
095: } else if (!option.isInNegotiation() && option.isSupported()) {
096: negotiateOption(DO, optionID);
097: option.enable();
098: } else if (!option.isInNegotiation() && !option.isSupported()) {
099: negotiateOption(DONT, optionID);
100: option.disable();
101: }
102: }
103:
104: private void pleaseDoEnableOption(int optionID) throws IOException {
105:
106: println("DO " + optionID);
107: TelnetOption option = getOption(optionID);
108:
109: if (option.hasBeenNegotiated())
110: return;
111:
112: if (option.isInNegotiation()) {
113: option.enable();
114: } else if (!option.isInNegotiation() && option.isSupported()) {
115: negotiateOption(WILL, optionID);
116: option.enable();
117: } else if (!option.isInNegotiation() && !option.isSupported()) {
118: negotiateOption(WONT, optionID);
119: option.disable();
120: }
121: }
122:
123: private void senderWontEnableOption(int optionID)
124: throws IOException {
125: println("WONT " + optionID);
126: TelnetOption option = getOption(optionID);
127:
128: if (option.hasBeenNegotiated())
129: return;
130:
131: if (!option.isInNegotiation()) {
132: negotiateOption(DONT, optionID);
133: }
134: option.disable();
135: }
136:
137: private void pleaseDontEnableOption(int optionID)
138: throws IOException {
139:
140: println("DONT " + optionID);
141:
142: TelnetOption option = getOption(optionID);
143:
144: if (option.hasBeenNegotiated())
145: return;
146:
147: if (!option.isInNegotiation()) {
148: negotiateOption(WONT, optionID);
149: }
150: option.disable();
151: }
152:
153: private void println(String s) {
154:
155: }
156:
157: private void print(String s) {
158:
159: }
160:
161: private void negotiateOption(int negotiate, int optionID)
162: throws IOException {
163: TelnetOption option = getOption(optionID);
164: option.inNegotiation = true;
165:
166: String n = null;
167: switch (negotiate) {
168: case WILL:
169: n = "WILL ";
170: break;
171: case DO:
172: n = "DO ";
173: break;
174: case WONT:
175: n = "WONT ";
176: break;
177: case DONT:
178: n = "DONT ";
179: break;
180: }
181:
182: println("S: IAC " + n + optionID);
183:
184: synchronized (out) {
185: out.write(IAC);
186: out.write(negotiate);
187: out.write(optionID);
188: }
189: }
190:
191: private TelnetOption getOption(int optionID) {
192: TelnetOption opt = options[optionID];
193: if (opt == null) {
194: opt = new TelnetOption(optionID);
195: options[optionID] = opt;
196: }
197: return opt;
198: }
199:
200: }
|