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 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 {
console.joinThread();
} catch (InterruptedException e) {

View File

@@ -9,7 +9,7 @@ import network.protocol.packets.*;
public class Client {
private final ClientConnexion connexion;
private final ClientListener callback;
private ClientListener callback;
public Client(InetSocketAddress serverAddress, ClientListener callback, String pseudo) throws SocketException {
this.connexion = new ClientConnexion(new Socket(), serverAddress, callback);
@@ -50,4 +50,9 @@ public class Client {
public void RequestActualRoom() {
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 Socket socket;
private final ClientListener callback;
private ClientListener callback;
public ClientConnexion(Socket socket, InetSocketAddress serverAddress, ClientListener callback) {
this.serverAddress = serverAddress;
@@ -111,4 +111,7 @@ public class ClientConnexion implements PacketVisitor, PacketHandler {
throw new UnsupportedOperationException("Unimplemented method 'visitPacket'");
}
public void changeCallback(ClientListener callback) {
this.callback = callback;
}
}

View File

@@ -1,6 +1,56 @@
package client;
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.stage.Stage;
import javafx.util.Duration;
import network.protocol.packets.ServerResponsePacket;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import static utilities.FxUtilities.centerStage;
public class ClientLoading {
public class ClientLoading implements ClientListener {
@FXML
private Arc spinnerArc;
private Client client;
public void initialize() throws IOException, InterruptedException {
// Création de l'animation de rotation
RotateTransition rotate = new RotateTransition(Duration.seconds(1), spinnerArc);
@@ -23,11 +29,57 @@ public class ClientLoading {
rotate.play();
}
public void setClient(Client client) {
this.client = client;
}
public void login() throws IOException {
var loader = new FXMLLoader(getClass().getResource("/client/clientVue.fxml"));
loader.load();
client.changeCallback(loader.getController());
((ClientGuiController) loader.getController()).setClient(client);
Stage stage = (Stage) spinnerArc.getScene().getWindow();
stage.setScene(new Scene(loader.getRoot(), 800, 600));
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 client.ClientGui;
import java.io.IOException;
import java.net.InetSocketAddress;
import static utilities.FxUtilities.centerStage;
@@ -16,11 +17,14 @@ public class ClientLogin {
@FXML
private void login() throws IOException {
if(usernameField.getText().isEmpty()) {
String username = usernameField.getText();
if(username.isEmpty()) {
return;
}
var loader = new FXMLLoader(getClass().getResource("/client/clientLoading.fxml"));
Client client = new Client(new InetSocketAddress("localhost", 6665), loader.getController(), username);
loader.load();
((ClientLoading) loader.getController()).setClient(client);
Stage stage = (Stage) usernameField.getScene().getWindow();
stage.setScene(new Scene(loader.getRoot(), 800, 600));
centerStage(stage);

View File

@@ -23,9 +23,9 @@ public class PacketPool {
private final Socket socket;
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 float PACKET_LOSS_PROBABILITY = 0.5f;
private static float PACKET_LOSS_PROBABILITY = 0.1f;
private static record ReliablePacketAddress(ReliablePacket packet, InetSocketAddress address) {
@Override

View File

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