org.opengts.util
Class ServerSocketThread

java.lang.Object
  extended by java.lang.Thread
      extended by org.opengts.util.ServerSocketThread
All Implemented Interfaces:
java.lang.Runnable

public class ServerSocketThread
extends java.lang.Thread


Nested Class Summary
 class ServerSocketThread.ServerSessionThread
          ServerSessionThread
static interface ServerSocketThread.SessionInfo
          SessionInfo
static class ServerSocketThread.SSEndOfStreamException
          SSEndOfStreamException
static class ServerSocketThread.SSReadTimeoutException
          SSReadTimeoutException
static class ServerSocketThread.SSSessionTimeoutException
          SSSessionTimeoutException
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
static boolean ACK_FROM_LISTEN_PORT
           
static int PACKET_LEN_END_OF_STREAM
           
static int PACKET_LEN_INCREMENT_EOL
           
static int PACKET_LEN_INCREMENTAL_
           
static int PACKET_LEN_INCREMENTAL_MASK
           
static int PACKET_LEN_LINE_TERMINATOR
           
static int PACKET_LEN_MATCH_PATTERN
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ServerSocketThread(java.net.DatagramSocket ds)
          Constructor for UDP connections
ServerSocketThread(java.net.InetAddress bindAddr, int port)
          Constructor for TCP connections
ServerSocketThread(java.net.InetAddress bindAddr, int port, boolean useSSL)
          Constructor for TCP connections
ServerSocketThread(int port)
          Constructor for TCP connections
ServerSocketThread(int port, boolean useSSL)
          Constructor for TCP connections
ServerSocketThread(java.net.ServerSocket ss)
          Constructor for TCP connections
 
Method Summary
 void addActionListener(java.awt.event.ActionListener al)
          Adds an action listener
static java.net.DatagramSocket createDatagramSocket(java.net.InetAddress bindAddr, int port)
          Creates a DatagramSocket bound to the default local interface
static java.net.DatagramSocket createDatagramSocket(int port)
          Creates a DatagramSocket bound to the default local interface
static java.net.ServerSocket createServerSocket(java.net.InetAddress bindAddr, int port)
          Creates a ServerSocket bound to the default local interface
 int[] getBackspaceChar()
          Gets the backspace characters
 java.net.InetAddress getBindAddress()
          Gets the local bind address
 ClientPacketHandler getClientPacketHandler()
          Gets the current client packet handler
 java.net.DatagramSocket getDatagramSocket()
          Gets the bound UDP DatagramSocket for this server handler.
static java.net.InetAddress getDefaultBindAddress()
          Gets the local bind address for all created ServerSocket's, or null if no specific bind address has been set
 long getIdleTimeout()
          Gets the idle timeout in milliseconds
 int[] getIgnoreChar()
          Gets the characters to ignore
 boolean getIncludePacketLineTerminator()
          Returns True if the line terminator character should be included in the returned packet
 int[] getLineTerminatorChar()
          Gets the line terminator characters
 int getLingerTimeoutSec()
          Gets the linger timeout in seconds
 int getLocalPort()
          Gets the local port to which this socket is bound
 int getMaximumPacketLength()
          Gets the maximum packet length
 int getMinimumPacketLength()
          Gets the minimum packet length
static java.net.InetAddress[] getNetworkInterfaceAddresses()
          Returns an array of all local network interface addresses (excluding loopback)
 byte[] getPacketTerminatorPattern()
          Returns the packet terminator pattern
 long getPacketTimeout()
          Gets the packet timeout in milliseconds
protected  byte[] getPrompt(int ndx)
          Gets the prompt for a specified index
protected  int getPromptIndex()
          Gets the current prompt index (used for auto prompt)
 int getRemotePort()
          Gets the remote UDP response port
 java.net.ServerSocket getServerSocket()
          Gets the bound TCP ServerSocket for this server handler.
 long getSessionTimeout()
          Gets the session timeout in milliseconds
 boolean getTerminateOnTimeout()
          Gets if the thread should be terminated after a timeout [CHECK]
static boolean hasBindAddress()
          Returns true if a local bind address has been defined, otherwise false
 boolean hasListeners()
          Returns true if this object has action listeners
 boolean hasPrompt()
          If this server has a valid prompt
 boolean includePacketLineTerminator()
          Returns True if the line terminator character should be included in the returned packet
