fr.gedeon.telnetservice
Class Session

java.lang.Object
  extended by fr.gedeon.telnetservice.Session
All Implemented Interfaces:
Runnable

public class Session
extends Object
implements Runnable

The Telnet Session is the main engine for a telnet connection; it handles inputs and outputs, keeping telnet state, responding to telnet specific commands, and notifies the Telnet Session Controller of important events (such as client input, client screen resize, etc) through the available notify* methods of ActionListeners provided by registered TelnetServiceExtension.

The typical connection life cycle is as follows:

  1. The Telnet Session is created by the Telnet Service when it receives an incoming connection, it is initialized as described above;
  2. The Telnet Session creates a SessionState instance. Only one state is created per session. It included in most calls from the session, and allows the integrating components to store their state;
  3. The Telnet Session goes through the telnet options negociation, and interacts with the service extensions for the behavioral features;
  4. When initial telnet setup and negociation is done (there can be further telnet messaging during the session), registered SessionListeners are notified that the connection was established; this means that interaction with the user is going to start as of that point.
    (hint: this is a good place to display a 'welcome' message, or a notification of, say, the number of messages in their inbox ;-)
  5. The next steps are repeated through the rest of the life-cycle of the Telnet Session (until a call to Telnet Session State isActive method returns false; at which point the connection is terminated and the Session is stopped).
  6. The prompt is displayed to the user;
  7. The user enters text, arrows, ctrl-char, or a trigger character;
  8. The Action Listeners are notified of the entered text. Depending on the telnet client capabilities, and also on the Telnet Session configuration, the text entered may either be on a line-by-line or character-by-character basis -- the Telnet Session will attempt to setup character-by-character interaction by default and act as a filter to the remainder of the application; however, if the telnet client does not accept this request, key based triggers other than a new line may not be received.
  9. The integrating application may spontaneously instruct the Session on its behavior at anytime, using the Session State provided methods.

