Add username and password
This commit is contained in:
parent
931e34bcaa
commit
1e24c3aba1
6 changed files with 85 additions and 47 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -5,34 +5,47 @@
|
|||
<?import javafx.scene.layout.*?>
|
||||
<?import javafx.scene.text.*?>
|
||||
|
||||
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
|
||||
prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1"
|
||||
fx:controller="fr.univ.lyon1.gui.controller.ConnectGuiController">
|
||||
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fr.univ.lyon1.gui.controller.ConnectGuiController">
|
||||
<Label text="Connexion">
|
||||
<font>
|
||||
<Font name="System Bold" size="18.0"/>
|
||||
<Font name="System Bold" size="18.0" />
|
||||
</font>
|
||||
</Label>
|
||||
<StackPane prefHeight="10.0" prefWidth="200.0"/>
|
||||
<StackPane prefHeight="10.0" prefWidth="200.0" />
|
||||
<HBox alignment="CENTER" prefHeight="50.0" prefWidth="200.0">
|
||||
<Label text="Adresse :"/>
|
||||
<Label text="Adresse :" />
|
||||
<TextField fx:id="addressTextField">
|
||||
<HBox.margin>
|
||||
<Insets left="10.0"/>
|
||||
<Insets left="10.0" />
|
||||
</HBox.margin>
|
||||
</TextField>
|
||||
</HBox>
|
||||
<HBox alignment="CENTER" layoutX="10.0" layoutY="174.0" prefHeight="50.0" prefWidth="200.0">
|
||||
<Label text="Port :"/>
|
||||
<Label text="Port :" />
|
||||
<TextField fx:id="portTextField">
|
||||
<HBox.margin>
|
||||
<Insets left="10.0" />
|
||||
</HBox.margin>
|
||||
</TextField>
|
||||
</HBox>
|
||||
<HBox alignment="CENTER" prefHeight="50.0" prefWidth="200.0">
|
||||
<Label text="Pseudonyme :"/>
|
||||
<TextField fx:id="pseudoTextField">
|
||||
<HBox.margin>
|
||||
<Insets left="10.0"/>
|
||||
</HBox.margin>
|
||||
</TextField>
|
||||
<StackPane prefHeight="50.0" prefWidth="90.0"/>
|
||||
<Label text="Mot de passe :"/>
|
||||
<TextField fx:id="passwordTextField" cache="true">
|
||||
<HBox.margin>
|
||||
<Insets left="10.0"/>
|
||||
</HBox.margin>
|
||||
</TextField>
|
||||
</HBox>
|
||||
<Button mnemonicParsing="false" text="Se connecter" onAction="#connect" fx:id="connectButton">
|
||||
<Button fx:id="connectButton" mnemonicParsing="false" onAction="#connect" text="Se connecter">
|
||||
<VBox.margin>
|
||||
<Insets top="20.0"/>
|
||||
<Insets top="20.0" />
|
||||
</VBox.margin>
|
||||
</Button>
|
||||
</VBox>
|
||||
|
|
Reference in a new issue