mirror of
https://github.com/Akomry/sae302_applicom.git
synced 2025-12-06 11:43:53 +00:00
feat(event): ContactWithEventTest3.java: test passé (2.6.3)
This commit is contained in:
parent
df7442f7a6
commit
8b3a0e5b21
4 changed files with 158 additions and 5 deletions
|
|
@ -1,6 +1,8 @@
|
|||
package rtgre.modeles;
|
||||
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
|
|
@ -101,6 +103,20 @@ public class Contact {
|
|||
return 0;
|
||||
}
|
||||
|
||||
public JSONObject toJsonObject() {
|
||||
return new JSONObject()
|
||||
.put("login", this.login)
|
||||
.put("connected", this.connected);
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return toJsonObject().toString();
|
||||
}
|
||||
|
||||
public static Contact fromJSON(JSONObject jsonObject, File banque_avatars) {
|
||||
return new Contact(jsonObject.getString("login"), jsonObject.getBoolean("connected"), banque_avatars);
|
||||
}
|
||||
|
||||
public static BufferedImage avatarFromLogin(File fichier, String login) throws IOException {
|
||||
/**
|
||||
* Renvoie une sous-image en fonction d'une banque d'image et d'un login.
|
||||
|
|
|
|||
|
|
@ -110,6 +110,35 @@ public class ChatServer {
|
|||
//client.echoLoop();
|
||||
}
|
||||
|
||||
public ChatClientHandler findClient(Contact contact) {
|
||||
for (ChatClientHandler user: clientList) {
|
||||
if (user.user.equals(contact)) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void sendEventToContact(Contact contact, Event event) {
|
||||
ChatClientHandler user = findClient(contact);
|
||||
if (!(user == null)) {
|
||||
try {
|
||||
user.send(event.toString());
|
||||
} catch (Exception e) {
|
||||
LOGGER.warning("!!Erreur de l'envoi d'Event à %s, fermeture de la connexion".formatted(user.user.getLogin()));
|
||||
user.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void sendEventToAllContacts(Event event) {
|
||||
for (Contact contact: contactMap.values()) {
|
||||
if (contact.isConnected()) {
|
||||
sendEventToContact(contact, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ContactMap getContactMap() {
|
||||
return contactMap;
|
||||
}
|
||||
|
|
@ -133,6 +162,8 @@ public class ChatServer {
|
|||
*/
|
||||
private String ipPort;
|
||||
|
||||
private Contact user;
|
||||
|
||||
/**
|
||||
* Initialise les attributs {@link #sock} (socket connecté au client),
|
||||
* {@link #out} (flux de caractères UTF-8 en sortie) et
|
||||
|
|
@ -220,14 +251,10 @@ public class ChatServer {
|
|||
} else {
|
||||
LOGGER.info("Connexion de " + login);
|
||||
contactMap.getContact(login).setConnected(true);
|
||||
this.user = contactMap.getContact(login);
|
||||
}
|
||||
}
|
||||
|
||||
public ChatClientHandler findClient(Contact contact) {
|
||||
String login = contact.getLogin();
|
||||
Contact contactRes = contactMap.get()
|
||||
}
|
||||
|
||||
public void send(String message) throws IOException {
|
||||
LOGGER.finest("send: %s".formatted(message));
|
||||
out.println(message);
|
||||
|
|
|
|||
BIN
chat/src/main/resources/rtgre/chat/banque_avatars.png
Normal file
BIN
chat/src/main/resources/rtgre/chat/banque_avatars.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 53 KiB |
Loading…
Add table
Add a link
Reference in a new issue