From 1e24c3aba172febeadb398430acc2856c235f216 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Fri, 7 Jan 2022 11:58:51 +0100 Subject: [PATCH] Add username and password --- src/fr/univ/lyon1/client/Client.java | 4 +- .../lyon1/common/ServerConfiguration.java | 18 ++++- src/fr/univ/lyon1/gui/ClientGUI.java | 9 ++- .../gui/controller/ConnectGuiController.java | 66 +++++++++++-------- .../univ/lyon1/gui/handlers/MainHandler.java | 2 +- src/main/resources/connect_gui.fxml | 33 +++++++--- 6 files changed, 85 insertions(+), 47 deletions(-) diff --git a/src/fr/univ/lyon1/client/Client.java b/src/fr/univ/lyon1/client/Client.java index 0174ac1..2590cc8 100644 --- a/src/fr/univ/lyon1/client/Client.java +++ b/src/fr/univ/lyon1/client/Client.java @@ -22,8 +22,8 @@ import java.util.List; public class Client { private final int port; private final String address; - private final String username; - private final String password; + protected final String username; + protected final String password; protected final Socket socket; protected final ObjectOutputStream out; private ObjectInputStream in; diff --git a/src/fr/univ/lyon1/common/ServerConfiguration.java b/src/fr/univ/lyon1/common/ServerConfiguration.java index 339cf0f..a035c32 100644 --- a/src/fr/univ/lyon1/common/ServerConfiguration.java +++ b/src/fr/univ/lyon1/common/ServerConfiguration.java @@ -5,13 +5,15 @@ import org.jetbrains.annotations.NotNull; import java.io.*; import java.util.Properties; -public record ServerConfiguration(@NotNull String address, int port) { +public record ServerConfiguration(@NotNull String address, int port, @NotNull String pseudo, @NotNull String password) { @NotNull private static final File file = new File("connection.properties"); - public ServerConfiguration(@NotNull String address, int port) { + public ServerConfiguration(@NotNull String address, int port, @NotNull String pseudo, @NotNull String password) { this.address = address; this.port = port; + this.pseudo = pseudo; + this.password = password; } public static ServerConfiguration load() throws IOException, NumberFormatException { @@ -23,13 +25,15 @@ public record ServerConfiguration(@NotNull String address, int port) { @NotNull final Properties properties = new Properties(); properties.load(new FileReader(file)); - return new ServerConfiguration(properties.getProperty("address"), Integer.parseInt(properties.getProperty("port"))); + return new ServerConfiguration(properties.getProperty("address"), Integer.parseInt(properties.getProperty("port")), properties.getProperty("pseudo"), properties.getProperty("password")); } public void save() throws IOException { @NotNull final Properties properties = new Properties(); properties.setProperty("address", this.address); properties.setProperty("port", String.valueOf(this.port)); + properties.setProperty("pseudo", this.pseudo); + properties.setProperty("password", this.password); properties.store(new FileWriter(file), "Information needed to connect to the server"); } @@ -40,4 +44,12 @@ public record ServerConfiguration(@NotNull String address, int port) { public int getPort() { return port; } + + public String getPseudo() { + return pseudo; + } + + public String getPassword() { + return password; + } } diff --git a/src/fr/univ/lyon1/gui/ClientGUI.java b/src/fr/univ/lyon1/gui/ClientGUI.java index a41ef37..84326d8 100644 --- a/src/fr/univ/lyon1/gui/ClientGUI.java +++ b/src/fr/univ/lyon1/gui/ClientGUI.java @@ -2,10 +2,9 @@ package fr.univ.lyon1.gui; import fr.univ.lyon1.client.Client; import fr.univ.lyon1.client.ClientReceive; -import fr.univ.lyon1.common.Message; +import fr.univ.lyon1.common.command.Command; import fr.univ.lyon1.common.command.CommandType; import fr.univ.lyon1.gui.handlers.MainHandler; -import fr.univ.lyon1.common.command.Command; import java.io.IOException; import java.util.List; @@ -13,8 +12,8 @@ import java.util.List; public class ClientGUI extends Client { private final MainHandler gui; - public ClientGUI(MainHandler handler, String address, int port) throws IOException { - super(address, port, null, null); + public ClientGUI(MainHandler handler, String address, int port, String pseudo, String password) throws IOException { + super(address, port, pseudo, password); this.gui = handler; } @@ -31,7 +30,7 @@ public class ClientGUI extends Client { Thread clientReceiveThread = new Thread(new ClientReceive(this, super.socket)); clientReceiveThread.start(); - out.writeObject(new Command(CommandType.login, List.of("test", "test"))); // ToDo: Setup login + out.writeObject(new Command(CommandType.login, List.of(this.username, this.password))); // ToDo: Setup login out.flush(); started = true; diff --git a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java index 5995936..262df8b 100644 --- a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java +++ b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java @@ -17,6 +17,10 @@ public class ConnectGuiController { @FXML public TextField portTextField; @FXML + public TextField pseudoTextField; + @FXML + public TextField passwordTextField; + @FXML public Button connectButton; /** @@ -29,43 +33,53 @@ public class ConnectGuiController { final String address = this.addressTextField.getText(); @NotNull final String port = this.portTextField.getText(); + @NotNull + final String pseudo = this.pseudoTextField.getText(); + @NotNull + final String password = this.passwordTextField.getText(); - System.out.println("Checking if port is valid..."); - // Check if the port is a number and if it's an available port - if (this.isNumber(port) && this.isAvailablePort(Integer.parseInt(port))) { - final int iPort = Integer.parseInt(port); - System.out.println("Port valid, next step..."); - System.out.println("Checking if connection is available..."); + System.out.println("Checking if all text fields is not empty"); + if (!address.isEmpty() && !port.isEmpty() && !pseudo.isEmpty() && !password.isEmpty()) { + System.out.println("Checking if port is valid..."); + // Check if the port is a number and if it's an available port + if (this.isNumber(port) && this.isAvailablePort(Integer.parseInt(port))) { + final int iPort = Integer.parseInt(port); + System.out.println("Port valid, next step..."); + System.out.println("Checking if connection is available..."); - // Check if the connection is available - if (this.isAccessible(address, iPort)) { - System.out.println("Connection available, saving file..."); + // Check if the connection is available + if (this.isAccessible(address, iPort)) { + System.out.println("Connection available, saving file..."); - try { - // Save server configuration - new ServerConfiguration(address, iPort).save(); -// Dialog.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur"); - System.out.println("File saved, next step..."); + try { + // Save server configuration + new ServerConfiguration(address, iPort, pseudo, password).save(); + Dialog.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur"); + System.out.println("File saved, next step..."); - System.out.println("Get scene..."); - @NotNull - final Stage stage = (Stage) this.connectButton.getScene().getWindow(); - System.out.println("Scene : " + stage.toString()); - new MainHandler().launch(stage); - } catch (IOException e) { - System.out.println(e.getMessage()); + System.out.println("Get scene..."); + @NotNull + final Stage stage = (Stage) this.connectButton.getScene().getWindow(); + System.out.println("Scene : " + stage.toString()); + new MainHandler().launch(stage); + } catch (IOException e) { + System.out.println(e.getMessage()); - Dialog.showErrorDialog("Erreur", "Impossible de sauvegarder les informations de connexion au serveur"); + Dialog.showErrorDialog("Erreur", "Impossible de sauvegarder les informations de connexion au serveur"); + this.connectButton.setDisable(false); + System.out.println("Failed to save file, error: " + e.getMessage()); + } + } else { + System.out.println("Connection not available"); + Dialog.showErrorDialog("Erreur de connexion", "Impossible de se connecter au serveur, veuillez vérifier les informations saisies"); this.connectButton.setDisable(false); - System.out.println("Failed to save file, error: " + e.getMessage()); } } else { - System.out.println("Connection not available"); - Dialog.showErrorDialog("Erreur de connexion", "Impossible de se connecter au serveur, veuillez vérifier les informations saisies"); + Dialog.showErrorDialog("Erreur", "Veuillez saisir un numéro de port valide"); this.connectButton.setDisable(false); } } else { - Dialog.showErrorDialog("Erreur", "Veuillez saisir un numéro de port valide"); + Dialog.showErrorDialog("Erreur", "Veuillez renseigner tout les champs"); this.connectButton.setDisable(false); } } diff --git a/src/fr/univ/lyon1/gui/handlers/MainHandler.java b/src/fr/univ/lyon1/gui/handlers/MainHandler.java index 795b206..636e7df 100644 --- a/src/fr/univ/lyon1/gui/handlers/MainHandler.java +++ b/src/fr/univ/lyon1/gui/handlers/MainHandler.java @@ -22,7 +22,7 @@ public class MainHandler implements Handler { @NotNull final ServerConfiguration serverConfiguration = ServerConfiguration.load(); System.out.println("Launch Client GUI..."); - this.client = new ClientGUI(this, serverConfiguration.getAddress(), serverConfiguration.getPort()); + this.client = new ClientGUI(this, serverConfiguration.getAddress(), serverConfiguration.getPort(), serverConfiguration.getPseudo(), serverConfiguration.getPassword()); System.out.println("Set..."); stage.setTitle("Chat client"); diff --git a/src/main/resources/connect_gui.fxml b/src/main/resources/connect_gui.fxml index b574c85..8fe0383 100644 --- a/src/main/resources/connect_gui.fxml +++ b/src/main/resources/connect_gui.fxml @@ -5,34 +5,47 @@ - + - + - - + + -