documentation for all major modules

This commit is contained in:
Clément
2025-03-12 23:16:39 +01:00
parent 0b6f5193a0
commit dfdaae163b
4 changed files with 94 additions and 13 deletions

View File

@@ -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() {
if (this.chatterName != null && this.chatterName.isEmpty()) {
sendPacket(new ServerResponsePacket(Response.AuthError));
@@ -44,6 +48,11 @@ public class ServerConnexion implements PacketVisitor {
return true;
}
/**
* Make the server create a room & enter it
* @param packet the packet containing the room name
*/
@Override
public void visitPacket(CreateRoomPacket packet) {
if (!checkLogin())
@@ -54,6 +63,10 @@ public class ServerConnexion implements PacketVisitor {
onRoomJoin();
}
/**
* Make the server join a room
* @param packet the packet containing the room name
*/
@Override
public void visitPacket(JoinRoomPacket packet) {
if (!checkLogin())
@@ -68,6 +81,10 @@ public class ServerConnexion implements PacketVisitor {
onRoomJoin();
}
/**
* Make the server leave a room
* @param packet the packet containing the room name
*/
@Override
public void visitPacket(LeaveRoomPacket packet) {
if (!checkLogin())
@@ -79,6 +96,10 @@ public class ServerConnexion implements PacketVisitor {
onRoomLeave(roomName);
}
/**
* Checks if the pseudo is available (nobody connected with the same pseudo)
* @param packet the packet containing the pseudo
*/
@Override
public void visitPacket(LoginPacket packet) {
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 !");
}
/**
* Get the list of rooms
* @param packet the packet containing the request
*/
@Override
public void visitPacket(RequestRoomListPacket packet) {
if (!checkLogin())
@@ -99,6 +124,10 @@ public class ServerConnexion implements PacketVisitor {
sendPacket(new RoomListPacket(server.getRoomNames()));
}
/**
* Send a message to the room
* @param packet the packet containing the message
*/
@Override
public void visitPacket(SendChatMessagePacket packet) {
if (!checkLogin())
@@ -107,11 +136,18 @@ public class ServerConnexion implements PacketVisitor {
sendPacket(new ServerResponsePacket(messageSent ? Response.MessageSent : Response.MessageNotSent));
}
/**
* Disconnect the client
* @param packet the packet containing the request
*/
@Override
public void visitPacket(DisconnectPacket packet) {
this.onDisconnect();
}
/**
* Remove all room connections while disconnecting the client
*/
private void onDisconnect() {
if (this.server.isInRoom(this)) {
this.onRoomLeave(this.server.getRoomName(this));
@@ -121,11 +157,18 @@ public class ServerConnexion implements PacketVisitor {
System.out.println("[Server] Chatter " + chatterName + " disconnected !");
}
/**
* Send a message to the room when a client joins
*/
private void onRoomJoin() {
String joinMessage = "Chatter " + this.chatterName + " joined the room !";
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) {
String joinMessage = "Chatter " + this.chatterName + " left the room !";
this.server.sendToRoom(roomName, new ChatMessagePacket(Instant.now(), "", joinMessage));