protected  boolean invokeListeners(byte[] msgBytes)
          Invokes action listener with the specified message
 boolean isBackspaceChar(int ch)
          Returns true if ch is a backspace character
 boolean isIgnoreChar(int ch)
          Returns true if ch is a character to ignore
 boolean isLineTerminatorChar(int ch)
          Returns true if ch is a line terminator
static boolean isLocalInterfaceAddress(java.net.InetAddress addr)
          Returns true if the specified InetAddress is a local bound interface (including loopback)
 boolean isTextPackets()
          Returns true if the packets are text
static boolean isValidPort(int port)
          Returns true if the specified port is valid
 void removeActionListener(java.awt.event.ActionListener al)
          Removes an action listener
 void run()
          Listens for incoming connections and dispatches them to a handler thread
static void sendDatagram(java.net.InetAddress host, int port, byte[] data)
          Sends a datagram to the specified host:port
 void setAutoPrompt(boolean auto)
          If a default automatically generated prompt should be used [CHECK](all prompt related below)
 void setBackspaceChar(int bs)
          Sets the backspace character
 void setBackspaceChar(int[] bs)
          Sets the backspace characters
static void setBindAddress(java.net.InetAddress bindAddr)
          Sets the local bind address for all created ServerSocket's
 void setClientPacketHandler(ClientPacketHandler cph)
          Sets the client packet handler [CHECK]
 void setClientPacketHandlerClass(java.lang.Class cphc)
          Sets the client packet handler class [CHECK]
 void setIdleTimeout(long timeoutMS)
          Sets the idle timeout in milliseconds
 void setIgnoreChar(int[] bs)
          Sets the characters to ignore
 void setIncludePacketLineTerminator(boolean rtnTerm)
          Sets whether the line terminator character should be included in the returned packets
 void setLineTerminatorChar(int term)
          Sets the line terminator character
 void setLineTerminatorChar(int[] term)
          Sets the line terminator characters
 void setLingerTimeoutSec(int timeoutSec)
          Sets the linger timeout in seconds
static void setListenBacklog(int backlog)
          Sets the listen backlog for all created ServerSocket's
 void setMaximumPacketLength(int len)
          Sets the maximum packet length
 void setMinimumPacketLength(int len)
          Sets the minimum packet length
 void setPacketTerminatorPattern(byte[] pktTerm)
          Sets the packet terminator pattern
 void setPacketTimeout(long timeoutMS)
          Sets the packet timeout in milliseconds
 void setPrompt(byte[] prompt)
          Sets the prompt for TCP connections
 void setPrompt(java.lang.String prompt)
          Sets the prompt for TCP connections
 void setRemotePort(int remotePort)
          Sets the remote UDP response port
 void setSessionTimeout(long timeoutMS)
          Sets the session timeout in milliseconds
 void setTerminateOnTimeout(boolean timeoutQuit)
          Sets if the thread should be terminated after a timeout [CHECK]
 void setTextPackets(boolean isText)
          Sets if the packets to be sent are text
 void shutdown()
          Shuts down the server
 boolean tcpWriteToSessionID(java.lang.String sessionID, byte[] data)
          Find the named TCP session and write the specified bytes TCP output stream
 void testSession(byte[] data)
          Run a test session from the specified input data array
 void testSession(java.io.InputStream dataInput)
          Run a test session from the specified input stream
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PACKET_LEN_INCREMENTAL_

public static final int PACKET_LEN_INCREMENTAL_
See Also:
Constant Field Values

PACKET_LEN_INCREMENTAL_MASK

public static final int PACKET_LEN_INCREMENTAL_MASK
See Also:
Constant Field Values

PACKET_LEN_LINE_TERMINATOR

public static final int PACKET_LEN_LINE_TERMINATOR
See Also:
Constant Field Values

PACKET_LEN_END_OF_STREAM

public static final int PACKET_LEN_END_OF_STREAM
See Also:
Constant Field Values

PACKET_LEN_MATCH_PATTERN

public static final int PACKET_LEN_MATCH_PATTERN
See Also:
Constant Field Values

