Package net.sf.freecol.common.networking
Class ReceivingThread
- java.lang.Object
-
- java.lang.Thread
-
- net.sf.freecol.common.networking.ReceivingThread
-
- All Implemented Interfaces:
java.lang.Runnable
final class ReceivingThread extends java.lang.ThreadThe thread that checks for incoming messages.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classReceivingThread.QuestionThreadA class to handle questions.private static classReceivingThread.UpdateThread
-
Field Summary
Fields Modifier and Type Field Description private ConnectionconnectionThe connection to receive on.private static java.util.logging.Loggerloggerprivate static intMAXIMUM_RETRIESMaximum number of retries before closing the connection.private intnextNetworkReplyIdA counter for reply ids.private booleanshouldRunWhether the thread should run.private java.util.Map<java.lang.Integer,NetworkReplyObject>waitingThreadsA map of network ids to the corresponding waiting thread.
-
Constructor Summary
Constructors Constructor Description ReceivingThread(Connection connection, java.lang.String threadName)The constructor to use.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaskToStop(java.lang.String reason)Ask this thread to stop work.private voiddisconnect()Disconnects this thread.intgetNextNetworkReplyId()Gets the next network reply identifier that will be used when identifing a network message.private voidlisten()Listens to the InputStream and calls the message handler for each message received.private java.lang.ThreadmessageQuestion(QuestionMessage qm, int replyId)Create a thread to handle an incoming question message.private java.lang.ThreadmessageUpdate(Message message)Create a thread to handle an incoming ordinary message.voidrun()private booleanshouldRun()Checks if this thread should run.private booleanstopRun()Set the shouldRun state to false.private booleanstopThread()Stop this thread.NetworkReplyObjectwaitForNetworkReply(int networkReplyId)Creates and registers a newNetworkReplyObjectwith the specified object identifier.-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
MAXIMUM_RETRIES
private static final int MAXIMUM_RETRIES
Maximum number of retries before closing the connection.- See Also:
- Constant Field Values
-
waitingThreads
private final java.util.Map<java.lang.Integer,NetworkReplyObject> waitingThreads
A map of network ids to the corresponding waiting thread.
-
connection
private final Connection connection
The connection to receive on.
-
shouldRun
private boolean shouldRun
Whether the thread should run.
-
nextNetworkReplyId
private int nextNetworkReplyId
A counter for reply ids.
-
-
Constructor Detail
-
ReceivingThread
public ReceivingThread(Connection connection, java.lang.String threadName)
The constructor to use.- Parameters:
connection- TheConnectionthisReceivingThreadbelongs to.threadName- The base name for the thread.
-
-
Method Detail
-
getNextNetworkReplyId
public int getNextNetworkReplyId()
Gets the next network reply identifier that will be used when identifing a network message.- Returns:
- The next available network reply identifier.
-
waitForNetworkReply
public NetworkReplyObject waitForNetworkReply(int networkReplyId)
Creates and registers a newNetworkReplyObjectwith the specified object identifier.- Parameters:
networkReplyId- The identifier of the message the calling thread should wait for.- Returns:
- The
NetworkReplyObjectcontaining the network message.
-
shouldRun
private boolean shouldRun()
Checks if this thread should run.- Returns:
- True if the thread should run.
-
stopRun
private boolean stopRun()
Set the shouldRun state to false.- Returns:
- The old value of shouldRun.
-
stopThread
private boolean stopThread()
Stop this thread.- Returns:
- True if the thread was previously running and is now stopped.
-
askToStop
public void askToStop(java.lang.String reason)
Ask this thread to stop work.- Parameters:
reason- A brief description of why the thread should stop.
-
disconnect
private void disconnect()
Disconnects this thread.
-
messageQuestion
private java.lang.Thread messageQuestion(QuestionMessage qm, int replyId)
Create a thread to handle an incoming question message.- Parameters:
qm- TheQuestionMessageto handle.replyId- The network reply.- Returns:
- A new
Threadto do the work, or null if none required.
-
messageUpdate
private java.lang.Thread messageUpdate(Message message)
Create a thread to handle an incoming ordinary message.- Parameters:
message- TheMessageto handle.- Returns:
- A new
Threadto do the work, or null if none required.
-
listen
private void listen() throws java.io.IOException, org.xml.sax.SAXException, javax.xml.stream.XMLStreamExceptionListens to the InputStream and calls the message handler for each message received.- Throws:
java.io.IOException- on low level IO problems.org.xml.sax.SAXException- if a problem occured during parsing.javax.xml.stream.XMLStreamException- if a problem occured during parsing.
-
run
public void run()
- Specified by:
runin interfacejava.lang.Runnable- Overrides:
runin classjava.lang.Thread
-
-