View Javadoc

    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.net.telnet;
    17
    18  /****
    19   * The TelnetOptionHandler class is the base class to be used
    20   * for implementing handlers for telnet options.
    21   * <p>
    22   * TelnetOptionHandler implements basic option handling
    23   * functionality and defines abstract methods that must be
    24   * implemented to define subnegotiation behaviour.
    25   * <p>
    26   * @author Bruno D'Avanzo
    27   ***/
    28  public abstract class TelnetOptionHandler
    29  {
    30      /****
    31       * Option code
    32       ***/
    33      private int optionCode = -1;
    34
    35      /****
    36       * true if the option should be activated on the local side
    37       ***/
    38      private boolean initialLocal = false;
    39
    40      /****
    41       * true if the option should be activated on the remote side
    42       ***/
    43      private boolean initialRemote = false;
    44
    45      /****
    46       * true if the option should be accepted on the local side
    47       ***/
    48      private boolean acceptLocal = false;
    49
    50      /****
    51       * true if the option should be accepted on the remote side
    52       ***/
    53      private boolean acceptRemote = false;
    54
    55      /****
    56       * true if the option is active on the local side
    57       ***/
    58      private boolean doFlag = false;
    59
    60      /****
    61       * true if the option is active on the remote side
    62       ***/
    63      private boolean willFlag = false;
    64
    65      /****
    66       * Constructor for the TelnetOptionHandler. Allows defining desired
    67       * initial setting for local/remote activation of this option and
    68       * behaviour in case a local/remote activation request for this
    69       * option is received.
    70       * <p>
    71       * @param optcode - Option code.
    72       * @param initlocal - if set to true, a WILL is sent upon connection.
    73       * @param initremote - if set to true, a DO is sent upon connection.
    74       * @param acceptlocal - if set to true, any DO request is accepted.
    75       * @param acceptremote - if set to true, any WILL request is accepted.
    76       ***/
    77      public TelnetOptionHandler(int optcode,
    78                                  boolean initlocal,
    79                                  boolean initremote,
    80                                  boolean acceptlocal,
    81                                  boolean acceptremote)
    82      {
    83          optionCode = optcode;
    84          initialLocal = initlocal;
    85          initialRemote = initremote;
    86          acceptLocal = acceptlocal;
    87          acceptRemote = acceptremote;
    88      }
    89
    90
    91      /****
    92       * Returns the option code for this option.
    93       * <p>
    94       * @return Option code.
    95       ***/
    96      public int getOptionCode()
    97      {
    98          return (optionCode);
    99      }
    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 }