PACKET_LEN_INCREMENT_EOL

public static final int PACKET_LEN_INCREMENT_EOL
See Also:
Constant Field Values

ACK_FROM_LISTEN_PORT

public static final boolean ACK_FROM_LISTEN_PORT
See Also:
Constant Field Values
Constructor Detail

ServerSocketThread

public ServerSocketThread(java.net.DatagramSocket ds)
Constructor for UDP connections


ServerSocketThread

public ServerSocketThread(java.net.ServerSocket ss)
Constructor for TCP connections

Parameters:
ss - The ServerSocket containing the 'listen' port information

ServerSocketThread

public ServerSocketThread(java.net.InetAddress bindAddr,
                          int port)
                   throws java.io.IOException
Constructor for TCP connections

Parameters:
port - The port on which to listen for incoming connections
Throws:
java.io.IOException

ServerSocketThread

public ServerSocketThread(int port)
                   throws java.io.IOException
Constructor for TCP connections

Parameters:
port - The port on which to listen for incoming connections
Throws:
java.io.IOException

ServerSocketThread

public ServerSocketThread(java.net.InetAddress bindAddr,
                          int port,
                          boolean useSSL)
                   throws java.io.IOException
Constructor for TCP connections

Parameters:
port - The port on which to listen for incoming connections
useSSL - True to enable an SSL
Throws:
java.io.IOException

ServerSocketThread

public ServerSocketThread(int port,
                          boolean useSSL)
                   throws java.io.IOException
Constructor for TCP connections

Parameters:
port - The port on which to listen for incoming connections
useSSL - True to enable an SSL
Throws:
java.io.IOException
Method Detail

setListenBacklog

public static void setListenBacklog(int backlog)
Sets the listen backlog for all created ServerSocket's

Parameters:
backlog - The listen backlog

setBindAddress

public static void setBindAddress(java.net.InetAddress bindAddr)
Sets the local bind address for all created ServerSocket's

Parameters:
bindAddr - The local bind address

hasBindAddress

public static boolean hasBindAddress()
Returns true if a local bind address has been defined, otherwise false

Returns:
True if a local bind address has been defined, otherwise false

getDefaultBindAddress

public static java.net.InetAddress getDefaultBindAddress()
Gets the local bind address for all created ServerSocket's, or null if no specific bind address has been set

Returns:
The local bind address

getNetworkInterfaceAddresses

public static java.net.InetAddress[] getNetworkInterfaceAddresses()
                                                           throws java.net.SocketException
Returns an array of all local network interface addresses (excluding loopback)

Returns:
An array of all local network interface addresses
Throws:
java.net.SocketException

isLocalInterfaceAddress

public static boolean isLocalInterfaceAddress(java.net.InetAddress addr)
Returns true if the specified InetAddress is a local bound interface (including loopback)

Returns:
True if the specified InetAddress is a local bound interface

createDatagramSocket

public static java.net.DatagramSocket createDatagramSocket(java.net.InetAddress bindAddr,
                                                           int port)
                                                    throws java.net.SocketException
Creates a DatagramSocket bound to the default local interface

Returns:
The created DatagramSocket
Throws:
java.net.SocketException

createDatagramSocket

public static java.net.DatagramSocket createDatagramSocket(int port)
                                                    throws java.net.SocketException
Creates a DatagramSocket bound to the default local interface

Returns:
The created DatagramSocket
Throws:
java.net.SocketException

createServerSocket

public static java.net.ServerSocket createServerSocket(java.net.InetAddress bindAddr,
                                                       int port)
                                                throws java.io.IOException
Creates a ServerSocket bound to the default local interface

Returns:
The created ServerSocket
Throws:
java.io.IOException

isValidPort

public static boolean isValidPort(int port)
Returns true if the specified port is valid

Parameters:
port - The port to test
Returns:
True if the specified port is valid

getDatagramSocket

public java.net.DatagramSocket getDatagramSocket()
Gets the bound UDP DatagramSocket for this server handler. Will return null if this server handler does not handle UDP connections.

Returns:
The DatagramSocket handle

getServerSocket

public java.net.ServerSocket getServerSocket()
Gets the bound TCP ServerSocket for this server handler. Will return null if this server handler does not handle TCP connections.

