FileDocCategorySizeDatePackage
Telnet.javaAPI DocApache Commons NET 1.4.1 API35872Sat Dec 03 10:05:48 GMT 2005org.apache.commons.net.telnet

Telnet

public class Telnet extends org.apache.commons.net.SocketClient
author
Daniel F. Savarese
author
Bruno D'Avanzo

Fields Summary
static final boolean
debug
static final boolean
debugoptions
static final byte[]
_COMMAND_DO
static final byte[]
_COMMAND_DONT
static final byte[]
_COMMAND_WILL
static final byte[]
_COMMAND_WONT
static final byte[]
_COMMAND_SB
static final byte[]
_COMMAND_SE
static final int
_WILL_MASK
static final int
_DO_MASK
static final int
_REQUESTED_WILL_MASK
static final int
_REQUESTED_DO_MASK
static final int
DEFAULT_PORT
int[]
_doResponse
int[]
_willResponse
int[]
_options
protected static final int
TERMINAL_TYPE
Terminal type option
protected static final int
TERMINAL_TYPE_SEND
Send (for subnegotiation)
protected static final int
TERMINAL_TYPE_IS
Is (for subnegotiation)
static final byte[]
_COMMAND_IS
Is sequence (for subnegotiation)
private String
terminalType
Terminal type
private TelnetOptionHandler[]
optionHandlers
Array of option handlers
static final byte[]
_COMMAND_AYT
AYT sequence
private Object
aytMonitor
monitor to wait for AYT
private boolean
aytFlag
flag for AYT
private OutputStream
spyStream
The stream on which to spy
private TelnetNotificationHandler
__notifhand
The notification handler
Constructors Summary
Telnet()
Empty Constructor

           
    
    
        setDefaultPort(DEFAULT_PORT);
        _doResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];
        _willResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];
        _options = new int[TelnetOption.MAX_OPTION_VALUE + 1];
        optionHandlers =
            new TelnetOptionHandler[TelnetOption.MAX_OPTION_VALUE + 1];
    
Telnet(String termtype)
This constructor lets you specify the terminal type.

param
termtype - terminal type to be negotiated (ej. VT100)

        setDefaultPort(DEFAULT_PORT);
        _doResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];
        _willResponse = new int[TelnetOption.MAX_OPTION_VALUE + 1];
        _options = new int[TelnetOption.MAX_OPTION_VALUE + 1];
        terminalType = termtype;
        optionHandlers =
            new TelnetOptionHandler[TelnetOption.MAX_OPTION_VALUE + 1];
    
Methods Summary
protected void_connectAction_()
Called upon connection.

throws
IOException - Exception in I/O.

        /* (start). BUGFIX: clean the option info for each connection*/
        for (int ii = 0; ii < TelnetOption.MAX_OPTION_VALUE + 1; ii++)
        {
            _doResponse[ii] = 0;
            _willResponse[ii] = 0;
            _options[ii] = 0;
            if (optionHandlers[ii] != null)
            {
                optionHandlers[ii].setDo(false);
                optionHandlers[ii].setWill(false);
            }
        }
        /* (end). BUGFIX: clean the option info for each connection*/

        super._connectAction_();
        _input_ = new BufferedInputStream(_input_);
        _output_ = new BufferedOutputStream(_output_);

        /* open TelnetOptionHandler functionality (start)*/
        for (int ii = 0; ii < TelnetOption.MAX_OPTION_VALUE + 1; ii++)
        {
            if (optionHandlers[ii] != null)
            {
                if (optionHandlers[ii].getInitLocal())
                {
                    try
                    {
                        _requestWill(optionHandlers[ii].getOptionCode());
                    }
                    catch (IOException e)
                    {
                        System.err.println(
                            "Exception while initializing option: "
                            + e.getMessage());
                    }
                }

                if (optionHandlers[ii].getInitRemote())
                {
                    try
                    {
                        _requestDo(optionHandlers[ii].getOptionCode());
                    }
                    catch (IOException e)
                    {
                        System.err.println(
                            "Exception while initializing option: "
                            + e.getMessage());
                    }
                }
            }
        }
        /* open TelnetOptionHandler functionality (end)*/
    