Note that the core classes of this service library do not separate between login, password, and different state-based prompts; it is up to each Action Listener and Prompt Decorator to know what to display based on its Session State properties. The Action Listeners will also need to instruct the Console Delegate to suppress echo when it is expecting the user to type a password, for example. (this is done through ConsoleDelegate.setSuppressOutput(boolean).
Note: The telnetservice-jaas implementation of the Telnet Session Controller that is JAAS compliant and that takes care of the login procedure if it is needed by the configured LoginModule.

Author:
wgedeon

Field Summary
static int CHAR_BACKSPACE
           
static int CHAR_ESC
           
static int CHAR_TAB
           
protected  Thread current
          The session thread
static String implementedOptions
          Byte array of telnet options we can and want to negociate about, all other commands will result in a DONT or WONT response.
Those implemented so far are: TELNET_OPTION_TOGGLEFLOWCONTROL TELNET_OPTION_LINEMODE TELNET_OPTION_WINDOWSIZE TELNET_OPTION_TERMINAL_TYPE TELNET_OPTION_ECHO
protected  InputStream input
          The connection socket input stream
protected  OutputStream output
          The connection socket output stream
protected  Socket socket
          The connection socket
static int TELNET_AYT
          Are you there: Send back to the NVT some visible evidence that the AYT was received.
static int TELNET_DO
           
static int TELNET_DONT
           
static int TELNET_EC
          Erase character: The receiver should delete the last preceding undeleted character from the data stream.
static int TELNET_GA
           
static int TELNET_IAC
           
static int TELNET_NOP
           
static byte TELNET_OPTION_ECHO
           
static byte TELNET_OPTION_LINEMODE
           
static byte TELNET_OPTION_LINEMODE_FORWARDMASK
           
static byte TELNET_OPTION_LINEMODE_MODE
           
static byte TELNET_OPTION_LINEMODE_MODE_EDIT
           
static byte TELNET_OPTION_LINEMODE_MODE_LIT_ECHO
           
static byte TELNET_OPTION_LINEMODE_MODE_MODE_ACK
           
static byte TELNET_OPTION_LINEMODE_MODE_SOFT_TAB
           
static byte TELNET_OPTION_LINEMODE_MODE_TRAPSIG
           
static byte TELNET_OPTION_SUPRESSGOAHEAD
           
static byte TELNET_OPTION_TERMINAL_TYPE
           
static byte TELNET_OPTION_TERMINAL_TYPE_IS
           
static byte TELNET_OPTION_TERMINAL_TYPE_SEND
           
static byte TELNET_OPTION_TOGGLEFLOWCONTROL
           
static byte TELNET_OPTION_WINDOWSIZE
           
static int TELNET_SB
           
static int TELNET_SE
           
static byte[] TELNET_SEQUENCE_GOAHEAD
           
static int TELNET_WILL
           
static int TELNET_WONT
           
 
Method Summary
 void addSessionListener(SessionListener listener)
           
protected  void appendActiveText(char c)
           
protected  void appendActiveText(String txt, int offsetUpdateMask)
           
 void backspaceActiveText(int count)
           
protected  void close()
          Close all connections
protected  void commitActiveText()
           
 Date getConnectionTimestamp()
           
 ConsoleDelegate getConsoleDelegate()
           
 OutputFormat getOutputFormat()
           
 SocketAddress getRemoteSocketAddress()
           
 SessionState getSessionState()
           
 void notifyConnectionEstablished(SessionState sessionState)
           
 void notifyConnectionLost(SessionState sessionState)
           
 void output(SessionState sessionState, Object subject)
          Shortcut method for calling the session's OutputFormat to format the subject then calling the ConsoleDelegate to write the outcome to the console.
 void removeAllSessionListeners()
           
 void removeSessionListener(SessionListener listener)
           
protected  void replaceActiveText(String txt, int offsetUpdateMask)
           
 void run()
          Session run main loop
 void start()
          Start the session daemon: setup telnet options, then start the input processing.
 void stop()
          Stop this session
protected  void write(byte[] bbuf, int off, int len)
           
protected  void write(char[] cbuf, int off, int len)
           
protected  void write(String txt)
           
protected  void writeln(String txt)
           
protected  void writePrompt()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

socket

protected Socket socket
The connection socket


current

protected Thread current
The session thread


input

protected InputStream input
The connection socket input stream


output

protected OutputStream output
The connection socket output stream


CHAR_BACKSPACE

public static final int CHAR_BACKSPACE
See Also:
Constant Field Values

CHAR_TAB

public static final int CHAR_TAB
See Also:
Constant Field Values

CHAR_ESC

public static final int CHAR_ESC
See Also:
Constant Field Values

TELNET_OPTION_ECHO

public static final byte TELNET_OPTION_ECHO
See Also:
Constant Field Values

TELNET_OPTION_SUPRESSGOAHEAD

public static final byte TELNET_OPTION_SUPRESSGOAHEAD
See Also:
Constant Field Values

TELNET_OPTION_TERMINAL_TYPE

public static final byte TELNET_OPTION_TERMINAL_TYPE
See Also:
Constant Field Values

TELNET_OPTION_TERMINAL_TYPE_IS

public static final byte TELNET_OPTION_TERMINAL_TYPE_IS
See Also:
Constant Field Values

TELNET_OPTION_TERMINAL_TYPE_SEND

public static final byte TELNET_OPTION_TERMINAL_TYPE_SEND
See Also:
Constant Field Values

TELNET_OPTION_WINDOWSIZE

public static final byte TELNET_OPTION_WINDOWSIZE
See Also:
Constant Field Values

TELNET_OPTION_TOGGLEFLOWCONTROL

public static final byte TELNET_OPTION_TOGGLEFLOWCONTROL
See Also:
Constant Field Values

TELNET_OPTION_LINEMODE

public static final byte TELNET_OPTION_LINEMODE
See Also:
Constant Field Values

TELNET_OPTION_LINEMODE_MODE

public static final byte TELNET_OPTION_LINEMODE_MODE
See Also:
Constant Field Values

TELNET_OPTION_LINEMODE_MODE_EDIT

public static final byte TELNET_OPTION_LINEMODE_MODE_EDIT
See Also:
Constant Field Values

TELNET_OPTION_LINEMODE_MODE_TRAPSIG

public static final byte TELNET_OPTION_LINEMODE_MODE_TRAPSIG
See Also:
Constant Field Values

TELNET_OPTION_LINEMODE_MODE_MODE_ACK

public static final byte TELNET_OPTION_LINEMODE_MODE_MODE_ACK
See Also:
Constant Field Values

TELNET_OPTION_LINEMODE_MODE_SOFT_TAB

public static final byte TELNET_OPTION_LINEMODE_MODE_SOFT_TAB
See Also:
Constant Field Values

TELNET_OPTION_LINEMODE_MODE_LIT_ECHO

public static final byte TELNET_OPTION_LINEMODE_MODE_LIT_ECHO
See Also:
Constant Field Values

TELNET_OPTION_LINEMODE_FORWARDMASK

public static final byte TELNET_OPTION_LINEMODE_FORWARDMASK
See Also:
Constant Field Values

TELNET_SE

public static final int TELNET_SE
See Also:
Constant Field Values

TELNET_NOP

public static final int TELNET_NOP
See Also:
Constant Field Values

TELNET_AYT

public static final int TELNET_AYT
Are you there: Send back to the NVT some visible evidence that the AYT was received.

See Also:
Constant Field Values

TELNET_EC

public static final int TELNET_EC
Erase character: The receiver should delete the last preceding undeleted character from the data stream.

See Also:
Constant Field Values

TELNET_GA

public static final int TELNET_GA
See Also:
Constant Field Values

TELNET_SB

public static final int TELNET_SB
See Also:
Constant Field Values

TELNET_WILL

public static final int TELNET_WILL
See Also:
Constant Field Values

TELNET_WONT

public static final int TELNET_WONT
See Also:
Constant Field Values

TELNET_DO

public static final int TELNET_DO
See Also:
Constant Field Values

TELNET_DONT

public static final int TELNET_DONT
See Also:
Constant Field Values

TELNET_IAC

public static final int TELNET_IAC
See Also:
Constant Field Values

TELNET_SEQUENCE_GOAHEAD

public static final byte[] TELNET_SEQUENCE_GOAHEAD

implementedOptions

public static final String implementedOptions
Byte array of telnet options we can and want to negociate about, all other commands will result in a DONT or WONT response.
Those implemented so far are:
Method Detail

getConsoleDelegate

public ConsoleDelegate getConsoleDelegate()

getOutputFormat

public OutputFormat getOutputFormat()

start

public void start()
           throws TelnetServiceException
Start the session daemon: setup telnet options, then start the input processing.

Throws:
TelnetServiceException

stop

public void stop()
Stop this session


close

protected void close()
Close all connections


run

public void run()
Session run main loop

Specified by:
run in interface Runnable

writePrompt

protected void writePrompt()

write

protected void write(byte[] bbuf,
                     int off,
                     int len)

write

protected void write(char[] cbuf,
                     int off,
                     int len)

write

protected void write(String txt)

writeln

protected void writeln(String txt)

backspaceActiveText

public void backspaceActiveText(int count)

appendActiveText

protected void appendActiveText(char c)

appendActiveText

protected void appendActiveText(String txt,
                                int offsetUpdateMask)

replaceActiveText

protected void replaceActiveText(String txt,
                                 int offsetUpdateMask)

commitActiveText

protected void commitActiveText()

addSessionListener

public void addSessionListener(SessionListener listener)

removeAllSessionListeners

public void removeAllSessionListeners()

removeSessionListener

public void removeSessionListener(SessionListener listener)

notifyConnectionEstablished

public void notifyConnectionEstablished(SessionState sessionState)

notifyConnectionLost

public void notifyConnectionLost(SessionState sessionState)

getSessionState

public SessionState getSessionState()

getRemoteSocketAddress

public SocketAddress getRemoteSocketAddress()

getConnectionTimestamp

public Date getConnectionTimestamp()

output

public void output(SessionState sessionState,
                   Object subject)
Shortcut method for calling the session's OutputFormat to format the subject then calling the ConsoleDelegate to write the outcome to the console.

Parameters:
sessionState - the current session state
subject - the item to be output
Throws:
OutputFormatException


Copyright © 2008. All Rights Reserved.