Returns:
The DatagramSocket handle

getLocalPort

public int getLocalPort()
Gets the local port to which this socket is bound

Returns:
the local port to which this socket is bound

getBindAddress

public java.net.InetAddress getBindAddress()
Gets the local bind address


testSession

public void testSession(byte[] data)
Run a test session from the specified input data array

Parameters:
data - The test input data array

testSession

public void testSession(java.io.InputStream dataInput)
Run a test session from the specified input stream

Parameters:
dataInput - The test input stream

run

public void run()
Listens for incoming connections and dispatches them to a handler thread

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

shutdown

public void shutdown()
Shuts down the server


setRemotePort

public void setRemotePort(int remotePort)
Sets the remote UDP response port

Parameters:
remotePort - The remote UDP respose port

getRemotePort

public int getRemotePort()
Gets the remote UDP response port

Returns:
The remote UDP respose port

hasListeners

public boolean hasListeners()
Returns true if this object has action listeners

Returns:
True if this object has action listeners

addActionListener

public void addActionListener(java.awt.event.ActionListener al)
Adds an action listener

Parameters:
al - The action listener to add

removeActionListener

public void removeActionListener(java.awt.event.ActionListener al)
Removes an action listener

Parameters:
al - The action listener to remove

invokeListeners

protected boolean invokeListeners(byte[] msgBytes)
                           throws java.lang.Exception
Invokes action listener with the specified message

Parameters:
msgBytes - The message to invoke the listeners with as a byte array
Returns:
True if succesful
Throws:
java.lang.Exception

setClientPacketHandler

public void setClientPacketHandler(ClientPacketHandler cph)
Sets the client packet handler [CHECK]

Parameters:
cph - The client packet handler

setClientPacketHandlerClass

public void setClientPacketHandlerClass(java.lang.Class cphc)
Sets the client packet handler class [CHECK]

Parameters:
cphc - The client packet handler class

getClientPacketHandler

public ClientPacketHandler getClientPacketHandler()
Gets the current client packet handler

Returns:
The current client packet handler

setSessionTimeout

public void setSessionTimeout(long timeoutMS)
Sets the session timeout in milliseconds

Parameters:
timeoutMS - The session timeout in milliseconds

getSessionTimeout

public long getSessionTimeout()
Gets the session timeout in milliseconds

Returns:
The session timeout in milliseconds

setIdleTimeout

public void setIdleTimeout(long timeoutMS)
Sets the idle timeout in milliseconds

Parameters:
timeoutMS - The idle timeout in milliseconds

getIdleTimeout

public long getIdleTimeout()
Gets the idle timeout in milliseconds

Returns:
The idle timeout in milliseconds

setPacketTimeout

public void setPacketTimeout(long timeoutMS)
Sets the packet timeout in milliseconds

Parameters:
timeoutMS - The packet timeout in milliseconds

getPacketTimeout

public long getPacketTimeout()
Gets the packet timeout in milliseconds

Returns:
The packet timeout in milliseconds

setTerminateOnTimeout

public void setTerminateOnTimeout(boolean timeoutQuit)
Sets if the thread should be terminated after a timeout [CHECK]

Parameters:
timeoutQuit - True if the thread should be terminated after a timeout

getTerminateOnTimeout

public boolean getTerminateOnTimeout()
Gets if the thread should be terminated after a timeout [CHECK]

Returns:
True if the thread should be terminated after a timeout

setLingerTimeoutSec

public void setLingerTimeoutSec(int timeoutSec)
Sets the linger timeout in seconds

Parameters:
timeoutSec - The linger timeout in seconds

getLingerTimeoutSec

public int getLingerTimeoutSec()
Gets the linger timeout in seconds

Returns:
The linger timeout in seconds

setTextPackets

public void setTextPackets(boolean isText)
Sets if the packets to be sent are text

Parameters:
isText - True if the packets are text

isTextPackets

public boolean isTextPackets()
Returns true if the packets are text

Returns:
Ture if the packets are text

setMaximumPacketLength

public void setMaximumPacketLength(int len)
Sets the maximum packet length

Parameters:
len - The maximum packet length

getMaximumPacketLength

public int getMaximumPacketLength()
Gets the maximum packet length

