Class RMIHeartbeat
java.lang.Object
it.polimi.ingsw.gc14.Network.RMI.Server.RMIHeartbeat
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
DisconnectedPlayerevent to the action queue.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BlockingQueue<NetworkEvent> Shared event queue; aDisconnectedPlayerevent is posted here on timeout.private final Map<String, IClientCallback> Registry of active RMI callbacks; the entry for this player is removed on disconnect.private longLast time a ping was received from this client.private final LimitedMap<String, Boolean> Shared map used to mark the player as offline on timeout.private booleantruewhile the watchdog is running; set tofalseon first disconnect.private StringUsername of the client monitored by this watchdog.private final ScheduledExecutorServiceSingle-thread executor that periodically checks whether the silence threshold has been exceeded. -
Constructor Summary
ConstructorsConstructorDescriptionRMIHeartbeat(String username, LimitedMap<String, Boolean> playerList, Map<String, IClientCallback> clients, BlockingQueue<NetworkEvent> actionQueue) Constructs an RMI heartbeat handler for the specified client. -
Method Summary
Modifier and TypeMethodDescriptionvoidDisconnects the monitored RMI client.voidCalled byRMIServer.ping(String)each time the client pings.voidstart()Called byRMIServerwhenever it starts tracking this player.
-
Field Details
-
username
Username of the client monitored by this watchdog. -
playerList
Shared map used to mark the player as offline on timeout. -
clients
Registry of active RMI callbacks; the entry for this player is removed on disconnect. -
actionQueue
Shared event queue; aDisconnectedPlayerevent is posted here on timeout. -
lastPingTime
private volatile long lastPingTimeLast time a ping was received from this client. -
running
private volatile boolean runningtruewhile the watchdog is running; set tofalseon first disconnect. -
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 byRMIServerwhenever it starts tracking this player. -
receivePing
public void receivePing()Called byRMIServer.ping(String)each time the client pings. Resets the silence timer — mirrors writinglastReceivedTimeinHeartbeatHandler. -
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.
-