001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.telnet;
017:
018: /***
019: * The TelnetOptionHandler class is the base class to be used
020: * for implementing handlers for telnet options.
021: * <p>
022: * TelnetOptionHandler implements basic option handling
023: * functionality and defines abstract methods that must be
024: * implemented to define subnegotiation behaviour.
025: * <p>
026: * @author Bruno D'Avanzo
027: ***/
028: public abstract class TelnetOptionHandler {
029: /***
030: * Option code
031: ***/
032: private int optionCode = -1;
033:
034: /***
035: * true if the option should be activated on the local side
036: ***/
037: private boolean initialLocal = false;
038:
039: /***
040: * true if the option should be activated on the remote side
041: ***/
042: private boolean initialRemote = false;
043:
044: /***
045: * true if the option should be accepted on the local side
046: ***/
047: private boolean acceptLocal = false;
048:
049: /***
050: * true if the option should be accepted on the remote side
051: ***/
052: private boolean acceptRemote = false;
053:
054: /***
055: * true if the option is active on the local side
056: ***/
057: private boolean doFlag = false;
058:
059: /***
060: * true if the option is active on the remote side
061: ***/
062: private boolean willFlag = false;
063:
064: /***
065: * Constructor for the TelnetOptionHandler. Allows defining desired
066: * initial setting for local/remote activation of this option and
067: * behaviour in case a local/remote activation request for this
068: * option is received.
069: * <p>
070: * @param optcode - Option code.
071: * @param initlocal - if set to true, a WILL is sent upon connection.
072: * @param initremote - if set to true, a DO is sent upon connection.
073: * @param acceptlocal - if set to true, any DO request is accepted.
074: * @param acceptremote - if set to true, any WILL request is accepted.
075: ***/
076: public TelnetOptionHandler(int optcode, boolean initlocal,
077: boolean initremote, boolean acceptlocal,
078: boolean acceptremote) {
079: optionCode = optcode;
080: initialLocal = initlocal;
081: initialRemote = initremote;
082: acceptLocal = acceptlocal;
083: acceptRemote = acceptremote;
084: }
085:
086: /***
087: * Returns the option code for this option.
088: * <p>
089: * @return Option code.
090: ***/
091: public int getOptionCode() {
092: return (optionCode);
093: }
094:
095: /***
096: * Returns a boolean indicating whether to accept a DO
097: * request coming from the other end.
098: * <p>
099: * @return true if a DO request shall be accepted.
100: ***/
101: public boolean getAcceptLocal() {
102: return (acceptLocal);
103: }
104:
105: /***
106: * Returns a boolean indicating whether to accept a WILL
107: * request coming from the other end.
108: * <p>
109: * @return true if a WILL request shall be accepted.
110: ***/
111: public boolean getAcceptRemote() {
112: return (acceptRemote);
113: }
114:
115: /***
116: * Set behaviour of the option for DO requests coming from
117: * the other end.
118: * <p>
119: * @param accept - if true, subsequent DO requests will be accepted.
120: ***/
121: public void setAcceptLocal(boolean accept) {
122: acceptLocal = accept;
123: }
124:
125: /***
126: * Set behaviour of the option for WILL requests coming from
127: * the other end.
128: * <p>
129: * @param accept - if true, subsequent WILL requests will be accepted.
130: ***/
131: public void setAcceptRemote(boolean accept) {
132: acceptRemote = accept;
133: }
134:
135: /***
136: * Returns a boolean indicating whether to send a WILL request
137: * to the other end upon connection.
138: * <p>
139: * @return true if a WILL request shall be sent upon connection.
140: ***/
141: public boolean getInitLocal() {
142: return (initialLocal);
143: }
144:
145: /***
146: * Returns a boolean indicating whether to send a DO request
147: * to the other end upon connection.
148: * <p>
149: * @return true if a DO request shall be sent upon connection.
150: ***/
151: public boolean getInitRemote() {
152: return (initialRemote);
153: }
154:
155: /***
156: * Tells this option whether to send a WILL request upon connection.
157: * <p>
158: * @param init - if true, a WILL request will be sent upon subsequent
159: * connections.
160: ***/
161: public void setInitLocal(boolean init) {
162: initialLocal = init;
163: }
164:
165: /***
166: * Tells this option whether to send a DO request upon connection.
167: * <p>
168: * @param init - if true, a DO request will be sent upon subsequent
169: * connections.
170: ***/
171: public void setInitRemote(boolean init) {
172: initialRemote = init;
173: }
174:
175: /***
176: * Method called upon reception of a subnegotiation for this option
177: * coming from the other end.
178: * Must be implemented by the actual TelnetOptionHandler to specify
179: * which response must be sent for the subnegotiation request.
180: * <p>
181: * @param suboptionData - the sequence received, whithout IAC SB & IAC SE
182: * @param suboptionLength - the length of data in suboption_data
183: * <p>
184: * @return response to be sent to the subnegotiation sequence. TelnetClient
185: * will add IAC SB & IAC SE. null means no response
186: ***/
187: public abstract int[] answerSubnegotiation(int suboptionData[],
188: int suboptionLength);
189:
190: /***
191: * This method is invoked whenever this option is acknowledged active on
192: * the local end (TelnetClient sent a WILL, remote side sent a DO).
193: * The method is used to specify a subnegotiation sequence that will be
194: * sent by TelnetClient when the option is activated.
195: * <p>
196: * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient
197: * will add IAC SB & IAC SE. null means no subnegotiation.
198: ***/
199: public abstract int[] startSubnegotiationLocal();
200:
201: /***
202: * This method is invoked whenever this option is acknowledged active on
203: * the remote end (TelnetClient sent a DO, remote side sent a WILL).
204: * The method is used to specify a subnegotiation sequence that will be
205: * sent by TelnetClient when the option is activated.
206: * <p>
207: * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient
208: * will add IAC SB & IAC SE. null means no subnegotiation.
209: ***/
210: public abstract int[] startSubnegotiationRemote();
211:
212: /***
213: * Returns a boolean indicating whether a WILL request sent to the other
214: * side has been acknowledged.
215: * <p>
216: * @return true if a WILL sent to the other side has been acknowledged.
217: ***/
218: boolean getWill() {
219: return willFlag;
220: }
221:
222: /***
223: * Tells this option whether a WILL request sent to the other
224: * side has been acknowledged (invoked by TelnetClient).
225: * <p>
226: * @param state - if true, a WILL request has been acknowledged.
227: ***/
228: void setWill(boolean state) {
229: willFlag = state;
230: }
231:
232: /***
233: * Returns a boolean indicating whether a DO request sent to the other
234: * side has been acknowledged.
235: * <p>
236: * @return true if a DO sent to the other side has been acknowledged.
237: ***/
238: boolean getDo() {
239: return doFlag;
240: }
241:
242: /***
243: * Tells this option whether a DO request sent to the other
244: * side has been acknowledged (invoked by TelnetClient).
245: * <p>
246: * @param state - if true, a DO request has been acknowledged.
247: ***/
248: void setDo(boolean state) {
249: doFlag = state;
250: }
251: }
|