FIx: Fixed Coverage Screenshots And htmlRepot.
This commit is contained in:
@@ -0,0 +1,442 @@
|
||||
|
||||
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html id="htmlId">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
|
||||
<title>Coverage Report > TCPClient</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.Client</a>
|
||||
</div>
|
||||
|
||||
<h1>Coverage Summary for Class: TCPClient (it.polimi.ingsw.gc14.Network.TCP.Client)</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">TCPClient</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/15)
|
||||
</span>
|
||||
</td>
|
||||
<td class="coverageStat">
|
||||
<span class="percent">
|
||||
0%
|
||||
</span>
|
||||
<span class="absValue">
|
||||
(0/24)
|
||||
</span>
|
||||
</td>
|
||||
<td class="coverageStat">
|
||||
<span class="percent">
|
||||
0%
|
||||
</span>
|
||||
<span class="absValue">
|
||||
(0/83)
|
||||
</span>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
|
||||
<pre>
|
||||
<code class="sourceCode" id="sourceCode"> package it.polimi.ingsw.gc14.Network.TCP.Client;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.ClientController;
|
||||
import it.polimi.ingsw.gc14.ErrorType;
|
||||
import it.polimi.ingsw.gc14.Model.MiniModel;
|
||||
import it.polimi.ingsw.gc14.Network.IClient;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkConfig;
|
||||
import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Client TCP. Sends and receives messages with the TCP server.
|
||||
*/
|
||||
public class TCPClient implements IClient {
|
||||
/** Byte value sent by the client to signal it is still alive. */
|
||||
private static final int PING = 1;
|
||||
/** Byte value expected from the server in response to a {@link #PING}. */
|
||||
private static final int PONG = 2;
|
||||
|
||||
/** Socket TCP */
|
||||
private Socket communicationSocket;
|
||||
|
||||
/** Input stream used receive objects from the server */
|
||||
private ObjectInputStream socketReceive;
|
||||
|
||||
/** Output stream used to send objects to the server */
|
||||
private ObjectOutputStream socketSend;
|
||||
|
||||
/** Client game's controller */
|
||||
private final ClientController controller;
|
||||
|
||||
/** IP address of the server to connect to */
|
||||
private final String hostname;
|
||||
/** {@code true} while the connection is active; set to {@code false} on disconnect. */
|
||||
private boolean running;
|
||||
/** TCP port */
|
||||
private final int mainPort;
|
||||
/** TCP port dedicated to heartbeat communication. */
|
||||
private final int heartbeatPort;
|
||||
/** Socket used exclusively for heartbeat ping/pong exchange. */
|
||||
private Socket heartbeatSocket;
|
||||
/** Output stream of {@link #heartbeatSocket}, used to write {@link #PING} bytes. */
|
||||
private OutputStream heartbeatOut;
|
||||
/** Input stream of {@link #heartbeatSocket}, used to read {@link #PONG} bytes. */
|
||||
private InputStream heartbeatIn;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a TCP client and initializes its connection parameters.
|
||||
*
|
||||
* @param controller the client controller.
|
||||
* @param hostname the IP address or hostname of the server.
|
||||
* @param mainPort the main TCP port of the server.
|
||||
* @param heartbeatPort the TCP port used for heartbeat communication.
|
||||
*/
|
||||
<b class="nc"> public TCPClient(ClientController controller, String hostname, int mainPort,int heartbeatPort ) {</b>
|
||||
<b class="nc"> this.controller = controller;</b>
|
||||
<b class="nc"> this.hostname = hostname;</b>
|
||||
<b class="nc"> this.mainPort = mainPort;</b>
|
||||
<b class="nc"> this.heartbeatPort = heartbeatPort;</b>
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts the TCP connection with the server.
|
||||
* Sends an {@link AddPlayer} event, if the server responds with {@code -1}, the connection is refused and the method returns {@code false}.
|
||||
* Otherwise, a listener thread is started.
|
||||
* @param user The username of the player
|
||||
* @param proposedNPlayers The desired number of players for the game
|
||||
* @return null if the connection is successful, GENERIC_ERROR otherwise.
|
||||
*/
|
||||
public ErrorType connect(String user, int proposedNPlayers) {
|
||||
try {
|
||||
<b class="nc"> communicationSocket = new Socket(hostname, mainPort);</b>
|
||||
<b class="nc"> socketSend = new ObjectOutputStream(communicationSocket.getOutputStream());</b>
|
||||
<b class="nc"> socketReceive = new ObjectInputStream(communicationSocket.getInputStream());</b>
|
||||
<b class="nc"> NetworkEvent event= new AddPlayer(user, proposedNPlayers);</b>
|
||||
<b class="nc"> socketSend.writeObject(event);</b>
|
||||
try {
|
||||
<b class="nc"> if( socketReceive.readObject() instanceof AddPlayer x)</b>
|
||||
{
|
||||
<b class="nc"> if(x.isError())</b>
|
||||
<b class="nc"> return x.getErrorType();</b>
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
<b class="nc"> return ErrorType.GENERIC_ERROR;</b>
|
||||
}
|
||||
|
||||
<b class="nc"> running = true;</b>
|
||||
|
||||
<b class="nc"> new Thread(this::receiveMessage, "tcp-reader").start();</b>
|
||||
|
||||
// heartbeat socket
|
||||
<b class="nc"> this.heartbeatSocket = new Socket(hostname, heartbeatPort);</b>
|
||||
<b class="nc"> this.heartbeatOut = heartbeatSocket.getOutputStream();</b>
|
||||
<b class="nc"> this.heartbeatIn = heartbeatSocket.getInputStream();</b>
|
||||
|
||||
// send username immediately so server can associate the two sockets
|
||||
<b class="nc"> new ObjectOutputStream(heartbeatSocket.getOutputStream()).writeObject(user);</b>
|
||||
<b class="nc"> heartbeatOut.flush();</b>
|
||||
<b class="nc"> new Thread(this::heartbeatLoop, "heartbeat").start();</b>
|
||||
|
||||
<b class="nc"> return null;</b>
|
||||
|
||||
} catch (IOException e) {
|
||||
<b class="nc"> e.printStackTrace();</b>
|
||||
<b class="nc"> return ErrorType.SERVER_UNREACHABLE;</b>
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Sends a {@link #PING} byte to the server every 3 seconds and waits for a {@link #PONG} reply.
|
||||
* If no reply arrives within {@value NetworkConfig#SILENCE_THRESHOLD_MS} ms, the server is
|
||||
* considered unreachable and {@link #disconnect()} is called.
|
||||
*/
|
||||
private void heartbeatLoop() {
|
||||
<b class="nc"> ScheduledExecutorService sender = Executors.newSingleThreadScheduledExecutor();</b>
|
||||
<b class="nc"> sender.scheduleAtFixedRate(() -> {</b>
|
||||
try {
|
||||
<b class="nc"> synchronized (heartbeatOut)</b>
|
||||
{
|
||||
<b class="nc"> heartbeatOut.write(PING);</b>
|
||||
<b class="nc"> heartbeatOut.flush();</b>
|
||||
<b class="nc"> }</b>
|
||||
|
||||
} catch (IOException e) {
|
||||
<b class="nc"> sender.shutdownNow();</b>
|
||||
<b class="nc"> if(running)</b>
|
||||
{
|
||||
<b class="nc"> disconnect();</b>
|
||||
<b class="nc"> controller.getView().showError(ErrorType.SERVER_CRASHED,ErrorType.SERVER_CRASHED.toString());</b>
|
||||
}
|
||||
|
||||
}
|
||||
}, 0, 3, TimeUnit.SECONDS);
|
||||
|
||||
try {
|
||||
<b class="nc"> heartbeatSocket.setSoTimeout((int) NetworkConfig.SILENCE_THRESHOLD_MS);</b>
|
||||
<b class="nc"> while (running) {</b>
|
||||
<b class="nc"> int b = heartbeatIn.read();</b>
|
||||
<b class="nc"> if (b != PONG) {</b>
|
||||
<b class="nc"> disconnect();</b>
|
||||
<b class="nc"> controller.getView().showError(ErrorType.SERVER_CRASHED, ErrorType.SERVER_CRASHED.toString());</b>
|
||||
break;
|
||||
}
|
||||
// pong received: server is alive
|
||||
}
|
||||
} catch (SocketTimeoutException e) {
|
||||
<b class="nc"> System.out.println("Server heartbeat timeout");</b>
|
||||
<b class="nc"> disconnect();</b>
|
||||
<b class="nc"> controller.getView().showError(ErrorType.SERVER_CRASHED, ErrorType.SERVER_CRASHED.toString());</b>
|
||||
} catch (IOException e) {
|
||||
<b class="nc"> if (running) {</b>
|
||||
<b class="nc"> disconnect();</b>
|
||||
<b class="nc"> controller.getView().showError(ErrorType.SERVER_CRASHED, ErrorType.SERVER_CRASHED.toString());</b>
|
||||
}
|
||||
} finally {
|
||||
<b class="nc"> sender.shutdownNow();</b>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnects the player and cleans model and controller
|
||||
*/
|
||||
private synchronized void disconnect() {
|
||||
<b class="nc"> if (!running) return;</b>
|
||||
<b class="nc"> running = false;</b>
|
||||
<b class="nc"> try { communicationSocket.close(); } catch (IOException ignored) {}</b>
|
||||
<b class="nc"> try { heartbeatSocket.close(); } catch (IOException ignored) {}</b>
|
||||
<b class="nc"> controller.setClient(null);</b>
|
||||
<b class="nc"> controller.setModel(null);</b>
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Listens continuously for incoming objects from the server.
|
||||
* - If the received object is a {@link NetworkEvent} flagged as an error, it is printed.
|
||||
* - If the received object is a valid {@link NetworkEvent}, it is applied to the game controller.
|
||||
* - If the received object is a {@link MiniModel}, the controller's model is set.
|
||||
*/
|
||||
private void receiveMessage() {
|
||||
while (true) {
|
||||
try {
|
||||
Object read;
|
||||
try {
|
||||
<b class="nc"> read = socketReceive.readObject();</b>
|
||||
} catch (IOException e) {
|
||||
<b class="nc"> if (running) disconnect();</b>
|
||||
break;
|
||||
}
|
||||
|
||||
<b class="nc"> if (read instanceof NetworkEvent event) {</b>
|
||||
<b class="nc"> if (event.isError()) {</b>
|
||||
<b class="nc"> if(event.getErrorType() == ErrorType.WRONG_ACTION) {</b>
|
||||
<b class="nc"> controller.getView().showError(ErrorType.WRONG_ACTION,event.toString());</b>
|
||||
}
|
||||
else
|
||||
<b class="nc"> controller.getView().showError(event.getErrorType(),event.getErrorType().toString());</b>
|
||||
} else {
|
||||
<b class="nc"> event.apply(controller.getMiniModel());</b>
|
||||
<b class="nc"> controller.getView().render();</b>
|
||||
}
|
||||
|
||||
<b class="nc"> } else if (read instanceof MiniModel model) {</b>
|
||||
<b class="nc"> controller.setModel(model);</b>
|
||||
<b class="nc"> controller.getView().render();</b>
|
||||
}
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
<b class="nc"> e.printStackTrace();</b>
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests to draw a tribe card from the upper list.
|
||||
* Creates a NetworkEvent and sends it through the network client.
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public void drawUpperTribeCard(String playerUsername, int pos) {
|
||||
<b class="nc"> doEvent(new DrawUpperTribeCard(playerUsername,pos));</b>
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Requests to draw a tribe card from the lower list.
|
||||
* Creates a NetworkEvent and sends it through the network client.
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public void drawLowerTribeCard(String playerUsername,int pos) {
|
||||
<b class="nc"> doEvent(new DrawLowerTribeCard(playerUsername,pos));</b>
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Requests to draw a building card from the upper list.
|
||||
* Creates a NetworkEvent and sends it through the network client.
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public void drawUpperBuildingCard(String playerUsername,int pos) {
|
||||
<b class="nc"> doEvent(new DrawUpperBuildingCard(playerUsername,pos));</b>
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Requests to draw a building card from the lower list.
|
||||
* Creates a NetworkEvent and sends it through the network client.
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the card to draw
|
||||
*/
|
||||
public void drawLowerBuildingCard(String playerUsername,int pos) {
|
||||
<b class="nc"> doEvent(new DrawLowerBuildingCard(playerUsername,pos));</b>
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Requests to skip drawing turn.
|
||||
* This action is available only when the player cannot draw any tribe card, but still can buy some buildings.
|
||||
* @param playerUsername the name of the player performing the action
|
||||
*/
|
||||
public void skipTurn(String playerUsername) {
|
||||
<b class="nc"> doEvent(new SkipTurn(playerUsername));</b>
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Used to perform the slot choice action for the specified player at the specified position.
|
||||
* @param playerUsername the name of the player performing the action
|
||||
* @param pos the index of the selected slot
|
||||
*/
|
||||
public void slotChoice(String playerUsername,int pos) {
|
||||
<b class="nc"> doEvent(new SlotChoice(playerUsername,pos));</b>
|
||||
}
|
||||
/**
|
||||
* Sends a {@link NetworkEvent} to the server.
|
||||
* @param event The NetworkEvent to send.
|
||||
*/
|
||||
private void doEvent(NetworkEvent event) {
|
||||
try {
|
||||
<b class="nc"> synchronized (socketSend) {</b>
|
||||
<b class="nc"> socketSend.writeObject(event);</b>
|
||||
<b class="nc"> }</b>
|
||||
} catch (IOException e) {
|
||||
<b class="nc"> e.printStackTrace();</b>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a totem choice event to the server for the specified player.
|
||||
*
|
||||
* @param playerUsername the username of the player choosing the totem.
|
||||
* @param totems the name of the chosen totem.
|
||||
*/
|
||||
public void totemChoice(String playerUsername,String totems) {
|
||||
<b class="nc"> doEvent(new TotemChoice(playerUsername,totems));</b>
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies the server of a voluntary disconnection by sending a sentinel value
|
||||
* on the heartbeat channel, then closes the connection.
|
||||
*/
|
||||
public void notifyDisconnection()
|
||||
{
|
||||
<b class="nc"> synchronized (heartbeatOut)</b>
|
||||
{
|
||||
try {
|
||||
<b class="nc"> heartbeatOut.write(-1);</b>
|
||||
} catch (IOException e) {
|
||||
<b class="nc"> disconnect();</b>
|
||||
}
|
||||
<b class="nc"> }</b>
|
||||
<b class="nc"> disconnect();</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>
|
||||
Reference in New Issue
Block a user