feat(Room): Traitement des requêtes JOIN, LSTP, POST et MESG liées aux salons textuels, fix des vues des messages client-side

This commit is contained in:
Emi Boucly 2025-01-15 15:45:30 +01:00
parent 29cf0a3b43
commit b34ada0942
5 changed files with 133 additions and 26 deletions

View file

@ -110,7 +110,7 @@ public class ChatController implements Initializable {
contactsListView.getSelectionModel().selectedItemProperty().addListener(
(observableValue, previous, selected) -> handleContactSelection((Contact) selected));
roomsListView.getSelectionModel().selectedItemProperty().addListener(
(observableValue, previous, selected) -> handleContactSelection((Contact) selected));
(observableValue, previous, selected) -> handleRoomSelection((Room) selected));
validatorLogin.createCheck()
.dependsOn("login", loginTextField.textProperty())
@ -119,7 +119,11 @@ public class ChatController implements Initializable {
.immediate();
ObservableValue<Boolean> canSendCondition = connectionButton.selectedProperty().not()
.or(contactsListView.getSelectionModel().selectedItemProperty().isNull());
.or(
roomsListView.getSelectionModel().selectedItemProperty().isNull()
.and(contactsListView.getSelectionModel().selectedItemProperty().isNull())
);
sendButton.disableProperty().bind(canSendCondition);
messageTextField.disableProperty().bind(canSendCondition);
@ -140,7 +144,12 @@ public class ChatController implements Initializable {
}
private void onActionSend(ActionEvent actionEvent) {
String login = getSelectedContactLogin();
String login = null;
if (!(getSelectedContactLogin() == null)) {
login = getSelectedContactLogin();
} else if (!(getSelectedRoomName() == null)) {
login = getSelectedRoomName();
}
if (login != null) {
Message message = new Message(login, messageTextField.getText());
LOGGER.info("Sending " + message);
@ -212,8 +221,10 @@ public class ChatController implements Initializable {
private void clearLists() {
this.contactMap = new ContactMap();
this.postVector = new PostVector();
this.roomMap = new RoomMap();
contactObservableList.clear();
postsObservableList.clear();
roomObservableList.clear();
}
private void checkLogin(Check.Context context) {
@ -281,6 +292,19 @@ public class ChatController implements Initializable {
return login;
}
public String getSelectedRoomName() {
Room room;
String roomName;
try {
room = (Room) roomsListView.getSelectionModel().getSelectedItem();
roomName = room.getRoomName();
} catch (Exception e) {
roomName = null;
}
LOGGER.info("Selected room: " + roomName);
return roomName;
}
public Contact getContact() {
return contact;
}
@ -289,10 +313,33 @@ public class ChatController implements Initializable {
return contactMap;
}
void handleRoomSelection(Room roomSelected) {
if (roomSelected != null) {
LOGGER.info("Clic sur " + roomSelected);
}
if (!contactsListView.getSelectionModel().isEmpty()) {
contactsListView.getSelectionModel().clearSelection();
}
contact.setCurrentRoom(roomSelected.getRoomName());
Post postSys = new Post("system", loginTextField.getText(), "Bienvenue dans le salon " + roomSelected);
postsObservableList.clear();
postsObservableList.add(postSys);
client.sendEvent(new rtgre.modeles.Event("JOIN", new JSONObject().put("room", roomSelected.getRoomName())));
client.sendListPostEvent(0, roomSelected.toString());
postListView.refresh();
}
void handleContactSelection(Contact contactSelected) {
if (contactSelected != null) {
LOGGER.info("Clic sur " + contactSelected);
}
if (!roomsListView.getSelectionModel().isEmpty()) {
roomsListView.getSelectionModel().clearSelection();
}
Post postSys = new Post("system", loginTextField.getText(), "Bienvenue dans la discussion avec " + contactSelected.getLogin());
postsObservableList.clear();
postsObservableList.add(postSys);
@ -324,13 +371,24 @@ public class ChatController implements Initializable {
}
private void handlePostEvent(JSONObject content) {
System.out.println(content.getString("to").equals(((Contact) contactsListView.getSelectionModel().getSelectedItem()).getLogin()));
if (content.getString("to").equals(((Contact) contactsListView.getSelectionModel().getSelectedItem()).getLogin()) ||
content.getString("from").equals(loginTextField.getText())) {
postVector.add(Post.fromJson(content));
postsObservableList.add(Post.fromJson(content));
postListView.refresh();
if (contactsListView.getSelectionModel().getSelectedItem() != null) {
System.out.println(contactsListView.getSelectionModel().getSelectedItem());
if (content.getString("to").equals(contact.getLogin()) ||
content.getString("from").equals(loginTextField.getText())) {
System.out.println("New message! to:dm");
postVector.add(Post.fromJson(content));
postsObservableList.add(Post.fromJson(content));
postListView.refresh();
}
} else if (roomsListView.getSelectionModel().getSelectedItem() != null) {
if (content.getString("to").contains("#")) {
if (this.contact.getCurrentRoom().contains(content.getString("to"))) {
System.out.println("New Message! to:room");
postVector.add(Post.fromJson(content));
postsObservableList.add(Post.fromJson(content));
postListView.refresh();
}
}
}
}

View file

@ -88,6 +88,7 @@ public class ChatClient extends ClientTCP {
LOGGER.info(RED + "Réception: " + message + RST);
LOGGER.info(RED + message + RST);
if (listener != null) {
System.out.println(message);
Platform.runLater(() -> listener.handleEvent(Event.fromJson(message)));
}
}

View file

@ -137,4 +137,9 @@ public class Contact {
System.out.println("Erreur : " + e.getMessage());
}
}
public void setCurrentRoom(String currentRoom) {
this.currentRoom = currentRoom;
}
}

View file

@ -4,7 +4,7 @@ import java.util.TreeMap;
public class RoomMap extends TreeMap<String, Room> {
public void add(Room room) {
this.put(room.roomName, room);
this.put(room.getRoomName(), room);
}
public void loadDefaultRooms() {

View file

@ -261,6 +261,10 @@ public class ChatServer {
doListRoom(event.getContent());
LOGGER.info("Sending Rooms");
return true;
} else if (event.getType().equals(Event.JOIN)) {
doJoin(event.getContent());
LOGGER.info("New user joining room!");
return true;
} else if (event.getType().equals(Event.QUIT)) {
LOGGER.info("Déconnexion");
return false;
@ -270,16 +274,28 @@ public class ChatServer {
}
}
private void doJoin(JSONObject content) {
if (content.getString("room").isEmpty()) {
user.setCurrentRoom(null);
}
if (user.getLogin().isEmpty()) {
user.setCurrentRoom(null);
return;
}
if (roomMap.get(content.getString("room")).getLoginSet() == null) {
user.setCurrentRoom(content.getString("room"));
}
else if (roomMap.get(content.getString("room")).getLoginSet().contains(user.getLogin())) {
user.setCurrentRoom(content.getString("room"));
}
}
private void doListRoom(JSONObject content) {
System.out.println(contactMap.getContact(user.getLogin()).isConnected());
if (contactMap.getContact(user.getLogin()).isConnected()) {
for (Room room: roomMap.values()) {
System.out.println(room);
try {
System.out.println(new Event("ROOM", room.toJsonObject()).toJson());
send(new Event("ROOM", room.toJsonObject()).toJson());
} catch (IOException e) {
System.out.println(e.getMessage());
throw new IllegalStateException();
}
}
@ -287,14 +303,22 @@ public class ChatServer {
}
private void doListPost(JSONObject content) throws JSONException, IllegalStateException {
if (contactMap.getContact(user.getLogin()).isConnected()) {
if (!contactMap.containsKey(content.getString("select"))) {
if (!contactMap.containsKey(content.getString("select")) && !roomMap.containsKey(content.getString("select"))) {
System.out.println("!select");
throw new IllegalStateException();
}
for (Post post: postVector.getPostsSince(content.getLong("since"))) {
if (post.getTo().equals(content.getString("select")) ||
post.getFrom().equals(content.getString("select"))) {
if (!content.getString("select").contains("#")) {
System.out.println("!#");
for (Post post : postVector.getPostsSince(content.getLong("since"))) {
if (post.getTo().equals(content.getString("select")) ||
post.getFrom().equals(content.getString("select"))) {
sendEventToContact(contactMap.getContact(user.getLogin()), new Event(Event.POST, post.toJsonObject()));
}
}
} else if (user.getCurrentRoom().equals(content.getString("select"))) {
System.out.println("#");
for (Post post: postVector.getPostsSince(content.getLong("since"))) {
sendEventToContact(contactMap.getContact(user.getLogin()), new Event(Event.POST, post.toJsonObject()));
}
}
@ -303,9 +327,10 @@ public class ChatServer {
private void doMessage(JSONObject content) throws JSONException, IllegalStateException {
if (contactMap.getContact(user.getLogin()).isConnected()) {
if (content.getString("to").equals(user.getLogin()) || !contactMap.containsKey(content.getString("to"))) {
throw new IllegalStateException();
} else {
if (content.getString("to").equals(user.getLogin()) ||
(!contactMap.containsKey(content.getString("to"))) && !roomMap.containsKey(content.getString("to"))) {
throw new IllegalStateException("IllegalStateException! Cannot Post");
} if(!content.getString("to").contains("#")) {
Post post = new Post(
user.getLogin(),
Message.fromJson(content)
@ -316,7 +341,24 @@ public class ChatServer {
sendEventToContact(contactMap.getContact(post.getTo()), postEvent);
postVector.add(post);
LOGGER.info("Fin de doMessage");
LOGGER.info("Fin de doMessage:dm");
} else {
Post post = new Post(
user.getLogin(),
Message.fromJson(content)
);
Event postEvent = new Event("POST", post.toJsonObject());
for (ChatClientHandler client: clientList) {
if (client.user.getCurrentRoom() != null) {
if (client.user.getCurrentRoom().equals(content.getString("to"))) {
sendEventToContact(client.user, postEvent);
}
}
}
postVector.add(post);
LOGGER.info("Fin de doMessage:room");
}
}
}
@ -345,6 +387,7 @@ public class ChatServer {
LOGGER.info("Connexion de " + login);
contactMap.getContact(login).setConnected(true);
this.user = contactMap.getContact(login);
System.out.println(user.isConnected());
sendAllOtherClients(
findClient(contactMap.getContact(login)),
new Event("CONT", user.toJsonObject()).toJson()
@ -366,11 +409,11 @@ public class ChatServer {
public void sendAllOtherClients(ChatClientHandler fromClient, String message) {
for (ChatClientHandler client : clientList) {
if (!client.equals(this)) {
if (!client.equals(fromClient)) {
LOGGER.fine(clientList.toString());
LOGGER.fine("Envoi vers [%s] : %s".formatted(client.getIpPort(), message));
try {
client.send("[%s] %s".formatted(fromClient.getIpPort(), message));
client.send(message);
} catch (Exception e) {
LOGGER.severe("[%s] %s".formatted(client.getIpPort(), e));
client.close();