org.opengts.util
Class ClientSocketThread

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

public class ClientSocketThread
extends java.lang.Thread

Threaded messaging socket client


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ClientSocketThread(java.lang.String host, int port)
          Constructor
ClientSocketThread(java.lang.String host, int port, boolean useSSL)
          Constructor
 
Method Summary
 void addActionListener(java.awt.event.ActionListener al)
          Adds an action listener
 void closeSocket()
          Closes the socket connection
static java.lang.String getLineTerminator()
          Returns the line terminator character (as a String value) for "socketWriteLine"
static char getLineTerminatorChar()
          Returns the line terminator character for "socketWriteLine"
 long getReadTimeout()
          Gets the read timeout in milliseconds
protected  void handleMessage(java.lang.String msg)
          Invokes all listeners with an assciated command.
protected  void invokeListeners(java.lang.String r)
          Invokes all listeners with an assciated command
 boolean isRunning()
          Returns true if the thread is currently running
static void main(java.lang.String[] argv)
          Main entry point for testing/debugging
 void openSocket()
          Opens the socket connection
 void openSocket(long timeoutMS)
          Opens the socket connection
 void removeActionListener(java.awt.event.ActionListener al)
          Removes an action listener
 void run()
          Main client thread loop
 void sendMessage(java.lang.String msg)
          Queues a message to be sent
static void setLineTerminatorChar(char lt)
          Sets the line terminator character for "socketWriteLine"
 void setReadTimeout(long ms)
          Sets the read timeout in milliseconds
 boolean setSocketReadTimeout()
          Sets the read timeout on the currently open socket to the previous specified value
 boolean setSocketReadTimeout(long ms)
          Sets the read timeout on the currently open socket to the specified value
 int socketReadBytes(java.io.ByteArrayOutputStream baos, int maxLength)
          Reads the bytes from the specifed socket until an eod-of-stream error occurs, or until the maximum number of bytes has bee read.
 byte[] socketReadBytes(int length)
          Reads the specified number of bytes from the socket
protected static int socketReadBytes(java.net.Socket socket, java.io.ByteArrayOutputStream baos, int maxLength)
          Reads the bytes from the specifed socket until an eod-of-stream error occurs, or until the maximum number of bytes has bee read.
protected static byte[] socketReadBytes(java.net.Socket socket, int length)
          Reads the specified number of bytes from the specifed socket
 java.lang.String socketReadLine()
          Reads a line from the socket input stream
 java.lang.String socketReadLine(int maxLen)
          Reads a line from the socket input stream
 java.lang.String socketReadLine(int maxLen, java.lang.StringBuffer sb)
          Reads a line from the socket input stream
protected static java.lang.String socketReadLine(java.net.Socket socket, int maxLen, java.lang.StringBuffer sb)
          Reads a line from the specified socket's input stream
 java.lang.String socketReadLine(java.lang.StringBuffer sb)
          Reads a line from the socket input strea
 void socketWriteBytes(byte[] b)
          Writes the specified byte array to the socket output stream
 void socketWriteBytes(byte[] b, int offset, int length)
          Writes length bytes from the specified byte array starting at offset to the socket output stream.
protected static void socketWriteBytes(java.net.Socket socket, byte[] b)
          Writes the specified byte array to the specified socket's output stream
protected static void socketWriteBytes(java.net.Socket socket, byte[] b, int offset, int length)
          Writes length bytes from the specified byte array starting at offset to the specified socket's output stream.
protected static void socketWriteLine(java.net.Socket socket, java.lang.String val)
          Writes the specified line to the specified socket's output stream
 void socketWriteLine(java.lang.String val)
          Writes the specified line to the socket output stream
protected static void socketWriteString(java.net.Socket socket, java.lang.String val)
          Writes the specified String to the specified socket's output stream
protected static void socketWriteString(java.net.Socket socket, java.lang.StringBuffer val)
          Writes the specified StringBuffer to the specified socket's output stream
 void socketWriteString(java.lang.String val)
          Writes the specified String to the socket output stream
 void socketWriteString(java.lang.StringBuffer val)
          Writes the specified StringBuffer to the socket output stream
 void startThread()
          Causes this thread to begin execution
 void stopThread()
          Attemps to stop the thread [CHECK]
protected  void threadStarted()
          Called when the thread is started
protected  void threadStopped()
          Called when the thread has stopped
 
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
 

Constructor Detail

ClientSocketThread

public ClientSocketThread(java.lang.String host,
                          int port)
Constructor

Parameters:
host - The host name to connect to
port - The port number to use

ClientSocketThread

public ClientSocketThread(java.lang.String host,
                          int port,
                          boolean useSSL)
