Package net.sf.freecol.server
Class FreeColServer
- java.lang.Object
-
- net.sf.freecol.server.FreeColServer
-
public final class FreeColServer extends java.lang.Object
The main control class for the FreeCol server. This class both starts and keeps references to all of the server objects and the game model objects. If you would like to start a new server you just create a new object of this class.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FreeColServer.ServerState
The server is either starting, loading, being played, or ending.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ACTIVE_UNIT_TAG
private AIMain
aiMain
The AI controller.static java.lang.String
DEBUG_TAG
static java.lang.String
DEFAULT_SPEC
The ruleset to use when loading old format games where a spec may not be readily available.private InGameController
inGameController
The in-game controller.private ServerInputHandler
inputHandler
The input handler.private Constants.IntegrityType
integrity
The game integrity state.private static java.util.logging.Logger
logger
static java.lang.String
MAP_EDITOR_NAME
The name to use as the owner of maps that have been edited by the map editor.private MapGenerator
mapGenerator
The map generator.static int
MINIMUM_SAVEGAME_VERSION
The oldest save game format that can still be loaded.private java.lang.String
name
The name of this server.static java.lang.String
OLD_SERVER_OBJECTS_TAG
static java.lang.String
OWNER_TAG
private PreGameController
preGameController
The pre-game controller.static java.lang.String
PUBLIC_SERVER_TAG
private boolean
publicServer
Should this game be listed on the meta-server?private java.util.Random
random
The internal provider of random numbers.static java.lang.String
RANDOM_STATE_TAG
static java.lang.String
SAVED_GAME_TAG
static int
SAVEGAME_VERSION
The save game format used for saving games.private Server
server
The underlying interface to the network.private ServerGame
serverGame
The game underway.private FreeColServer.ServerState
serverState
The current state of the game.static java.lang.String
SINGLE_PLAYER_TAG
private boolean
singlePlayer
Is this a single player game?private UserConnectionHandler
userConnectionHandler
The handler for new user connections.
-
Constructor Summary
Constructors Modifier Constructor Description FreeColServer(boolean publicServer, boolean singlePlayer, Specification specification, java.net.InetAddress address, int port, java.lang.String name)
Starts a new server, with a new game.private
FreeColServer(java.lang.String name, java.net.InetAddress address, int port)
Base constructor common to the following new-game and saved-game constructors.FreeColServer(FreeColSavegameFile savegame, Specification specification, java.net.InetAddress address, int port, java.lang.String name)
Starts a new networked server, initializing from a saved game.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addAIConnection(Player aiPlayer)
Establish the connections for an AI player.void
addNewUserConnection(java.net.Socket socket)
Add a new user connection.void
addPlayerConnection(Connection connection)
Add player connection.private Game
buildGame()
Builds a new game using the parameters that exist in the game as it stands.private FreeColServer.ServerState
changeServerState(FreeColServer.ServerState serverState)
Change the server state.private Server
createServer(java.net.InetAddress address, int firstPort)
Create a Server at port.void
endGame()
The game is ending, tell all the non-admin players to quit.void
exploreMapForAllPlayers(boolean reveal)
Reveals or hides the entire map for all players.Map
generateEmptyMap(int width, int height)
Generate an empty map in this server/game.Map
generateMap(boolean generateEuropeanPlayerUnits)
Generate a new map in this server/game.AIMain
getAIMain()
Gets the main AI-object.AIPlayer
getAIPlayer(Player player)
Gets the AI player corresponding to a given player.Controller
getController()
Gets theController
.ServerGame
getGame()
Gets theGame
that is being played.java.lang.String
getHost()
Gets the host this FreeColServer was started on.InGameController
getInGameController()
Gets the controller being used while the game is running.ServerInputHandler
getInputHandler()
Get the current input handler.Constants.IntegrityType
getIntegrity()
Gets the integrity check result.MapGenerator
getMapGenerator()
Get the map generator.java.lang.String
getName()
Gets the name of this server.ServerPlayer
getPlayer(Connection conn)
Get aServerPlayer
by connection.int
getPort()
Gets the port this FreeColServer was started on.PreGameController
getPreGameController()
Gets thePreGameController
.boolean
getPublicServer()
Get the public server state.Server
getServer()
Gets the network server responsible of handling the connections.private ServerInfo
getServerInfo()
Create aServerInfo
record for this server and connection.java.util.Random
getServerRandom()
Gets the server random number generator.FreeColServer.ServerState
getServerState()
Gets the current state of the server.boolean
getSinglePlayer()
Is the user playing in single player mode?Specification
getSpecification()
Gets the specification from the game run by this server.UserConnectionHandler
getUserConnectionHandler()
Gets theUserConnectionHandler
.ServerGame
loadGame(FreeColSavegameFile fis)
Loads a game.private ServerGame
loadGame(FreeColSavegameFile fis, Specification specification)
Loads a game.ServerPlayer
makeAIPlayer(Nation nation)
Make a new AI player and add it to the game.private static ServerGame
readGame(java.io.File file, Specification spec, FreeColServer freeColServer)
Read just the game part from a file.static ServerGame
readGame(FreeColSavegameFile fis, Specification specification, FreeColServer freeColServer)
Reads just the game part from a save game from a stream.static Map
readMap(java.io.File file, Specification spec)
Read just the map part from a file.boolean
registerWithMetaServer()
Register this FreeColServer with the meta-server.boolean
removeFromMetaServer()
Removes this server from the meta-server.void
removePlayerConnection(Player player)
Remove a player connection.private void
saveGame(java.io.File file, java.lang.String owner, OptionGroup options, Unit active, java.awt.image.BufferedImage image)
Saves a game.void
saveGame(java.io.File file, OptionGroup options, Unit active)
Saves a normal (non-map-editor) game.void
saveMapEditorGame(java.io.File file, java.awt.image.BufferedImage image)
Save a game from the map editor.void
sendToAll(Message msg, Player player)
Send a message to all connections.private void
sendToAll(Message msg, Connection conn)
Send a message to all connections.void
setAIMain(AIMain aiMain)
Sets the main AI-object.void
setGame(ServerGame serverGame)
Sets theGame
that is being played.void
setMapGenerator(MapGenerator mapGenerator)
Set the map generator.void
setName(java.lang.String name)
Sets the name of this server.void
setPublicServer(boolean publicServer)
Sets the public server state.void
setServerRandom(java.util.Random random)
Sets the server random number generator.void
setSinglePlayer(boolean singlePlayer)
Sets the single/multiplayer state of the game.void
shutdown()
Shut down this FreeColServer.void
startGame()
Start the game.boolean
updateMetaServer()
Update this FreeColServer with the meta-server.ServerGame
waitForGame()
Wait until the game has been created.
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
MAP_EDITOR_NAME
public static final java.lang.String MAP_EDITOR_NAME
The name to use as the owner of maps that have been edited by the map editor. This is used so we can tell when a user tries to start a map (which may be lacking vital pieces) as a game. Make sure all the maps in data/maps have this owner. TODO: Make loading a map as a game work.- See Also:
- Constant Field Values
-
ACTIVE_UNIT_TAG
public static final java.lang.String ACTIVE_UNIT_TAG
- See Also:
- Constant Field Values
-
OLD_SERVER_OBJECTS_TAG
public static final java.lang.String OLD_SERVER_OBJECTS_TAG
- See Also:
- Constant Field Values
-
DEBUG_TAG
public static final java.lang.String DEBUG_TAG
- See Also:
- Constant Field Values
-
RANDOM_STATE_TAG
public static final java.lang.String RANDOM_STATE_TAG
- See Also:
- Constant Field Values
-
OWNER_TAG
public static final java.lang.String OWNER_TAG
- See Also:
- Constant Field Values
-
PUBLIC_SERVER_TAG
public static final java.lang.String PUBLIC_SERVER_TAG
- See Also:
- Constant Field Values
-
SAVED_GAME_TAG
public static final java.lang.String SAVED_GAME_TAG
- See Also:
- Constant Field Values
-
SINGLE_PLAYER_TAG
public static final java.lang.String SINGLE_PLAYER_TAG
- See Also:
- Constant Field Values
-
SAVEGAME_VERSION
public static final int SAVEGAME_VERSION
The save game format used for saving games. Version 7-10 were used in 0.9.x. Version 11 made a lot of changes and was introduced for 0.10.0. Version 12 was introduced with HighSeas post-0.10.1. Version 13 coincides with the start of the 0.11.x series. Version 14 coincides with the start of the 0.12.x series. Please add to this comment if you increase the version. These days we *try* to change this only for (semi-)major releases (i.e. when x changes in 0.x.y).- See Also:
- Constant Field Values
-
MINIMUM_SAVEGAME_VERSION
public static final int MINIMUM_SAVEGAME_VERSION
The oldest save game format that can still be loaded. The promise is that FreeCol 0.n.* can load 0.(n-1).* games. Revisit the numbering scheme and save compatibility promise when 1.0 is released?- See Also:
- Constant Field Values
-
DEFAULT_SPEC
public static final java.lang.String DEFAULT_SPEC
The ruleset to use when loading old format games where a spec may not be readily available.- See Also:
- Constant Field Values
-
name
private java.lang.String name
The name of this server.
-
publicServer
private boolean publicServer
Should this game be listed on the meta-server?
-
singlePlayer
private boolean singlePlayer
Is this a single player game?
-
random
private java.util.Random random
The internal provider of random numbers.
-
serverGame
private ServerGame serverGame
The game underway.
-
server
private Server server
The underlying interface to the network.
-
serverState
private FreeColServer.ServerState serverState
The current state of the game.
-
userConnectionHandler
private final UserConnectionHandler userConnectionHandler
The handler for new user connections.
-
preGameController
private final PreGameController preGameController
The pre-game controller.
-
inGameController
private final InGameController inGameController
The in-game controller.
-
inputHandler
private final ServerInputHandler inputHandler
The input handler.
-
aiMain
private AIMain aiMain
The AI controller.
-
mapGenerator
private MapGenerator mapGenerator
The map generator.
-
integrity
private Constants.IntegrityType integrity
The game integrity state.
-
-
Constructor Detail
-
FreeColServer
private FreeColServer(java.lang.String name, java.net.InetAddress address, int port) throws java.io.IOException
Base constructor common to the following new-game and saved-game constructors.- Parameters:
name
- An optional name for the server.address
- The address to use for the public socket.port
- The TCP port to use for the public socket.- Throws:
java.io.IOException
- on failure to open the port.
-
FreeColServer
public FreeColServer(boolean publicServer, boolean singlePlayer, Specification specification, java.net.InetAddress address, int port, java.lang.String name) throws java.io.IOException
Starts a new server, with a new game.- Parameters:
publicServer
- If true, add to the meta-server.singlePlayer
- True if this is a single player game.specification
- TheSpecification
to use in this game.address
- The address to use for the public socket.port
- The TCP port to use for the public socket.name
- An optional name for the server.- Throws:
java.io.IOException
- If the public socket cannot be created.
-
FreeColServer
public FreeColServer(FreeColSavegameFile savegame, Specification specification, java.net.InetAddress address, int port, java.lang.String name) throws FreeColException, java.io.IOException, javax.xml.stream.XMLStreamException
Starts a new networked server, initializing from a saved game. The specification is usually null, which means it will be initialized by extracting it from the saved game. However MapConverter does call this with an overriding specification.- Parameters:
savegame
- The file where the game data is located.specification
- An optionalSpecification
to use.address
- The address to use for the public socket.port
- The TCP port to use for the public socket.name
- An optional name for the server.- Throws:
java.io.IOException
- If save game can not be found.FreeColException
- If the savegame could not be loaded.javax.xml.stream.XMLStreamException
- If the server comms fail.
-
-
Method Detail
-
createServer
private Server createServer(java.net.InetAddress address, int firstPort) throws java.io.IOException
Create a Server at port. If the port is specified, just try once. If the port is unspecified (negative), try multiple times.- Parameters:
address
- The address to use for the public socket.firstPort
- The port to start trying to connect at.- Returns:
- A started
Server
. - Throws:
java.io.IOException
- on failure to open the port.
-
getName
public java.lang.String getName()
Gets the name of this server.- Returns:
- The name.
-
setName
public void setName(java.lang.String name)
Sets the name of this server.- Parameters:
name
- The new name.
-
getPublicServer
public boolean getPublicServer()
Get the public server state.- Returns:
- The public server state.
-
setPublicServer
public void setPublicServer(boolean publicServer)
Sets the public server state.- Parameters:
publicServer
- The new public server state.
-
getSinglePlayer
public boolean getSinglePlayer()
Is the user playing in single player mode?- Returns:
- True if this is a single player game.
-
setSinglePlayer
public void setSinglePlayer(boolean singlePlayer)
Sets the single/multiplayer state of the game.- Parameters:
singlePlayer
- The new single/multiplayer status.
-
getServerRandom
public java.util.Random getServerRandom()
Gets the server random number generator.- Returns:
- The server random number generator.
-
setServerRandom
public void setServerRandom(java.util.Random random)
Sets the server random number generator.- Parameters:
random
- The new random number generator.
-
getGame
public ServerGame getGame()
Gets theGame
that is being played.- Returns:
- The
Game
which is the main class of the game-model being used in this game.
-
setGame
public void setGame(ServerGame serverGame)
Sets theGame
that is being played.- Parameters:
serverGame
- The newGame
.
-
getSpecification
public Specification getSpecification()
Gets the specification from the game run by this server.- Returns:
- The specification from the game.
-
getServer
public Server getServer()
Gets the network server responsible of handling the connections.- Returns:
- The network server.
-
getHost
public java.lang.String getHost()
Gets the host this FreeColServer was started on.- Returns:
- The host.
-
getPort
public int getPort()
Gets the port this FreeColServer was started on.- Returns:
- The port.
-
shutdown
public void shutdown()
Shut down this FreeColServer.
-
getServerState
public FreeColServer.ServerState getServerState()
Gets the current state of the server.- Returns:
- The current
ServerState
.
-
changeServerState
private FreeColServer.ServerState changeServerState(FreeColServer.ServerState serverState)
Change the server state.- Parameters:
serverState
- The newServerState
.- Returns:
- The old server state.
-
getUserConnectionHandler
public UserConnectionHandler getUserConnectionHandler()
Gets theUserConnectionHandler
.- Returns:
- The
UserConnectionHandler
that is used when a new client connects.
-
getController
public Controller getController()
Gets theController
.- Returns:
- The
Controller
.
-
getInputHandler
public ServerInputHandler getInputHandler()
Get the current input handler.- Returns:
- The
ServerInputHandler
for the current server state.
-
getPreGameController
public PreGameController getPreGameController()
Gets thePreGameController
.- Returns:
- The
PreGameController
.
-
getInGameController
public InGameController getInGameController()
Gets the controller being used while the game is running.- Returns:
- The controller from making a new turn etc.
-
setAIMain
public void setAIMain(AIMain aiMain)
Sets the main AI-object.- Parameters:
aiMain
- The main AI-object which is responsible for controlling, updating and saving the AI objects.
-
getAIMain
public AIMain getAIMain()
Gets the main AI-object.- Returns:
- The main AI-object which is responsible for controlling, updating and saving the AI objects.
-
getMapGenerator
public MapGenerator getMapGenerator()
Get the map generator.- Returns:
- The
MapGenerator
.
-
setMapGenerator
public void setMapGenerator(MapGenerator mapGenerator)
Set the map generator.- Parameters:
mapGenerator
- The newMapGenerator
.
-
getIntegrity
public Constants.IntegrityType getIntegrity()
Gets the integrity check result.- Returns:
- The integrity check result.
-
endGame
public void endGame()
The game is ending, tell all the non-admin players to quit.
-
addNewUserConnection
public void addNewUserConnection(java.net.Socket socket) throws FreeColException, java.io.IOException, javax.xml.stream.XMLStreamException
Add a new user connection. That is a new connection to the server that has not yet logged in as a player.- Parameters:
socket
- The clientSocket
the connection arrives on.- Throws:
FreeColException
- on extreme confusion.java.io.IOException
- if the socket was already broken.javax.xml.stream.XMLStreamException
- on stream problem.
-
addPlayerConnection
public void addPlayerConnection(Connection connection)
Add player connection. That is, a user connection is now transitioning to a player connection.- Parameters:
connection
- The newConnection
.
-
removePlayerConnection
public void removePlayerConnection(Player player)
Remove a player connection.- Parameters:
player
- ThePlayer
to disconnect.
-
addAIConnection
private void addAIConnection(Player aiPlayer)
Establish the connections for an AI player.- Parameters:
aiPlayer
- The AIPlayer
to connect.
-
waitForGame
public ServerGame waitForGame()
Wait until the game has been created. 2017: This should not be needed, but just occasionally the client and server threads conspire to race in a way that causes a hang. Check this again in due course.- Returns:
- The
ServerGame
, or null if the wait times out.
-
startGame
public void startGame() throws FreeColException
Start the game. Called from PreGameController following a requestLaunch message (or from the test suite).- Creates the game.
- Sends updated game information to the clients.
- Changes the game state to
FreeColServer.ServerState.IN_GAME
. - Sends the "startGame"-message to the clients.
- Switches to using the in-game version of the input handler.
- Throws:
FreeColException
- if there is a problem creating the game.
-
sendToAll
private void sendToAll(Message msg, Connection conn)
Send a message to all connections.- Parameters:
msg
- TheMessage
to send.conn
- An optionalConnection
to omit.
-
sendToAll
public void sendToAll(Message msg, Player player)
Send a message to all connections.- Parameters:
msg
- TheMessage
to send.player
- An optionalPlayer
to omit.
-
saveMapEditorGame
public void saveMapEditorGame(java.io.File file, java.awt.image.BufferedImage image) throws java.io.IOException
Save a game from the map editor.- Parameters:
file
- The file where the data will be written.image
- A thumbnail image for the map.- Throws:
java.io.IOException
- If a problem was encountered while trying to open, write or close the file.
-
saveGame
public void saveGame(java.io.File file, OptionGroup options, Unit active) throws java.io.IOException
Saves a normal (non-map-editor) game.- Parameters:
file
- The file where the data will be written.options
- The client options to save.active
- An optional activeUnit
.- Throws:
java.io.IOException
- If a problem was encountered while trying to open, write or close the file.
-
saveGame
private void saveGame(java.io.File file, java.lang.String owner, OptionGroup options, Unit active, java.awt.image.BufferedImage image) throws java.io.IOException
Saves a game.- Parameters:
file
- The file where the data will be written.owner
- An optional name to use as the owner of the game.options
- Optional client options to save in the game.active
- An optional activeUnit
.image
- A thumbnailImage
value to save in the game.- Throws:
java.io.IOException
- If a problem was encountered while trying to open, write or close the file.
-
loadGame
public ServerGame loadGame(FreeColSavegameFile fis) throws java.io.IOException, FreeColException, javax.xml.stream.XMLStreamException
Loads a game.- Parameters:
fis
- The file where the game data is located.- Returns:
- The game found in the stream.
- Throws:
FreeColException
- if the savegame contains incompatible data.java.io.IOException
- if the stream can not be created.javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
readMap
public static Map readMap(java.io.File file, Specification spec) throws FreeColException, java.io.IOException, javax.xml.stream.XMLStreamException
Read just the map part from a file. When the specification is not supplied, the one found in the saved game will be used.- Parameters:
file
- TheFile
to read from.spec
- An optionalSpecification
to use.- Returns:
- The
Map
found in the stream. - Throws:
FreeColException
- if the format is incompatible.java.io.IOException
- if the stream can not be created.javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
readGame
private static ServerGame readGame(java.io.File file, Specification spec, FreeColServer freeColServer) throws FreeColException, java.io.IOException, javax.xml.stream.XMLStreamException
Read just the game part from a file. When the specification is not supplied, the one found in the saved game will be used.- Parameters:
file
- TheFile
to read from.spec
- An optionalSpecification
to use.freeColServer
- Use this (optional) server to load into.- Returns:
- The game found in the stream.
- Throws:
FreeColException
- if the format is incompatible.java.io.IOException
- if the stream can not be created.javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
readGame
public static ServerGame readGame(FreeColSavegameFile fis, Specification specification, FreeColServer freeColServer) throws FreeColException, java.io.IOException, javax.xml.stream.XMLStreamException
Reads just the game part from a save game from a stream. When the specification is not supplied, the one found in the saved game will be used.- Parameters:
fis
- The stream to read from.specification
- An optionalSpecification
to use.freeColServer
- Use this (optional) server to load into.- Returns:
- The game found in the stream.
- Throws:
FreeColException
- if the format is incompatible.java.io.IOException
- if the stream can not be created.javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
loadGame
private ServerGame loadGame(FreeColSavegameFile fis, Specification specification) throws FreeColException, java.io.IOException, javax.xml.stream.XMLStreamException
Loads a game.- Parameters:
fis
- The file where the game data is located.specification
- TheSpecification
to refer to.- Returns:
- The new game.
- Throws:
FreeColException
- if the savegame contains incompatible data.java.io.IOException
- if the stream can not be created.javax.xml.stream.XMLStreamException
- if there a problem reading the stream.
-
buildGame
private Game buildGame() throws FreeColException
Builds a new game using the parameters that exist in the game as it stands.- Returns:
- The updated
Game
. - Throws:
FreeColException
- on map generation failure.
-
generateEmptyMap
public Map generateEmptyMap(int width, int height)
Generate an empty map in this server/game. Public for the map generator.- Parameters:
width
- The map width.height
- The map height.- Returns:
- The new empty
Map
.
-
generateMap
public Map generateMap(boolean generateEuropeanPlayerUnits)
Generate a new map in this server/game.- Parameters:
generateEuropeanPlayerUnits
- Iftrue
, then the european player units are generated.- Returns:
- The
Map
that was created.
-
makeAIPlayer
public ServerPlayer makeAIPlayer(Nation nation)
Make a new AI player and add it to the game. Public so the controller can add REF players.- Parameters:
nation
- TheNation
to add.- Returns:
- The new AI
ServerPlayer
.
-
exploreMapForAllPlayers
public void exploreMapForAllPlayers(boolean reveal)
Reveals or hides the entire map for all players. Debug menu helper.- Parameters:
reveal
- If true, reveal, if false, hide.
-
getPlayer
public ServerPlayer getPlayer(Connection conn)
Get aServerPlayer
by connection.- Parameters:
conn
- TheConnection
to use while searching for aServerPlayer
.- Returns:
- The
ServerPlayer
found, or null if none.
-
getAIPlayer
public AIPlayer getAIPlayer(Player player)
Gets the AI player corresponding to a given player.- Parameters:
player
- ThePlayer
to look up.- Returns:
- The corresponding AI player, or null if not found.
-
getServerInfo
private ServerInfo getServerInfo()
Create aServerInfo
record for this server and connection.- Returns:
- A suitable record.
-
registerWithMetaServer
public boolean registerWithMetaServer()
Register this FreeColServer with the meta-server.- Returns:
- True if the meta-server was updated.
-
removeFromMetaServer
public boolean removeFromMetaServer()
Removes this server from the meta-server.- Returns:
- True if the meta-server was updated.
-
updateMetaServer
public boolean updateMetaServer()
Update this FreeColServer with the meta-server.- Returns:
- True if the meta-server was updated.
-
-