documentation for all major modules
This commit is contained in:
@@ -45,25 +45,28 @@ public class ClientGuiController implements ClientListener {
|
|||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void initialize() throws SocketException {
|
public void initialize() throws SocketException {
|
||||||
client = new Client(new InetSocketAddress("localhost", 6665), this, UsernameSingleton.getInstance().getUsername());
|
client = new Client(new InetSocketAddress("localhost", 6665), this, UsernameSingleton.getInstance().getUsername());
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
Stage stage = (Stage) vueContainer.getScene().getWindow();
|
Stage stage = (Stage) vueContainer.getScene().getWindow();
|
||||||
// stage.setResizable(true); // Fixed a bug that made the close button disappear under my Wayland setup (don't know if it's the same for x11)
|
stage.setResizable(true); // Fixed a bug that made the close button disappear under my Wayland setup (don't know if it's the same for x11)
|
||||||
stage.setOnCloseRequest(event -> {
|
stage.setOnCloseRequest(event -> {
|
||||||
connected = false;
|
connected = false;
|
||||||
client.close();
|
client.close();
|
||||||
|
});
|
||||||
|
chatList.heightProperty().addListener((obs, oldVal, newVal) -> chatPane.setVvalue(1.0));
|
||||||
});
|
});
|
||||||
chatList.heightProperty().addListener((obs, oldVal, newVal) -> chatPane.setVvalue(1.0));
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleDisconnect() {
|
public void handleDisconnect() {
|
||||||
System.out.println("Disconnected");
|
System.out.println("Disconnected");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an alert dialog when the connection to the server fails.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void handleConnexionError() {
|
public void handleConnexionError() {
|
||||||
if(connected) {
|
if(connected) {
|
||||||
@@ -79,6 +82,9 @@ public void initialize() throws SocketException {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an alert dialog when the connection to the server is successful.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void handleConnect() {
|
public void handleConnect() {
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
@@ -88,6 +94,13 @@ public void initialize() throws SocketException {
|
|||||||
alert.showAndWait();
|
alert.showAndWait();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format and insert the received chat message into the chat list.
|
||||||
|
* @param time the time the message was sent
|
||||||
|
* @param chatter the name of the person who sent the message
|
||||||
|
* @param content the content of the message
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void handleChatMessage(Instant time, String chatter, String content) {
|
public void handleChatMessage(Instant time, String chatter, String content) {
|
||||||
String untagged = untag(content);
|
String untagged = untag(content);
|
||||||
@@ -129,7 +142,10 @@ public void initialize() throws SocketException {
|
|||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the list of rooms in the room list.
|
||||||
|
* @param roomNames the list of room names
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void handleRoomList(List<String> roomNames) {
|
public void handleRoomList(List<String> roomNames) {
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
@@ -145,6 +161,10 @@ public void initialize() throws SocketException {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the chat (message wall + input) environment.
|
||||||
|
* @param roomName the name of the room
|
||||||
|
*/
|
||||||
private void createChatEnv(String roomName) {
|
private void createChatEnv(String roomName) {
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
chatList.getChildren().clear();
|
chatList.getChildren().clear();
|
||||||
@@ -177,8 +197,15 @@ public void initialize() throws SocketException {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Colorize the message according to the color codes.
|
||||||
|
* @param chatter the name of the person who sent the message
|
||||||
|
* @param content the content of the message
|
||||||
|
* @return the formatted message
|
||||||
|
*/
|
||||||
private TextFlow formatMessage(String chatter, String content) {
|
private TextFlow formatMessage(String chatter, String content) {
|
||||||
TextFlow textFlow = new TextFlow();
|
TextFlow textFlow = new TextFlow();
|
||||||
|
// Evil regex : match every sequence starting with a color code ending with the next color code
|
||||||
Pattern pattern = Pattern.compile("&([rbgyn])([^&]*)");
|
Pattern pattern = Pattern.compile("&([rbgyn])([^&]*)");
|
||||||
Matcher matcher = pattern.matcher(content);
|
Matcher matcher = pattern.matcher(content);
|
||||||
int lastIndex = 0;
|
int lastIndex = 0;
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ public class ClientLogin {
|
|||||||
@FXML
|
@FXML
|
||||||
public TextField usernameField;
|
public TextField usernameField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the username and switches to the loading screen.
|
||||||
|
* @throws IOException if the FXML file is not found.
|
||||||
|
*/
|
||||||
@FXML
|
@FXML
|
||||||
private void login() throws IOException {
|
private void login() throws IOException {
|
||||||
String username = usernameField.getText();
|
String username = usernameField.getText();
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package client;
|
package client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class aims to make the username available to all controllers. (Recommended by JavaFX, because otherwise, FXMLLoader bugs and doesn't switch scenes properly.)
|
||||||
|
*/
|
||||||
public class UsernameSingleton {
|
public class UsernameSingleton {
|
||||||
private static UsernameSingleton instance;
|
private static UsernameSingleton instance;
|
||||||
private String username;
|
private String username;
|
||||||
@@ -7,6 +10,10 @@ public class UsernameSingleton {
|
|||||||
private UsernameSingleton() {
|
private UsernameSingleton() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the instance of the singleton.
|
||||||
|
* @return the instance
|
||||||
|
*/
|
||||||
public static UsernameSingleton getInstance() {
|
public static UsernameSingleton getInstance() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
instance = new UsernameSingleton();
|
instance = new UsernameSingleton();
|
||||||
|
|||||||
@@ -36,6 +36,10 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the client is logged in
|
||||||
|
* @return true if the client is logged in
|
||||||
|
*/
|
||||||
private boolean checkLogin() {
|
private boolean checkLogin() {
|
||||||
if (this.chatterName != null && this.chatterName.isEmpty()) {
|
if (this.chatterName != null && this.chatterName.isEmpty()) {
|
||||||
sendPacket(new ServerResponsePacket(Response.AuthError));
|
sendPacket(new ServerResponsePacket(Response.AuthError));
|
||||||
@@ -44,6 +48,11 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make the server create a room & enter it
|
||||||
|
* @param packet the packet containing the room name
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(CreateRoomPacket packet) {
|
public void visitPacket(CreateRoomPacket packet) {
|
||||||
if (!checkLogin())
|
if (!checkLogin())
|
||||||
@@ -54,6 +63,10 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
onRoomJoin();
|
onRoomJoin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make the server join a room
|
||||||
|
* @param packet the packet containing the room name
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(JoinRoomPacket packet) {
|
public void visitPacket(JoinRoomPacket packet) {
|
||||||
if (!checkLogin())
|
if (!checkLogin())
|
||||||
@@ -68,6 +81,10 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
onRoomJoin();
|
onRoomJoin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make the server leave a room
|
||||||
|
* @param packet the packet containing the room name
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(LeaveRoomPacket packet) {
|
public void visitPacket(LeaveRoomPacket packet) {
|
||||||
if (!checkLogin())
|
if (!checkLogin())
|
||||||
@@ -79,6 +96,10 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
onRoomLeave(roomName);
|
onRoomLeave(roomName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the pseudo is available (nobody connected with the same pseudo)
|
||||||
|
* @param packet the packet containing the pseudo
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(LoginPacket packet) {
|
public void visitPacket(LoginPacket packet) {
|
||||||
if (packet.getPseudo().isEmpty() || server.hasChatterName(packet.getPseudo())) {
|
if (packet.getPseudo().isEmpty() || server.hasChatterName(packet.getPseudo())) {
|
||||||
@@ -92,6 +113,10 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
System.out.println("[Server] Chatter " + packet.getPseudo() + " connected !");
|
System.out.println("[Server] Chatter " + packet.getPseudo() + " connected !");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the list of rooms
|
||||||
|
* @param packet the packet containing the request
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(RequestRoomListPacket packet) {
|
public void visitPacket(RequestRoomListPacket packet) {
|
||||||
if (!checkLogin())
|
if (!checkLogin())
|
||||||
@@ -99,6 +124,10 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
sendPacket(new RoomListPacket(server.getRoomNames()));
|
sendPacket(new RoomListPacket(server.getRoomNames()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a message to the room
|
||||||
|
* @param packet the packet containing the message
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(SendChatMessagePacket packet) {
|
public void visitPacket(SendChatMessagePacket packet) {
|
||||||
if (!checkLogin())
|
if (!checkLogin())
|
||||||
@@ -107,11 +136,18 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
sendPacket(new ServerResponsePacket(messageSent ? Response.MessageSent : Response.MessageNotSent));
|
sendPacket(new ServerResponsePacket(messageSent ? Response.MessageSent : Response.MessageNotSent));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnect the client
|
||||||
|
* @param packet the packet containing the request
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void visitPacket(DisconnectPacket packet) {
|
public void visitPacket(DisconnectPacket packet) {
|
||||||
this.onDisconnect();
|
this.onDisconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all room connections while disconnecting the client
|
||||||
|
*/
|
||||||
private void onDisconnect() {
|
private void onDisconnect() {
|
||||||
if (this.server.isInRoom(this)) {
|
if (this.server.isInRoom(this)) {
|
||||||
this.onRoomLeave(this.server.getRoomName(this));
|
this.onRoomLeave(this.server.getRoomName(this));
|
||||||
@@ -121,11 +157,18 @@ public class ServerConnexion implements PacketVisitor {
|
|||||||
System.out.println("[Server] Chatter " + chatterName + " disconnected !");
|
System.out.println("[Server] Chatter " + chatterName + " disconnected !");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a message to the room when a client joins
|
||||||
|
*/
|
||||||
private void onRoomJoin() {
|
private void onRoomJoin() {
|
||||||
String joinMessage = "Chatter " + this.chatterName + " joined the room !";
|
String joinMessage = "Chatter " + this.chatterName + " joined the room !";
|
||||||
this.server.sendToRoom(this, new ChatMessagePacket(Instant.now(), "", joinMessage));
|
this.server.sendToRoom(this, new ChatMessagePacket(Instant.now(), "", joinMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a message to the room when a client leaves
|
||||||
|
* @param roomName the name of the room
|
||||||
|
*/
|
||||||
private void onRoomLeave(String roomName) {
|
private void onRoomLeave(String roomName) {
|
||||||
String joinMessage = "Chatter " + this.chatterName + " left the room !";
|
String joinMessage = "Chatter " + this.chatterName + " left the room !";
|
||||||
this.server.sendToRoom(roomName, new ChatMessagePacket(Instant.now(), "", joinMessage));
|
this.server.sendToRoom(roomName, new ChatMessagePacket(Instant.now(), "", joinMessage));
|
||||||
|
|||||||
Reference in New Issue
Block a user