net.sf.freecol.server
Class FreeColServer

java.lang.Object
  extended by 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
static class FreeColServer.GameState
          Constant for storing the state of the game.
 
Field Summary
static java.util.Comparator<HighScore> highScoreComparator
           
static int MINIMUM_SAVEGAME_VERSION
          The oldest save game format that can still be loaded.
static int SAVEGAME_VERSION
          The save game format used for saving games.
 
Constructor Summary
FreeColServer(boolean publicServer, boolean singleplayer, int port, java.lang.String name)
          Starts a new server in a specified mode and with a specified port.
FreeColServer(boolean publicServer, boolean singleplayer, int port, java.lang.String name, NationOptions nationOptions)
           
FreeColServer(FreeColSavegameFile savegame, boolean publicServer, boolean singleplayer, int port, java.lang.String name)
          Starts a new server in a specified mode and with a specified port and loads the game from the given file.
 
Method Summary
 ServerPlayer addAIPlayer(Nation nation)
          Adds a new AIPlayer to the Game.
static void checkSavegameVersion(javax.xml.stream.XMLStreamReader xsr)
           
static XMLStream createXMLStreamReader(FreeColSavegameFile fis)
          Creates a XMLStream for reading the given file.
 void enterRevengeMode(java.lang.String username)
          Enters revenge mode against those evil AIs.
 IndianSettlement getAdjacentIndianSettlementSafely(java.lang.String settlementId, Unit unit)
          Get an adjacent Indian settlement by ID, validating as much as possible, including checking whether the nation involved has been contacted.
 Settlement getAdjacentSettlementSafely(java.lang.String settlementId, Unit unit)
          Get a settlement by ID, validating the ID as much as possible.
 AIMain getAIMain()
          Gets the main AI-object.
 Controller getController()
          Gets the Controller.
 ServerGame getGame()
          Gets the Game that is being played.
 FreeColServer.GameState getGameState()
          Gets the current state of the game.
 java.util.List<HighScore> getHighScores()
          Get the HighScores value.
 InGameController getInGameController()
          Gets the controller being used while the game is running.
 InGameInputHandler getInGameInputHandler()
          Gets the InGameInputHandler.
 boolean getIntegrity()
          Gets the integrity check result.
 IMapGenerator getMapGenerator()
          Gets the MapGenerator this FreeColServer is using when creating random maps.
 ServerModelController getModelController()
          Gets the ModelController.
 java.lang.String getName()
          Returns the name of this server.
 int getNumberOfLivingHumanPlayers()
          Gets the number of human players in this game that is still playing.
 java.lang.String getOwner()
          Gets the owner of the Game.
 ServerPlayer getPlayer(Connection connection)
          Gets a Player specified by a connection.
 PreGameInputHandler getPreGameInputHandler()
          Gets the PreGameInputHandler.
 java.lang.String getRandomState(java.util.Random random)
          Get the internal state of a random number generator as a string.
 Server getServer()
          Gets the network server responsible of handling the connections.
 java.util.Random getServerRandom()
          Get the server-private random number generator.
 int getSlotsAvailable()
          Gets the number of player that may connect.
 Unit getUnitSafely(java.lang.String unitId, ServerPlayer serverPlayer)
          Get a unit by ID, validating the ID as much as possible.
 UserConnectionHandler getUserConnectionHandler()
          Gets the UserConnectionHandler.
 boolean isSingleplayer()
          Checks if the user is playing in singleplayer mode.
 java.lang.String loadGame(FreeColSavegameFile fis)
          Loads a game.
 void loadHighScores()
          Loads high scores.
 boolean newHighScore(Player player)
          Adds a new high score for player and returns true if possible.
static void removeAutosaves(java.lang.String prefix)
          Removes automatically created save games.
 void removeFromMetaServer()
          Removes this server from the metaserver's list.
 java.util.Random restoreRandomState(java.lang.String state)
          Restore a previously saved state.
 void revealMapForAllPlayers()
          Makes the entire map visible for all players.
 void saveGame(java.io.File file, java.lang.String username)
          Saves a game.
 void saveHighScores()
          Saves high scores.
 void setAIMain(AIMain aiMain)
          Sets the main AI-object.
 void setGameState(FreeColServer.GameState state)
          Sets the current state of the game.
 void setMapGenerator(IMapGenerator mapGenerator)
          Sets the MapGenerator this FreeColServer is using when creating random maps.
 void setName(java.lang.String name)
          Sets the name of this server.
 void setSingleplayer(boolean singleplayer)
          Sets the mode of the game: singleplayer/multiplayer.
 void startMetaServerUpdateThread()
          Starts the metaserver update thread if publicServer == true.
 void updateMetaServer()
          Sends information about this server to the meta-server.
 void updateMetaServer(boolean firstTime)
          Sends information about this server to the meta-server.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SAVEGAME_VERSION

public static final int SAVEGAME_VERSION
The save game format used for saving games.

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.

See Also:
Constant Field Values

highScoreComparator

public static final java.util.Comparator<HighScore> highScoreComparator
Constructor Detail

FreeColServer

public FreeColServer(boolean publicServer,
                     boolean singleplayer,
                     int port,
                     java.lang.String name)
              throws java.io.IOException,
                     NoRouteToServerException
Starts a new server in a specified mode and with a specified port.

Parameters:
publicServer - This value should be set to true in order to appear on the meta server's listing.
singleplayer - Sets the game as singleplayer (if true) or multiplayer (if false).
port - The TCP port to use for the public socket. That is the port the clients will connect to.
name - The name of the server, or null if the default name should be used.
Throws:
java.io.IOException - if the public socket cannot be created (the exception will be logged by this class).
NoRouteToServerException

FreeColServer

public FreeColServer(boolean publicServer,
                     boolean singleplayer,
                     int port,
                     java.lang.String name,
                     NationOptions nationOptions)
              throws java.io.IOException,
                     NoRouteToServerException
Throws:
java.io.IOException
NoRouteToServerException

FreeColServer

public FreeColServer(FreeColSavegameFile savegame,
                     boolean publicServer,
                     boolean singleplayer,
                     int port,
                     java.lang.String name)
              throws java.io.IOException,
                     FreeColException,
                     NoRouteToServerException
Starts a new server in a specified mode and with a specified port and loads the game from the given file.

Parameters:
savegame - The file where the game data is located.
publicServer - This value should be set to true in order to appear on the meta server's listing.
singleplayer - Sets the game as singleplayer (if true) or multiplayer (if false).
port - The TCP port to use for the public socket. That is the port the clients will connect to.
name - The name of the server, or null if the default name should be used.
Throws:
java.io.IOException - if the public socket cannot be created (the exception will be logged by this class).
FreeColException - if the savegame could not be loaded.
NoRouteToServerException
Method Detail

startMetaServerUpdateThread

public void startMetaServerUpdateThread()
Starts the metaserver update thread if publicServer == true. This update is really a "Hi! I am still here!"-message, since an additional update should be sent when a new player is added to/removed from this server etc.


enterRevengeMode

public void enterRevengeMode(java.lang.String username)
Enters revenge mode against those evil AIs.

Parameters:
username - The player to enter revenge mode.

getMapGenerator

public IMapGenerator getMapGenerator()
Gets the MapGenerator this FreeColServer is using when creating random maps.

Returns:
The MapGenerator.

setMapGenerator

public void setMapGenerator(IMapGenerator mapGenerator)
Sets the MapGenerator this FreeColServer is using when creating random maps.

Parameters:
mapGenerator - The MapGenerator.

updateMetaServer

public void updateMetaServer()
                      throws NoRouteToServerException
Sends information about this server to the meta-server. The information is only sent if public == true.

Throws:
NoRouteToServerException

getName

public java.lang.String getName()
Returns 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 name.

updateMetaServer

public void updateMetaServer(boolean firstTime)
                      throws NoRouteToServerException
Sends information about this server to the meta-server. The information is only sent if public == true.

Parameters:
firstTime - Should be set to true> when calling this method for the first time.
Throws:
NoRouteToServerException - if the meta-server cannot connect to this server.

