Class TCPServer

java.lang.Object
it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer

public class TCPServer extends Object
TCP server responsible for accepting client connections, handling player registration and reconnection, and sending game updates to connected clients.

The server also manages a dedicated heartbeat channel used to detect disconnected clients and associate each heartbeat connection with the corresponding ClientHandler.

  • Field Details

    • port

      private final int port
      Main TCP port used for standard client-server communication.
    • heartbeatPort

      private final int heartbeatPort
      TCP port dedicated to heartbeat communication.
    • socketTCP

      private ServerSocket socketTCP
      Main server socket used to accept client connections.
    • heartbeatSocketTCP

      private ServerSocket heartbeatSocketTCP
      Server socket used to accept heartbeat connections.
    • controller

      private final GameController controller
      Game controller used to manage the server-side game logic.
    • actionQueue

      private final BlockingQueue<NetworkEvent> actionQueue
      Queue containing network events received from clients.
    • playerList

      private final LimitedMap<String,Boolean> playerList
      Map storing the online/offline status of connected players.
    • clientHandlers

      private final CopyOnWriteArrayList<ClientHandler> clientHandlers
      List of active TCP client handlers.
    • pendingHeartbeat

      private final Map<String, ClientHandler> pendingHeartbeat
      Temporary map associating each username with the corresponding ClientHandler waiting for its heartbeat connection.
  • Constructor Details

    • TCPServer

      public TCPServer(GameController controller, int port, int heartbeatPort, BlockingQueue<NetworkEvent> actionQueue, LimitedMap<String,Boolean> playerList)
      Constructs a TCP server with the required game and network components.
      Parameters:
      controller - the game controller used to manage the game logic.
      port - the main TCP port used for client communication.
      heartbeatPort - the TCP port dedicated to heartbeat connections.
      actionQueue - the queue containing incoming network events.
      playerList - the map storing the connection status of the players.
  • Method Details

    • start

      public void start()
      Starts the TCP server and begins accepting client connections.

      The method opens both the main TCP server socket and the dedicated heartbeat server socket. It then starts a separate thread for heartbeat connections and continuously waits for new players or reconnecting clients.

      When a new connection is received, the first event must be an AddPlayer request. Depending on the current server state, the connection is handled either as a new player joining the game or as a reconnection attempt.

    • acceptHeartbeat

      private void acceptHeartbeat()
      Accepts heartbeat connections and associates them with the correct client handler.

      Each client immediately sends its username on the heartbeat channel. The method uses that username to retrieve the pending ClientHandler and starts a dedicated HeartbeatHandler. If no pending handler is found, the heartbeat socket is closed.

    • notifyAll

      public void notifyAll(NetworkEvent event)
      Notifies connected TCP clients of a new network event.

      If the event does not represent an error, it is sent to all connected clients. If it represents an error, it is sent only to the client that requested the action.

      Parameters:
      event - the network event to send to the clients.
    • notifyAll

      public void notifyAll(MiniModel model)
      Notifies all connected TCP clients of a new game model.
      Parameters:
      model - the updated mini model to send to the clients.
    • createAndRegisterHandler

      private ClientHandler createAndRegisterHandler(String username, Socket socket, ObjectOutputStream out, ObjectInputStream in)
      Creates a ClientHandler, registers it in pendingHeartbeat and clientHandlers, but does NOT start its thread — callers start the thread after any extra setup (e.g. sending a model snapshot).
    • disconnectedUsernames

      private static ArrayList<String> disconnectedUsernames(Game game)
      Returns usernames of players currently marked as disconnected in the given game.