Package megamek.client
Class AbstractClient
java.lang.Object
megamek.client.AbstractClient
- All Implemented Interfaces:
IClient
AbstractClient that handles basic client features.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classCustom ConnectionHandler for use withAbstractClient. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected Map<String, AbstractClient> The bots controlled by the local player; maps a bot's name String to a bot's client.protected booleanprotected AbstractConnectionprotected ConnectionListenerCustom connection Listener for AbstractClientprotected Threadprotected booleanprotected final Stringprotected intThe ID of the local player (the player connected through this client)protected GameLogprotected Stringprotected final intprotected final UnitNameTracker -
Constructor Summary
ConstructorsConstructorDescriptionAbstractClient(String name, String host, int port) Construct a client which will try to connect. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the specified close client listener to receive close client events.voidchangePhase(GamePhase phase) Changes the game phase, and the displays that go along with it.protected voidcheckDuplicateNamesDuringAdd(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.booleanconnect()Attempt to connect to the specified hostprotected voidcorrectName(Packet inP) voiddie()Shuts down threads and socketsprotected voidThe client has become disconnected from the serverprotected voidfireSaveCompleted(File savedFile) Runs and clears any registered save-completion callback.protected voidsend all buffered packets on their way this should be called after everything which causes us to wait for a reply.getBots()Returns the map containing this Client's local bots, wherein the key is the bot's player name and the value the Client.getHost()intgetName()intgetPort()protected booleanhandleGameIndependentPacket(Packet packet) Handles any Packets that are independent of the game type (TW, AS...).protected abstract booleanhandleGameSpecificPacket(Packet packet) Handles any Packets that are specific to the game type (TW, AS...).protected voidhandlePacket(Packet packet) This method is the starting point that handles all received Packets.protected voidbooleanprotected booleanisBot()booleanprotected booleanCalled to determine whether the game log should be kept.protected voidPerform a dump of the current memory usage.protected voidpossiblyWriteToLog(String message) protected voidreceivePlayerInfo(Packet packet) Receives player information from the message packet.protected voidreceiveUnitReplace(Packet packet) protected voidSends the packet to the server, if this client is connected.voidsendArtilleryRevealPreference(boolean revealAll) Sends this player's "reveal all artillery rounds" testing preference to the server.voidvoidsendDone(boolean done) Sends a "this player is done/not done" message to the server.voidgive the initiative to the next player on the team.voidvoidSends the info associated with the local player.voidsendPlayerInfo(Player player) Sends the info associated with a player, usually the local game's local player but sometimes the modified version of that player.voidsendServerChat(int connId, String message) Broadcast a general chat message from the local playervoidvoidsetAwaitingSave(boolean awaitingSave) voidsetLocalPlayerNumber(int localPlayerNumber) Sets the ID of the player playing at this Client.protected voidvoidsetSaveCompletionCallback(Consumer<File> saveCompletionCallback) Registers a one-shot callback to be run once a requested local save has actually landed on disk.protected voidMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface megamek.client.IClient
getBotClient, getGame, getInGameObject, getInGameObjects, getLocalPlayer, getPlayer, isLocalBot, isMyTurn, playerExists
-
Field Details
-
connection
-
connThread
-
name
-
connected
protected boolean connected -
disconnectFlag
protected boolean disconnectFlag -
awaitingSave
protected boolean awaitingSave -
host
-
port
protected final int port -
localPlayerNumber
protected int localPlayerNumberThe ID of the local player (the player connected through this client) -
log
-
phaseReport
-
roundReport
-
unitNameTracker
-
bots
The bots controlled by the local player; maps a bot's name String to a bot's client. -
iconCache
-
connectionListener
Custom connection Listener for AbstractClient- See Also:
-
-
Constructor Details
-
AbstractClient
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 clienthost- the hostnameport- the host port
-
-
Method Details
-
getLocalPlayerNumber
public int getLocalPlayerNumber()- Specified by:
getLocalPlayerNumberin interfaceIClient- Returns:
- The ID of the player playing at this Client.
-
setLocalPlayerNumber
public void setLocalPlayerNumber(int localPlayerNumber) Description copied from interface:IClientSets the ID of the player playing at this Client.- Specified by:
setLocalPlayerNumberin interfaceIClient- Parameters:
localPlayerNumber- The new local player's ID
-
updateConnection
protected void updateConnection() -
connect
public boolean connect()Attempt to connect to the specified host -
isConnected
public boolean isConnected() -
die
public void die()Shuts down threads and sockets -
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 toPreferenceManager.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
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
-
sendServerChat
Broadcast a general chat message from the local player- Parameters:
connId- Connection IDmessage- Message to send
-
sendDone
public void sendDone(boolean done) Description copied from interface:IClientSends a "this player is done/not done" message to the server. -
sendPause
public void sendPause() -
sendUnpause
public void sendUnpause() -
sendArtilleryRevealPreference
public void sendArtilleryRevealPreference(boolean revealAll) Sends this player's "reveal all artillery rounds" testing preference to the server. Whentrue, the server includes enemy artillery attacks in this player's artillery packet so the Rounds in the Air view can show both sides; whenfalse, 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
Receives player information from the message packet.- Parameters:
packet- The packet we received.- Throws:
InvalidPacketDataException
-
send
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
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
-
isBot
protected boolean isBot() -
getPort
public int getPort() -
getHost
-
correctName
- Throws:
InvalidPacketDataException
-
setName
-
checkDuplicateNamesDuringAdd
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
- Throws:
InvalidPacketDataException
-
addCloseClientListener
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
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
Handles any Packets that are specific to the game type (TW, AS...). When implementing this, make sure that this doesn't do duplicate actions withhandleGameIndependentPacket(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
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
-
changePhase
Changes the game phase, and the displays that go along with it.- Parameters:
phase- the phase switching from.
-
getBots
Description copied from interface:IClientReturns the map containing this Client's local bots, wherein the key is the bot's player name and the value the Client. -
setAwaitingSave
public void setAwaitingSave(boolean awaitingSave) -
isAwaitingSave
public boolean isAwaitingSave() -
setSaveCompletionCallback
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 withnullif the save could not be written; passnullto cancel a pending registration
-
fireSaveCompleted
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, ornullif the save failed
-