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 /***
031 * Option code
032 ***/
033 private int optionCode = -1;
034
035 /***
036 * true if the option should be activated on the local side
037 ***/
038 private boolean initialLocal = false;
039
040 /***
041 * true if the option should be activated on the remote side
042 ***/
043 private boolean initialRemote = false;
044
045 /***
046 * true if the option should be accepted on the local side
047 ***/
048 private boolean acceptLocal = false;
049
050 /***
051 * true if the option should be accepted on the remote side
052 ***/
053 private boolean acceptRemote = false;
054
055 /***
056 * true if the option is active on the local side
057 ***/
058 private boolean doFlag = false;
059
060 /***
061 * true if the option is active on the remote side
062 ***/
063 private boolean willFlag = false;
064
065 /***
066 * Constructor for the TelnetOptionHandler. Allows defining desired
067 * initial setting for local/remote activation of this option and
068 * behaviour in case a local/remote activation request for this
069 * option is received.
070 * <p>
071 * @param optcode - Option code.
072 * @param initlocal - if set to true, a WILL is sent upon connection.
073 * @param initremote - if set to true, a DO is sent upon connection.
074 * @param acceptlocal - if set to true, any DO request is accepted.
075 * @param acceptremote - if set to true, any WILL request is accepted.
076 ***/
077 public TelnetOptionHandler(int optcode,
078 boolean initlocal,
079 boolean initremote,
080 boolean acceptlocal,
081 boolean acceptremote)
082 {
083 optionCode = optcode;
084 initialLocal = initlocal;
085 initialRemote = initremote;
086 acceptLocal = acceptlocal;
087 acceptRemote = acceptremote;
088 }
089
090
091 /***
092 * Returns the option code for this option.
093 * <p>
094 * @return Option code.
095 ***/
096 public int getOptionCode()
097 {
098 return (optionCode);
099 }
100
101 /***
102 * Returns a boolean indicating whether to accept a DO
103 * request coming from the other end.
104 * <p>
105 * @return true if a DO request shall be accepted.
106 ***/
107 public boolean getAcceptLocal()
108 {
109 return (acceptLocal);
110 }
111
112 /***
113 * Returns a boolean indicating whether to accept a WILL
114 * request coming from the other end.
115 * <p>
116 * @return true if a WILL request shall be accepted.
117 ***/
118 public boolean getAcceptRemote()
119 {
120 return (acceptRemote);
121 }
122
123 /***
124 * Set behaviour of the option for DO requests coming from
125 * the other end.
126 * <p>
127 * @param accept - if true, subsequent DO requests will be accepted.
128 ***/
129 public void setAcceptLocal(boolean accept)
130 {
131 acceptLocal = accept;
132 }
133
134 /***
135 * Set behaviour of the option for WILL requests coming from
136 * the other end.
137 * <p>
138 * @param accept - if true, subsequent WILL requests will be accepted.
139 ***/
140 public void setAcceptRemote(boolean accept)
141 {
142 acceptRemote = accept;
143 }
144
145 /***
146 * Returns a boolean indicating whether to send a WILL request
147 * to the other end upon connection.
148 * <p>
149 * @return true if a WILL request shall be sent upon connection.
150 ***/
151 public boolean getInitLocal()
152 {
153 return (initialLocal);
154 }
155
156 /***
157 * Returns a boolean indicating whether to send a DO request
158 * to the other end upon connection.
159 * <p>
160 * @return true if a DO request shall be sent upon connection.
161 ***/
162 public boolean getInitRemote()
163 {
164 return (initialRemote);
165 }
166
167 /***
168 * Tells this option whether to send a WILL request upon connection.
169 * <p>
170 * @param init - if true, a WILL request will be sent upon subsequent
171 * connections.
172 ***/
173 public void setInitLocal(boolean init)
174 {
175 initialLocal = init;
176 }
177
178 /***
179 * Tells this option whether to send a DO request upon connection.
180 * <p>
181 * @param init - if true, a DO request will be sent upon subsequent
182 * connections.
183 ***/
184 public void setInitRemote(boolean init)
185 {
186 initialRemote = init;
187 }
188
189 /***
190 * Method called upon reception of a subnegotiation for this option
191 * coming from the other end.
192 * Must be implemented by the actual TelnetOptionHandler to specify
193 * which response must be sent for the subnegotiation request.
194 * <p>
195 * @param suboptionData - the sequence received, whithout IAC SB & IAC SE
196 * @param suboptionLength - the length of data in suboption_data
197 * <p>
198 * @return response to be sent to the subnegotiation sequence. TelnetClient
199 * will add IAC SB & IAC SE. null means no response
200 ***/
201 public abstract int[] answerSubnegotiation(int suboptionData[],
202 int suboptionLength);
203
204 /***
205 * This method is invoked whenever this option is acknowledged active on
206 * the local end (TelnetClient sent a WILL, remote side sent a DO).
207 * The method is used to specify a subnegotiation sequence that will be
208 * sent by TelnetClient when the option is activated.
209 * <p>
210 * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient
211 * will add IAC SB & IAC SE. null means no subnegotiation.
212 ***/
213 public abstract int[] startSubnegotiationLocal();
214
215 /***
216 * This method is invoked whenever this option is acknowledged active on
217 * the remote end (TelnetClient sent a DO, remote side sent a WILL).
218 * The method is used to specify a subnegotiation sequence that will be
219 * sent by TelnetClient when the option is activated.
220 * <p>
221 * @return subnegotiation sequence to be sent by TelnetClient. TelnetClient
222 * will add IAC SB & IAC SE. null means no subnegotiation.
223 ***/
224 public abstract int[] startSubnegotiationRemote();
225
226 /***
227 * Returns a boolean indicating whether a WILL request sent to the other
228 * side has been acknowledged.
229 * <p>
230 * @return true if a WILL sent to the other side has been acknowledged.
231 ***/
232 boolean getWill()
233 {
234 return willFlag;
235 }
236
237 /***
238 * Tells this option whether a WILL request sent to the other
239 * side has been acknowledged (invoked by TelnetClient).
240 * <p>
241 * @param state - if true, a WILL request has been acknowledged.
242 ***/
243 void setWill(boolean state)
244 {
245 willFlag = state;
246 }
247
248 /***
249 * Returns a boolean indicating whether a DO request sent to the other
250 * side has been acknowledged.
251 * <p>
252 * @return true if a DO sent to the other side has been acknowledged.
253 ***/
254 boolean getDo()
255 {
256 return doFlag;
257 }
258
259
260 /***
261 * Tells this option whether a DO request sent to the other
262 * side has been acknowledged (invoked by TelnetClient).
263 * <p>
264 * @param state - if true, a DO request has been acknowledged.
265 ***/
266 void setDo(boolean state)
267 {
268 doFlag = state;
269 }
270 }