final synchronized void_processAYTResponse()
Processes the response of an AYT

        if (!aytFlag)
        {
            synchronized (aytMonitor)
            {
                aytFlag = true;
                try
                {
                    aytMonitor.notifyAll();
                }
                catch (Exception e)
                {
                    System.err.println("Exception notifying:" + e.getMessage());
                }
            }
        }
    
void_processDo(int option)
Processes a DO request.

throws
IOException - Exception in I/O.

param
option - option code to be set.

        if (debugoptions)
        {
            System.err.println("RECEIVED DO: "
                + TelnetOption.getOption(option));
        }

        if (__notifhand != null)
        {
            __notifhand.receivedNegotiation(
                TelnetNotificationHandler.RECEIVED_DO,
                option);
        }

        boolean acceptNewState = false;


        /* open TelnetOptionHandler functionality (start)*/
        if (optionHandlers[option] != null)
        {
            acceptNewState = optionHandlers[option].getAcceptLocal();
        }
        else
        {
        /* open TelnetOptionHandler functionality (end)*/
            /* TERMINAL-TYPE option (start)*/
            if (option == TERMINAL_TYPE)
            {
                if ((terminalType != null) && (terminalType.length() > 0))
                {
                    acceptNewState = true;
                }
            }
            /* TERMINAL-TYPE option (end)*/
        /* open TelnetOptionHandler functionality (start)*/
        }
        /* open TelnetOptionHandler functionality (end)*/

        if (_willResponse[option] > 0)
        {
            --_willResponse[option];
            if (_willResponse[option] > 0 && _stateIsWill(option))
            {
                --_willResponse[option];
            }
        }

        if (_willResponse[option] == 0)
        {
            if (_requestedWont(option))
            {

                switch (option)
                {

                default:
                    break;

                }


                if (acceptNewState)
                {
                    _setWantWill(option);
                    _sendWill(option);
                }
                else
                {
                    ++_willResponse[option];
                    _sendWont(option);
                }
            }
            else
            {
                // Other end has acknowledged option.

                switch (option)
                {

                default:
                    break;

                }

            }
        }

        _setWill(option);
    
void_processDont(int option)
Processes a DONT request.

throws
IOException - Exception in I/O.

param
option - option code to be set.

        if (debugoptions)
        {
            System.err.println("RECEIVED DONT: "
                + TelnetOption.getOption(option));
        }
        if (__notifhand != null)
        {
            __notifhand.receivedNegotiation(
                TelnetNotificationHandler.RECEIVED_DONT,
                option);
        }
        if (_willResponse[option] > 0)
        {
            --_willResponse[option];
            if (_willResponse[option] > 0 && _stateIsWont(option))
            {
                --_willResponse[option];
            }
        }

        if (_willResponse[option] == 0 && _requestedWill(option))
        {

            switch (option)
            {

            default:
                break;

            }

            /* FIX for a BUG in the negotiation (start)*/
            if ((_stateIsWill(option)) || (_requestedWill(option)))
            {
                _sendWont(option);
            }

            _setWantWont(option);
            /* FIX for a BUG in the negotiation (end)*/
        }

        _setWont(option);
    
void_processSuboption(int[] suboption, int suboptionLength)
Processes a suboption negotiation.

throws
IOException - Exception in I/O.

param
suboption - subnegotiation data received
param
suboptionLength - length of data received

        if (debug)
        {
            System.err.println("PROCESS SUBOPTION.");
        }

        /* open TelnetOptionHandler functionality (start)*/
        if (suboptionLength > 0)
        {
            if (optionHandlers[suboption[0]] != null)
            {
                int responseSuboption[] =
                  optionHandlers[suboption[0]].answerSubnegotiation(suboption,
                  suboptionLength);
                _sendSubnegotiation(responseSuboption);
            }
            else
            {
                if (suboptionLength > 1)
                {
                    if (debug)
                    {
                        for (int ii = 0; ii < suboptionLength; ii++)
                        {
                            System.err.println("SUB[" + ii + "]: "
                                + suboption[ii]);
                        }
                    }
                    if ((suboption[0] == TERMINAL_TYPE)
                        && (suboption[1] == TERMINAL_TYPE_SEND))
                    {
                        _sendTerminalType();
                    }
                }
            }
        }
        /* open TelnetOptionHandler functionality (end)*/
    
