mirror of
https://github.com/Akomry/sae302_applicom.git
synced 2025-12-06 11:43:53 +00:00
feat(event): 2.6.4: Listing des contacts connus du serveur envoyé au client
This commit is contained in:
parent
8b3a0e5b21
commit
b145e80978
3 changed files with 104 additions and 12 deletions
|
|
@ -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();
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
// 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;
|
||||
default:
|
||||
} 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()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue