Class 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.
    • 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
      • OLD_SERVER_OBJECTS_TAG

        public static final java.lang.String OLD_SERVER_OBJECTS_TAG
        See Also:
        Constant Field Values
      • RANDOM_STATE_TAG

        public static final java.lang.String RANDOM_STATE_TAG
        See Also:
        Constant Field Values
      • PUBLIC_SERVER_TAG

        public static final java.lang.String PUBLIC_SERVER_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.
      • 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.
      • aiMain

        private AIMain aiMain
        The AI controller.
      • mapGenerator

        private MapGenerator mapGenerator
        The map generator.
    • 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 - The Specification 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 optional Specification 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 the Game 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 the Game that is being played.
        Parameters:
        serverGame - The new Game.
      • 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.
      • getUserConnectionHandler

        public UserConnectionHandler getUserConnectionHandler()
        Gets the UserConnectionHandler.
        Returns:
        The UserConnectionHandler that is used when a new client connects.
      • getController

        public Controller getController()
        Gets the Controller.
        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 the PreGameController.
        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 new MapGenerator.
      • 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 client Socket 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 new Connection.
      • removePlayerConnection

        public void removePlayerConnection​(Player player)
        Remove a player connection.
        Parameters:
        player - The Player to disconnect.
      • addAIConnection

        private void addAIConnection​(Player aiPlayer)
        Establish the connections for an AI player.
        Parameters:
        aiPlayer - The AI Player 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).
        1. Creates the game.
        2. Sends updated game information to the clients.
        3. Changes the game state to FreeColServer.ServerState.IN_GAME.
        4. Sends the "startGame"-message to the clients.
        5. 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 - The Message to send.
        conn - An optional Connection to omit.
      • sendToAll

        public void sendToAll​(Message msg,
                              Player player)
        Send a message to all connections.
        Parameters:
        msg - The Message to send.
        player - An optional Player 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 active Unit.
        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 active Unit.
        image - A thumbnail Image 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 - The File to read from.
        spec - An optional Specification 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 - The File to read from.
        spec - An optional Specification 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 optional Specification 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 - The Specification 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 - If true, 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 - The Nation 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 a ServerPlayer by connection.
        Parameters:
        conn - The Connection to use while searching for a ServerPlayer.
        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 - The Player to look up.
        Returns:
        The corresponding AI player, or null if not found.
      • getServerInfo

        private ServerInfo getServerInfo()
        Create a ServerInfo 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.