void_processWill(int option)
Processes a WILL request.

throws
IOException - Exception in I/O.

param
option - option code to be set.

        if (debugoptions)
        {
            System.err.println("RECEIVED WILL: "
                + TelnetOption.getOption(option));
        }

        if (__notifhand != null)
        {
            __notifhand.receivedNegotiation(
                TelnetNotificationHandler.RECEIVED_WILL,
                option);
        }

        boolean acceptNewState = false;

        /* open TelnetOptionHandler functionality (start)*/
        if (optionHandlers[option] != null)
        {
            acceptNewState = optionHandlers[option].getAcceptRemote();
        }
        /* open TelnetOptionHandler functionality (end)*/

        if (_doResponse[option] > 0)
        {
            --_doResponse[option];
            if (_doResponse[option] > 0 && _stateIsDo(option))
            {
                --_doResponse[option];
            }
        }

        if (_doResponse[option] == 0 && _requestedDont(option))
        {

            switch (option)
            {

            default:
                break;

            }


            if (acceptNewState)
            {
                _setWantDo(option);
                _sendDo(option);
            }
            else
            {
                ++_doResponse[option];
                _sendDont(option);
            }
        }

        _setDo(option);
    
void_processWont(int option)
Processes a WONT request.

throws
IOException - Exception in I/O.

param
option - option code to be set.

        if (debugoptions)
        {
            System.err.println("RECEIVED WONT: "
                + TelnetOption.getOption(option));
        }

        if (__notifhand != null)
        {
            __notifhand.receivedNegotiation(
                TelnetNotificationHandler.RECEIVED_WONT,
                option);
        }

        if (_doResponse[option] > 0)
        {
            --_doResponse[option];
            if (_doResponse[option] > 0 && _stateIsDont(option))
            {
                --_doResponse[option];
            }
        }

        if (_doResponse[option] == 0 && _requestedDo(option))
        {

            switch (option)
            {

            default:
                break;

            }

            /* FIX for a BUG in the negotiation (start)*/
            if ((_stateIsDo(option)) || (_requestedDo(option)))
            {
                _sendDont(option);
            }

            _setWantDont(option);
            /* FIX for a BUG in the negotiation (end)*/
        }

        _setDont(option);
    
void_registerSpyStream(java.io.OutputStream spystream)
Registers an OutputStream for spying what's going on in the Telnet session.

param
spystream - OutputStream on which session activity will be echoed.

        spyStream = spystream;
    
final synchronized void_requestDo(int option)
Requests a DO.

throws
IOException - Exception in I/O.

param
option - Option code.

        if ((_doResponse[option] == 0 && _stateIsDo(option))
            || _requestedDo(option))
        {
            return ;
        }
        _setWantDo(option);
        ++_doResponse[option];
        _sendDo(option);
    
final synchronized void_requestDont(int option)
Requests a DONT.

throws
IOException - Exception in I/O.

param
option - Option code.

        if ((_doResponse[option] == 0 && _stateIsDont(option))
            || _requestedDont(option))
        {
            return ;
        }
        _setWantDont(option);
        ++_doResponse[option];
        _sendDont(option);
    
final synchronized void_requestWill(int option)
Requests a WILL.

throws
IOException - Exception in I/O.

param
option - Option code.

        if ((_willResponse[option] == 0 && _stateIsWill(option))
            || _requestedWill(option))
        {
            return ;
        }
        _setWantWill(option);
        ++_doResponse[option];
        _sendWill(option);
    
final synchronized void_requestWont(int option)
Requests a WONT.

throws
IOException - Exception in I/O.

param
option - Option code.

        if ((_willResponse[option] == 0 && _stateIsWont(option))
            || _requestedWont(option))
        {
            return ;
        }
        _setWantWont(option);
        ++_doResponse[option];
        _sendWont(option);
    
boolean_requestedDo(int option)
Looks for the state of the option.