removeFromMetaServer

public void removeFromMetaServer()
Removes this server from the metaserver's list. The information is only sent if public == true.


getSlotsAvailable

public int getSlotsAvailable()
Gets the number of player that may connect.

Returns:
The number of available slots for human players. This number also includes european players currently controlled by the AI.

getNumberOfLivingHumanPlayers

public int getNumberOfLivingHumanPlayers()
Gets the number of human players in this game that is still playing.

Returns:
The number.

getOwner

public java.lang.String getOwner()
Gets the owner of the Game.

Returns:
The owner of the game. THis is the player that has loaded the game (if any).
See Also:
loadGame(net.sf.freecol.common.io.FreeColSavegameFile)

saveGame

public void saveGame(java.io.File file,
                     java.lang.String username)
              throws java.io.IOException
Saves a game.

Parameters:
file - The file where the data will be written.
username - The username of the player saving the game.
Throws:
java.io.IOException - If a problem was encountered while trying to open, write or close the file.

createXMLStreamReader

public static XMLStream createXMLStreamReader(FreeColSavegameFile fis)
                                       throws java.io.IOException
Creates a XMLStream for reading the given file. Compression is automatically detected.

Parameters:
fis - The file to be read.
Returns:
The XMLStreamr.
Throws:
java.io.IOException - if thrown while loading the game or if a XMLStreamException have been thrown by the parser.

loadGame

public java.lang.String loadGame(FreeColSavegameFile fis)
                          throws java.io.IOException,
                                 FreeColException
Loads a game.

Parameters:
fis - The file where the game data is located.
Returns:
The username of the player saving the game.
Throws:
java.io.IOException - If a problem was encountered while trying to open, read or close the file.
java.io.IOException - if thrown while loading the game or if a XMLStreamException have been thrown by the parser.
FreeColException - if the savegame contains incompatible data.

checkSavegameVersion

public static void checkSavegameVersion(javax.xml.stream.XMLStreamReader xsr)
                                 throws FreeColException
Throws:
FreeColException

removeAutosaves

public static void removeAutosaves(java.lang.String prefix)
Removes automatically created save games. Call this function to delete the automatically created save games from a previous game.


setSingleplayer

public void setSingleplayer(boolean singleplayer)
Sets the mode of the game: singleplayer/multiplayer.

Parameters:
singleplayer - Sets the game as singleplayer (if true) or multiplayer (if false).

isSingleplayer

public boolean isSingleplayer()
Checks if the user is playing in singleplayer mode.

Returns:
true if the user is playing in singleplayer mode, false otherwise.

revealMapForAllPlayers

public void revealMapForAllPlayers()
Makes the entire map visible for all players. Used only when debugging (will be removed).


getPlayer

public ServerPlayer getPlayer(Connection connection)
Gets a Player specified by a connection.

Parameters:
connection - The connection to use while searching for a ServerPlayer.
Returns:
The player.

getUserConnectionHandler

public UserConnectionHandler getUserConnectionHandler()
Gets the UserConnectionHandler.

Returns:
The UserConnectionHandler that is beeing used when new client connect.

getController

public Controller getController()
Gets the Controller.

Returns:
The Controller.

getPreGameInputHandler

public PreGameInputHandler getPreGameInputHandler()
Gets the PreGameInputHandler.

Returns:
The PreGameInputHandler.

getInGameInputHandler

public InGameInputHandler getInGameInputHandler()
Gets the InGameInputHandler.

Returns:
The InGameInputHandler.

getInGameController

public InGameController getInGameController()
Gets the controller being used while the game is running.

Returns:
The controller from making a new turn etc.

getModelController

public ServerModelController getModelController()
Gets the ModelController.

Returns:
The controller used for generating random numbers and creating new FreeColGameObjects.

getGame

public ServerGame getGame()
Gets the Game that is being played.

Returns:
The Game which is the main class of the game-model being used in this game.

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.

getGameState

public FreeColServer.GameState getGameState()
Gets the current state of the game.

