Merge branch 'main' into javadoc-fixes

This commit is contained in:
rubenpirreram
2026-05-06 17:36:37 +02:00
committed by GitHub
191 changed files with 2398 additions and 367 deletions
@@ -1,8 +1,33 @@
package it.polimi.ingsw.gc14.Network;
import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
import java.rmi.RemoteException;
import java.util.Objects;
public interface IClient {
public boolean connect(String username,int preferredInt);
public void doEvent(NetworkEvent event) ;
public void drawUpperTribeCard(String playerUsername, int pos) throws RemoteException;
public void drawLowerTribeCard(String playerUsername,int pos) throws RemoteException;
public void drawUpperBuildingCard(String playerUsername,int pos) throws RemoteException;
public void drawLowerBuildingCard(String playerUsername,int pos) throws RemoteException;
public void skipUpper(String playerUsername) throws RemoteException;
public void skipLower(String playerUsername) throws RemoteException;
public void pickOptionalTribeCard(String playerUsername,int pos) throws RemoteException;
public void pickOptionalBuildingCard(String playerUsername,int pos) throws RemoteException;
public void noOptionalCard(String playerUsername) throws RemoteException;
public void slotChoice(String playerUsername,int pos) throws RemoteException;
}
@@ -1,7 +0,0 @@
package it.polimi.ingsw.gc14.Network;
import it.polimi.ingsw.gc14.Model.Game;
public interface Observer {
public void update(Game model);
}
@@ -53,7 +53,7 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa
@Override
public void onAction(NetworkEvent event) throws RemoteException {
if(event.getIsError()) {
System.out.println(event.toString());
clientController.view.showError(event.toString());
} else {
event.apply(clientController.localController);
clientController.view.render();
@@ -1,11 +1,14 @@
package it.polimi.ingsw.gc14.Network.RMI.Client;
import java.net.InetAddress;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Objects;
import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Network.IClient;
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 it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
@@ -27,6 +30,7 @@ public class RMIClient implements IClient {
/** Client game's controller */
ClientController controller;
private String myIP;
/**
* Class constructor.
@@ -34,10 +38,11 @@ public class RMIClient implements IClient {
* @param host the host address of the RMI server
* @param port the port of the RMI server
*/
public RMIClient(ClientController controller, String host, int port) {
public RMIClient(ClientController controller, String host, int port, String myIP) {
this.controller=controller;
this.host = host;
this.port = port;
this.myIP = myIP;
}
@@ -51,6 +56,7 @@ public class RMIClient implements IClient {
*/
public boolean connect(String username,int preferredInt) {
try {
System.setProperty("java.rmi.server.hostname", this.myIP);
Registry registry = LocateRegistry.getRegistry(host, port);
this.stub = (IGameServer) registry.lookup("RMIGameServer");
ClientCallbackImpl callback = new ClientCallbackImpl(controller);
@@ -64,17 +70,111 @@ public class RMIClient implements IClient {
}
/**
* Sends a {@link NetworkEvent} to the server.
* @param event the event to send
* @throws RemoteException if any RMI error occurs
*/
public void doEvent(NetworkEvent event) {
try {
stub.doEvent(event);
}catch (Exception e) {
}
/**
* 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) throws RemoteException {
stub.drawUpperTribeCard(playerUsername,pos);
}
/**
* 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) throws RemoteException {
stub.drawLowerTribeCard(playerUsername,pos);
}
/**
* 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) throws RemoteException {
stub.drawUpperBuildingCard(playerUsername,pos);
}
/**
* 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) throws RemoteException {
stub.drawLowerBuildingCard(playerUsername,pos);
}
/**
* Requests to skip drawing from the upper list.
* This action is available only when the upper list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipUpper(String playerUsername) throws RemoteException {
stub.skipUpper(playerUsername);
}
/**
* Requests to skip drawing from the lower list.
* This action is available only when the lower list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipLower(String playerUsername) throws RemoteException {
stub.skipLower(playerUsername);
}
/**
* Used to draw a tribe card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalTribeCard(String playerUsername,int pos) throws RemoteException {
stub.pickOptionalTribeCard(playerUsername,pos);
}
/**
* Used to draw a building card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalBuildingCard(String playerUsername,int pos) throws RemoteException {
stub.pickOptionalBuildingCard(playerUsername,pos);
}
/**
* Used to skip the action of drawing a card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
*/
public void noOptionalCard(String playerUsername) throws RemoteException {
stub.noOptionalCard(playerUsername);
}
/**
* 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) throws RemoteException {
stub.slotChoice(playerUsername,pos);
}
}
@@ -30,5 +30,27 @@ public interface IGameServer extends Remote {
* @throws RemoteException if an RMI communication error occurs.
*/
boolean doEvent(NetworkEvent event) throws RemoteException;
void drawUpperTribeCard(String playerUsername, int pos) throws RemoteException;
void drawLowerTribeCard(String playerUsername,int pos) throws RemoteException;
void drawUpperBuildingCard(String playerUsername,int pos) throws RemoteException;
void drawLowerBuildingCard(String playerUsername,int pos) throws RemoteException;
void skipUpper(String playerUsername) throws RemoteException;
void skipLower(String playerUsername) throws RemoteException;
void pickOptionalTribeCard(String playerUsername,int pos) throws RemoteException;
void pickOptionalBuildingCard(String playerUsername,int pos) throws RemoteException;
void noOptionalCard(String playerUsername) throws RemoteException;
void slotChoice(String playerUsername,int pos) throws RemoteException;
}
@@ -4,14 +4,18 @@ import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.LimitedList;
import it.polimi.ingsw.gc14.Model.Game;
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.net.InetAddress;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
@@ -22,7 +26,7 @@ import java.rmi.*;
* Server RMI. Exposes a method to join the game and one to execute an event.
*/
public class RMIServer extends UnicastRemoteObject implements IGameServer {
private String host;
/** Server game's controller */
private GameController controller;
@@ -56,11 +60,12 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
* @param playerList The player's usernames list
* @throws RemoteException if an RMI error occurs
*/
public RMIServer(GameController controller, int nPort, BlockingQueue<NetworkEvent> actionQueue,LimitedList<String> playerList) throws RemoteException {
public RMIServer(GameController controller, int nPort, BlockingQueue<NetworkEvent> actionQueue,LimitedList<String> playerList,String host) throws RemoteException {
this.controller = controller;
this.nPort = nPort;
this.actionQueue = actionQueue;
this.playerList = playerList;
this.host = host;
}
@@ -76,7 +81,6 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
* @param callback The client's callback interface
* @return true if the player successfully joined the game, false otherwise
*/
@Override
public boolean joinGame(String username, int preferredInt, IClientCallback callback) {
if (preferredInt<2 || preferredInt>5) {
return false;
@@ -90,6 +94,7 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
if (controller.addPlayer(username)) {
clients.put(username, callback);
playerList.add(username);
System.out.println("Accepted player: " + username);
return true;
}
return false;
@@ -103,11 +108,114 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
* @param action The desired actio
* @return true if the action was successfully added, false otherwise
*/
@Override
public boolean doEvent(NetworkEvent action) {
return actionQueue.offer(action);
}
/**
* 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) {
actionQueue.offer(new DrawUpperTribeCard(playerUsername,pos));
}
/**
* 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) {
actionQueue.offer(new DrawLowerTribeCard(playerUsername,pos));
}
/**
* 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) {
actionQueue.offer(new DrawUpperBuildingCard(playerUsername,pos));
}
/**
* 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) {
actionQueue.offer(new DrawLowerBuildingCard(playerUsername,pos));
}
/**
* Requests to skip drawing from the upper list.
* This action is available only when the upper list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipUpper(String playerUsername) {
actionQueue.offer(new SkipUpper(playerUsername));
}
/**
* Requests to skip drawing from the lower list.
* This action is available only when the lower list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipLower(String playerUsername) {
actionQueue.offer(new SkipLower(playerUsername));
}
/**
* Used to draw a tribe card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalTribeCard(String playerUsername,int pos) {
actionQueue.offer(new PickOptionalTribeCard(playerUsername,pos));
}
/**
* Used to draw a building card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalBuildingCard(String playerUsername,int pos) {
actionQueue.offer(new PickOptionalBuildingCard(playerUsername,pos));
}
/**
* Used to skip the action of drawing a card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
*/
public void noOptionalCard(String playerUsername) {
actionQueue.offer(new NoOptionalCard(playerUsername));
}
/**
* 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) {
actionQueue.offer(new SlotChoice(playerUsername,pos));
}
// RMI's internal methods
@@ -116,8 +224,9 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
* @param action The desired action
*/
public void notifyAll(NetworkEvent action) throws RemoteException {
for (IClientCallback cb : clients.values()) {
cb.onAction(action);
for (Map.Entry<String,IClientCallback> entry : clients.entrySet()) {
if(!action.getIsError() ||(action.getIsError()&& action.getUsername().equals(entry.getKey())))
entry.getValue().onAction(action);
}
}
@@ -141,6 +250,7 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
*/
public boolean start() {
try {
System.setProperty("java.rmi.server.hostname", host); // o il tuo IP/hostname
registry = LocateRegistry.createRegistry(nPort);
registry.rebind("RMIGameServer", this);
System.out.println("RMI Server started on port: "+nPort);
@@ -4,7 +4,7 @@ import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Network.IClient;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer;
import it.polimi.ingsw.gc14.Network.NetworkEvents.*;
import java.io.*;
import java.net.*;
@@ -87,10 +87,17 @@ public class TCPClient implements IClient {
private void receiveMessage() {
while (true) {
try {
Object read = socketReceive.readObject();
Object read;
try {
read = socketReceive.readObject();
} catch (IOException e) {
e.printStackTrace();
break;
}
if (read instanceof NetworkEvent event) { //TODO: avoid instanceof
if (event.getIsError()) {
System.out.println(event);
controller.view.showError(event.toString());
} else {
event.apply(controller.localController);
controller.view.render();
@@ -99,20 +106,124 @@ public class TCPClient implements IClient {
controller.setModel(model);
controller.view.render();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
e.printStackTrace();
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) {
doEvent(new DrawUpperTribeCard(playerUsername,pos));
}
/**
* 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) {
doEvent(new DrawLowerTribeCard(playerUsername,pos));
}
/**
* 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) {
doEvent(new DrawUpperBuildingCard(playerUsername,pos));
}
/**
* 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) {
doEvent(new DrawLowerBuildingCard(playerUsername,pos));
}
/**
* Requests to skip drawing from the upper list.
* This action is available only when the upper list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipUpper(String playerUsername) {
doEvent(new SkipUpper(playerUsername));
}
/**
* Requests to skip drawing from the lower list.
* This action is available only when the lower list is empty or the player cannot draw any card.
* @param playerUsername the name of the player performing the action
*/
public void skipLower(String playerUsername) {
doEvent(new SkipLower(playerUsername));
}
/**
* Used to draw a tribe card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalTribeCard(String playerUsername,int pos) {
doEvent(new PickOptionalTribeCard(playerUsername,pos));
}
/**
* Used to draw a building card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
* @param pos the index of the card to draw
*/
public void pickOptionalBuildingCard(String playerUsername,int pos) {
doEvent(new PickOptionalBuildingCard(playerUsername,pos));
}
/**
* Used to skip the action of drawing a card from the upper list.
* Available only if the player owns the building 12.
* @param playerUsername the name of the player performing the action
*/
public void noOptionalCard(String playerUsername) {
doEvent(new NoOptionalCard(playerUsername));
}
/**
* 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) {
doEvent(new SlotChoice(playerUsername,pos));
}
/**
* Sends a {@link NetworkEvent} to the server.
* @param event The NetworkEvent to send.
*/
public void doEvent(NetworkEvent event) {
private void doEvent(NetworkEvent event) {
try {
socketSend.writeObject(event);
} catch (IOException e) {
@@ -15,7 +15,13 @@ import java.util.concurrent.BlockingQueue;
* 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;
public String getUsername() {
return username;
}
/** The TCP socket */
private Socket clientSocket;
@@ -44,7 +50,8 @@ public class ClientHandler implements Runnable {
* @param clientHandlers the shared list of all active client handlers.
* @param actionQueue the queue containing incoming events.
*/
public ClientHandler(Socket clientSocket, ObjectOutputStream out, ObjectInputStream in, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
public ClientHandler(String username, Socket clientSocket, ObjectOutputStream out, ObjectInputStream in, List<ClientHandler> clientHandlers, BlockingQueue<NetworkEvent> actionQueue) {
this.username=username;
this.clientSocket = clientSocket;
this.in = in;
this.out = out;
@@ -115,7 +115,7 @@ public class TCPServer {
clientSocket.getOutputStream().write((int) (1));
System.out.println("Accepted player: " + eventAddPlayer.getUsername());
ClientHandler clientHandler = new ClientHandler(clientSocket, clientSend, clientReceive, clientHandlers, actionQueue);
ClientHandler clientHandler = new ClientHandler(eventAddPlayer.getUsername(),clientSocket, clientSend, clientReceive, clientHandlers, actionQueue);
clientHandlers.add(clientHandler);
ConnectedPlayers++;
@@ -145,7 +145,10 @@ public class TCPServer {
* @param event the network event to send to all connected TCP clients.
*/
public void notifyAll(NetworkEvent event){
clientHandlers.forEach((x) -> x.notifyEvent(event));
clientHandlers.forEach((x) -> {
if(!event.getIsError()||(event.getIsError()&& event.getUsername().equals(x.getUsername())))
x.notifyEvent(event);
});
}