return
returns true if a do has been reuqested

param
option - option code to be looked up.

        return ((_options[option] & _REQUESTED_DO_MASK) != 0);
    
boolean_requestedDont(int option)
Looks for the state of the option.

return
returns true if a dont has been reuqested

param
option - option code to be looked up.

        return !_requestedDo(option);
    
boolean_requestedWill(int option)
Looks for the state of the option.

return
returns true if a will has been reuqested

param
option - option code to be looked up.

        return ((_options[option] & _REQUESTED_WILL_MASK) != 0);
    
boolean_requestedWont(int option)
Looks for the state of the option.

return
returns true if a wont has been reuqested

param
option - option code to be looked up.

        return !_requestedWill(option);
    
final boolean_sendAYT(long timeout)
Sends an Are You There sequence and waits for the result.

throws
IOException - Exception in I/O.
throws
IllegalArgumentException - Illegal argument
throws
InterruptedException - Interrupted during wait.

param
timeout - Time to wait for a response (millis.)

return
true if AYT received a response, false otherwise

        boolean retValue = false;
        synchronized (aytMonitor)
        {
            synchronized (this)
            {
                aytFlag = false;
                _output_.write(_COMMAND_AYT);
                _output_.flush();
            }

            try
            {
                aytMonitor.wait(timeout);
                if (aytFlag == false)
                {
                    retValue = false;
                    aytFlag = true;
                }
                else
                {
                    retValue = true;
                }
            }
            catch (IllegalMonitorStateException e)
            {
                System.err.println("Exception processing AYT:"
                    + e.getMessage());
            }
        }

        return (retValue);
    
final synchronized void_sendByte(int b)
Sends a byte.

throws
IOException - Exception in I/O.

param
b - byte to send

        _output_.write(b);

        /* Code Section added for supporting spystreams (start)*/
        _spyWrite(b);
        /* Code Section added for supporting spystreams (end)*/

    
final synchronized void_sendDo(int option)
Sends a DO.

throws
IOException - Exception in I/O.

param
option - Option code.

        if (debug || debugoptions)
        {
            System.err.println("DO: " + TelnetOption.getOption(option));
        }
        _output_.write(_COMMAND_DO);
        _output_.write(option);

        /* Code Section added for sending the negotiation ASAP (start)*/
        _output_.flush();
        /* Code Section added for sending the negotiation ASAP (end)*/
    
final synchronized void_sendDont(int option)
Sends a DONT.

throws
IOException - Exception in I/O.

param
option - Option code.

        if (debug || debugoptions)
        {
            System.err.println("DONT: " + TelnetOption.getOption(option));
        }
        _output_.write(_COMMAND_DONT);
        _output_.write(option);

        /* Code Section added for sending the negotiation ASAP (start)*/
        _output_.flush();
        /* Code Section added for sending the negotiation ASAP (end)*/
    
final synchronized void_sendSubnegotiation(int[] subn)
Manages subnegotiation for Terminal Type.

throws
IOException - Exception in I/O.

param
subn - subnegotiation data to be sent

        if (debug)
        {
            System.err.println("SEND SUBNEGOTIATION: ");
            if (subn != null)
            {
                for (int ii = 0; ii < subn.length; ii++)
                {
                    System.err.println("subn["  + ii + "]=" + subn[ii]);
                }
            }
        }
        if (subn != null)
        {
            byte byteresp[] = new byte[subn.length];
            for (int ii = 0; ii < subn.length; ii++)
            {
                byteresp[ii] = (byte) subn[ii];
            }

            _output_.write(_COMMAND_SB);
            _output_.write(byteresp);
            _output_.write(_COMMAND_SE);

            /* Code Section added for sending the negotiation ASAP (start)*/
            _output_.flush();
            /* Code Section added for sending the negotiation ASAP (end)*/
        }
    
final synchronized void_sendTerminalType()
Sends terminal type information.

throws
IOException - Exception in I/O.

        if (debug)
        {
            System.err.println("SEND TERMINAL-TYPE: " + terminalType);
        }
        if (terminalType != null)
        {
            _output_.write(_COMMAND_SB);
            _output_.write(_COMMAND_IS);
            _output_.write(terminalType.getBytes());
            _output_.write(_COMMAND_SE);
            _output_.flush();
        }
    