Returns:
The maximum packet length

setMinimumPacketLength

public void setMinimumPacketLength(int len)
Sets the minimum packet length

Parameters:
len - The minimum packet length

getMinimumPacketLength

public int getMinimumPacketLength()
Gets the minimum packet length

Returns:
The minimum packet length

setLineTerminatorChar

public void setLineTerminatorChar(int term)
Sets the line terminator character

Parameters:
term - The line terminator character

setLineTerminatorChar

public void setLineTerminatorChar(int[] term)
Sets the line terminator characters

Parameters:
term - The line terminator characters

getLineTerminatorChar

public int[] getLineTerminatorChar()
Gets the line terminator characters

Returns:
The line terminator characters

isLineTerminatorChar

public boolean isLineTerminatorChar(int ch)
Returns true if ch is a line terminator

Returns:
True if ch is a line terminator

setIncludePacketLineTerminator

public void setIncludePacketLineTerminator(boolean rtnTerm)
Sets whether the line terminator character should be included in the returned packets

Parameters:
rtnTerm - True to include the line terminator character in returned packets

getIncludePacketLineTerminator

public boolean getIncludePacketLineTerminator()
Returns True if the line terminator character should be included in the returned packet

Returns:
True if the line terminator character should be included in the returned packet

includePacketLineTerminator

public boolean includePacketLineTerminator()
Returns True if the line terminator character should be included in the returned packet

Returns:
True if the line terminator character should be included in the returned packet

setPacketTerminatorPattern

public void setPacketTerminatorPattern(byte[] pktTerm)
Sets the packet terminator pattern

Parameters:
pktTerm - The packet terminator pattern

getPacketTerminatorPattern

public byte[] getPacketTerminatorPattern()
Returns the packet terminator pattern

Returns:
The packet terminator pattern

setBackspaceChar

public void setBackspaceChar(int bs)
Sets the backspace character

Parameters:
bs - The backspace character

setBackspaceChar

public void setBackspaceChar(int[] bs)
Sets the backspace characters

Parameters:
bs - The backspace characters

getBackspaceChar

public int[] getBackspaceChar()
Gets the backspace characters

Returns:
The backspace characters

isBackspaceChar

public boolean isBackspaceChar(int ch)
Returns true if ch is a backspace character

Returns:
True if ch is a backspace character

setIgnoreChar

public void setIgnoreChar(int[] bs)
Sets the characters to ignore

Parameters:
bs - The characters to ignore

getIgnoreChar

public int[] getIgnoreChar()
Gets the characters to ignore

Returns:
The characters to ignore

isIgnoreChar

public boolean isIgnoreChar(int ch)
Returns true if ch is a character to ignore

Returns:
True if ch is a character to ignore

setAutoPrompt

public void setAutoPrompt(boolean auto)
If a default automatically generated prompt should be used [CHECK](all prompt related below)

Parameters:
auto - Ture if default automatic prompt should be used

setPrompt

public void setPrompt(byte[] prompt)
Sets the prompt for TCP connections

Parameters:
prompt - The prompt

setPrompt

public void setPrompt(java.lang.String prompt)
Sets the prompt for TCP connections

Parameters:
prompt - The prompt

getPrompt

protected byte[] getPrompt(int ndx)
Gets the prompt for a specified index

Parameters:
ndx - The index (used for auto prompt)

hasPrompt

public boolean hasPrompt()
If this server has a valid prompt

Returns:
True if this server has a valid prompt

getPromptIndex

protected int getPromptIndex()
Gets the current prompt index (used for auto prompt)

Returns:
The current prompt index

tcpWriteToSessionID

public boolean tcpWriteToSessionID(java.lang.String sessionID,
                                   byte[] data)
Find the named TCP session and write the specified bytes TCP output stream

Parameters:
sessionID - The session ID
data - The bytes to write
Returns:
True if the bytes were written, false otherwise

sendDatagram

public static void sendDatagram(java.net.InetAddress host,
                                int port,
                                byte[] data)
                         throws java.io.IOException
Sends a datagram to the specified host:port

Parameters:
host - The destination host
port - The destination port
data - The data to send
Throws:
java.io.IOException - if an IO error occurs