Pass Client & ClientListener through all controllers

This commit is contained in:
Clément
2025-03-10 13:52:35 +01:00
parent 3abdc09819
commit 07272732d4
9 changed files with 154 additions and 36 deletions

View File

@@ -4,7 +4,7 @@ import java.net.InetSocketAddress;
public class ChatAppClient { public class ChatAppClient {
public static void main(String[] args) { public static void main(String[] args) {
ClientConsole console = new ClientConsole(new InetSocketAddress("localhost", 6665)); ClientConsole console = new ClientConsole(new InetSocketAddress("192.168.163.131", 6665));
try { try {
console.joinThread(); console.joinThread();
} catch (InterruptedException e) { } catch (InterruptedException e) {

View File

@@ -9,7 +9,7 @@ import network.protocol.packets.*;
public class Client { public class Client {
private final ClientConnexion connexion; private final ClientConnexion connexion;
private final ClientListener callback; private ClientListener callback;
public Client(InetSocketAddress serverAddress, ClientListener callback, String pseudo) throws SocketException { public Client(InetSocketAddress serverAddress, ClientListener callback, String pseudo) throws SocketException {
this.connexion = new ClientConnexion(new Socket(), serverAddress, callback); this.connexion = new ClientConnexion(new Socket(), serverAddress, callback);
@@ -50,4 +50,9 @@ public class Client {
public void RequestActualRoom() { public void RequestActualRoom() {
this.connexion.sendPacket(new RequestActualRoomPacket()); this.connexion.sendPacket(new RequestActualRoomPacket());
} }
public void changeCallback(ClientListener callback) {
this.callback = callback;
this.connexion.changeCallback(callback);
}
} }

View File

@@ -14,7 +14,7 @@ public class ClientConnexion implements PacketVisitor, PacketHandler {
private final InetSocketAddress serverAddress; private final InetSocketAddress serverAddress;
private final Socket socket; private final Socket socket;
private final ClientListener callback; private ClientListener callback;
public ClientConnexion(Socket socket, InetSocketAddress serverAddress, ClientListener callback) { public ClientConnexion(Socket socket, InetSocketAddress serverAddress, ClientListener callback) {
this.serverAddress = serverAddress; this.serverAddress = serverAddress;
@@ -111,4 +111,7 @@ public class ClientConnexion implements PacketVisitor, PacketHandler {
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'"); throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
} }
public void changeCallback(ClientListener callback) {
this.callback = callback;
}
} }

View File

@@ -1,6 +1,56 @@
package client; package client;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import network.protocol.packets.ServerResponsePacket;
public class ClientGuiController { import java.time.Instant;
import java.util.List;
public class ClientGuiController implements ClientListener {
private Client client;
public void setClient(Client client) {
this.client = client;
}
@FXML
public void initialize() {
}
@Override
public void handleDisconnect() {
}
@Override
public void handleConnexionError() {
}
@Override
public void handleConnect() {
}
@Override
public void handleChatMessage(Instant time, String chatter, String content) {
}
@Override
public void handleRoomList(List<String> roomNames) {
}
@Override
public void handleServerResponse(ServerResponsePacket.Response response) {
}
@Override
public void handleActualRoom(String roomName) {
}
} }

View File

@@ -7,14 +7,20 @@ import javafx.scene.Scene;
import javafx.scene.shape.Arc; import javafx.scene.shape.Arc;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.util.Duration; import javafx.util.Duration;
import network.protocol.packets.ServerResponsePacket;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.util.List;
import static utilities.FxUtilities.centerStage; import static utilities.FxUtilities.centerStage;
public class ClientLoading { public class ClientLoading implements ClientListener {
@FXML @FXML
private Arc spinnerArc; private Arc spinnerArc;
private Client client;
public void initialize() throws IOException, InterruptedException { public void initialize() throws IOException, InterruptedException {
// Création de l'animation de rotation // Création de l'animation de rotation
RotateTransition rotate = new RotateTransition(Duration.seconds(1), spinnerArc); RotateTransition rotate = new RotateTransition(Duration.seconds(1), spinnerArc);
@@ -23,11 +29,57 @@ public class ClientLoading {
rotate.play(); rotate.play();
} }
public void setClient(Client client) {
this.client = client;
}
public void login() throws IOException { public void login() throws IOException {
var loader = new FXMLLoader(getClass().getResource("/client/clientVue.fxml")); var loader = new FXMLLoader(getClass().getResource("/client/clientVue.fxml"));
loader.load(); loader.load();
client.changeCallback(loader.getController());
((ClientGuiController) loader.getController()).setClient(client);
Stage stage = (Stage) spinnerArc.getScene().getWindow(); Stage stage = (Stage) spinnerArc.getScene().getWindow();
stage.setScene(new Scene(loader.getRoot(), 800, 600)); stage.setScene(new Scene(loader.getRoot(), 800, 600));
centerStage(stage); centerStage(stage);
stage.getScene().getStylesheets().add(getClass().getResource("clientStyle.css").toExternalForm());
}
@Override
public void handleDisconnect() {
System.out.println("Disconnected");
}
@Override
public void handleConnexionError() {
System.out.println("Connection error");
}
@Override
public void handleConnect() {
try {
login();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void handleChatMessage(Instant time, String chatter, String content) {
}
@Override
public void handleRoomList(List<String> roomNames) {
}
@Override
public void handleServerResponse(ServerResponsePacket.Response response) {
}
@Override
public void handleActualRoom(String roomName) {
} }
} }

View File

@@ -7,6 +7,7 @@ import javafx.scene.control.TextField;
import javafx.stage.Stage; import javafx.stage.Stage;
import client.ClientGui; import client.ClientGui;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress;
import static utilities.FxUtilities.centerStage; import static utilities.FxUtilities.centerStage;
@@ -16,11 +17,14 @@ public class ClientLogin {
@FXML @FXML
private void login() throws IOException { private void login() throws IOException {
if(usernameField.getText().isEmpty()) { String username = usernameField.getText();
if(username.isEmpty()) {
return; return;
} }
var loader = new FXMLLoader(getClass().getResource("/client/clientLoading.fxml")); var loader = new FXMLLoader(getClass().getResource("/client/clientLoading.fxml"));
Client client = new Client(new InetSocketAddress("localhost", 6665), loader.getController(), username);
loader.load(); loader.load();
((ClientLoading) loader.getController()).setClient(client);
Stage stage = (Stage) usernameField.getScene().getWindow(); Stage stage = (Stage) usernameField.getScene().getWindow();
stage.setScene(new Scene(loader.getRoot(), 800, 600)); stage.setScene(new Scene(loader.getRoot(), 800, 600));
centerStage(stage); centerStage(stage);

View File

@@ -23,9 +23,9 @@ public class PacketPool {
private final Socket socket; private final Socket socket;
private static int MAX_SEND_TRY = 50; private static int MAX_SEND_TRY = 50;
private static long SEND_DELAY = 100; private static long SEND_DELAY = 10;
private static long RETRY_INTERVAL = SEND_DELAY * 2; private static long RETRY_INTERVAL = SEND_DELAY * 2;
private static float PACKET_LOSS_PROBABILITY = 0.5f; private static float PACKET_LOSS_PROBABILITY = 0.1f;
private static record ReliablePacketAddress(ReliablePacket packet, InetSocketAddress address) { private static record ReliablePacketAddress(ReliablePacket packet, InetSocketAddress address) {
@Override @Override

View File

@@ -1,4 +1,4 @@
.login-box { .login-box, .logo-box, .rooms-list, .chat-list {
-fx-background-color: lightgray; -fx-background-color: lightgray;
} }
@@ -8,4 +8,10 @@
.login:focused { .login:focused {
-fx-border-color: lightgrey; -fx-border-color: lightgrey;
}
.rooms-label {
-fx-font-size: 18px;
-fx-padding: 10px;
-fx-background-color: lightgray;
} }

View File

@@ -9,37 +9,35 @@
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.image.ImageView?> <?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?> <?import javafx.scene.image.Image?>
<AnchorPane xmlns="http://javafx.com/javafx" <BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml" xmlns:fx="http://javafx.com/fxml"
fx:controller="client.ClientGuiController" fx:controller="client.ClientGuiController"
prefHeight="400.0" prefWidth="600.0"> prefHeight="400.0" prefWidth="600.0" styleClass="vue-container" fx:id="vueContainer">
<VBox> <top>
<padding> <HBox alignment="CENTER" styleClass="logo-box">
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/> <padding>
</padding> <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
<!-- Logo --> </padding>
<HBox alignment="CENTER">
<ImageView fitHeight="50.0" fitWidth="50.0"> <ImageView fitHeight="50.0" fitWidth="50.0">
<Image url="@/liscord.png" /> <Image url="@/liscord.png" />
</ImageView> </ImageView>
<Label text="Liscord" style="-fx-font-size: 24px; -fx-font-weight: bold; -fx-padding: 0 0 0 10px"/> <Label text="Liscord" style="-fx-font-size: 24px; -fx-font-weight: bold; -fx-padding: 0 0 0 10px"/>
</HBox> </HBox>
<!-- Client --> </top>
<HBox> <left>
<!-- Rooms --> <ScrollPane prefWidth="200.0" styleClass="rooms-pane" fx:id="roomsPane">
<ScrollPane> <VBox fx:id="rooms" spacing="10.0" alignment="CENTER">
<VBox fx:id="rooms" spacing="10.0"> <Label text="Rooms" styleClass="rooms-label"/>
<Label text="Rooms" style="-fx-font-size: 18px;"/> <VBox fx:id="roomList" styleClass="rooms-list" spacing="5.0"/>
<VBox fx:id="roomList" spacing="5.0"/> </VBox>
</VBox> </ScrollPane>
</ScrollPane> </left>
<!-- Chat --> <center>
<ScrollPane> <ScrollPane styleClass="chat-pane" fx:id="chatPane">
<VBox fx:id="chat" spacing="10.0"> <VBox fx:id="chat" spacing="10.0">
<Label text="Chat" style="-fx-font-size: 18px;"/> <Label text="Chat" style="-fx-font-size: 18px;"/>
<VBox fx:id="chatList" spacing="5.0"/> <VBox fx:id="chatList" styleClass="chat-list" spacing="5.0"/>
</VBox> </VBox>
</ScrollPane> </ScrollPane>
</HBox> </center>
</VBox> </BorderPane>
</AnchorPane>