Constructor

Parameters:
host - The host name to connect to
port - The port number to use
useSSL - True if SSL is to be used
Method Detail

getReadTimeout

public long getReadTimeout()
Gets the read timeout in milliseconds

Returns:
The read timeout in milliseconds

setReadTimeout

public void setReadTimeout(long ms)
Sets the read timeout in milliseconds

Parameters:
ms - The read timeout in milliseconds

setSocketReadTimeout

public boolean setSocketReadTimeout()
                             throws java.net.SocketException
Sets the read timeout on the currently open socket to the previous specified value

Throws:
java.net.SocketException

setSocketReadTimeout

public boolean setSocketReadTimeout(long ms)
                             throws java.net.SocketException
Sets the read timeout on the currently open socket to the specified value

Throws:
java.net.SocketException

openSocket

public void openSocket()
                throws java.io.IOException
Opens the socket connection

Throws:
java.io.IOException - if an error occurs while creating the socket

openSocket

public void openSocket(long timeoutMS)
                throws java.io.IOException
Opens the socket connection

Parameters:
timeoutMS - open timeout in milliseconds
Throws:
java.io.IOException - if an error occurs while creating the socket

closeSocket

public void closeSocket()
Closes the socket connection


socketWriteBytes

public void socketWriteBytes(byte[] b)
                      throws java.io.IOException
Writes the specified byte array to the socket output stream

Parameters:
b - The byte array to write to the socket output stream
Throws:
java.io.IOException - if an error occurs

socketWriteBytes

public void socketWriteBytes(byte[] b,
                             int offset,
                             int length)
                      throws java.io.IOException
Writes length bytes from the specified byte array starting at offset to the socket output stream.

Parameters:
b - The byte array to write to the socket output stream
offset - The start offset in the data to begin writing at
length - The length of the data. Normally b.length
Throws:
java.io.IOException - if an error occurs

socketWriteBytes

protected static void socketWriteBytes(java.net.Socket socket,
                                       byte[] b,
                                       int offset,
                                       int length)
                                throws java.io.IOException
Writes length bytes from the specified byte array starting at offset to the specified socket's output stream.

Parameters:
socket - The socket which's output stream to write to
b - The byte array to write to the socket output stream
offset - The start offset in the data to begin writing at
length - The length of the data. Normally b.length
Throws:
java.io.IOException - if an error occurs

socketWriteBytes

protected static void socketWriteBytes(java.net.Socket socket,
                                       byte[] b)
                                throws java.io.IOException
Writes the specified byte array to the specified socket's output stream

Parameters:
socket - The socket which's output stream to write to
b - The byte array to write to the socket output stream
Throws:
java.io.IOException - if an error occurs

socketWriteString

public void socketWriteString(java.lang.StringBuffer val)
                       throws java.io.IOException
Writes the specified StringBuffer to the socket output stream

Parameters:
val - The StringBuffer to write to the socket output stream.
Throws:
java.io.IOException - if an error occurs

socketWriteString

protected static void socketWriteString(java.net.Socket socket,
                                        java.lang.StringBuffer val)
                                 throws java.io.IOException
Writes the specified StringBuffer to the specified socket's output stream

Parameters:
socket - The socket which's output stream to write to
val - The StringBuffer to write to the socket output stream.
Throws:
java.io.IOException - if an error occurs

socketWriteString

public void socketWriteString(java.lang.String val)
                       throws java.io.IOException
Writes the specified String to the socket output stream

Parameters:
val - The String to write to the socket output stream.
Throws:
java.io.IOException - if an error occurs

socketWriteString

protected static void socketWriteString(java.net.Socket socket,
                                        java.lang.String val)
                                 throws java.io.IOException
Writes the specified String to the specified socket's output stream

Parameters:
socket - The socket which's output stream to write to
val - The String to write to the socket output stream.
Throws:
java.io.IOException - if an error occurs

socketWriteLine

public void socketWriteLine(java.lang.String val)
                     throws java.io.IOException
Writes the specified line to the socket output stream

Parameters:
val - The line to write to the socket output stream. A newline is appended if it does not already end with one.
Throws:
java.io.IOException - if an error occurs

socketWriteLine

protected static void socketWriteLine(java.net.Socket socket,
                                      java.lang.String val)
                               throws java.io.IOException
Writes the specified line to the specified socket's output stream

Parameters:
socket - The socket which's output stream to write to
val - The line to write to the socket output stream. A newline is appended if it does not already end with one.
Throws:
java.io.IOException - if an error occurs

getLineTerminator

public static java.lang.String getLineTerminator()
Returns the line terminator character (as a String value) for "socketWriteLine"

