Archived
1
0
Fork 0

Add username and password

This commit is contained in:
Ziedelth 2022-01-07 11:58:51 +01:00
parent 931e34bcaa
commit 1e24c3aba1
6 changed files with 85 additions and 47 deletions

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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;

View file

@ -17,6 +17,10 @@ public class ConnectGuiController {
@FXML
public TextField portTextField;
@FXML
public TextField pseudoTextField;
@FXML
public TextField passwordTextField;
@FXML
public Button connectButton;
/**
@ -29,7 +33,13 @@ 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 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))) {
@ -43,8 +53,8 @@ public class ConnectGuiController {
try {
// Save server configuration
new ServerConfiguration(address, iPort).save();
// Dialog.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur");
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...");
@ -68,6 +78,10 @@ public class ConnectGuiController {
Dialog.showErrorDialog("Erreur", "Veuillez saisir un numéro de port valide");
this.connectButton.setDisable(false);
}
} else {
Dialog.showErrorDialog("Erreur", "Veuillez renseigner tout les champs");
this.connectButton.setDisable(false);
}
}
private boolean isNumber(String text) {

View file

@ -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");

View file

@ -5,9 +5,7 @@
<?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" />
@ -30,7 +28,22 @@
</HBox.margin>
</TextField>
</HBox>
<Button mnemonicParsing="false" text="Se connecter" onAction="#connect" fx:id="connectButton">
<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 fx:id="connectButton" mnemonicParsing="false" onAction="#connect" text="Se connecter">
<VBox.margin>
<Insets top="20.0" />
</VBox.margin>