From 931e34bcaa17da8a2f1149f1a594b43a3d76ea69 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Fri, 7 Jan 2022 11:38:15 +0100 Subject: [PATCH 1/5] Fix stupid bug --- src/fr/univ/lyon1/client/Client.java | 7 ++++--- src/fr/univ/lyon1/gui/MainGui.java | 2 ++ .../lyon1/gui/controller/ConnectGuiController.java | 10 ++++++++-- src/fr/univ/lyon1/gui/controller/Dialog.java | 5 +++++ src/fr/univ/lyon1/gui/handlers/MainHandler.java | 6 ++++++ src/module-info.java | 2 +- 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/fr/univ/lyon1/client/Client.java b/src/fr/univ/lyon1/client/Client.java index 36b4b73..0174ac1 100644 --- a/src/fr/univ/lyon1/client/Client.java +++ b/src/fr/univ/lyon1/client/Client.java @@ -30,22 +30,23 @@ public class Client { private List channels = new ArrayList<>(); protected boolean started = false; - public Client(String address, int port, String username, String password) throws IOException { this.address = address; this.port = port; this.username = username; this.password = password; + System.out.println("Init SSL..."); socket = initSSL(); + System.out.println("Set output stream..."); out = new ObjectOutputStream(socket.getOutputStream()); + System.out.println("Get In..."); getIn(); + System.out.println("Client ok"); } private Socket initSSL() throws IOException { SSLContext ctx = ChatSSL.getSSLContext(); - SocketFactory factory = ctx.getSocketFactory(); - Socket connection = factory.createSocket(address, port); ((SSLSocket) connection).setEnabledProtocols(new String[] {ChatSSL.tlsVersion}); SSLParameters sslParams = new SSLParameters(); diff --git a/src/fr/univ/lyon1/gui/MainGui.java b/src/fr/univ/lyon1/gui/MainGui.java index 36281f9..cdf6c45 100644 --- a/src/fr/univ/lyon1/gui/MainGui.java +++ b/src/fr/univ/lyon1/gui/MainGui.java @@ -14,6 +14,8 @@ public class MainGui extends Application { try { new MainHandler().launch(stage); } catch (IOException e) { + System.out.println(e.getMessage()); + // Launch server configuration try { new ServerConfigurationHandler().launch(stage); diff --git a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java index 2016191..5995936 100644 --- a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java +++ b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java @@ -44,13 +44,17 @@ public class ConnectGuiController { try { // Save server configuration new ServerConfiguration(address, iPort).save(); - Dialog.showSuccessDialog("Connecté", "Vous êtes bien connecté au serveur"); +// 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()); + 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()); @@ -84,8 +88,10 @@ public class ConnectGuiController { @NotNull final Socket socket = new Socket(address, port); socket.setSoTimeout(5 * 1000); + boolean connected = socket.isConnected(); + socket.close(); - return true; + return connected; } catch (IOException exception) { return false; } diff --git a/src/fr/univ/lyon1/gui/controller/Dialog.java b/src/fr/univ/lyon1/gui/controller/Dialog.java index 6623ab8..b148506 100644 --- a/src/fr/univ/lyon1/gui/controller/Dialog.java +++ b/src/fr/univ/lyon1/gui/controller/Dialog.java @@ -11,14 +11,19 @@ public class Dialog { } public static void showSuccessDialog(String title, String description) { + System.out.println("Create success dialog..."); @NotNull final Alert alert = new Alert(Alert.AlertType.INFORMATION); + System.out.println("Extracting..."); extracted(title, description, alert); } private static void extracted(String title, String description, @NotNull Alert alert) { + System.out.println("Set title"); alert.setTitle(title); + System.out.println("Set description"); alert.setContentText(description); + System.out.println("Show"); alert.show(); } } diff --git a/src/fr/univ/lyon1/gui/handlers/MainHandler.java b/src/fr/univ/lyon1/gui/handlers/MainHandler.java index be9a06f..795b206 100644 --- a/src/fr/univ/lyon1/gui/handlers/MainHandler.java +++ b/src/fr/univ/lyon1/gui/handlers/MainHandler.java @@ -18,22 +18,28 @@ public class MainHandler implements Handler { @Override public void launch(Stage stage) throws IOException { + System.out.println("New..."); @NotNull final ServerConfiguration serverConfiguration = ServerConfiguration.load(); + System.out.println("Launch Client GUI..."); this.client = new ClientGUI(this, serverConfiguration.getAddress(), serverConfiguration.getPort()); + System.out.println("Set..."); stage.setTitle("Chat client"); stage.setWidth(440); + System.out.println("Set panel..."); this.clientPanel = new ClientPanel(this); Group root = new Group(); root.getChildren().add(this.clientPanel); Scene scene = new Scene(root, 600, 500); + System.out.println("Set scene..."); stage.setScene(scene); stage.show(); if (this.client != null) { + System.out.println("Run..."); this.client.run(); } } diff --git a/src/module-info.java b/src/module-info.java index 998ef49..e8047f3 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -1,4 +1,4 @@ -module fr.univ.lyon { +module fr.univ.lyon1 { requires javafx.controls; requires javafx.fxml; From 1e24c3aba172febeadb398430acc2856c235f216 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Fri, 7 Jan 2022 11:58:51 +0100 Subject: [PATCH 2/5] 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 @@ - + - + - - + + - From 7ac2b05254a19a6b362bbda0e3fad479ced239be Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Wed, 19 Jan 2022 10:22:50 +0100 Subject: [PATCH 3/5] New controller --- src/fr/univ/lyon1/client/Client.java | 14 +- src/fr/univ/lyon1/client/ClientReceive.java | 14 +- src/fr/univ/lyon1/gui/ClientGUI.java | 2 +- src/fr/univ/lyon1/gui/MainGui.java | 4 +- .../gui/controller/ApplicationController.java | 221 ++++++++++++++++++ .../gui/controller/ConnectGuiController.java | 8 +- .../gui/handlers/ApplicationHandler.java | 18 ++ .../handlers/ServerConfigurationHandler.java | 2 +- src/main/resources/message_gui.fxml | 33 +++ 9 files changed, 293 insertions(+), 23 deletions(-) create mode 100644 src/fr/univ/lyon1/gui/controller/ApplicationController.java create mode 100644 src/fr/univ/lyon1/gui/handlers/ApplicationHandler.java create mode 100644 src/main/resources/message_gui.fxml diff --git a/src/fr/univ/lyon1/client/Client.java b/src/fr/univ/lyon1/client/Client.java index 2590cc8..72248e9 100644 --- a/src/fr/univ/lyon1/client/Client.java +++ b/src/fr/univ/lyon1/client/Client.java @@ -27,7 +27,7 @@ public class Client { protected final Socket socket; protected final ObjectOutputStream out; private ObjectInputStream in; - private List channels = new ArrayList<>(); + private final List channels = new ArrayList<>(); protected boolean started = false; public Client(String address, int port, String username, String password) throws IOException { @@ -64,8 +64,7 @@ public class Client { System.exit(0); } - public String sendMessage(String content) { - + public void sendMessage(String content) { try { out.writeObject(new Command(CommandType.message, List.of(new Message(content, channels.get(0))))); out.flush(); @@ -73,8 +72,6 @@ public class Client { System.err.println("Fail to send message !"); e.printStackTrace(); } - - return content; } public void action(Object data) throws IOException { @@ -100,6 +97,7 @@ public class Client { private void commandLogin() throws IOException { out.writeObject(new Command(CommandType.list, null)); out.flush(); + out.writeObject(new Command(CommandType.join, List.of("general"))); out.flush(); } @@ -129,17 +127,13 @@ public class Client { Thread clientSendThread = new Thread(new ClientSend(this, out, socket)); clientSendThread.start(); - Thread clientReceiveThread = new Thread(new ClientReceive(this, socket)); + Thread clientReceiveThread = new Thread(new ClientReceive(this)); clientReceiveThread.start(); started = true; out.writeObject(new Command(CommandType.login, List.of(username, password))); out.flush(); - - clientSendThread.join(); - socket.close(); - clientReceiveThread.interrupt(); } public ObjectInputStream getIn() throws IOException { diff --git a/src/fr/univ/lyon1/client/ClientReceive.java b/src/fr/univ/lyon1/client/ClientReceive.java index 4f5aa9d..9f59465 100644 --- a/src/fr/univ/lyon1/client/ClientReceive.java +++ b/src/fr/univ/lyon1/client/ClientReceive.java @@ -2,21 +2,19 @@ package fr.univ.lyon1.client; import java.io.IOException; import java.io.ObjectInputStream; -import java.net.Socket; import java.net.SocketException; public class ClientReceive implements Runnable { private final Client client; - private ObjectInputStream in; - private final Socket socket; - public ClientReceive(Client client, Socket socket) { + public ClientReceive(Client client) { this.client = client; - this.socket = socket; } @Override public void run() { + ObjectInputStream in; + try { in = client.getIn(); } catch (IOException e) { @@ -26,6 +24,7 @@ public class ClientReceive implements Runnable { while(true) { Object data; + try { data = in.readObject(); } catch (ClassNotFoundException|IOException e) { @@ -33,13 +32,16 @@ public class ClientReceive implements Runnable { System.err.println("Connexion closed"); break; } + System.err.println("Fail to read object !"); e.printStackTrace(); + try { - Thread.sleep(1000); + Thread.currentThread().join(1000); } catch (InterruptedException ex) { break; } + continue; } diff --git a/src/fr/univ/lyon1/gui/ClientGUI.java b/src/fr/univ/lyon1/gui/ClientGUI.java index 84326d8..1f43b9d 100644 --- a/src/fr/univ/lyon1/gui/ClientGUI.java +++ b/src/fr/univ/lyon1/gui/ClientGUI.java @@ -27,7 +27,7 @@ public class ClientGUI extends Client { if (started) return; - Thread clientReceiveThread = new Thread(new ClientReceive(this, super.socket)); + Thread clientReceiveThread = new Thread(new ClientReceive(this)); clientReceiveThread.start(); out.writeObject(new Command(CommandType.login, List.of(this.username, this.password))); // ToDo: Setup login diff --git a/src/fr/univ/lyon1/gui/MainGui.java b/src/fr/univ/lyon1/gui/MainGui.java index cdf6c45..27320e7 100644 --- a/src/fr/univ/lyon1/gui/MainGui.java +++ b/src/fr/univ/lyon1/gui/MainGui.java @@ -1,6 +1,6 @@ package fr.univ.lyon1.gui; -import fr.univ.lyon1.gui.handlers.MainHandler; +import fr.univ.lyon1.gui.handlers.ApplicationHandler; import fr.univ.lyon1.gui.handlers.ServerConfigurationHandler; import javafx.application.Application; import javafx.application.Platform; @@ -12,7 +12,7 @@ public class MainGui extends Application { @Override public void start(Stage stage) { try { - new MainHandler().launch(stage); + new ApplicationHandler().launch(stage); } catch (IOException e) { System.out.println(e.getMessage()); diff --git a/src/fr/univ/lyon1/gui/controller/ApplicationController.java b/src/fr/univ/lyon1/gui/controller/ApplicationController.java new file mode 100644 index 0000000..dd3a3cb --- /dev/null +++ b/src/fr/univ/lyon1/gui/controller/ApplicationController.java @@ -0,0 +1,221 @@ +package fr.univ.lyon1.gui.controller; + +import fr.univ.lyon1.common.Channel; +import fr.univ.lyon1.common.ChatSSL; +import fr.univ.lyon1.common.Message; +import fr.univ.lyon1.common.ServerConfiguration; +import fr.univ.lyon1.common.command.Command; +import fr.univ.lyon1.common.command.CommandType; +import fr.univ.lyon1.common.exception.ChatException; +import fr.univ.lyon1.common.exception.UnknownCommand; +import javafx.application.Platform; +import javafx.fxml.FXML; +import javafx.scene.control.TextArea; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; +import javafx.scene.layout.VBox; +import javafx.scene.paint.Color; +import javafx.scene.text.Text; +import javafx.scene.text.TextFlow; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.net.SocketFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.SSLSocket; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.Socket; +import java.net.SocketException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ApplicationController { + @NotNull + private final List channels = new ArrayList<>(); + @Nullable + private Channel currentChannel = null; + + private boolean isRunning = true; + @NotNull + private final Socket socket; + @NotNull + private final ObjectOutputStream objectOutputStream; + @NotNull + private final ObjectInputStream objectInputStream; + + @FXML + public TextArea textSendArea; + @FXML + public TextFlow textReceiveArea; + @FXML + public VBox vboxChannelsList; + + public ApplicationController() throws IOException { + System.out.println("Loading configuration..."); + @NotNull + final ServerConfiguration serverConfiguration = ServerConfiguration.load(); + + System.out.println("Init socket..."); + this.socket = this.initSSLSocket(serverConfiguration.getAddress(), serverConfiguration.getPort()); + this.objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); + this.objectInputStream = new ObjectInputStream(socket.getInputStream()); + + @NotNull + final Thread receiveObjectThread = new Thread(() -> { + while (this.isRunning) { + try { + @Nullable + final Object object = this.objectInputStream.readObject(); + + if (object != null) { + if (object instanceof Command) { + switch (((Command) object).getType()) { + // If client receive login type message + case login -> { + // Request all list of users + this.sendObject(new Command(CommandType.list, null)); + // TODO: List all channels available + // Join a channel + this.sendObject(new Command(CommandType.join, List.of("general"))); + } + + // If client receive a message type + case message -> { + @NotNull + final Message message = (Message) ((Command) object).getArgs().get(0); + System.out.println("Message received : " + message.toString()); + + // If same channel + if (this.currentChannel != null && message.getChannel().getUUID().equals(this.currentChannel.getUUID())) { + Platform.runLater(() -> this.textReceiveArea.getChildren().add(new Text("@" + message.getSender().getUsername() + ": " + message.getContent()))); + } + } + + // If client receive all users + case list -> { + System.out.println("Connected users : " + Arrays.toString(((Command) object).getArgs().toArray())); + } + + // If client receive a join type message + case join -> { + @NotNull + final Channel channel = (Channel) ((Command) object).getArgs().get(0); + boolean ifChannelExists = this.channels.stream().anyMatch(fChannel -> fChannel.getName().equalsIgnoreCase(channel.getName())); + System.out.println("Channel exists : " + ifChannelExists); + this.currentChannel = channel; + this.channels.add(channel); + System.out.println("Successfully connected to : " + this.currentChannel.toString()); + + if (!ifChannelExists) { + @NotNull + final Text text = new Text("- #" + channel.getName()); + text.setFill(Color.RED); + + text.setOnMousePressed(event -> { + System.out.println("Selected channel : #" + channel.getName()); + + try { + this.sendObject(new Command(CommandType.join, List.of(channel.getName()))); + } catch (IOException e) { + e.printStackTrace(); + } + }); + + Platform.runLater(() -> this.vboxChannelsList.getChildren().add(text)); + } else { + this.vboxChannelsList.getChildren().stream().filter(node -> node instanceof Text).forEach(txt -> ((Text) txt).setFill(Color.BLACK)); + + @NotNull + final Text text = (Text) this.vboxChannelsList.getChildren().stream().filter(node -> node instanceof Text && ((Text) node).getText().equalsIgnoreCase("- #" + channel.getName())).toList().get(0); + text.setFill(Color.RED); + } + } + } + } else if (object instanceof ChatException) { + ((ChatException) object).printStackTrace(); + } else + this.sendObject(new UnknownCommand()); + } else { + try { + this.closeSocket(); + } catch (IOException ex) { + System.out.println("Can not close connection"); + ex.printStackTrace(); + } + } + } catch (IOException | ClassNotFoundException e) { + if (e instanceof SocketException) { + try { + this.closeSocket(); + } catch (IOException ex) { + System.out.println("Can not close connection"); + ex.printStackTrace(); + } + } + + e.printStackTrace(); + } + } + }); + + receiveObjectThread.start(); + + System.out.println("Login..."); + this.sendObject(new Command(CommandType.login, List.of(serverConfiguration.getPseudo(), serverConfiguration.getPassword()))); + } + + private void closeSocket() throws IOException { + this.isRunning = false; + this.objectOutputStream.close(); + this.objectInputStream.close(); + this.socket.close(); + } + + @NotNull + private Socket initSSLSocket(@NotNull String address, int port) throws IOException { + SSLContext ctx = ChatSSL.getSSLContext(); + SocketFactory factory = ctx.getSocketFactory(); + Socket connection = factory.createSocket(address, port); + ((SSLSocket) connection).setEnabledProtocols(new String[] {ChatSSL.tlsVersion}); + SSLParameters sslParams = new SSLParameters(); + sslParams.setEndpointIdentificationAlgorithm("HTTPS"); + ((SSLSocket) connection).setSSLParameters(sslParams); + return connection; + } + + private void sendObject(@NotNull Object object) throws IOException { + this.objectOutputStream.writeObject(object); + this.objectOutputStream.flush(); + } + + public void keyPressedOnTextSendArea(KeyEvent keyEvent) { + if (keyEvent.getCode() == KeyCode.ENTER) { + this.send(); + } + } + + public void send() { + @NotNull + final String textToSend = this.textSendArea.getText(); + this.clear(); + System.out.print(">> Send : " + textToSend); + Platform.runLater(() -> this.textReceiveArea.getChildren().add(new Text("*>> " + textToSend))); + + if (this.currentChannel != null) { + try { + this.sendObject(new Command(CommandType.message, List.of(new Message(textToSend, this.currentChannel)))); + } catch (IOException e) { + System.out.println("Can not send message to server : " + e.getMessage()); + e.printStackTrace(); + } + } + } + + public void clear() { + this.textSendArea.setText(""); + } +} \ No newline at end of file diff --git a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java index 262df8b..59d836b 100644 --- a/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java +++ b/src/fr/univ/lyon1/gui/controller/ConnectGuiController.java @@ -1,7 +1,7 @@ package fr.univ.lyon1.gui.controller; import fr.univ.lyon1.common.ServerConfiguration; -import fr.univ.lyon1.gui.handlers.MainHandler; +import fr.univ.lyon1.gui.handlers.ApplicationHandler; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.TextField; @@ -58,10 +58,12 @@ public class ConnectGuiController { System.out.println("File saved, next step..."); System.out.println("Get scene..."); + + // Change scene + @NotNull final Stage stage = (Stage) this.connectButton.getScene().getWindow(); - System.out.println("Scene : " + stage.toString()); - new MainHandler().launch(stage); + new ApplicationHandler().launch(stage); } catch (IOException e) { System.out.println(e.getMessage()); diff --git a/src/fr/univ/lyon1/gui/handlers/ApplicationHandler.java b/src/fr/univ/lyon1/gui/handlers/ApplicationHandler.java new file mode 100644 index 0000000..173d929 --- /dev/null +++ b/src/fr/univ/lyon1/gui/handlers/ApplicationHandler.java @@ -0,0 +1,18 @@ +package fr.univ.lyon1.gui.handlers; + +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.stage.Stage; + +import java.io.IOException; + +public class ApplicationHandler implements Handler { + @Override + public void launch(Stage stage) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(ClassLoader.getSystemClassLoader().getResource("message_gui.fxml")); + Scene scene = new Scene(fxmlLoader.load(), 600, 400); + stage.setTitle("Chat client"); + stage.setScene(scene); + stage.show(); + } +} diff --git a/src/fr/univ/lyon1/gui/handlers/ServerConfigurationHandler.java b/src/fr/univ/lyon1/gui/handlers/ServerConfigurationHandler.java index 5c4211d..bc70d2c 100644 --- a/src/fr/univ/lyon1/gui/handlers/ServerConfigurationHandler.java +++ b/src/fr/univ/lyon1/gui/handlers/ServerConfigurationHandler.java @@ -10,7 +10,7 @@ public class ServerConfigurationHandler implements Handler { @Override public void launch(Stage stage) throws IOException { FXMLLoader fxmlLoader = new FXMLLoader(ClassLoader.getSystemClassLoader().getResource("connect_gui.fxml")); - Scene scene = new Scene(fxmlLoader.load(), 320, 240); + Scene scene = new Scene(fxmlLoader.load(), 640, 480); stage.setTitle("Configuration du serveur"); stage.setScene(scene); stage.show(); diff --git a/src/main/resources/message_gui.fxml b/src/main/resources/message_gui.fxml new file mode 100644 index 0000000..feaee24 --- /dev/null +++ b/src/main/resources/message_gui.fxml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + +