Package net.sf.freecol.server.networking
Class Server
- java.lang.Object
-
- java.lang.Thread
-
- 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 likesendToAll
are kept.
When a new client connects to the server a newConnection
is made, withUserConnectionHandler
as the control object.- See Also:
net.sf.freecol.common.networking
-
-
Field Summary
Fields Modifier and Type Field Description private static int
BACKLOG_DEFAULT
Backlog for socket.private java.util.HashMap<java.net.Socket,Connection>
connections
A map of Connection objects, keyed by their Socket.private FreeColServer
freeColServer
The owner of thisServer
.private java.lang.String
host
The name of the host for the public socket.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.
-
Constructor Summary
Constructors Constructor Description Server(FreeColServer freeColServer, java.lang.String host, int port)
Creates a new network server.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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 aConnection
identified by aSocket
.java.lang.String
getHost()
Gets the host that is being used for the public socket.int
getPort()
Gets the TCP port that is being used for the public socket.void
removeConnection(Connection connection)
Removes the given connection.void
run()
Start the thread processing.void
sendToAll(Message message)
Sends a network message to all connections.void
sendToAll(Message message, Connection exceptConnection)
Sends a network message to all connections with an optional exception.void
setCommsLogging(boolean log)
Set the logging state for all connections.void
setMessageHandlerToAllConnections(MessageHandler mh)
Sets the specifiedMessageHandler
to all connections.void
shutdown()
Shuts down the server thread.-
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
-
BACKLOG_DEFAULT
private static final int BACKLOG_DEFAULT
Backlog for socket.- See Also:
- Constant Field Values
-
serverSocket
private final java.net.ServerSocket serverSocket
The public "well-known" socket to which clients may connect.
-
connections
private final java.util.HashMap<java.net.Socket,Connection> connections
A map of Connection objects, keyed by their Socket.
-
running
private boolean running
Whether to keep running the main loop that is awaiting new client connections.
-
freeColServer
private final FreeColServer freeColServer
The owner of thisServer
.
-
host
private final java.lang.String host
The name of the host for the public socket.
-
port
private final 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, java.lang.String host, int port) throws java.io.IOException
Creates a new network server. Useserver.start()
to start listening for new connections.- Parameters:
freeColServer
- The owner of thisServer
.host
- The name of the host for the public socket.port
- The TCP port to use for the public socket.- Throws:
java.io.IOException
- if the public socket cannot be created.
-
-
Method Detail
-
getHost
public java.lang.String getHost()
Gets the host that is being used for the public socket.- Returns:
- The name of the host.
-
getPort
public int getPort()
Gets the TCP port that is being used for the public socket.- Returns:
- The TCP port.
-
getConnection
public Connection getConnection(java.net.Socket socket)
Gets aConnection
identified by aSocket
.- Parameters:
socket
- TheSocket
that identifies theConnection
- 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.
-
setMessageHandlerToAllConnections
public void setMessageHandlerToAllConnections(MessageHandler mh)
Sets the specifiedMessageHandler
to all connections.- Parameters:
mh
- TheMessageHandler
to use.
-
sendToAll
public void sendToAll(Message message, Connection exceptConnection)
Sends a network message to all connections with an optional exception.- Parameters:
message
- TheMessage
to send.exceptConnection
- An optionalConnection
not to send to.
-
setCommsLogging
public void setCommsLogging(boolean log)
Set the logging state for all connections.- Parameters:
log
- If true, enable logging.
-
sendToAll
public void sendToAll(Message message)
Sends a network message to all connections.- Parameters:
message
- TheMessage
to send.
-
run
public void run()
Start the thread processing. Contains the loop that is waiting for new connections to the public socket. When a new client connects to the server a newConnection
is made, withUserConnectionHandler
as the control object.- Specified by:
run
in interfacejava.lang.Runnable
- Overrides:
run
in classjava.lang.Thread
-
shutdown
public void shutdown()
Shuts down the server thread.
-
-