Package megamek.server
Class Server
java.lang.Object
megamek.server.Server
- All Implemented Interfaces:
Runnable
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionServer(String password, int port, IGameManager gameManager) Server(String password, int port, IGameManager gameManager, boolean registerWithServerBrowser, String metaServerUrl) Server(String password, int port, IGameManager gameManager, boolean registerWithServerBrowser, String metaServerUrl, EmailService mailer, boolean dedicated) Construct a new GameHost and begin listening for incoming clients. -
Method Summary
Modifier and TypeMethodDescriptionvoiddie()Shuts down the server.voidforEachConnection(Consumer<AbstractConnection> process) Executes the process on each active connection.static StringformatChatMessage(String origin, String message) Returns an enumeration of all the command namesgetCommand(String name) Returns the command associated with the specified namegetConnection(int connId) Returns a connection, indexed by idbooleanintReturns a free connection id.intDeprecated, for removal: This API element is subject to removal in a future version.getGame()getHost()getPendingConnection(int connId) Returns a pending connectiongetPlayer(int id) Shortcut to game.getPlayer(id)intgetPort()static Serverprotected voidProcess a packet from a connection.booleanisPassword(Object guess) booleanbooleanload the gamebooleanload the gamebooleanpasswordMatches(Object password) Checks a String against the server passwordvoidWhen 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.voidreportRoll(Roll roll) Adds a roll report to the GameManager's current pending report list.voidrequestGameMaster(Player player) voidrequestTeamChangeForPlayer(int teamID, Player player) Player can request its own change of teamvoidvoidrun()Listen for incoming clients.voidvoidSends the given packet to the given connection (= player ID) if it is not null.voidTransmits a chat message to all playersvoidTransmits a chat message to all playersvoidsendCurrentInfo(int connId) Sends a player the info they need to look at the current phase.voidsendLoadGame(int connId, String sFile) send a packet to the connection tells it load a locally saved gamevoidsendSaveGame(int connId, String fileName, String localPath) voidsendServerChat(int connId, String message) voidsendServerChat(String message) voidSets the game for this server.static StringvalidatePassword(String password) voidvalidatePlayerInfo(int playerId) Validates the player info.static StringvalidatePlayerName(String playerName) static intvalidatePort(int port) static StringvalidateServerAddress(String serverAddress)
-
Field Details
-
ORIGIN
- See Also:
-
SERVER_CONN
public static final int SERVER_CONN- See Also:
-
-
Constructor Details
-
Server
- 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- theStringthat is set as a passwordport- theintvalue that specifies the port that is usedgameManager- theIGameManagerinstance for this server instance.registerWithServerBrowser- abooleanindicating 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
- Parameters:
password- Password to set for server.- Returns:
- valid password or null if no password or password is blank string
-
passwordMatches
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
-
setGame
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
-
getEmailService
-
isPassworded
public boolean isPassworded()- Returns:
- true if the server has a password
-
isPassword
- Returns:
- true if the password matches
-
getCommand
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
Returns an enumeration of all the command names -
getFreeConnectionId
public int getFreeConnectionId()Returns a free connection id. -
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
send a packet to the connection tells it load a locally saved game- Parameters:
connId- Theintconnection id to send tosFile- TheStringfilename to use
-
loadGame
load the game- Parameters:
f- TheFileto load- Returns:
- A
booleanvalue whether the loading was successful
-
loadGame
load the game- Parameters:
f- TheFileto loadsendInfo- 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
booleanvalue whether the loading was successful
-
saveGame
-
sendSaveGame
-
remapConnIds
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 IDidToNameMap- This maps a current conn ID to a player name, and is just the inverse mapping from nameToIdMap
-
getPlayer
Shortcut to game.getPlayer(id) -
forEachConnection
Executes the process on each active connection.- Parameters:
process- The process to execute.
-
getConnection
Returns a connection, indexed by id -
getPendingConnection
Returns a pending connection -
requestTeamChangeForPlayer
Player can request its own change of team- Parameters:
teamID- target team idplayer- player requesting the change
-
requestGameMaster
-
formatChatMessage
-
sendChat
Transmits a chat message to all players -
sendChat
Transmits a chat message to all players -
sendServerChat
-
sendServerChat
-
send
Sends the given packet to the given connection (= player ID) if it is not null. Does nothing otherwise. -
handle
Process a packet from a connection.- Parameters:
connId- - theintID the connection that received the packet.packet- - thePacketto be processed.
-
run
public void run()Listen for incoming clients. -
getHost
- Returns:
- a
Stringrepresenting the hostname
-
getPort
public int getPort()- Returns:
- the
intthis server is listening on
-
getServerInstance
- Returns:
- the current server instance. This may be null if a server has not been started
-
reportRoll
Adds a roll report to the GameManager's current pending report list.- Parameters:
roll- The roll to add
-