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.Thread
The thread that checks for incoming messages.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ReceivingThread.QuestionThread
A class to handle questions.private static class
ReceivingThread.UpdateThread
-
Field Summary
Fields Modifier and Type Field Description private Connection
connection
The connection to receive on.private static java.util.logging.Logger
logger
private static int
MAXIMUM_RETRIES
Maximum number of retries before closing the connection.private int
nextNetworkReplyId
A counter for reply ids.private boolean
shouldRun
Whether the thread should run.private java.util.Map<java.lang.Integer,NetworkReplyObject>
waitingThreads
A 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 void
askToStop(java.lang.String reason)
Ask this thread to stop work.private void
disconnect()
Disconnects this thread.int
getNextNetworkReplyId()
Gets the next network reply identifier that will be used when identifing a network message.private void
listen()
Listens to the InputStream and calls the message handler for each message received.private java.lang.Thread
messageQuestion(QuestionMessage qm, int replyId)
Create a thread to handle an incoming question message.private java.lang.Thread
messageUpdate(Message message)
Create a thread to handle an incoming ordinary message.void
run()
private boolean
shouldRun()
Checks if this thread should run.private boolean
stopRun()
Set the shouldRun state to false.private boolean
stopThread()
Stop this thread.NetworkReplyObject
waitForNetworkReply(int networkReplyId)
Creates and registers a newNetworkReplyObject
with 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
- TheConnection
thisReceivingThread
belongs 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 newNetworkReplyObject
with the specified object identifier.- Parameters:
networkReplyId
- The identifier of the message the calling thread should wait for.- Returns:
- The
NetworkReplyObject
containing 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
- TheQuestionMessage
to handle.replyId
- The network reply.- Returns:
- A new
Thread
to 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
- TheMessage
to handle.- Returns:
- A new
Thread
to do the work, or null if none required.
-
listen
private void listen() throws java.io.IOException, org.xml.sax.SAXException, javax.xml.stream.XMLStreamException
Listens 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:
run
in interfacejava.lang.Runnable
- Overrides:
run
in classjava.lang.Thread
-
-