Class ClientRequestsHandler

java.lang.Object
java.lang.Thread
it.polimi.ingsw.am13.network.socket.ClientRequestsHandler
All Implemented Interfaces:
Runnable

public class ClientRequestsHandler extends Thread
This class listens for request messages coming from a client connected via socket. It has a corresponding handle method for each possible MsgCommand
  • Field Details

    • clientSocket

      private final Socket clientSocket
      Socket opened with the connected client
    • inputStream

      private final ObjectInputStream inputStream
      Socket input stream generated from the current socket
    • outputStream

      private final ObjectOutputStream outputStream
      Socket output stream generated from the current socket
    • lobby

      private final Lobby lobby
      Instance of the Lobby which exposes the main methods for match, init and disconnection
    • player

      private PlayerLobby player
      PlayerLobby of the current socket
    • gameController

      private GameController gameController
      GameController associated to the match the current player is playing (if he's in a match)
    • gameListener

      private GameListenerServerSocket gameListener
      GameListener instance associated to the current player
  • Constructor Details

    • ClientRequestsHandler

      public ClientRequestsHandler(Socket clientSocket)
      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
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • logCommand

      private void logCommand(String cmd)
      Logging function which notifies the command which has been received on the server console .
      Parameters:
      cmd - Command name
    • logResponse

      public void logResponse(String res)
      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

      public void handleStartGame(GameController newGameController)
      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

      public void handleReconnectGame(MsgCommandReconnectGame command)
      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

      private void handleCreateRoom(MsgCommandCreateRoom command)
      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

      private void handleJoinRoom(MsgCommandJoinRoom command)
      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

      private void handlePlayStarter(MsgCommandPlayStarter command)
      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

      private void handleChoosePersonalObjective(MsgCommandChoosePersonalObjective command)
      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

      private void handlePlayCard(MsgCommandPlayCard command)
      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

      private void handlePickCard(MsgCommandPickCard command)
      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

      private void handleChatMessage(MsgCommandChat command)
      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 the GameController
      Parameters:
      command - a MsgCommandChat command