net.sf.freecol.common.networking
Class ReceivingThread

java.lang.Object
  extended by java.lang.Thread
      extended by 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
(package private)  class ReceivingThread.FreeColNetworkInputStream
          Input stream for buffering the data from the network.
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
private  Connection connection
           
private  ReceivingThread.FreeColNetworkInputStream in
           
private  boolean locked
           
private static java.util.logging.Logger logger
           
private static int MAXIMUM_RETRIES
          Maximum number og retries before closing the connection.
private  int nextNetworkReplyId
           
private  boolean shouldRun
           
private  java.util.Map<java.lang.Integer,NetworkReplyObject> threadsWaitingForNetworkReply
           
private  javax.xml.stream.XMLStreamReader xmlIn
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ReceivingThread(Connection connection, java.io.InputStream in, java.lang.String threadName)
          The constructor to use.
 
Method Summary
(package private)  void askToStop()
          Tells this thread that it doesn't need to do any more work.
private  void disconnect()
           
 int getNextNetworkReplyId()
          Gets the next networkReplyId that will be used when identifing a network message.
private  void listen()
          Listens to the inputstream and calls the messagehandler for each message received.
 void run()
          Receives messages from the network in a loop.
private  boolean shouldRun()
          Checks if this thread has been halted.
 void unlock()
           
 NetworkReplyObject waitForNetworkReply(int networkReplyId)
          Creates and registers a new NetworkReplyObject with the specified ID.
 NetworkReplyObject waitForStreamedNetworkReply(int networkReplyId)
          Creates and registers a new NetworkReplyObject with the specified ID.
 
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
 

Field Detail

logger

private static final java.util.logging.Logger logger

MAXIMUM_RETRIES

private static final int MAXIMUM_RETRIES
Maximum number og retries before closing the connection.

See Also:
Constant Field Values

in

private final ReceivingThread.FreeColNetworkInputStream in

xmlIn

private javax.xml.stream.XMLStreamReader xmlIn

shouldRun

private boolean shouldRun

nextNetworkReplyId

private int nextNetworkReplyId

threadsWaitingForNetworkReply

private final java.util.Map<java.lang.Integer,NetworkReplyObject> threadsWaitingForNetworkReply

connection

private final Connection connection

locked

private boolean locked
Constructor Detail

ReceivingThread

ReceivingThread(Connection connection,
                java.io.InputStream in,
                java.lang.String threadName)
The constructor to use.

Parameters:
connection - The Connection this ReceivingThread belongs to.
in - The stream to read from.
Method Detail

getNextNetworkReplyId

public int getNextNetworkReplyId()
Gets the next networkReplyId that will be used when identifing a network message.

Returns:
The next available networkReplyId.

waitForNetworkReply

public NetworkReplyObject waitForNetworkReply(int networkReplyId)
Creates and registers a new NetworkReplyObject with the specified ID.

Parameters:
networkReplyId - The id of the message the calling thread should wait for.
Returns:
The NetworkReplyObject containing the network message.

waitForStreamedNetworkReply

public NetworkReplyObject waitForStreamedNetworkReply(int networkReplyId)
Creates and registers a new NetworkReplyObject with the specified ID.

Parameters:
networkReplyId - The id of the message the calling thread should wait for.
Returns:
The NetworkReplyObject containing the network message.

run

public void run()
Receives messages from the network in a loop. This method is invoked when the thread starts and the thread will stop when this method returns.

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

unlock

public void unlock()

listen

private void listen()
             throws java.io.IOException,
                    org.xml.sax.SAXException,
                    javax.xml.stream.XMLStreamException
Listens to the inputstream and calls the messagehandler for each message received.

Throws:
java.io.IOException - If thrown by the ReceivingThread.FreeColNetworkInputStream.
org.xml.sax.SAXException - if a problem occured during parsing.
javax.xml.stream.XMLStreamException - if a problem occured during parsing.

shouldRun

private boolean shouldRun()
Checks if this thread has been halted.


askToStop

void askToStop()
Tells this thread that it doesn't need to do any more work.


disconnect

private void disconnect()