From b145e8097828962848c3763e8fbc0b4b276e7def Mon Sep 17 00:00:00 2001 From: bouclyma Date: Tue, 7 Jan 2025 10:44:53 +0100 Subject: [PATCH] =?UTF-8?q?feat(event):=202.6.4:=20Listing=20des=20contact?= =?UTF-8?q?s=20connus=20du=20serveur=20envoy=C3=A9=20au=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/rtgre/chat/ChatController.java | 54 ++++++++++++++++- .../main/java/rtgre/chat/net/ChatClient.java | 4 ++ .../main/java/rtgre/server/ChatServer.java | 58 ++++++++++++++++--- 3 files changed, 104 insertions(+), 12 deletions(-) diff --git a/chat/src/main/java/rtgre/chat/ChatController.java b/chat/src/main/java/rtgre/chat/ChatController.java index d14a9e9..79b474d 100644 --- a/chat/src/main/java/rtgre/chat/ChatController.java +++ b/chat/src/main/java/rtgre/chat/ChatController.java @@ -22,11 +22,13 @@ import javafx.stage.Stage; import net.synedra.validatorfx.Check; import net.synedra.validatorfx.TooltipWrapper; import net.synedra.validatorfx.Validator; +import org.json.JSONObject; import rtgre.chat.graphisme.ContactListViewCell; import rtgre.chat.graphisme.PostListViewCell; import rtgre.chat.net.ChatClient; import rtgre.modeles.*; +import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -154,24 +156,30 @@ public class ChatController implements Initializable { String host = matcher.group(1); int port = (matcher.group(2) != null) ? Integer.parseInt(matcher.group(2)) : 2024; try { + LOGGER.info(host + ":" + port); this.client = new ChatClient(host, port, this); initContactListView(); initPostListView(); clearLists(); contactMap.add(this.contact); this.contact.setConnected(true); + + client.sendAuthEvent(contact); + client.sendEvent(new rtgre.modeles.Event(rtgre.modeles.Event.LIST_CONTACTS, new JSONObject())); + initContactListView(); initPostListView(); this.statusLabel.setText("Connected to %s@%s:%s".formatted(this.contact.getLogin(), host, port)); - client.sendAuthEvent(contact); } catch (IOException e) { new Alert(Alert.AlertType.ERROR, "Erreur de connexion").showAndWait(); connectionButton.setSelected(false); } } else if (!connectionButton.isSelected()) { clearLists(); - this.client.close(); - this.contact.setConnected(false); + if (this.client.isConnected()) { + this.client.close(); + this.contact.setConnected(false); + } statusLabel.setText("not connected to " + hostComboBox.getValue()); } @@ -265,4 +273,44 @@ public class ChatController implements Initializable { postsObservableList.add(postSys); postListView.refresh(); } + + public void handleEvent(rtgre.modeles.Event event) { + LOGGER.info("Received new event! : " + event); + LOGGER.info(event.getType()); + +// switch (event.getType()) { +// case "CONT": +// handleContEvent(event.getContent()); +// default: +// LOGGER.warning("Unhandled event type: " + event.getType()); +// this.client.close(); +// } + + if (event.getType().equals("CONT")) { + handleContEvent(event.getContent()); + } else { + LOGGER.warning("Unhandled event type: " + event.getType()); + this.client.close(); + } + } + + private void handleContEvent(JSONObject content) { + Contact contact = contactMap.getContact(content.getString("login")); + if (contact != null) { + LOGGER.info(contactMap.toString()); + contactMap.getContact(content.getString("login")).setConnected(content.getBoolean("connected")); + contactsListView.refresh(); + LOGGER.info(contactMap.toString()); + } else { + LOGGER.info(contactMap.toString()); + Contact user = Contact.fromJSON( + content, + new File("src/main/resources/rtgre/chat/avatars.png") + ); + contactMap.add(user); + contactObservableList.add(user); + LOGGER.info(contactMap.toString()); + } + } + } \ No newline at end of file diff --git a/chat/src/main/java/rtgre/chat/net/ChatClient.java b/chat/src/main/java/rtgre/chat/net/ChatClient.java index b7b1571..4c60c58 100644 --- a/chat/src/main/java/rtgre/chat/net/ChatClient.java +++ b/chat/src/main/java/rtgre/chat/net/ChatClient.java @@ -1,5 +1,6 @@ package rtgre.chat.net; +import javafx.application.Platform; import org.json.JSONObject; import rtgre.chat.ChatController; import rtgre.modeles.Contact; @@ -65,6 +66,9 @@ public class ChatClient extends ClientTCP { String message = this.receive(); LOGGER.info(RED + "Réception: " + message + RST); LOGGER.info(RED + message + RST); + if (listener != null) { + Platform.runLater(() -> listener.handleEvent(Event.fromJson(message))); + } } } catch (IOException e) { LOGGER.severe("[%s] %s".formatted(ipPort, e)); diff --git a/chat/src/main/java/rtgre/server/ChatServer.java b/chat/src/main/java/rtgre/server/ChatServer.java index cd620dd..ec234f1 100644 --- a/chat/src/main/java/rtgre/server/ChatServer.java +++ b/chat/src/main/java/rtgre/server/ChatServer.java @@ -2,6 +2,7 @@ package rtgre.server; import org.json.JSONException; import org.json.JSONObject; +import rtgre.chat.net.ChatClient; import rtgre.modeles.Contact; import rtgre.modeles.ContactMap; import rtgre.modeles.Event; @@ -38,6 +39,7 @@ public class ChatServer { public static void main(String[] args) throws IOException { ChatServer server = new ChatServer(2024); + daisyConnect(); server.acceptClients(); } @@ -123,7 +125,7 @@ public class ChatServer { ChatClientHandler user = findClient(contact); if (!(user == null)) { try { - user.send(event.toString()); + user.send(event.toJson()); } catch (Exception e) { LOGGER.warning("!!Erreur de l'envoi d'Event à %s, fermeture de la connexion".formatted(user.user.getLogin())); user.close(); @@ -143,6 +145,12 @@ public class ChatServer { return contactMap; } + /** Temporaire : connecte daisy pour test */ + public static void daisyConnect() throws IOException { + ChatClient client = new ChatClient("localhost", 2024, null); + client.sendAuthEvent(new Contact("daisy", null)); + } + private class ChatClientHandler { public static final String END_MESSAGE = "fin"; /** @@ -229,14 +237,42 @@ public class ChatServer { private boolean handleEvent(String message) throws JSONException, IllegalStateException { Event event = Event.fromJson(message); - switch (event.getType()) { - case Event.AUTH: - doLogin(event.getContent()); - LOGGER.finest("Login successful"); - return true; - default: - LOGGER.warning("Unhandled event type: " + event.getType()); - return false; +// switch (event.getType()) { +// case Event.AUTH: +// doLogin(event.getContent()); +// LOGGER.finest("Login successful"); +// return true; +// case Event.LIST_CONTACTS: +// doListContact(event.getContent()); +// LOGGER.finest("Sending contacts"); +// default: +// LOGGER.warning("Unhandled event type: " + event.getType()); +// return false; +// } + if (event.getType().equals(Event.AUTH)) { + doLogin(event.getContent()); + LOGGER.finest("Login successful"); + return true; + } else if (event.getType().equals(Event.LIST_CONTACTS)) { + doListContact(event.getContent()); + LOGGER.finest("Sending contacts"); + } else { + LOGGER.warning("Unhandled event type: " + event.getType()); + return false; + + } + return false; + } + + private void doListContact(JSONObject content) throws JSONException, IllegalStateException { + for (Contact contact: contactMap.values()) { + if (contactMap.getContact(user.getLogin()).isConnected()) { + try { + send(new Event(Event.CONT, contact.toJsonObject()).toJson()); + } catch (IOException e) { + throw new IllegalStateException(); + } + } } } @@ -252,6 +288,10 @@ public class ChatServer { LOGGER.info("Connexion de " + login); contactMap.getContact(login).setConnected(true); this.user = contactMap.getContact(login); + sendAllOtherClients( + findClient(contactMap.getContact(login)), + new Event("CONT", user.toJsonObject()).toJson() + ); } }