Returns:
One of: FreeColServer.GameState.STARTING_GAME, FreeColServer.GameState.IN_GAME and FreeColServer.GameState.ENDING_GAME.

setGameState

public void setGameState(FreeColServer.GameState state)
Sets the current state of the game.

Parameters:
state - The new state to be set. One of: FreeColServer.GameState.STARTING_GAME, FreeColServer.GameState.IN_GAME and FreeColServer.GameState.ENDING_GAME.

getServer

public Server getServer()
Gets the network server responsible of handling the connections.

Returns:
The network server.

getIntegrity

public boolean getIntegrity()
Gets the integrity check result.

Returns:
The integrity check result.

getServerRandom

public java.util.Random getServerRandom()
Get the server-private random number generator.

Returns:
The server-private random number generator.

getUnitSafely

public Unit getUnitSafely(java.lang.String unitId,
                          ServerPlayer serverPlayer)
                   throws java.lang.IllegalStateException
Get a unit by ID, validating the ID as much as possible. Designed for message unpacking where the ID should not be trusted.

Parameters:
unitId - The ID of the unit to be found.
serverPlayer - The ServerPlayer to whom the unit must belong.
Returns:
The unit corresponding to the unitId argument.
Throws:
java.lang.IllegalStateException - on failure to validate the unitId in any way. In the worst case this may be indicative of a malign client.

getAdjacentSettlementSafely

public Settlement getAdjacentSettlementSafely(java.lang.String settlementId,
                                              Unit unit)
                                       throws java.lang.IllegalStateException
Get a settlement by ID, validating the ID as much as possible. Designed for message unpacking where the ID should not be trusted.

Parameters:
settlementId - The ID of the Settlement to be found.
unit - A Unit which must be adjacent to the Settlement.
Returns:
The settlement corresponding to the settlementId argument.
Throws:
java.lang.IllegalStateException - on failure to validate the settlementId in any way. In the worst case this may be indicative of a malign client.

getAdjacentIndianSettlementSafely

public IndianSettlement getAdjacentIndianSettlementSafely(java.lang.String settlementId,
                                                          Unit unit)
                                                   throws java.lang.IllegalStateException
Get an adjacent Indian settlement by ID, validating as much as possible, including checking whether the nation involved has been contacted. Designed for message unpacking where the ID should not be trusted.

Parameters:
settlementId - The ID of the Settlement to be found.
unit - A Unit which must be adjacent to the Settlement.
Returns:
The settlement corresponding to the settlementId argument.
Throws:
java.lang.IllegalStateException - on failure to validate the settlementId in any way. In the worst case this may be indicative of a malign client.

addAIPlayer

public ServerPlayer addAIPlayer(Nation nation)
Adds a new AIPlayer to the Game.

Parameters:
nation - a Nation value
Returns:
a ServerPlayer value

getHighScores

public java.util.List<HighScore> getHighScores()
Get the HighScores value.

Returns:
a List value

newHighScore

public boolean newHighScore(Player player)
Adds a new high score for player and returns true if possible.

Parameters:
player - a Player value
Returns:
a boolean value

saveHighScores

public void saveHighScores()
                    throws java.io.IOException
Saves high scores.

Throws:
java.io.IOException - If a problem was encountered while trying to open, write or close the file.

loadHighScores

public void loadHighScores()
                    throws java.io.IOException,
                           FreeColException
Loads high scores.

Throws:
java.io.IOException - If a problem was encountered while trying to open, read or close the file.
java.io.IOException - if thrown while loading the game or if a XMLStreamException have been thrown by the parser.
FreeColException - if the savegame contains incompatible data.

getRandomState

public java.lang.String getRandomState(java.util.Random random)
Get the internal state of a random number generator as a string. It would have been more convenient to simply return the current seed, but unfortunately it is private.

Parameters:
random - The Random to use.
Returns:
A String encapsulating the object state.

restoreRandomState

public java.util.Random restoreRandomState(java.lang.String state)
                                    throws java.io.IOException
Restore a previously saved state.

Parameters:
state - The saved state (@see #getRandomState()).
Returns:
The restored Random.
Throws:
java.io.IOException - if unable to restore state.