final synchronized void_sendWill(int option)
Sends a WILL.

throws
IOException - Exception in I/O.

param
option - Option code.

        if (debug || debugoptions)
        {
            System.err.println("WILL: " + TelnetOption.getOption(option));
        }
        _output_.write(_COMMAND_WILL);
        _output_.write(option);

        /* Code Section added for sending the negotiation ASAP (start)*/
        _output_.flush();
        /* Code Section added for sending the negotiation ASAP (end)*/
    
final synchronized void_sendWont(int option)
Sends a WONT.

throws
IOException - Exception in I/O.

param
option - Option code.

        if (debug || debugoptions)
        {
            System.err.println("WONT: " + TelnetOption.getOption(option));
        }
        _output_.write(_COMMAND_WONT);
        _output_.write(option);

        /* Code Section added for sending the negotiation ASAP (start)*/
        _output_.flush();
        /* Code Section added for sending the negotiation ASAP (end)*/
    
void_setDo(int option)
Sets the state of the option.

param
option - option code to be set.

        _options[option] |= _DO_MASK;

        /* open TelnetOptionHandler functionality (start)*/
        if (_requestedDo(option))
        {
            if (optionHandlers[option] != null)
            {
                optionHandlers[option].setDo(true);

                int subneg[] =
                    optionHandlers[option].startSubnegotiationRemote();

                if (subneg != null)
                {
                    try
                    {
                        _sendSubnegotiation(subneg);
                    }
                    catch (Exception e)
                    {
                        System.err.println("Exception in option subnegotiation"
                            + e.getMessage());
                    }
                }
            }
        }
        /* open TelnetOptionHandler functionality (end)*/
    
void_setDont(int option)
Sets the state of the option.

param
option - option code to be set.

        _options[option] &= ~_DO_MASK;

        /* open TelnetOptionHandler functionality (start)*/
        if (optionHandlers[option] != null)
        {
            optionHandlers[option].setDo(false);
        }
        /* open TelnetOptionHandler functionality (end)*/
    
void_setWantDo(int option)
Sets the state of the option.

param
option - option code to be set.

        _options[option] |= _REQUESTED_DO_MASK;
    
void_setWantDont(int option)
Sets the state of the option.

param
option - option code to be set.

        _options[option] &= ~_REQUESTED_DO_MASK;
    
void_setWantWill(int option)
Sets the state of the option.

param
option - option code to be set.

        _options[option] |= _REQUESTED_WILL_MASK;
    
void_setWantWont(int option)
Sets the state of the option.

param
option - option code to be set.

        _options[option] &= ~_REQUESTED_WILL_MASK;
    
void_setWill(int option)
Sets the state of the option.

param
option - option code to be set.

        _options[option] |= _WILL_MASK;

        /* open TelnetOptionHandler functionality (start)*/
        if (_requestedWill(option))
        {
            if (optionHandlers[option] != null)
            {
                optionHandlers[option].setWill(true);

                int subneg[] =
                    optionHandlers[option].startSubnegotiationLocal();

                if (subneg != null)
                {
                    try
                    {
                        _sendSubnegotiation(subneg);
                    }
                    catch (Exception e)
                    {
                        System.err.println(
                            "Exception in option subnegotiation"
                            + e.getMessage());
                    }
                }
            }
        }
        /* open TelnetOptionHandler functionality (end)*/
    
void_setWont(int option)
Sets the state of the option.

param
option - option code to be set.

        _options[option] &= ~_WILL_MASK;

        /* open TelnetOptionHandler functionality (start)*/
        if (optionHandlers[option] != null)
        {
            optionHandlers[option].setWill(false);
        }
        /* open TelnetOptionHandler functionality (end)*/
    
void_spyRead(int ch)
Sends a read char on the spy stream.

