Files
Progetto-ingegneria-del-sof…/deliverables/Coverage/htmReport/ns-11/sources/source-2.html
T
2026-06-19 22:57:43 +02:00

250 lines
8.0 KiB
HTML

<!DOCTYPE html>
<html id="htmlId">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Coverage Report > HeartbeatHandler</title>
<style type="text/css">
@import "../../css/coverage.css";
@import "../../css/idea.min.css";
</style>
<script type="text/javascript" src="../../js/highlight.min.js"></script>
<script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
</head>
<body>
<div class="content">
<div class="breadCrumbs">
Current scope: <a href="../../index.html">all classes</a>
<span class="separator">|</span>
<a href="../index.html">it.polimi.ingsw.gc14.Network.TCP.Server</a>
</div>
<h1>Coverage Summary for Class: HeartbeatHandler (it.polimi.ingsw.gc14.Network.TCP.Server)</h1>
<table class="coverageStats">
<tr>
<th class="name">Class</th>
<th class="coverageStat
">
Class, %
</th>
<th class="coverageStat
">
Method, %
</th>
<th class="coverageStat
">
Branch, %
</th>
<th class="coverageStat
">
Line, %
</th>
</tr>
<tr>
<td class="name">HeartbeatHandler</td>
<td class="coverageStat">
<span class="percent">
0%
</span>
<span class="absValue">
(0/1)
</span>
</td>
<td class="coverageStat">
<span class="percent">
0%
</span>
<span class="absValue">
(0/5)
</span>
</td>
<td class="coverageStat">
<span class="percent">
0%
</span>
<span class="absValue">
(0/8)
</span>
</td>
<td class="coverageStat">
<span class="percent">
0%
</span>
<span class="absValue">
(0/28)
</span>
</td>
</tr>
</table>
<br/>
<br/>
<pre>
<code class="sourceCode" id="sourceCode">&nbsp;package it.polimi.ingsw.gc14.Network.TCP.Server;
&nbsp;
&nbsp;import java.io.*;
&nbsp;import java.net.*;
&nbsp;import java.util.concurrent.Executors;
&nbsp;import java.util.concurrent.ScheduledExecutorService;
&nbsp;import java.util.concurrent.TimeUnit;
&nbsp;import it.polimi.ingsw.gc14.Network.NetworkConfig;
&nbsp;
&nbsp;/**
&nbsp; * Handles the heartbeat communication associated with a TCP client.
&nbsp; *
&nbsp; * &lt;p&gt;This component listens for heartbeat {@code PING} messages from the client,
&nbsp; * replies with {@code PONG} messages, and periodically checks whether the client
&nbsp; * has remained silent for too long. If a timeout or communication error occurs,
&nbsp; * both the heartbeat connection and the main client connection are disconnected.
&nbsp; */
&nbsp;public class HeartbeatHandler implements Runnable {
&nbsp; /** Username of the client associated with this heartbeat connection. */
&nbsp; private final String username;
&nbsp; /** Dedicated socket used solely for heartbeat ping/pong exchange. */
&nbsp; private final Socket socket;
&nbsp; /** Input stream of {@link #socket} used to receive {@code PING} bytes from the client. */
&nbsp; private final InputStream in;
&nbsp; /** Output stream of {@link #socket} used to send {@code PONG} bytes back to the client. */
&nbsp; private final OutputStream out;
&nbsp;
&nbsp; /**
&nbsp; * Byte value representing a heartbeat ping message.
&nbsp; */
&nbsp; private static final int PING = 1;
&nbsp;
&nbsp; /**
&nbsp; * Byte value representing a heartbeat pong response.
&nbsp; */
&nbsp; private static final int PONG = 2;
&nbsp;
&nbsp; /**
&nbsp; * Main client handler associated with this heartbeat connection.
&nbsp; */
&nbsp; private final ClientHandler mainHandler;
&nbsp;
&nbsp; /**
&nbsp; * Timestamp of the last received heartbeat message.
&nbsp; */
<b class="nc">&nbsp; private volatile long lastReceivedTime = System.currentTimeMillis();</b>
&nbsp;
&nbsp; /**
&nbsp; * Indicates whether the heartbeat handler is still active.
&nbsp; */
<b class="nc">&nbsp; private volatile boolean running = true;</b>
&nbsp;
&nbsp;
&nbsp; /**
&nbsp; * Executor used to periodically check heartbeat timeouts.
&nbsp; */
<b class="nc">&nbsp; private final ScheduledExecutorService watchdog =</b>
<b class="nc">&nbsp; Executors.newSingleThreadScheduledExecutor();</b>
&nbsp;
&nbsp; /**
&nbsp; * Constructs a heartbeat handler for the specified client.
&nbsp; *
&nbsp; * @param username the username of the connected client.
&nbsp; * @param socket the socket dedicated to heartbeat communication.
&nbsp; * @param mainHandler the main client handler associated with the same player.
&nbsp; * @throws IOException if the input or output stream cannot be obtained from the socket.
&nbsp; */
&nbsp; public HeartbeatHandler(String username, Socket socket, ClientHandler mainHandler)
<b class="nc">&nbsp; throws IOException {</b>
<b class="nc">&nbsp; this.username = username;</b>
<b class="nc">&nbsp; this.socket = socket;</b>
<b class="nc">&nbsp; this.mainHandler = mainHandler;</b>
<b class="nc">&nbsp; this.in = socket.getInputStream();</b>
<b class="nc">&nbsp; this.out = socket.getOutputStream();</b>
&nbsp; }
&nbsp;
&nbsp; /**
&nbsp; * Starts the heartbeat listening loop.
&nbsp; *
&nbsp; * &lt;p&gt;The method activates the watchdog task, then waits for incoming heartbeat
&nbsp; * messages. When a {@code PING} is received, the last-received timestamp is
&nbsp; * updated and a {@code PONG} response is sent back to the client.
&nbsp; * If the stream is closed or an I/O error occurs, the client is disconnected.
&nbsp; */
&nbsp; @Override
&nbsp; public void run() {
<b class="nc">&nbsp; startWatchdog();</b>
&nbsp; try {
<b class="nc">&nbsp; while (running) {</b>
<b class="nc">&nbsp; int b = in.read();</b>
<b class="nc">&nbsp; if (b == -1) {</b>
<b class="nc">&nbsp; disconnect();</b>
&nbsp; break;
&nbsp; }
<b class="nc">&nbsp; if (b == PING) {</b>
<b class="nc">&nbsp; lastReceivedTime = System.currentTimeMillis();</b>
<b class="nc">&nbsp; out.write(PONG);</b>
<b class="nc">&nbsp; out.flush();</b>
&nbsp; }
&nbsp; }
&nbsp; } catch (IOException e) {
<b class="nc">&nbsp; disconnect();</b>
&nbsp; }
&nbsp; }
&nbsp;
&nbsp; /**
&nbsp; * Starts the periodic watchdog task that detects heartbeat timeouts.
&nbsp; *
&nbsp; * &lt;p&gt;If no heartbeat message is received within the configured silence threshold,
&nbsp; * the associated client is disconnected.
&nbsp; */
&nbsp; private void startWatchdog() {
<b class="nc">&nbsp; watchdog.scheduleAtFixedRate(() -&gt; {</b>
<b class="nc">&nbsp; if (System.currentTimeMillis() - lastReceivedTime &gt; NetworkConfig.SILENCE_THRESHOLD_MS) {</b>
<b class="nc">&nbsp; System.out.println(&quot;Heartbeat timeout: &quot; + username);</b>
<b class="nc">&nbsp; disconnect();</b>
&nbsp; }
&nbsp; }, 1, 1, TimeUnit.SECONDS);
&nbsp; }
&nbsp;
&nbsp; /**
&nbsp; * Disconnects the heartbeat channel and the associated main client connection.
&nbsp; *
&nbsp; * &lt;p&gt;The handler is stopped, the watchdog task is terminated, the main
&nbsp; * {@link ClientHandler} is disconnected, and the heartbeat socket is closed.
&nbsp; */
&nbsp; private synchronized void disconnect() {
<b class="nc">&nbsp; running = false;</b>
<b class="nc">&nbsp; watchdog.shutdownNow();</b>
<b class="nc">&nbsp; mainHandler.disconnect();</b>
<b class="nc">&nbsp; try { socket.close(); } catch (IOException ignored) {}</b>
&nbsp; }
&nbsp;}
</code>
</pre>
</div>
<script type="text/javascript">
(function() {
var msie = false, msie9 = false;
/*@cc_on
msie = true;
@if (@_jscript_version >= 9)
msie9 = true;
@end
@*/
if (!msie || msie && msie9) {
hljs.highlightAll()
hljs.initLineNumbersOnLoad();
}
})();
</script>
<div class="footer">
<div style="float:right;">generated on 2026-06-19 22:53</div>
</div>
</body>
</html>