gausssoft.net
Class SocketConnector

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--gausssoft.net.SocketConnector
All Implemented Interfaces:
Runnable

public class SocketConnector
extends Thread

This class is a simple class which extracts a Socket from a ServerSocket.

Even though this class is extremely simple, many applications which use ServerSockets normally create a class that uses a thread, which will take a ServerSocket and then invoke the ServerSocket.accept() method. This class exists mainly to just be useful and preventing people writing code like this again and again.

You must invoke the Thread.start() method before the SocketConnector attempts to retrieve a socket. The update() method is invoked whenever a socket is created (or an IOException has been thrown), so subclasses may override this method for whatever purpose they wish (for example, sending a message to another object indicating that a socket has been created).

Whenever a socket is created (or an IOException is thrown), it is stored internally inside the object and is retrievable via the get methods.

Since:
GSDK 1.1
Author:
Allan Crooks
See Also:
ServerSocket, Socket

Field Summary
static int IOE
          State identifier, used for indicating that this object is holding an IOException.
static int RUNNING
          State identifier, used for indicating that this object is running.
static int SOCKET
          State identifier, used for indicating that this object is holding a socket.
static int UNKNOWN
          State identifier, used for indicating that this object is not sure what state it is in.
static int UNSTARTED
          State identifier, used for indicating that this object hasn't started yet.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SocketConnector(ServerSocket server)
          Creates a new SocketConnector using the specified ServerSocket.
SocketConnector(ServerSocket server, ThreadGroup group)
          Creates a new SocketConnector existing as part of the specified ThreadGroup, using the specified ServerSocket.
SocketConnector(ServerSocket server, ThreadGroup group, String name)
          Creates a new SocketConnector existing as part of the specified ThreadGroup, using the specified ServerSocket and specified name.
 
Method Summary
 Socket get()
          Returns the socket when created.
 IOException getIOE()
          Returns the IOException held by this object.
 ServerSocket getServerSocket()
          Returns the ServerSocket used by this object.
 Socket getSocket()
          Returns the socket held by this object.
 boolean hasIOE()
          Indicates true if this object is storing an IOException.
 boolean hasResult()
          Indicates true if this object is storing either a socket or IOException.
 boolean hasSocket()
          Indicates true if this object is storing a socket.
 void run()
          Causes this object to create a socket from the internal ServerSocket.
 boolean started()
          Indicates if this object has been started yet.
 int state()
          Returns the state the thread is currently in.
protected  void update()
          This method is invoked whenever either a socket has been created or an IOException has been thrown.
 void waitForResult()
          This method pauses and only returns when either a socket or IOException has been created.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

UNSTARTED

public static final int UNSTARTED
State identifier, used for indicating that this object hasn't started yet.
See Also:
state()

RUNNING

public static final int RUNNING
State identifier, used for indicating that this object is running.
See Also:
state()

SOCKET

public static final int SOCKET
State identifier, used for indicating that this object is holding a socket.
See Also:
state()

IOE

public static final int IOE
State identifier, used for indicating that this object is holding an IOException.
See Also:
state()

UNKNOWN

public static final int UNKNOWN
State identifier, used for indicating that this object is not sure what state it is in.
See Also:
state()
Constructor Detail

SocketConnector

public SocketConnector(ServerSocket server)
Creates a new SocketConnector using the specified ServerSocket.
Parameters:
server - The ServerSocket to use.
Throws:
NullPointerException - If server is null.

SocketConnector

public SocketConnector(ServerSocket server,
                       ThreadGroup group)
Creates a new SocketConnector existing as part of the specified ThreadGroup, using the specified ServerSocket.

The resulting object will use the name "SocketConnector".

Parameters:
server - The ServerSocket to use.
group - The ThreadGroup to belong to.
Throws:
NullPointerException - If either parameter is null.

SocketConnector

public SocketConnector(ServerSocket server,
                       ThreadGroup group,
                       String name)
Creates a new SocketConnector existing as part of the specified ThreadGroup, using the specified ServerSocket and specified name.
Parameters:
server - The ServerSocket to use.
group - The ThreadGroup to belong to.
name - The name for this thread.
Throws:
NullPointerException - If any parameter is null.
Method Detail

run

public void run()
Causes this object to create a socket from the internal ServerSocket.
Overrides:
run in class Thread
See Also:
Thread.start()

getSocket

public final Socket getSocket()
Returns the socket held by this object. If no socket is stored in this object, it will return null.
Returns:
The socket held in this object.

getIOE

public final IOException getIOE()
Returns the IOException held by this object. If no IOException is stored in this object, it will return null.
Returns:
The IOException held in this object.

get

public final Socket get()
                 throws IOException
Returns the socket when created. If it is not retrieved, an IOException is thrown. This is the equivalent to calling ServerSocket.accept().

This method will pause until either a socket is returned or an IOException is thrown.

Note: This method calls waitForResult() and it is suggested you read the documentation for that method before invoking this one.

Returns:
The newly created socket.
Throws:
IOException - If a socket could not be created.
IllegalStateException - If this thread has not been started yet.

waitForResult

public final void waitForResult()
This method pauses and only returns when either a socket or IOException has been created.

If this method is invoked before the thread is started, it will throw an IllegalStateException.

Note: When this method is invoked, any attempts to interrupt this thread will be ignored. If you need the thread to respond to interrupts, it is suggested you use the Thread.join() method.

Throws:
IllegalStateException - If this thread has not been started yet.

getServerSocket

public final ServerSocket getServerSocket()
Returns the ServerSocket used by this object.
Returns:
The ServerSocket used by this object.

started

public final boolean started()
Indicates if this object has been started yet. This object is considered to have started if it is currently trying to extract a socket, or a socket (or IOException) is being stored.
Returns:
true if this object has been started.

hasSocket

public final boolean hasSocket()
Indicates true if this object is storing a socket.
Returns:
true if this object is storing a socket.

hasIOE

public final boolean hasIOE()
Indicates true if this object is storing an IOException.
Returns:
true if this object is storing an IOException.

hasResult

public final boolean hasResult()
Indicates true if this object is storing either a socket or IOException.
Returns:
true if this object is storing either a socket or IOException.

state

public int state()
Returns the state the thread is currently in. This matches the static identifiers of this class.
Returns:
An integer defining which state the object is in.
See Also:
UNKNOWN, UNSTARTED, RUNNING, SOCKET, IOE

update

protected void update()
This method is invoked whenever either a socket has been created or an IOException has been thrown. This is for subclasses that wish to specify some form of behaviour after a result has been generated, for example, notifying some object that a result now exists.

This implementation does nothing.