Class Server

java.lang.Object
megamek.server.Server
All Implemented Interfaces:
Runnable

public class Server extends Object implements Runnable
  • Field Details

  • Constructor Details

    • Server

      public Server(@Nullable String password, int port, IGameManager gameManager) throws IOException
      Throws:
      IOException
    • Server

      public Server(@Nullable String password, int port, IGameManager gameManager, boolean registerWithServerBrowser, @Nullable String metaServerUrl) throws IOException
      Throws:
      IOException
    • Server

      public Server(@Nullable String password, int port, IGameManager gameManager, boolean registerWithServerBrowser, @Nullable String metaServerUrl, @Nullable EmailService mailer, boolean dedicated) throws IOException
      Construct a new GameHost and begin listening for incoming clients.
      Parameters:
      password - the String that is set as a password
      port - the int value that specifies the port that is used
      gameManager - the IGameManager instance for this server instance.
      registerWithServerBrowser - a boolean indicating whether we should register with the master server browser on ...
      mailer - an email service instance to use for sending round reports.
      dedicated - set to true if this server is started from a GUI-less context
      Throws:
      IOException
  • Method Details

    • validateServerAddress

      public static String validateServerAddress(String serverAddress) throws AbstractCommandLineParser.ParseException
      Parameters:
      serverAddress - IP or Domain Name Of Server
      Returns:
      valid hostName
      Throws:
      AbstractCommandLineParser.ParseException - for null or empty serverAddress
    • validatePlayerName

      public static String validatePlayerName(String playerName) throws AbstractCommandLineParser.ParseException
      Parameters:
      playerName - throw ParseException if null or empty
      Returns:
      valid playerName
      Throws:
      AbstractCommandLineParser.ParseException
    • validatePassword

      @Nullable public static String validatePassword(@Nullable String password)
      Parameters:
      password - Password to set for server.
      Returns:
      valid password or null if no password or password is blank string
    • passwordMatches

      public boolean passwordMatches(Object password)
      Checks a String against the server password
      Parameters:
      password - The password provided by the user.
      Returns:
      true if the user-supplied data matches the server password or no password is set.
    • validatePort

      public static int validatePort(int port)
      Parameters:
      port - if outside the established range of # and # return # or the passed in port.
      Returns:
      valid port number
    • getGameManager

      public IGameManager getGameManager()
    • setGame

      public void setGame(IGame g)
      Sets the game for this server. Restores any transient fields, and sets all players as ghosts. This should only be called during server initialization before any players have connected.
    • getGame

      public IGame getGame()
    • getEmailService

      public EmailService getEmailService()
    • isPassworded

      public boolean isPassworded()
      Returns:
      true if the server has a password
    • isPassword

      public boolean isPassword(Object guess)
      Returns:
      true if the password matches
    • getCommand

      public ServerCommand getCommand(String name)
      Returns the command associated with the specified name
    • getDedicated

      public boolean getDedicated()
      Returns:
      true run from a GUI-less context
    • die

      public void die()
      Shuts down the server.
    • getAllCommandNames

      public Collection<String> getAllCommandNames()
      Returns an enumeration of all the command names
    • getFreeConnectionId

      public int getFreeConnectionId()
      Returns a free connection id.
    • getFreeEntityId

      @Deprecated(since="0.51.0", forRemoval=true) public int getFreeEntityId()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Returns a free entity id. Perhaps this should be in Game instead.
    • sendCurrentInfo

      public void sendCurrentInfo(int connId)
      Sends a player the info they need to look at the current phase. This is triggered when a player first connects to the server.
    • validatePlayerInfo

      public void validatePlayerInfo(int playerId)
      Validates the player info.
    • resetGame

      public void resetGame()
    • sendLoadGame

      public void sendLoadGame(int connId, String sFile)
      send a packet to the connection tells it load a locally saved game
      Parameters:
      connId - The int connection id to send to
      sFile - The String filename to use
    • loadGame

      public boolean loadGame(File f)
      load the game
      Parameters:
      f - The File to load
      Returns:
      A boolean value whether the loading was successful
    • loadGame

      public boolean loadGame(File f, boolean sendInfo)
      load the game
      Parameters:
      f - The File to load
      sendInfo - Determines whether the connections should be updated with current info. This may be false if some reconnection remapping needs to be done first.
      Returns:
      A boolean value whether the loading was successful
    • saveGame

      public void saveGame(String fileName)
    • sendSaveGame

      public void sendSaveGame(int connId, String fileName, String localPath)
    • remapConnIds

      public void remapConnIds(Map<String,Integer> nameToIdMap, Map<Integer,String> idToNameMap)
      When the load command is used, there is a list of already connected players which have assigned names and player id numbers with the id numbers matching the connection numbers. When a new game is loaded, this mapping may need to be updated. This method takes a map of player names to their current ids, and uses the list of players to figure out what the current ids should change to.
      Parameters:
      nameToIdMap - This maps a player name to the current connection ID
      idToNameMap - This maps a current conn ID to a player name, and is just the inverse mapping from nameToIdMap
    • getPlayer

      public Player getPlayer(int id)
      Shortcut to game.getPlayer(id)
    • forEachConnection

      public void forEachConnection(Consumer<AbstractConnection> process)
      Executes the process on each active connection.
      Parameters:
      process - The process to execute.
    • getConnection

      public AbstractConnection getConnection(int connId)
      Returns a connection, indexed by id
    • getPendingConnection

      @Nullable public AbstractConnection getPendingConnection(int connId)
      Returns a pending connection
    • requestTeamChangeForPlayer

      public void requestTeamChangeForPlayer(int teamID, Player player)
      Player can request its own change of team
      Parameters:
      teamID - target team id
      player - player requesting the change
    • requestGameMaster

      public void requestGameMaster(Player player)
    • formatChatMessage

      public static String formatChatMessage(String origin, String message)
    • sendChat

      public void sendChat(int connId, String origin, String message)
      Transmits a chat message to all players
    • sendChat

      public void sendChat(String origin, String message)
      Transmits a chat message to all players
    • sendServerChat

      public void sendServerChat(int connId, String message)
    • sendServerChat

      public void sendServerChat(String message)
    • send

      public void send(int connId, Packet packet)
      Sends the given packet to the given connection (= player ID) if it is not null. Does nothing otherwise.
    • handle

      protected void handle(int connId, Packet packet)
      Process a packet from a connection.
      Parameters:
      connId - - the int ID the connection that received the packet.
      packet - - the Packet to be processed.
    • run

      public void run()
      Listen for incoming clients.
      Specified by:
      run in interface Runnable
    • getHost

      public String getHost()
      Returns:
      a String representing the hostname
    • getPort

      public int getPort()
      Returns:
      the int this server is listening on
    • getServerInstance

      @Nullable public static Server getServerInstance()
      Returns:
      the current server instance. This may be null if a server has not been started
    • reportRoll

      public void reportRoll(Roll roll)
      Adds a roll report to the GameManager's current pending report list.
      Parameters:
      roll - The roll to add