Returns:
The line terminator character as a String value

getLineTerminatorChar

public static char getLineTerminatorChar()
Returns the line terminator character for "socketWriteLine"

Returns:
The line terminator character

setLineTerminatorChar

public static void setLineTerminatorChar(char lt)
Sets the line terminator character for "socketWriteLine"

Parameters:
lt - The line terminator character

socketReadBytes

public byte[] socketReadBytes(int length)
                       throws java.io.IOException
Reads the specified number of bytes from the socket

Parameters:
length - The number of bytes to read from the socket
Throws:
java.io.IOException - if an error occured or the server has stopped

socketReadBytes

protected static byte[] socketReadBytes(java.net.Socket socket,
                                        int length)
                                 throws java.io.IOException
Reads the specified number of bytes from the specifed socket

Parameters:
socket - The socket from which bytes are read
length - The number of bytes to read from the socket
Throws:
java.io.IOException - if an error occured or the server has stopped

socketReadBytes

public int socketReadBytes(java.io.ByteArrayOutputStream baos,
                           int maxLength)
                    throws java.io.IOException
Reads the bytes from the specifed socket until an eod-of-stream error occurs, or until the maximum number of bytes has bee read.

Parameters:
baos - The ByteArrayOutputStream to which the bytes are written
maxLength - The number of bytes to read from the socket
Returns:
The number of bytes read if no exception has occurred
Throws:
java.io.IOException - if an error occured or the server has stopped. ByteArrayOutputStream will still contain all bytes read from the stream up until it failed.

socketReadBytes

protected static int socketReadBytes(java.net.Socket socket,
                                     java.io.ByteArrayOutputStream baos,
                                     int maxLength)
                              throws java.io.IOException
Reads the bytes from the specifed socket until an eod-of-stream error occurs, or until the maximum number of bytes has bee read.

Parameters:
socket - The socket from which bytes are read
baos - The ByteArrayOutputStream to which the bytes are written
maxLength - The number of bytes to read from the socket
Returns:
The number of bytes read if no exception has occurred
Throws:
java.io.IOException - if an error occured or the server has stopped

socketReadLine

public java.lang.String socketReadLine()
                                throws java.io.IOException
Reads a line from the socket input stream

Throws:
java.io.IOException - if an error occurs or the server has stopped

socketReadLine

public java.lang.String socketReadLine(int maxLen)
                                throws java.io.IOException
Reads a line from the socket input stream

Parameters:
maxLen - The maximum length of the line to read
Throws:
java.io.IOException - if an error occurs or the server has stopped

socketReadLine

public java.lang.String socketReadLine(java.lang.StringBuffer sb)
                                throws java.io.IOException
Reads a line from the socket input strea

Parameters:
sb - The string buffer to use
Throws:
java.io.IOException - if an error occurs or the server has stopped

socketReadLine

public java.lang.String socketReadLine(int maxLen,
                                       java.lang.StringBuffer sb)
                                throws java.io.IOException
Reads a line from the socket input stream

Parameters:
maxLen - The maximum length of of the line to read
sb - The string buffer to use
Throws:
java.io.IOException - if an error occurs or the server has stopped

socketReadLine

protected static java.lang.String socketReadLine(java.net.Socket socket,
                                                 int maxLen,
                                                 java.lang.StringBuffer sb)
                                          throws java.io.IOException
Reads a line from the specified socket's input stream

Parameters:
socket - The socket to read a line from
maxLen - The maximum length of of the line to read
sb - The string buffer to use
Throws:
java.io.IOException - if an error occurs or the server has stopped

run

public void run()
Main client thread loop

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

threadStarted

protected void threadStarted()
Called when the thread is started


threadStopped

protected void threadStopped()
Called when the thread has stopped


stopThread

public void stopThread()
Attemps to stop the thread [CHECK]


startThread

public void startThread()
Causes this thread to begin execution


isRunning

public boolean isRunning()
Returns true if the thread is currently running

Returns:
True if the thread is currently running

sendMessage

public void sendMessage(java.lang.String msg)
Queues a message to be sent


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 void invokeListeners(java.lang.String r)
Invokes all listeners with an assciated command

Parameters:
r - a string that may specify a command (possibly one of several) associated with the event

handleMessage

protected void handleMessage(java.lang.String msg)
Invokes all listeners with an assciated command. Same as calling invokeListeners

Parameters:
msg - a string that may specify a command (possibly one of several) associated with the event
See Also:
invokeListeners(java.lang.String)

main

public static void main(java.lang.String[] argv)
Main entry point for testing/debugging

Parameters:
argv - Comand-line arguments