Class HeartbeatHandler

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

public class HeartbeatHandler extends Object implements Runnable
Handles the heartbeat communication associated with a TCP client.

This component listens for heartbeat PING messages from the client, replies with PONG messages, and periodically checks whether the client has remained silent for too long. If a timeout or communication error occurs, both the heartbeat connection and the main client connection are disconnected.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final InputStream
    Input stream of socket used to receive PING bytes from the client.
    private long
    Timestamp of the last received heartbeat message.
    private final ClientHandler
    Main client handler associated with this heartbeat connection.
    private final OutputStream
    Output stream of socket used to send PONG bytes back to the client.
    private static final int
    Byte value representing a heartbeat ping message.
    private static final int
    Byte value representing a heartbeat pong response.
    private boolean
    Indicates whether the heartbeat handler is still active.
    private final Socket
    Dedicated socket used solely for heartbeat ping/pong exchange.
    private final String
    Username of the client associated with this heartbeat connection.
    Executor used to periodically check heartbeat timeouts.
  • Constructor Summary

    Constructors
    Constructor
    Description
    HeartbeatHandler(String username, Socket socket, ClientHandler mainHandler)
    Constructs a heartbeat handler for the specified client.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    Disconnects the heartbeat channel and the associated main client connection.
    void
    run()
    Starts the heartbeat listening loop.
    private void
    Starts the periodic watchdog task that detects heartbeat timeouts.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • username

      private final String username
      Username of the client associated with this heartbeat connection.
    • socket

      private final Socket socket
      Dedicated socket used solely for heartbeat ping/pong exchange.
    • in

      private final InputStream in
      Input stream of socket used to receive PING bytes from the client.
    • out

      private final OutputStream out
      Output stream of socket used to send PONG bytes back to the client.
    • PING

      private static final int PING
      Byte value representing a heartbeat ping message.
      See Also:
    • PONG

      private static final int PONG
      Byte value representing a heartbeat pong response.
      See Also:
    • mainHandler

      private final ClientHandler mainHandler
      Main client handler associated with this heartbeat connection.
    • lastReceivedTime

      private volatile long lastReceivedTime
      Timestamp of the last received heartbeat message.
    • running

      private volatile boolean running
      Indicates whether the heartbeat handler is still active.
    • watchdog

      private final ScheduledExecutorService watchdog
      Executor used to periodically check heartbeat timeouts.
  • Constructor Details

    • HeartbeatHandler

      public HeartbeatHandler(String username, Socket socket, ClientHandler mainHandler) throws IOException
      Constructs a heartbeat handler for the specified client.
      Parameters:
      username - the username of the connected client.
      socket - the socket dedicated to heartbeat communication.
      mainHandler - the main client handler associated with the same player.
      Throws:
      IOException - if the input or output stream cannot be obtained from the socket.
  • Method Details

    • run

      public void run()
      Starts the heartbeat listening loop.

      The method activates the watchdog task, then waits for incoming heartbeat messages. When a PING is received, the last-received timestamp is updated and a PONG response is sent back to the client. If the stream is closed or an I/O error occurs, the client is disconnected.

      Specified by:
      run in interface Runnable
    • startWatchdog

      private void startWatchdog()
      Starts the periodic watchdog task that detects heartbeat timeouts.

      If no heartbeat message is received within the configured silence threshold, the associated client is disconnected.

    • disconnect

      private void disconnect()
      Disconnects the heartbeat channel and the associated main client connection.

      The handler is stopped, the watchdog task is terminated, the main ClientHandler is disconnected, and the heartbeat socket is closed.