Class AbstractClient

java.lang.Object
megamek.client.AbstractClient
All Implemented Interfaces:
IClient
Direct Known Subclasses:
Client, SBFClient

public abstract class AbstractClient extends Object implements IClient
AbstractClient that handles basic client features.
  • Field Details

    • connection

      protected AbstractConnection connection
    • connThread

      protected Thread connThread
    • name

      protected String name
    • connected

      protected boolean connected
    • disconnectFlag

      protected boolean disconnectFlag
    • awaitingSave

      protected boolean awaitingSave
    • host

      protected final String host
    • port

      protected final int port
    • localPlayerNumber

      protected int localPlayerNumber
      The ID of the local player (the player connected through this client)
    • log

      protected GameLog log
    • phaseReport

      public String phaseReport
    • roundReport

      public String roundReport
    • unitNameTracker

      protected final UnitNameTracker unitNameTracker
    • bots

      protected Map<String,AbstractClient> bots
      The bots controlled by the local player; maps a bot's name String to a bot's client.
    • iconCache

      protected Map<Integer,String> iconCache
    • connectionListener

      protected ConnectionListener connectionListener
      Custom connection Listener for AbstractClient
      See Also:
  • Constructor Details

    • AbstractClient

      public AbstractClient(String name, String host, int port)
      Construct a client which will try to connect. If the connection fails, it will alert the player, free resources and hide the frame.
      Parameters:
      name - the player name for this client
      host - the hostname
      port - the host port
  • Method Details

    • getLocalPlayerNumber

      public int getLocalPlayerNumber()
      Specified by:
      getLocalPlayerNumber in interface IClient
      Returns:
      The ID of the player playing at this Client.
    • setLocalPlayerNumber

      public void setLocalPlayerNumber(int localPlayerNumber)
      Description copied from interface: IClient
      Sets the ID of the player playing at this Client.
      Specified by:
      setLocalPlayerNumber in interface IClient
      Parameters:
      localPlayerNumber - The new local player's ID
    • updateConnection

      protected void updateConnection()
    • connect

      public boolean connect()
      Attempt to connect to the specified host
      Specified by:
      connect in interface IClient
      Returns:
      true if successful.
    • isConnected

      public boolean isConnected()
    • die

      public void die()
      Shuts down threads and sockets
      Specified by:
      die in interface IClient
    • disconnected

      protected void disconnected()
      The client has become disconnected from the server
    • initGameLog

      protected void initGameLog()
    • keepGameLog

      protected boolean keepGameLog()
      Called to determine whether the game log should be kept. Default implementation delegates to PreferenceManager.getClientPreferences().
      Returns:
      True/False if the game log is to be kept.
    • sendNextPlayer

      public void sendNextPlayer()
      give the initiative to the next player on the team.
    • sendPlayerInfo

      public void sendPlayerInfo()
      Sends the info associated with the local player.
    • sendPlayerInfo

      public void sendPlayerInfo(Player player)
      Sends the info associated with a player, usually the local game's local player but sometimes the modified version of that player.
      Parameters:
      player - Player instance regarding which to send the update.
    • sendChat

      public void sendChat(String message)
      Specified by:
      sendChat in interface IClient
    • sendServerChat

      public void sendServerChat(int connId, String message)
      Broadcast a general chat message from the local player
      Parameters:
      connId - Connection ID
      message - Message to send
    • sendDone

      public void sendDone(boolean done)
      Description copied from interface: IClient
      Sends a "this player is done/not done" message to the server.
      Specified by:
      sendDone in interface IClient
      Parameters:
      done - Is the player done or not.
    • sendPause

      public void sendPause()
      Specified by:
      sendPause in interface IClient
    • sendUnpause

      public void sendUnpause()
    • sendArtilleryRevealPreference

      public void sendArtilleryRevealPreference(boolean revealAll)
      Sends this player's "reveal all artillery rounds" testing preference to the server. When true, the server includes enemy artillery attacks in this player's artillery packet so the Rounds in the Air view can show both sides; when false, only this player's team's rounds are sent (normal double-blind behavior).
      Parameters:
      revealAll - Whether to reveal all in-flight artillery to this player
    • receivePlayerInfo

      protected void receivePlayerInfo(Packet packet) throws InvalidPacketDataException
      Receives player information from the message packet.
      Parameters:
      packet - The packet we received.
      Throws:
      InvalidPacketDataException
    • send

      protected void send(Packet packet)
      Sends the packet to the server, if this client is connected. Otherwise, does nothing.
      Parameters:
      packet - Packet to send over the connection.
    • flushConn

      protected void flushConn()
      send all buffered packets on their way this should be called after everything which causes us to wait for a reply. For example "done" button presses etc. to make stuff more efficient, this should only be called after a batch of packets is sent, not separately for each packet
    • memDump

      protected void memDump(String where)
      Perform a dump of the current memory usage. This method is useful in tracking performance issues on various player's systems. You can activate it by changing the "memorydumpon" setting to "true" in the clientsettings.xml file.
      Parameters:
      where - A String indicating which part of the game is making this call.
    • getName

      public String getName()
      Specified by:
      getName in interface IClient
      Returns:
      The name of Client, typically the same as the local player's name.
    • isBot

      protected boolean isBot()
    • getPort

      public int getPort()
      Specified by:
      getPort in interface IClient
      Returns:
      The port that this Client uses to connect to the server.
    • getHost

      public String getHost()
      Specified by:
      getHost in interface IClient
      Returns:
      The server host address.
    • correctName

      protected void correctName(Packet inP) throws InvalidPacketDataException
      Throws:
      InvalidPacketDataException
    • setName

      protected void setName(String newName)
    • checkDuplicateNamesDuringAdd

      protected void checkDuplicateNamesDuringAdd(Entity entity)
      Before we officially "add" this unit to the game, check and see if this client (player) already has a unit in the game with the same name. If so, add an identifier to the units name.
      Parameters:
      entity - The entity to check if it is duplicated.
    • receiveUnitReplace

      protected void receiveUnitReplace(Packet packet) throws InvalidPacketDataException
      Throws:
      InvalidPacketDataException
    • addCloseClientListener

      public void addCloseClientListener(CloseClientListener l)
      Adds the specified close client listener to receive close client events. This is used by external programs running megamek
      Parameters:
      l - the game listener.
    • handlePacket

      protected void handlePacket(Packet packet)
      This method is the starting point that handles all received Packets. This method should only be overridden in very special cases such as in Princess to call Precognition.
      Parameters:
      packet - The packet to handle
    • handleGameSpecificPacket

      protected abstract boolean handleGameSpecificPacket(Packet packet) throws Exception
      Handles any Packets that are specific to the game type (TW, AS...). When implementing this, make sure that this doesn't do duplicate actions with handleGameIndependentPacket(Packet) - but packets may be handled in both methods (all packets traverse both methods).

      When making changes, do not forget to update Precognition which is a Client clone but unfortunately not a subclass.

      Parameters:
      packet - The packet to handle
      Returns:
      True when the packet has been handled
      Throws:
      Exception - If some error occurred.
    • handleGameIndependentPacket

      protected boolean handleGameIndependentPacket(Packet packet)
      Handles any Packets that are independent of the game type (TW, AS...).
      Parameters:
      packet - The packet to handle
      Returns:
      True when the packet has been handled
    • possiblyWriteToLog

      protected void possiblyWriteToLog(String message)
    • changePhase

      public void changePhase(GamePhase phase)
      Changes the game phase, and the displays that go along with it.
      Parameters:
      phase - the phase switching from.
    • getBots

      public Map<String,AbstractClient> getBots()
      Description copied from interface: IClient
      Returns the map containing this Client's local bots, wherein the key is the bot's player name and the value the Client.
      Specified by:
      getBots in interface IClient
      Returns:
      This Client's local bots mapped to their player name
    • setAwaitingSave

      public void setAwaitingSave(boolean awaitingSave)
    • isAwaitingSave

      public boolean isAwaitingSave()
    • setSaveCompletionCallback

      public void setSaveCompletionCallback(@Nullable Consumer<File> saveCompletionCallback)
      Registers a one-shot callback to be run once a requested local save has actually landed on disk.

      Saving is asynchronous in MegaMek: the request goes to the server as a chat command, the server serializes the game, and the resulting file is streamed back to this client, which then writes it out. A caller that needs the finished file - the bug report packager, for instance - therefore cannot simply read it after asking for the save, because at that moment it does not yet exist.

      The callback is cleared as it fires, so it runs at most once per registration. Registering a new callback replaces any previous one.

      Parameters:
      saveCompletionCallback - invoked with the saved file, or with null if the save could not be written; pass null to cancel a pending registration
    • fireSaveCompleted

      protected void fireSaveCompleted(@Nullable File savedFile)
      Runs and clears any registered save-completion callback. Safe to call when none is registered, and safe to call more than once for a single save.
      Parameters:
      savedFile - the file that was written, or null if the save failed