|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.lang.Thread | +--gausssoft.net.SocketConnector
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.
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 |
public static final int UNSTARTED
state()
public static final int RUNNING
state()
public static final int SOCKET
state()
public static final int IOE
state()
public static final int UNKNOWN
state()
Constructor Detail |
public SocketConnector(ServerSocket server)
server
- The ServerSocket to use.NullPointerException
- If server
is null
.public SocketConnector(ServerSocket server, ThreadGroup group)
The resulting object will use the name "SocketConnector".
server
- The ServerSocket to use.group
- The ThreadGroup to belong to.NullPointerException
- If either parameter is null
.public SocketConnector(ServerSocket server, ThreadGroup group, String name)
server
- The ServerSocket to use.group
- The ThreadGroup to belong to.name
- The name for this thread.NullPointerException
- If any parameter is null
.Method Detail |
public void run()
run
in class Thread
Thread.start()
public final Socket getSocket()
null
.public final IOException getIOE()
null
.public final Socket get() throws IOException
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.
IOException
- If a socket could not be created.IllegalStateException
- If this thread has not been started yet.public final void waitForResult()
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.
IllegalStateException
- If this thread has not been started yet.public final ServerSocket getServerSocket()
public final boolean started()
true
if this object has been started.public final boolean hasSocket()
true
if this object is storing a socket.true
if this object is storing a socket.public final boolean hasIOE()
true
if this object is storing an IOException.true
if this object is storing an IOException.public final boolean hasResult()
true
if this object is storing either a socket or
IOException.true
if this object is storing either a socket or IOException.public int state()
UNKNOWN
,
UNSTARTED
,
RUNNING
,
SOCKET
,
IOE
protected void update()
This implementation does nothing.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |