java.lang.Object
java.lang.Thread
it.polimi.ingsw.am13.network.socket.ClientRequestsHandler
- All Implemented Interfaces:
Runnable
This class listens for request messages coming from a client connected via socket.
It has a corresponding handle method for each possible
MsgCommand
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Socket
Socket opened with the connected clientprivate GameController
GameController associated to the match the current player is playing (if he's in a match)private GameListenerServerSocket
GameListener instance associated to the current playerprivate final ObjectInputStream
Socket input stream generated from the current socketprivate final Lobby
Instance of the Lobby which exposes the main methods for match, init and disconnectionprivate final ObjectOutputStream
Socket output stream generated from the current socketprivate PlayerLobby
PlayerLobby of the current socketFields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
Constructor Summary
ConstructorsConstructorDescriptionClientRequestsHandler
(Socket clientSocket) Constructor for the ClientRequestsHandler class. -
Method Summary
Modifier and TypeMethodDescriptionprivate boolean
Checks if the game controller has been set.private void
handleChatMessage
(MsgCommandChat command) It handles a chat command.private void
Command handler for "choosePersonalObjective" command.private void
handleCreateRoom
(MsgCommandCreateRoom command) Command handler for "createRoom" command.void
Handler which should be called from the GameListener when the player results disconnected (connection crash or closed the application).void
Handler which should be called from the GameListener when the match is ended.private void
Command handler for "getRooms" command.private void
handleJoinRoom
(MsgCommandJoinRoom command) Command handler for "joinRoom" command.private void
Command handler for "leaveRoom" command.private void
handlePickCard
(MsgCommandPickCard command) Command handler for "pickCard" command.private void
Command handler for "ping" command.private void
handlePlayCard
(MsgCommandPlayCard command) Command handler for "playCard" command.private void
handlePlayStarter
(MsgCommandPlayStarter command) Command handler for "playStarter" command.void
Command handler for "reconnectGame" command.void
handleStartGame
(GameController newGameController) Handler which should be called from the GameListener when the match is started.private void
logCommand
(String cmd) Logging function which notifies the command which has been received on the server console .void
logResponse
(String res) Logging function which notifies the response which has been sent on the server console .void
run()
Main method for the Thread executionMethods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, suspend, threadId, toString, yield
-
Field Details
-
clientSocket
Socket opened with the connected client -
inputStream
Socket input stream generated from the current socket -
outputStream
Socket output stream generated from the current socket -
lobby
Instance of the Lobby which exposes the main methods for match, init and disconnection -
player
PlayerLobby of the current socket -
gameController
GameController associated to the match the current player is playing (if he's in a match) -
gameListener
GameListener instance associated to the current player
-
-
Constructor Details
-
ClientRequestsHandler
Constructor for the ClientRequestsHandler class. It retrieves the existing Lobby instance and gets the Socket's input and output streams.- Parameters:
clientSocket
- Opened Socket of the newly connected client
-
-
Method Details
-
run
public void run()Main method for the Thread execution -
logCommand
Logging function which notifies the command which has been received on the server console .- Parameters:
cmd
- Command name
-
logResponse
Logging function which notifies the response which has been sent on the server console .- Parameters:
res
- Response name
-
assertGameController
private boolean assertGameController()Checks if the game controller has been set. This method should be called by all the handlers associated to Commands related to the match.- Returns:
- True if GameController is set, False otherwise.
-
handleStartGame
Handler which should be called from the GameListener when the match is started. It sets the GameController of the new mach the player is playing in.- Parameters:
newGameController
- GameController of the new match
-
handleEndGame
public void handleEndGame()Handler which should be called from the GameListener when the match is ended. It unsets all the attributes related to the ended match. -
handleDisconnection
public void handleDisconnection()Handler which should be called from the GameListener when the player results disconnected (connection crash or closed the application). When this happens, the inputStream associated with the client gets closed. The current ClientRequestsHandler should be "destroyed" (references are removed).
Please note that we close the socket no matter the type of disconnection. This enables a uniformed way of handling disconnections, even when the opened Socket could be reused. -
handleReconnectGame
Command handler for "reconnectGame" command. This command should be the first one received from a client which wants to reconnect after a disconnection. Will be checked if a player with given command's information exists, otherwise an error response is sent back to client.- Parameters:
command
- reconnectGame command
-
handlePing
private void handlePing()Command handler for "ping" command. Updates the game controller about the last ping received. -
handleGetRooms
private void handleGetRooms()Command handler for "getRooms" command. This command is handled differently compared with others as no player is associated with the request. -
handleCreateRoom
Command handler for "createRoom" command. The client sends its chosen nickname and token color. If the given information is valid for the creation of a new Room, it gets created. Otherwise, an error is sent back to client.- Parameters:
command
- a MsgCommandCreateRoom command
-
handleJoinRoom
Command handler for "joinRoom" command. The client sends its chosen nickname and token color. If the given information is valid, the player joins the room. Otherwise, an error is sent back to client- Parameters:
command
- a MsgCommandJoinRoom command
-
handleLeaveRoom
private void handleLeaveRoom()Command handler for "leaveRoom" command. If the player is inside a room, he leaves it. Otherwise, an error is sent back to client -
handlePlayStarter
Command handler for "playStarter" command. The client sends the chosen side. If the passed side is valid ,it is the right game status to play the starter and there is no other error while playing it, the starter card is played. Otherwise, an error is sent back to the client.- Parameters:
command
- a MsgCommandPlayStarter command
-
handleChoosePersonalObjective
Command handler for "choosePersonalObjective" command. The client sends the chosen personal objective. If the chosen personal objective is one of the two possible valid choices, it is the right game status to play the starter and there is no other error while playing it, the personal objective is chosen. Otherwise, an error is sent back to the client.- Parameters:
command
- a MsgChoosePersonalObjective command
-
handlePlayCard
Command handler for "playCard" command. The client sends the chosen card. If the passed card is valid ,it is the right moment for this player to play it, the requirements to play it are satisfied and there is no other error while playing it, the card is played. Otherwise, an error is sent back to the client.- Parameters:
command
- a MsgCommandPlayCard command
-
handlePickCard
Command handler for "pickCard" command. The client sends the chosen side. If the passed card is on the table ,it is the right game status to pick it and there is no other error while picking it, the card is picked. Otherwise, an error is sent back to the client.- Parameters:
command
- a MsgCommandPickCard command
-
handleChatMessage
It handles a chat command. First, if verifies that the command is valid (the receivers are all player of the match, the size of the receivers is either 1 or number of players-1, and the receivers do not contain this player). If it is not, it sends an error. Otherwise, it transmits to message to theGameController
- Parameters:
command
- a MsgCommandChat command
-