Class RMIHeartbeat

java.lang.Object
it.polimi.ingsw.gc14.Network.RMI.Server.RMIHeartbeat

public class RMIHeartbeat extends Object
Server-side heartbeat watchdog for a single RMI client.

Mirrors HeartbeatHandler used in the TCP stack, but adapted for RMI: instead of reading raw bytes from a dedicated socket, it relies on receivePing() being called by RMIServer.ping(String) every time the client sends a ping.

If no ping is received within 5000L ms, the player is considered disconnected and disconnect() is invoked, which:

  • stops the watchdog;
  • marks the player as offline in playerList;
  • removes the callback from clients;
  • adds a DisconnectedPlayer event to the action queue.
  • Field Details

    • username

      private String username
      Username of the client monitored by this watchdog.
    • playerList

      private final LimitedMap<String,Boolean> playerList
      Shared map used to mark the player as offline on timeout.
    • clients

      private final Map<String, IClientCallback> clients
      Registry of active RMI callbacks; the entry for this player is removed on disconnect.
    • actionQueue

      private final BlockingQueue<NetworkEvent> actionQueue
      Shared event queue; a DisconnectedPlayer event is posted here on timeout.
    • lastPingTime

      private volatile long lastPingTime
      Last time a ping was received from this client.
    • running

      private volatile boolean running
      true while the watchdog is running; set to false on first disconnect.
    • watchdog

      private final ScheduledExecutorService watchdog
      Single-thread executor that periodically checks whether the silence threshold has been exceeded.
  • Constructor Details

    • RMIHeartbeat

      public RMIHeartbeat(String username, LimitedMap<String,Boolean> playerList, Map<String, IClientCallback> clients, BlockingQueue<NetworkEvent> actionQueue)
      Constructs an RMI heartbeat handler for the specified client.
      Parameters:
      username - the username of the client monitored by the heartbeat.
      playerList - the map storing the connection status of the players.
      clients - the collection of currently registered RMI clients.
      actionQueue - the queue containing network events to be processed.
  • Method Details

    • start

      public void start()
      Called by RMIServer whenever it starts tracking this player.
    • receivePing

      public void receivePing()
      Called by RMIServer.ping(String) each time the client pings. Resets the silence timer — mirrors writing lastReceivedTime in HeartbeatHandler.
    • disconnect

      public void disconnect()
      Disconnects the monitored RMI client.

      The heartbeat handler is stopped, the player is marked as offline, the associated RMI callback is removed, and a disconnection event is added to the action queue for server-side processing.