Class ClientHandler

java.lang.Object
it.polimi.ingsw.gc14.Network.TCP.Server.ClientHandler
All Implemented Interfaces:
Runnable

public class ClientHandler extends Object implements Runnable
Handles the TCP connection with a single client. Each instance runs on a dedicated thread and is responsible for receiving NetworkEvent from its client and adding them to the actionQueue. It is also responsible to send events and game model updates back to the client.
  • Field Details

    • username

      private final String username
      The username of the connected client on this handler
    • running

      private volatile boolean running
      true while the handler is actively reading from the socket; set to false on disconnect.
    • clientSocket

      private final Socket clientSocket
      The TCP socket
    • in

      private final ObjectInputStream in
      Input stream used to receive objects from the client
    • out

      private final ObjectOutputStream out
      Output stream used to send objects to the client
    • clientHandlers

      private final List<ClientHandler> clientHandlers
      Shared list of all client handlers. This handler removes itself from the list when disconnected.
    • limitedMap

      private final LimitedMap<String,Boolean> limitedMap
      Maps each connected player's username to their connection state (true = connected).
    • actionQueue

      private final BlockingQueue<NetworkEvent> actionQueue
      Queue containing the events to be applied to the game model
  • Constructor Details

    • ClientHandler

      public ClientHandler(String username, Socket clientSocket, ObjectOutputStream out, ObjectInputStream in, List<ClientHandler> clientHandlers, LimitedMap<String,Boolean> playersMap, BlockingQueue<NetworkEvent> actionQueue)
      Class constructor that initializes the attributes.
      Parameters:
      username - the username associated with the client.
      clientSocket - the socket representing the client's TCP connection.
      out - the output stream used to send data to the client.
      in - the input stream used to receive data from the client.
      clientHandlers - the shared list of all active client handlers.
      playersMap - the shared map containing the connected players.
      actionQueue - the queue containing incoming events.
  • Method Details

    • getUsername

      public String getUsername()
      Returns the username associated with this client.
      Returns:
      the username associated with this client.
    • run

      public void run()
      Listens for incoming NetworkEvents from the client and adds them to the actionQueue. Upon disconnection, this handler removes itself from clientHandlers.
      Specified by:
      run in interface Runnable
    • notifyEvent

      public void notifyEvent(NetworkEvent event)
      Sends a NetworkEvent to the client.
      Parameters:
      event - The network event to send to the client.
    • notifyMiniModel

      public void notifyMiniModel(MiniModel game)
      Sends the current game model to this client.
      Parameters:
      game - The current state of the game to send to the client.
    • disconnect

      public void disconnect()
      Disconnects the client from the server.

      The client handler is stopped and removed from the list of active handlers. The player is marked as disconnected, a DisconnectedPlayer event is added to the action queue, and the associated socket is closed.