Skip to content

Commit

Permalink
Merge pull request #247 from mattteochen/formatting
Browse files Browse the repository at this point in the history
Applied automated formatting with checkstyle's Google rules
  • Loading branch information
mattteochen authored Jun 30, 2023
2 parents ed4c297 + 16f5138 commit 984432b
Show file tree
Hide file tree
Showing 133 changed files with 6,202 additions and 6,292 deletions.
67 changes: 41 additions & 26 deletions src/main/java/it/polimi/is23am10/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import it.polimi.is23am10.utils.config.exceptions.InvalidPortNumberException;
import it.polimi.is23am10.utils.exceptions.InvalidArgumentException;
import it.polimi.is23am10.utils.exceptions.MissingParameterException;

import java.io.IOException;
import java.net.ConnectException;
import java.net.ServerSocket;
Expand All @@ -32,13 +31,12 @@
import java.rmi.registry.Registry;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
* The entrypoint for the app. Parses args and launches
* either client or server with desired options.
* The entrypoint for the app. Parses args and launches either client or server with desired
* options.
*
* @author Alessandro Amandonico ([email protected])
* @author Francesco Buccoliero ([email protected])
Expand All @@ -47,15 +45,12 @@
*/
public class App {

/**
* Logger instance.
*
*/
protected final static Logger logger = LogManager.getLogger(App.class);
/** Logger instance. */
protected static final Logger logger = LogManager.getLogger(App.class);

/**
* The main function. Entrypoint for both server and client.
*
*
* @param args CLI arguments to parse.
*/
public static void main(String[] args) {
Expand All @@ -64,28 +59,41 @@ public static void main(String[] args) {
AppConfigContext ctx = new AppConfigContext();
if (ctx.getIsServer()) {
// SERVER MODE
Server server = new Server(new ServerSocket(ctx.getServerSocketPort()),
Executors.newFixedThreadPool(ctx.getMaxConnections()), new ServerControllerAction(),
LocateRegistry.createRegistry(ctx.getServerRmiPort()));
Server server =
new Server(
new ServerSocket(ctx.getServerSocketPort()),
Executors.newFixedThreadPool(ctx.getMaxConnections()),
new ServerControllerAction(),
LocateRegistry.createRegistry(ctx.getServerRmiPort()));

server.start(ctx);
} else {
// CLIENT MODE
UserInterface userInterface = ctx.getShowGUI() ? new GraphicUserInterface() : new CommandLineInterface(ctx.getShowDebug());
UserInterface userInterface =
ctx.getShowGUI()
? new GraphicUserInterface()
: new CommandLineInterface(ctx.getShowDebug());
Client client = null;
if (ctx.getUseRMI()) {
try {
Registry registry = LocateRegistry.getRegistry(ctx.getServerAddress(), ctx.getServerRmiPort());
PlayerConnectorRmi playerConnector = new PlayerConnectorRmi(new LinkedBlockingQueue<>(), client);
IServerControllerAction serverControllerActionServerRef = (IServerControllerAction) registry
.lookup(IServerControllerAction.class.getName());
client = new RMIClient(playerConnector, userInterface, serverControllerActionServerRef, registry);
Registry registry =
LocateRegistry.getRegistry(ctx.getServerAddress(), ctx.getServerRmiPort());
PlayerConnectorRmi playerConnector =
new PlayerConnectorRmi(new LinkedBlockingQueue<>(), client);
IServerControllerAction serverControllerActionServerRef =
(IServerControllerAction) registry.lookup(IServerControllerAction.class.getName());
client =
new RMIClient(
playerConnector, userInterface, serverControllerActionServerRef, registry);
playerConnector.setClient(client);
} catch (NotBoundException e) {
userInterface.displayError(new ErrorMessage("Server not found. Shutting down client...", ErrorSeverity.CRITICAL));
userInterface.displayError(
new ErrorMessage(
"Server not found. Shutting down client...", ErrorSeverity.CRITICAL));
return;
} catch(NullBlockingQueueException e) {
userInterface.displayError(new ErrorMessage("Client module error. Shutting down", ErrorSeverity.CRITICAL));
} catch (NullBlockingQueueException e) {
userInterface.displayError(
new ErrorMessage("Client module error. Shutting down", ErrorSeverity.CRITICAL));
return;
} catch (IOException e) {
logger.error("Cannot connect to server. Verify address and retry.");
Expand All @@ -94,19 +102,26 @@ public static void main(String[] args) {
} else {
try {
Socket socket = new Socket(ctx.getServerAddress(), ctx.getServerSocketPort());
PlayerConnectorSocket playerConnector = new PlayerConnectorSocket(socket, new LinkedBlockingQueue<>());
PlayerConnectorSocket playerConnector =
new PlayerConnectorSocket(socket, new LinkedBlockingQueue<>());
client = new SocketClient(playerConnector, userInterface);
} catch (UnknownHostException e) {
userInterface.displayError(new ErrorMessage("Server not found. Shutting down client...", ErrorSeverity.CRITICAL));
userInterface.displayError(
new ErrorMessage(
"Server not found. Shutting down client...", ErrorSeverity.CRITICAL));
return;
} catch (NullBlockingQueueException | NullSocketConnectorException e) {
userInterface.displayError(new ErrorMessage("Client module error. Shutting down", ErrorSeverity.CRITICAL));
userInterface.displayError(
new ErrorMessage("Client module error. Shutting down", ErrorSeverity.CRITICAL));
return;
}
}
client.run();
}
} catch (NumberFormatException | InvalidArgumentException | MissingParameterException | InvalidPortNumberException
} catch (NumberFormatException
| InvalidArgumentException
| MissingParameterException
| InvalidPortNumberException
| InvalidMaxConnectionsNumberException e) {
logger.error("Cannot parse CLI arguments.", e);
} catch (ConnectException e) {
Expand Down
35 changes: 18 additions & 17 deletions src/main/java/it/polimi/is23am10/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;

import it.polimi.is23am10.client.exceptions.ForceCloseApplicationException;
import it.polimi.is23am10.client.interfaces.AlarmConsumer;
import it.polimi.is23am10.client.userinterface.UserInterface;
Expand Down Expand Up @@ -159,9 +158,7 @@ public enum ClientGameStatus {
*/
protected static IPlayerConnector playerConnector;

/**
* Maximum player name length.
*/
/** Maximum player name length. */
private final Integer MAX_PLAYER_NAME_LENGTH = 15;

/**
Expand Down Expand Up @@ -209,11 +206,10 @@ protected boolean hasRequestedDisconnection() {
}

/**
* Set a force close application request.
* This method is defined as static as JavaFX thread must have access.
* Set a force close application request. This method is defined as static as JavaFX thread must
* have access.
*
* @param flag The request flag.
*
*/
public static void setForceCloseApplication(boolean flag) {
forceCloseApplication = flag;
Expand Down Expand Up @@ -301,7 +297,7 @@ protected void setGameIdRef(UUID id) {

/**
* Game id ref getter.
*
*
* @return game id.
*/
protected UUID getGameIdRef() {
Expand Down Expand Up @@ -363,7 +359,7 @@ protected VirtualView getVirtualView() {

/**
* Virtual view setter.
*
*
* @param vv virtual view to set.
*/
protected void setVirtualView(VirtualView vv) {
Expand Down Expand Up @@ -619,7 +615,7 @@ protected boolean handlePlayerNameSelection() throws IOException {
String selectedPlayerName = userInterface.getUserInput();
if (selectedPlayerName != null) {
selectedPlayerName = selectedPlayerName.stripLeading().split(" ")[0];
if (selectedPlayerName.length() > MAX_PLAYER_NAME_LENGTH){
if (selectedPlayerName.length() > MAX_PLAYER_NAME_LENGTH) {
selectedPlayerName = selectedPlayerName.substring(0, MAX_PLAYER_NAME_LENGTH);
}
try {
Expand Down Expand Up @@ -696,7 +692,8 @@ protected void handleGameSelection() throws IOException, InterruptedException {
} else {
userInterface.displayError(
new ErrorMessage(
"Insert the index of the game you want to join, if none present please create a new game",
"Insert the index of the game you want to join, if none present please create"
+ " a new game",
ErrorSeverity.ERROR));
}
break;
Expand Down Expand Up @@ -741,9 +738,12 @@ protected void handleGameSelection() throws IOException, InterruptedException {
* @throws ForceCloseApplicationException
*/
protected void clientRunnerCore()
throws IOException, InterruptedException, NullPlayerIdException, ForceCloseApplicationException {
throws IOException,
InterruptedException,
NullPlayerIdException,
ForceCloseApplicationException {

//this flag is raised by external threads, as JavaFX
// this flag is raised by external threads, as JavaFX
if (forceCloseApplication) {
throw new ForceCloseApplicationException();
}
Expand Down Expand Up @@ -771,14 +771,15 @@ protected void clientRunnerCore()
}
}

/**
* Method to terminate the client and all client's threads.
*/
/** Method to terminate the client and all client's threads. */
public void terminateClient() {
try {
UnicastRemoteObject.unexportObject(this, true);
} catch (NoSuchObjectException e) {
userInterface.displayError(new ErrorMessage("Unable to close connection safely. Please close client manually", ErrorSeverity.CRITICAL));
userInterface.displayError(
new ErrorMessage(
"Unable to close connection safely. Please close client manually",
ErrorSeverity.CRITICAL));
}
requestedDisconnection = true;
alarm.cancel();
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/it/polimi/is23am10/client/IClient.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package it.polimi.is23am10.client;

import it.polimi.is23am10.server.network.messages.AbstractMessage;
import java.rmi.Remote;
import java.rmi.RemoteException;

import it.polimi.is23am10.server.network.messages.AbstractMessage;

/**
* The <code>IClient</code> interface represents the remote interface for clients in a Java RMI application.
* It defines methods that the server can call on the client.
* The <code>IClient</code> interface represents the remote interface for clients in a Java RMI
* application. It defines methods that the server can call on the client.
*/
public interface IClient extends Remote {
/**
* Displays a server message on the client.
*
* @param msg The server message to be displayed on the client.
* @throws RemoteException If a communication-related exception occurs during the remote method invocation.
* @throws RemoteException If a communication-related exception occurs during the remote method
* invocation.
*/
void showServerMessage(AbstractMessage msg) throws RemoteException;
}
36 changes: 18 additions & 18 deletions src/main/java/it/polimi/is23am10/client/RMIClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ public class RMIClient extends Client {
/** The {@link ServerControllerAction} server object. */
protected transient IServerControllerAction serverControllerActionServer;

/**
* Rmi alarm snoozer.
*
*/
protected transient AlarmConsumer snoozer = () -> {
if (!hasJoined()) {
return;
}
try {
snoozeAlarm();
} catch(RemoteException e) {
userInterface.displayError(
new ErrorMessage("Internal job failed, you might have lost connection to the server. Try re-joining", ErrorSeverity.ERROR));
terminateClient();
}
};

/** Rmi alarm snoozer. */
protected transient AlarmConsumer snoozer =
() -> {
if (!hasJoined()) {
return;
}
try {
snoozeAlarm();
} catch (RemoteException e) {
userInterface.displayError(
new ErrorMessage(
"Internal job failed, you might have lost connection to the server. Try"
+ " re-joining",
ErrorSeverity.ERROR));
terminateClient();
}
};

/**
* Public constructor for client using RMI as communication method.
Expand Down Expand Up @@ -103,7 +103,7 @@ public void run() {
ErrorSeverity.CRITICAL));
terminateClient();
return;
} catch(ForceCloseApplicationException e) {
} catch (ForceCloseApplicationException e) {
terminateClient();
return;
}
Expand Down
Loading

0 comments on commit 984432b

Please sign in to comment.