net.sf.freecol.server.networking
Class Server

java.lang.Object
  extended by java.lang.Thread
      extended by net.sf.freecol.server.networking.Server
All Implemented Interfaces:
java.lang.Runnable

public final class Server
extends java.lang.Thread

The networking server in which new clients can connect and methods like sendToAll are kept.

When a new client connects to the server a new Connection is made, with UserConnectionHandler as the control object.

See Also:
net.sf.freecol.common.networking

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
private  java.util.HashMap<java.net.Socket,Connection> connections
          A hash of Connection objects, keyed by the Socket they relate to.
private  FreeColServer freeColServer
          The owner of this Server.
private static java.util.logging.Logger logger
           
private  int port
          The TCP port that is beeing used for the public socket.
private  boolean running
          Whether to keep running the main loop that is awaiting new client connections.
private  java.net.ServerSocket serverSocket
          The public "well-known" socket to which clients may connect.
private  java.lang.Object shutdownLock
          For information about this variable see the run method.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Server(FreeColServer freeColServer, int port)
          Creates a new network server.
 
Method Summary
 void addConnection(Connection connection)
          Adds a Connection into the hashmap.
 void addDummyConnection(Connection connection)
          Adds a (usually Dummy)Connection into the hashmap.
 Connection getConnection(java.net.Socket socket)
          Gets a Connection identified by a Socket.
 java.util.Iterator<Connection> getConnectionIterator()
          Gets an iterator of every connection to this server.
 int getPort()
          Gets the TCP port that is beeing used for the public socket.
 void removeConnection(Connection connection)
          Removes the given connection.
 void run()
          Starts the thread's processing.
 void sendToAll(org.w3c.dom.Element element)
          Sends a network message to all connections.
 void sendToAll(org.w3c.dom.Element element, Connection exceptConnection)
          Sends a network message to all connections except exceptConnection (if the argument is non-null).
 void setMessageHandlerToAllConnections(MessageHandler messageHandler)
          Sets the specified MessageHandler to all connections.
 void shutdown()
          Shuts down the server thread.
 
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 java.util.logging.Logger logger

serverSocket

private java.net.ServerSocket serverSocket
The public "well-known" socket to which clients may connect.


connections

private java.util.HashMap<java.net.Socket,Connection> connections
A hash of Connection objects, keyed by the Socket they relate to.


running

private boolean running
Whether to keep running the main loop that is awaiting new client connections.


freeColServer

private FreeColServer freeColServer
The owner of this Server.


port

private int port
The TCP port that is beeing used for the public socket.


shutdownLock

private final java.lang.Object shutdownLock
For information about this variable see the run method.

Constructor Detail

Server

public Server(FreeColServer freeColServer,
              int port)
       throws java.io.IOException
Creates a new network server. Use server.start() to start listening for new connections.

Parameters:
freeColServer - The owner of this Server.
port - The TCP port to use for the public socket.
Throws:
java.io.IOException - if the public socket cannot be created.
Method Detail

run

public void run()
Starts the thread's processing. Contains the loop that is waiting for new connections to the public socket. When a new client connects to the server a new Connection is made, with UserConnectionHandler as the control object.

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

sendToAll

public void sendToAll(org.w3c.dom.Element element,
                      Connection exceptConnection)
Sends a network message to all connections except exceptConnection (if the argument is non-null).

Parameters:
element - The root element of the message to send.
exceptConnection - If non-null, the Connection not to send to.

sendToAll

public void sendToAll(org.w3c.dom.Element element)
Sends a network message to all connections.

Parameters:
element - The root element of the message to send.

getPort

public int getPort()
Gets the TCP port that is beeing used for the public socket.

Returns:
The TCP port.

setMessageHandlerToAllConnections

public void setMessageHandlerToAllConnections(MessageHandler messageHandler)
Sets the specified MessageHandler to all connections.

Parameters:
messageHandler - The MessageHandler.

getConnectionIterator

public java.util.Iterator<Connection> getConnectionIterator()
Gets an iterator of every connection to this server.

Returns:
The Iterator.
See Also:
Connection

shutdown

public void shutdown()
Shuts down the server thread.


getConnection

public Connection getConnection(java.net.Socket socket)
Gets a Connection identified by a Socket.

Parameters:
socket - The Socket that identifies the Connection
Returns:
The Connection.

addDummyConnection

public void addDummyConnection(Connection connection)
Adds a (usually Dummy)Connection into the hashmap.

Parameters:
connection - The connection to add.

addConnection

public void addConnection(Connection connection)
Adds a Connection into the hashmap.

Parameters:
connection - The connection to add.

removeConnection

public void removeConnection(Connection connection)
Removes the given connection.

Parameters:
connection - The connection that should be removed.