491 lines
20 KiB
HTML
491 lines
20 KiB
HTML
|
|
|
|
|
|
<!DOCTYPE html>
|
|
<html id="htmlId">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
|
|
<title>Coverage Report > RMIServer</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.RMI.Server</a>
|
|
</div>
|
|
|
|
<h1>Coverage Summary for Class: RMIServer (it.polimi.ingsw.gc14.Network.RMI.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">RMIServer</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/17)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/32)
|
|
</span>
|
|
</td>
|
|
<td class="coverageStat">
|
|
<span class="percent">
|
|
0%
|
|
</span>
|
|
<span class="absValue">
|
|
(0/88)
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<br/>
|
|
<br/>
|
|
|
|
|
|
<pre>
|
|
<code class="sourceCode" id="sourceCode"> package it.polimi.ingsw.gc14.Network.RMI.Server;
|
|
|
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
|
import it.polimi.ingsw.gc14.ErrorType;
|
|
import it.polimi.ingsw.gc14.LimitedMap;
|
|
import it.polimi.ingsw.gc14.Model.Game;
|
|
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
|
|
import it.polimi.ingsw.gc14.Model.MiniModel;
|
|
import it.polimi.ingsw.gc14.Network.NetworkConfig;
|
|
import it.polimi.ingsw.gc14.Network.NetworkEvent;
|
|
import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
|
|
import it.polimi.ingsw.gc14.Network.RMI.Common.IClientCallback;
|
|
import it.polimi.ingsw.gc14.Network.RMI.Common.IGameServer;
|
|
|
|
import java.rmi.RemoteException;
|
|
import java.rmi.registry.LocateRegistry;
|
|
import java.rmi.registry.Registry;
|
|
import java.rmi.server.UnicastRemoteObject;
|
|
import java.util.ArrayList;
|
|
import java.util.Map;
|
|
import java.util.concurrent.BlockingQueue;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.rmi.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
* RMI server responsible for handling remote client connections,
|
|
* receiving player actions, and propagating game updates.
|
|
*
|
|
* <p>The server manages player registration, reconnection handling,
|
|
* heartbeat monitoring, and the forwarding of client requests to the
|
|
* shared network event queue.
|
|
*/
|
|
public class RMIServer extends UnicastRemoteObject implements IGameServer {
|
|
private final String host;
|
|
private final GameController controller;
|
|
private Registry registry;
|
|
private final int nPort;
|
|
|
|
/** username → callback */
|
|
<b class="nc"> private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();</b>
|
|
|
|
/**
|
|
* username → watchdog.
|
|
* One watchdog per connected player, mirrors {@code pendingHeartbeat} / per-socket
|
|
* HeartbeatHandler in the TCP stack.
|
|
*/
|
|
<b class="nc"> private final Map<String, RMIHeartbeat> watchdogs = new ConcurrentHashMap<>();</b>
|
|
|
|
private final BlockingQueue<NetworkEvent> actionQueue;
|
|
private final LimitedMap<String, Boolean> playerList;
|
|
|
|
|
|
/**
|
|
* Constructs an RMI server with the required game and network components.
|
|
*
|
|
* @param controller the game controller used to manage the game logic.
|
|
* @param nPort the port used by the RMI registry.
|
|
* @param actionQueue the queue containing incoming network events.
|
|
* @param playerList the map storing the connection status of the players.
|
|
* @param host the hostname or IP address exposed by the RMI server.
|
|
* @throws RemoteException if the remote object cannot be exported.
|
|
*/
|
|
public RMIServer(GameController controller, int nPort,
|
|
BlockingQueue<NetworkEvent> actionQueue,
|
|
LimitedMap<String, Boolean> playerList,
|
|
<b class="nc"> String host) throws RemoteException {</b>
|
|
<b class="nc"> this.controller = controller;</b>
|
|
<b class="nc"> this.nPort = nPort;</b>
|
|
<b class="nc"> this.actionQueue = actionQueue;</b>
|
|
<b class="nc"> this.playerList = playerList;</b>
|
|
<b class="nc"> this.host = host;</b>
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Join
|
|
// -------------------------------------------------------------------------
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*
|
|
* <p>After a successful join an {@link RMIHeartbeat} watchdog is created and
|
|
* started for the new player — mirrors creating a {@code HeartbeatHandler} in
|
|
* {@code TCPServer.acceptHeartbeat()}.
|
|
*
|
|
* @return {@code null} on success; an {@link it.polimi.ingsw.gc14.ErrorType} value on rejection.
|
|
*/
|
|
@Override
|
|
public ErrorType joinGame(String username, int preferredInt, IClientCallback callback)
|
|
throws RemoteException {
|
|
<b class="nc"> if (preferredInt < NetworkConfig.MIN_PLAYERS || preferredInt > NetworkConfig.MAX_PLAYERS)</b>
|
|
<b class="nc"> return ErrorType.WRONG_PLAYER_NUMBER;</b>
|
|
|
|
<b class="nc"> synchronized (controller) {</b>
|
|
|
|
<b class="nc"> if (playerList.isEmpty() && controller.getModel()==null) {</b>
|
|
<b class="nc"> controller.setModel(new Game(preferredInt));</b>
|
|
<b class="nc"> playerList.setLimit(preferredInt);</b>
|
|
<b class="nc"> System.out.println("Game Created With :"+preferredInt+" Players");</b>
|
|
}
|
|
<b class="nc"> if(controller.getModel().getCurrentState().getGameStage()!= GameStages.WAITING ) {</b>
|
|
<b class="nc"> if (controller.getModel().getPlayers().stream().noneMatch(p -> p.getUserName().equals(username))|| controller.getModel().getCurrentState().getGameStage()==GameStages.ENDED) {</b>
|
|
<b class="nc"> return ErrorType.GAME_ALREADY_STARTED;</b>
|
|
}
|
|
}
|
|
else {
|
|
<b class="nc"> if (controller.addPlayer(username)) {</b>
|
|
<b class="nc"> clients.put(username, callback);</b>
|
|
<b class="nc"> playerList.put(username, true);</b>
|
|
<b class="nc"> startWatchdog(username);</b>
|
|
<b class="nc"> System.out.println("Accepted player: " + username);</b>
|
|
<b class="nc"> return null;</b>
|
|
} else {
|
|
<b class="nc"> return ErrorType.USERNAME_ALREADY_USED;</b>
|
|
}
|
|
}
|
|
|
|
|
|
<b class="nc"> if(!playerList.containsKey(username)){</b>
|
|
<b class="nc"> clients.put(username, callback);</b>
|
|
<b class="nc"> playerList.put(username, true);</b>
|
|
<b class="nc"> startWatchdog(username);</b>
|
|
<b class="nc"> System.out.println("(After crash)Reconnected player: " + username);</b>
|
|
<b class="nc"> return null;</b>
|
|
}
|
|
else {
|
|
<b class="nc"> if(!playerList.get(username))</b>
|
|
{
|
|
<b class="nc"> playerList.put(username, true);</b>
|
|
<b class="nc"> clients.put(username, callback);</b>
|
|
<b class="nc"> System.out.println("Reconnected player: " + username);</b>
|
|
<b class="nc"> startWatchdog(username);</b>
|
|
<b class="nc"> Game game = controller.getModel();</b>
|
|
<b class="nc"> callback.onGameInit(new MiniModel(</b>
|
|
<b class="nc"> game.getSlotMap(), game.getOrderLogicCard(), game.getCurrentState(),</b>
|
|
<b class="nc"> game.getPlayers(), game.getAvailableTotems(),</b>
|
|
<b class="nc"> game.getUpperListTribeCards(), game.getLowerListTribeCards(),</b>
|
|
<b class="nc"> game.getUpperListBuilding(), game.getLowerListBuilding(),</b>
|
|
<b class="nc"> disconnectedUsernames(game)));</b>
|
|
<b class="nc"> System.out.println("Model sent: " + username);</b>
|
|
<b class="nc"> actionQueue.add(new ReconnectPlayer(username));</b>
|
|
<b class="nc"> return null;</b>
|
|
}
|
|
else
|
|
<b class="nc"> return ErrorType.USER_ALREADY_CONNECTED;</b>
|
|
}
|
|
<b class="nc"> }</b>
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Heartbeat — called by RMIClient every ~3 s
|
|
// -------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Receives a heartbeat ping from the client.
|
|
* Mirrors the server reading {@code PING} and replying {@code PONG} in
|
|
* {@code HeartbeatHandler.run()}.
|
|
*
|
|
* @param username the username of the pinging client.
|
|
*/
|
|
@Override
|
|
public void ping(String username) throws RemoteException {
|
|
<b class="nc"> RMIHeartbeat wd = watchdogs.get(username);</b>
|
|
<b class="nc"> if (wd != null) wd.receivePing();</b>
|
|
}
|
|
|
|
// -------------------------------------------------------------------------
|
|
// Game model propagation — keep watchdogs in sync
|
|
// -------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Notifies connected RMI clients of a new network event.
|
|
*
|
|
* <p>If the event does not represent an error, it is sent to all registered clients.
|
|
* If it represents an error, it is sent only to the client that requested the action.
|
|
* Communication errors are logged without interrupting the notification process.
|
|
*
|
|
* @param action the network event to send to the clients.
|
|
*/
|
|
public void notifyAll(NetworkEvent action){
|
|
<b class="nc"> for (Map.Entry<String, IClientCallback> entry : clients.entrySet()) {</b>
|
|
<b class="nc"> if (!action.isError() || action.getUsername().equals(entry.getKey())) {</b>
|
|
try{
|
|
<b class="nc"> entry.getValue().onAction(action);</b>
|
|
}
|
|
catch (RemoteException e)
|
|
{
|
|
<b class="nc"> System.out.println("Remote exception: Exception during action sending attempt " + e.getMessage());</b>
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notifies all connected RMI clients of a new game model.
|
|
*
|
|
* <p>The updated mini model is sent to each registered client callback.
|
|
* If a communication error occurs for a client, the exception is logged
|
|
* without interrupting the notification of the remaining clients.
|
|
*
|
|
* @param model the updated mini model to send to the connected clients.
|
|
*/
|
|
public void notifyAll(MiniModel model) {
|
|
<b class="nc"> for (IClientCallback cb : clients.values()) {</b>
|
|
try{
|
|
<b class="nc"> cb.onGameInit(model);</b>
|
|
}
|
|
catch (RemoteException e)
|
|
{
|
|
<b class="nc"> System.out.println("Remote exception: Exception during model sending attempt " + e.getMessage());</b>
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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"> actionQueue.offer(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"> actionQueue.offer(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"> actionQueue.offer(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"> actionQueue.offer(new DrawLowerBuildingCard(playerUsername,pos));</b>
|
|
}
|
|
|
|
|
|
/**
|
|
* Requests to skip the 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"> actionQueue.offer(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"> actionQueue.offer(new SlotChoice(playerUsername,pos));</b>
|
|
}
|
|
|
|
|
|
/**
|
|
* Enqueues a totem choice event for the specified player.
|
|
*
|
|
* @param playerUsername the username of the player choosing the totem.
|
|
* @param totems the name of the chosen totem.
|
|
* @throws RemoteException if the RMI call fails.
|
|
*/
|
|
public void totemChoice(String playerUsername, String totems) throws RemoteException {
|
|
<b class="nc"> actionQueue.offer(new TotemChoice(playerUsername,totems));</b>
|
|
}
|
|
|
|
|
|
/**
|
|
* Starts the RMI server and binds it to the configured registry.
|
|
*
|
|
* <p>The method configures the server hostname, creates the RMI registry
|
|
* on the specified port, and registers this server instance under the
|
|
* {@code RMIGameServer} name.
|
|
*
|
|
* @return {@code true} if the server starts successfully,
|
|
* {@code false} otherwise.
|
|
*/
|
|
public boolean start() {
|
|
try {
|
|
<b class="nc"> System.setProperty("java.rmi.server.hostname", host);</b>
|
|
<b class="nc"> registry = LocateRegistry.createRegistry(nPort);</b>
|
|
<b class="nc"> registry.rebind("RMIGameServer", this);</b>
|
|
<b class="nc"> System.out.println("RMI Server started on port: " + nPort);</b>
|
|
<b class="nc"> return true;</b>
|
|
} catch (RemoteException e) {
|
|
<b class="nc"> e.printStackTrace();</b>
|
|
<b class="nc"> return false;</b>
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Stops the RMI server and removes its binding from the registry.
|
|
*
|
|
* <p>The remote server object is unexported, the registry entry associated
|
|
* with {@code RMIGameServer} is removed, and the server shutdown is logged.
|
|
*
|
|
* @return {@code true} if the server is stopped successfully,
|
|
* {@code false} otherwise.
|
|
*/
|
|
public boolean stop() {
|
|
try {
|
|
<b class="nc"> registry.unbind("RMIGameServer");</b>
|
|
<b class="nc"> UnicastRemoteObject.unexportObject(this, true);</b>
|
|
<b class="nc"> watchdogs.values().forEach(RMIHeartbeat::disconnect);</b>
|
|
<b class="nc"> watchdogs.clear();</b>
|
|
<b class="nc"> System.out.println("RMI Server stopped");</b>
|
|
<b class="nc"> return true;</b>
|
|
} catch (RemoteException | NotBoundException e) {
|
|
<b class="nc"> e.printStackTrace();</b>
|
|
<b class="nc"> return false;</b>
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Creates and starts an {@link RMIHeartbeat} for the specified player.
|
|
*
|
|
* @param username the username of the player monitored by the heartbeat watchdog.
|
|
*/
|
|
private void startWatchdog(String username) {
|
|
<b class="nc"> RMIHeartbeat wd = new RMIHeartbeat(</b>
|
|
username, playerList, clients, actionQueue);
|
|
<b class="nc"> watchdogs.put(username, wd);</b>
|
|
<b class="nc"> wd.start();</b>
|
|
}
|
|
/**
|
|
* Triggers a voluntary disconnection for the specified player
|
|
* by stopping their heartbeat watchdog.
|
|
*
|
|
* @param username the username of the player to disconnect.
|
|
*/
|
|
@Override
|
|
public void disconnectPlayer(String username) {
|
|
<b class="nc"> RMIHeartbeat wd = watchdogs.get(username);</b>
|
|
<b class="nc"> if (wd != null) wd.disconnect();</b>
|
|
}
|
|
|
|
/** Returns usernames of players currently marked as disconnected in the given game. */
|
|
private static ArrayList<String> disconnectedUsernames(Game game) {
|
|
<b class="nc"> return game.getDisconnectedPlayers().entrySet().stream()</b>
|
|
<b class="nc"> .filter(Map.Entry::getValue)</b>
|
|
<b class="nc"> .map(e -> e.getKey().getUserName())</b>
|
|
<b class="nc"> .collect(Collectors.toCollection(ArrayList::new));</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-14 21:53</div>
|
|
</div>
|
|
</body>
|
|
</html>
|