param
ch - character read from the session

        if (spyStream != null)
        {
            try
            {
                if (ch != (int) '\r")
                {
                    spyStream.write(ch);
                    if (ch == (int) '\n")
                    {
                        spyStream.write((int) '\r");
                    }
                    spyStream.flush();
                }
            }
            catch (Exception e)
            {
                spyStream = null;
            }
        }
    
void_spyWrite(int ch)
Sends a written char on the spy stream.

param
ch - character written to the session

        if (!(_stateIsDo(TelnetOption.ECHO)
            && _requestedDo(TelnetOption.ECHO)))
        {
            if (spyStream != null)
            {
                try
                {
                    spyStream.write(ch);
                    spyStream.flush();
                }
                catch (Exception e)
                {
                    spyStream = null;
                }
            }
        }
    
boolean_stateIsDo(int option)
Looks for the state of the option.

return
returns true if a do has been acknowledged

param
option - option code to be looked up.

        return ((_options[option] & _DO_MASK) != 0);
    
boolean_stateIsDont(int option)
Looks for the state of the option.

return
returns true if a dont has been acknowledged

param
option - option code to be looked up.

        return !_stateIsDo(option);
    
boolean_stateIsWill(int option)
Looks for the state of the option.

return
returns true if a will has been acknowledged

param
option - option code to be looked up.

        return ((_options[option] & _WILL_MASK) != 0);
    
boolean_stateIsWont(int option)
Looks for the state of the option.

return
returns true if a wont has been acknowledged

param
option - option code to be looked up.

        return !_stateIsWill(option);
    
void_stopSpyStream()
Stops spying this Telnet.

        spyStream = null;
    
voidaddOptionHandler(TelnetOptionHandler opthand)
Registers a new TelnetOptionHandler for this telnet to use.

throws
InvalidTelnetOptionException - The option code is invalid.

param
opthand - option handler to be registered.

        int optcode = opthand.getOptionCode();
        if (TelnetOption.isValidOption(optcode))
        {
            if (optionHandlers[optcode] == null)
            {
                optionHandlers[optcode] = opthand;
                if (isConnected())
                {
                    if (opthand.getInitLocal())
                    {
                        try
                        {
                            _requestWill(optcode);
                        }
                        catch (IOException e)
                        {
                            System.err.println(
                                "Exception while initializing option: "
                                + e.getMessage());
                        }
                    }

                    if (opthand.getInitRemote())
                    {
                        try
                        {
                            _requestDo(optcode);
                        }
                        catch (IOException e)
                        {
                            System.err.println(
                                "Exception while initializing option: "
                                + e.getMessage());
                        }
                    }
                }
            }
            else
            {
                throw (new InvalidTelnetOptionException(
                    "Already registered option", optcode));
            }
        }
        else
        {
            throw (new InvalidTelnetOptionException(
                "Invalid Option Code", optcode));
        }
    
voiddeleteOptionHandler(int optcode)
Unregisters a TelnetOptionHandler.

throws
InvalidTelnetOptionException - The option code is invalid.

param
optcode - Code of the option to be unregistered.

        if (TelnetOption.isValidOption(optcode))
        {
            if (optionHandlers[optcode] == null)
            {
                throw (new InvalidTelnetOptionException(
                    "Unregistered option", optcode));
            }
            else
            {
                TelnetOptionHandler opthand = optionHandlers[optcode];
                optionHandlers[optcode] = null;

                if (opthand.getWill())
                {
                    try
                    {
                        _requestWont(optcode);
                    }
                    catch (IOException e)
                    {
                        System.err.println(
                            "Exception while turning off option: "
                            + e.getMessage());
                    }
                }

                if (opthand.getDo())
                {
                    try
                    {
                        _requestDont(optcode);
                    }
                    catch (IOException e)
                    {
                        System.err.println(
                            "Exception while turning off option: "
                            + e.getMessage());
                    }
                }
            }
        }
        else
        {
            throw (new InvalidTelnetOptionException(
                "Invalid Option Code", optcode));
        }
    
public voidregisterNotifHandler(TelnetNotificationHandler notifhand)
Registers a notification handler to which will be sent notifications of received telnet option negotiation commands.

param
notifhand - TelnetNotificationHandler to be registered

        __notifhand = notifhand;
    
public voidunregisterNotifHandler()
Unregisters the current notification handler.

        __notifhand = null;