Package net.sf.freecol.server
Class FreeColServer
- java.lang.Object
-
- net.sf.freecol.server.FreeColServer
-
public final class FreeColServer extends java.lang.ObjectThe 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 classFreeColServer.ServerStateThe server is either starting, loading, being played, or ending.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringACTIVE_UNIT_TAGprivate AIMainaiMainThe AI controller.static java.lang.StringDEBUG_TAGstatic java.lang.StringDEFAULT_SPECThe ruleset to use when loading old format games where a spec may not be readily available.private InGameControllerinGameControllerThe in-game controller.private ServerInputHandlerinputHandlerThe input handler.private Constants.IntegrityTypeintegrityThe game integrity state.private static java.util.logging.Loggerloggerstatic java.lang.StringMAP_EDITOR_NAMEThe name to use as the owner of maps that have been edited by the map editor.private MapGeneratormapGeneratorThe map generator.static intMINIMUM_SAVEGAME_VERSIONThe oldest save game format that can still be loaded.private java.lang.StringnameThe name of this server.static java.lang.StringOLD_SERVER_OBJECTS_TAGstatic java.lang.StringOWNER_TAGprivate PreGameControllerpreGameControllerThe pre-game controller.static java.lang.StringPUBLIC_SERVER_TAGprivate booleanpublicServerShould this game be listed on the meta-server?private java.util.RandomrandomThe internal provider of random numbers.static java.lang.StringRANDOM_STATE_TAGstatic java.lang.StringSAVED_GAME_TAGstatic intSAVEGAME_VERSIONThe save game format used for saving games.private ServerserverThe underlying interface to the network.private ServerGameserverGameThe game underway.private FreeColServer.ServerStateserverStateThe current state of the game.static java.lang.StringSINGLE_PLAYER_TAGprivate booleansinglePlayerIs this a single player game?private UserConnectionHandleruserConnectionHandlerThe 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.privateFreeColServer(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 voidaddAIConnection(Player aiPlayer)Establish the connections for an AI player.voidaddNewUserConnection(java.net.Socket socket)Add a new user connection.voidaddPlayerConnection(Connection connection)Add player connection.private GamebuildGame()Builds a new game using the parameters that exist in the game as it stands.private FreeColServer.ServerStatechangeServerState(FreeColServer.ServerState serverState)Change the server state.private ServercreateServer(java.net.InetAddress address, int firstPort)Create a Server at port.voidendGame()The game is ending, tell all the non-admin players to quit.voidexploreMapForAllPlayers(boolean reveal)Reveals or hides the entire map for all players.MapgenerateEmptyMap(int width, int height)Generate an empty map in this server/game.MapgenerateMap(boolean generateEuropeanPlayerUnits)Generate a new map in this server/game.AIMaingetAIMain()Gets the main AI-object.AIPlayergetAIPlayer(Player player)Gets the AI player corresponding to a given player.ControllergetController()Gets theController.ServerGamegetGame()Gets theGamethat is being played.java.lang.StringgetHost()Gets the host this FreeColServer was started on.InGameControllergetInGameController()Gets the controller being used while the game is running.ServerInputHandlergetInputHandler()Get the current input handler.Constants.IntegrityTypegetIntegrity()Gets the integrity check result.MapGeneratorgetMapGenerator()Get the map generator.java.lang.StringgetName()Gets the name of this server.ServerPlayergetPlayer(Connection conn)Get aServerPlayerby connection.intgetPort()Gets the port this FreeColServer was started on.PreGameControllergetPreGameController()Gets thePreGameController.booleangetPublicServer()Get the public server state.ServergetServer()Gets the network server responsible of handling the connections.private ServerInfogetServerInfo()Create aServerInforecord for this server and connection.java.util.RandomgetServerRandom()Gets the server random number generator.FreeColServer.ServerStategetServerState()Gets the current state of the server.booleangetSinglePlayer()Is the user playing in single player mode?SpecificationgetSpecification()Gets the specification from the game run by this server.UserConnectionHandlergetUserConnectionHandler()Gets theUserConnectionHandler.ServerGameloadGame(FreeColSavegameFile fis)Loads a game.private ServerGameloadGame(FreeColSavegameFile fis, Specification specification)Loads a game.ServerPlayermakeAIPlayer(Nation nation)Make a new AI player and add it to the game.private static ServerGamereadGame(java.io.File file, Specification spec, FreeColServer freeColServer)Read just the game part from a file.static ServerGamereadGame(FreeColSavegameFile fis, Specification specification, FreeColServer freeColServer)Reads just the game part from a save game from a stream.static MapreadMap(java.io.File file, Specification spec)Read just the map part from a file.booleanregisterWithMetaServer()Register this FreeColServer with the meta-server.booleanremoveFromMetaServer()Removes this server from the meta-server.voidremovePlayerConnection(Player player)Remove a player connection.private voidsaveGame(java.io.File file, java.lang.String owner, OptionGroup options, Unit active, java.awt.image.BufferedImage image)Saves a game.voidsaveGame(java.io.File file, OptionGroup options, Unit active)Saves a normal (non-map-editor) game.voidsaveMapEditorGame(java.io.File file, java.awt.image.BufferedImage image)Save a game from the map editor.voidsendToAll(Message msg, Player player)Send a message to all connections.private voidsendToAll(Message msg, Connection conn)Send a message to all connections.voidsetAIMain(AIMain aiMain)Sets the main AI-object.voidsetGame(ServerGame serverGame)Sets theGamethat is being played.voidsetMapGenerator(MapGenerator mapGenerator)Set the map generator.voidsetName(java.lang.String name)Sets the name of this server.voidsetPublicServer(boolean publicServer)Sets the public server state.voidsetServerRandom(java.util.Random random)Sets the server random number generator.voidsetSinglePlayer(boolean singlePlayer)Sets the single/multiplayer state of the game.voidshutdown()Shut down this FreeColServer.voidstartGame()Start the game.booleanupdateMetaServer()Update this FreeColServer with the meta-server.ServerGamewaitForGame()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.IOExceptionBase 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.IOExceptionStarts 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- TheSpecificationto 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 optionalSpecificationto 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 theGamethat is being played.- Returns:
- The
Gamewhich is the main class of the game-model being used in this game.
-
setGame
public void setGame(ServerGame serverGame)
Sets theGamethat 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
UserConnectionHandlerthat 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
ServerInputHandlerfor 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.XMLStreamExceptionAdd a new user connection. That is a new connection to the server that has not yet logged in as a player.- Parameters:
socket- The clientSocketthe 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- ThePlayerto disconnect.
-
addAIConnection
private void addAIConnection(Player aiPlayer)
Establish the connections for an AI player.- Parameters:
aiPlayer- The AIPlayerto 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 FreeColExceptionStart 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- TheMessageto send.conn- An optionalConnectionto omit.
-
sendToAll
public void sendToAll(Message msg, Player player)
Send a message to all connections.- Parameters:
msg- TheMessageto send.player- An optionalPlayerto omit.
-
saveMapEditorGame
public void saveMapEditorGame(java.io.File file, java.awt.image.BufferedImage image) throws java.io.IOExceptionSave 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.IOExceptionSaves 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.IOExceptionSaves 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 thumbnailImagevalue 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- TheFileto read from.spec- An optionalSpecificationto use.- Returns:
- The
Mapfound 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- TheFileto read from.spec- An optionalSpecificationto 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 optionalSpecificationto 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- TheSpecificationto 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
Mapthat 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- TheNationto 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 aServerPlayerby connection.- Parameters:
conn- TheConnectionto use while searching for aServerPlayer.- Returns:
- The
ServerPlayerfound, or null if none.
-
getAIPlayer
public AIPlayer getAIPlayer(Player player)
Gets the AI player corresponding to a given player.- Parameters:
player- ThePlayerto look up.- Returns:
- The corresponding AI player, or null if not found.
-
getServerInfo
private ServerInfo getServerInfo()
Create aServerInforecord 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.
-
-