266 lines
8.7 KiB
HTML
266 lines
8.7 KiB
HTML
|
|
|
|
|
|
<!DOCTYPE html>
|
|
<html id="htmlId">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
|
|
<title>Coverage Report > ClientHandler</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: ClientHandler (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">ClientHandler</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/6)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/14)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/31)
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<br/>
|
|
<br/>
|
|
|
|
|
|
<pre>
|
|
<code class="sourceCode" id="sourceCode"> package it.polimi.ingsw.gc14.Network.TCP.Server;
|
|
|
|
import it.polimi.ingsw.gc14.LimitedMap;
|
|
import it.polimi.ingsw.gc14.Model.MiniModel;
|
|
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
|
import it.polimi.ingsw.gc14.Network.NetworkEvents.DisconnectedPlayer;
|
|
|
|
import java.io.*;
|
|
import java.net.*;
|
|
import java.util.List;
|
|
import java.util.concurrent.BlockingQueue;
|
|
|
|
/**
|
|
* Handles the TCP connection with a single client.
|
|
* Each instance runs on a dedicated thread and is responsible for receiving {@link NetworkEvent} from its client and adding them
|
|
* to the {@link #actionQueue}.
|
|
* It is also responsible to send events and game model updates back to the client.
|
|
*/
|
|
public class ClientHandler implements Runnable {
|
|
/**
|
|
* The username of the connected client on this handler
|
|
*/
|
|
private final String username;
|
|
/** {@code true} while the handler is actively reading from the socket; set to {@code false} on disconnect. */
|
|
private volatile boolean running;
|
|
|
|
/**
|
|
* Returns the username associated with this client.
|
|
*
|
|
* @return the username associated with this client.
|
|
*/
|
|
public String getUsername() {
|
|
<b class="nc"> return username;</b>
|
|
}
|
|
/** The TCP socket */
|
|
private final Socket clientSocket;
|
|
|
|
/** Input stream used to receive objects from the client */
|
|
private final ObjectInputStream in;
|
|
|
|
/** Output stream used to send objects to the client */
|
|
private final ObjectOutputStream out;
|
|
|
|
/**
|
|
* Shared list of all client handlers.
|
|
* This handler removes itself from the list when disconnected.
|
|
*/
|
|
private final List<ClientHandler> clientHandlers;
|
|
|
|
/** Maps each connected player's username to their connection state (true = connected). */
|
|
private final LimitedMap<String, Boolean> limitedMap;
|
|
|
|
/** Queue containing the events to be applied to the game model */
|
|
private final BlockingQueue<NetworkEvent> actionQueue;
|
|
|
|
|
|
/**
|
|
* Class constructor that initializes the attributes.
|
|
*
|
|
* @param username the username associated with the client.
|
|
* @param clientSocket the socket representing the client's TCP connection.
|
|
* @param out the output stream used to send data to the client.
|
|
* @param in the input stream used to receive data from the client.
|
|
* @param clientHandlers the shared list of all active client handlers.
|
|
* @param playersMap the shared map containing the connected players.
|
|
* @param actionQueue the queue containing incoming events.
|
|
*/
|
|
<b class="nc"> public ClientHandler(String username, Socket clientSocket, ObjectOutputStream out, ObjectInputStream in, List<ClientHandler> clientHandlers, LimitedMap<String,Boolean> playersMap, BlockingQueue<NetworkEvent> actionQueue) {</b>
|
|
<b class="nc"> this.username=username;</b>
|
|
<b class="nc"> this.clientSocket = clientSocket;</b>
|
|
<b class="nc"> this.in = in;</b>
|
|
<b class="nc"> this.out = out;</b>
|
|
<b class="nc"> this.clientHandlers = clientHandlers;</b>
|
|
<b class="nc"> this.actionQueue = actionQueue;</b>
|
|
<b class="nc"> this.limitedMap = playersMap;</b>
|
|
}
|
|
|
|
|
|
/**
|
|
* Listens for incoming {@link NetworkEvent}s from the client and adds them to the {@link #actionQueue}.
|
|
* Upon disconnection, this handler removes itself from {@link #clientHandlers}.
|
|
*/
|
|
@Override
|
|
public void run() {
|
|
<b class="nc"> running = true;</b>
|
|
try {
|
|
<b class="nc"> while (running) {</b>
|
|
<b class="nc"> NetworkEvent event = (NetworkEvent) in.readObject();</b>
|
|
<b class="nc"> if (!actionQueue.offer(event)) {</b>
|
|
<b class="nc"> System.out.println("Error inserting action into queue");</b>
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
} catch (ClassNotFoundException e) {
|
|
<b class="nc"> System.err.println("Class not found: " + username);</b>
|
|
} finally {
|
|
<b class="nc"> if (running) disconnect();</b>
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Sends a {@link NetworkEvent} to the client.
|
|
* @param event The network event to send to the client.
|
|
*/
|
|
public synchronized void notifyEvent(NetworkEvent event) {
|
|
try {
|
|
<b class="nc"> out.reset();</b>
|
|
<b class="nc"> out.writeObject(event);</b>
|
|
} catch (IOException e) {
|
|
<b class="nc"> e.printStackTrace();</b>
|
|
<b class="nc"> disconnect();</b>
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* Sends the current game model to this client.
|
|
* @param game The current state of the game to send to the client.
|
|
*/
|
|
public synchronized void notifyMiniModel(MiniModel game) {
|
|
try {
|
|
<b class="nc"> out.reset();</b>
|
|
<b class="nc"> out.writeObject(game);</b>
|
|
} catch (IOException e) {
|
|
<b class="nc"> e.printStackTrace();</b>
|
|
<b class="nc"> disconnect();</b>
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Disconnects the client from the server.
|
|
*
|
|
* <p>The client handler is stopped and removed from the list of active handlers.
|
|
* The player is marked as disconnected, a {@link DisconnectedPlayer} event is
|
|
* added to the action queue, and the associated socket is closed.
|
|
*/
|
|
public synchronized void disconnect() {
|
|
<b class="nc"> if (!running) return;</b>
|
|
<b class="nc"> running = false;</b>
|
|
<b class="nc"> clientHandlers.remove(this);</b>
|
|
<b class="nc"> limitedMap.put(username, false);</b>
|
|
<b class="nc"> actionQueue.add(new DisconnectedPlayer(username));</b>
|
|
<b class="nc"> System.err.println("(TCP) Disconnected player: " + username);</b>
|
|
<b class="nc"> try { clientSocket.close(); } catch (IOException ignored) {}</b>
|
|
}
|
|
}
|
|
</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>
|