diff --git a/chat/src/main/java/rtgre/modeles/ContactMap.java b/chat/src/main/java/rtgre/modeles/ContactMap.java index f44bcc0..5924158 100644 --- a/chat/src/main/java/rtgre/modeles/ContactMap.java +++ b/chat/src/main/java/rtgre/modeles/ContactMap.java @@ -9,4 +9,16 @@ public class ContactMap extends TreeMap { public Contact getContact(String login) { return this.get(login); } + + public void loadDefaultContacts() { + this.put("mickey", new Contact("mickey", null)); + this.put("minnie", new Contact("minnie", null)); + this.put("dingo", new Contact("dingo", null)); + this.put("riri", new Contact("riri", null)); + this.put("fifi", new Contact("fifi", null)); + this.put("loulou", new Contact("loulou", null)); + this.put("donald", new Contact("donald", null)); + this.put("daisy", new Contact("daisy", null)); + this.put("picsou", new Contact("picsou", null)); + } } diff --git a/chat/src/main/java/rtgre/server/ChatServer.java b/chat/src/main/java/rtgre/server/ChatServer.java index 3665663..957765e 100644 --- a/chat/src/main/java/rtgre/server/ChatServer.java +++ b/chat/src/main/java/rtgre/server/ChatServer.java @@ -1,5 +1,10 @@ package rtgre.server; +import org.json.JSONException; +import org.json.JSONObject; +import rtgre.modeles.ContactMap; +import rtgre.modeles.Event; + import java.io.*; import java.net.ServerSocket; import java.net.Socket; @@ -17,6 +22,7 @@ public class ChatServer { private static final Logger LOGGER = Logger.getLogger(ChatServer.class.getCanonicalName()); private Vector clientList; + private ContactMap contactMap; static { try { @@ -43,6 +49,8 @@ public class ChatServer { passiveSock = new ServerSocket(port); LOGGER.info("Serveur en écoute " + passiveSock); clientList = new Vector<>(); + contactMap = new ContactMap(); + contactMap.loadDefaultContacts(); } public void close() throws IOException { @@ -101,6 +109,10 @@ public class ChatServer { //client.echoLoop(); } + public ContactMap getContactMap() { + return contactMap; + } + private class ChatClientHandler { public static final String END_MESSAGE = "fin"; /** @@ -160,6 +172,52 @@ public class ChatServer { close(); } + public void eventReceiveLoop() { + try { + String message = null; + while (!END_MESSAGE.equals(message)) { + message = in.readLine(); + if (message == null) { + break; + } + LOGGER.info("[%s] Réception de : %s".formatted(ipPort, message)); + LOGGER.info("[%s] Envoi de : %s".formatted(ipPort, message)); + try { + if (handleEvent(message)) { + break; + } + } catch (Exception e) { + break; + } + } + } catch (IOException e) { + LOGGER.severe("[%s] %s".formatted(ipPort, e)); + } + close(); + } + + private boolean handleEvent(String message) throws JSONException, IllegalStateException { + Event event = Event.fromJson(message); + switch (event.getType()) { + case Event.AUTH: + doLogin(event.getContent()); + return false; + default: + return true; + } + } + + private void doLogin(JSONObject content) { + String login = content.getString("login"); + if (login.equals("")) { + throw new JSONException("Aucun login fourni"); + } else if (!contactMap.containsKey(login)) { + throw new IllegalStateException("Login non-authorisé"); + } else { + contactMap.getContact(login).setConnected(true); + } + } + public void send(String message) throws IOException { LOGGER.finest("send: %s".formatted(message)); out.println(message);