feat(Event): 2.6.3 - findClient => mappage inexistant ??

This commit is contained in:
bouclyma 2025-01-04 18:35:58 +01:00
parent d2ad689b48
commit df7442f7a6
3 changed files with 49 additions and 7 deletions

View file

@ -163,6 +163,7 @@ public class ChatController implements Initializable {
initContactListView(); initContactListView();
initPostListView(); initPostListView();
this.statusLabel.setText("Connected to %s@%s:%s".formatted(this.contact.getLogin(), host, port)); this.statusLabel.setText("Connected to %s@%s:%s".formatted(this.contact.getLogin(), host, port));
client.sendAuthEvent(contact);
} catch (IOException e) { } catch (IOException e) {
new Alert(Alert.AlertType.ERROR, "Erreur de connexion").showAndWait(); new Alert(Alert.AlertType.ERROR, "Erreur de connexion").showAndWait();
connectionButton.setSelected(false); connectionButton.setSelected(false);

View file

@ -1,8 +1,13 @@
package rtgre.chat.net; package rtgre.chat.net;
import org.json.JSONObject;
import rtgre.chat.ChatController; import rtgre.chat.ChatController;
import rtgre.modeles.Contact;
import rtgre.modeles.Event;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Logger; import java.util.logging.Logger;
import static rtgre.chat.ChatApplication.LOGGER; import static rtgre.chat.ChatApplication.LOGGER;
@ -26,6 +31,32 @@ public class ChatClient extends ClientTCP {
this.listener = listener; this.listener = listener;
} }
public void sendEvent(Event event) {
connected = true;
try {
String message = event.toJson();
if (message == null) { // fin du flux stdIn
message = END_MESSAGE;
}
System.out.println(BLUE + "Envoi: " + message + RST);
this.send(message);
if (END_MESSAGE.equals(message)) {
connected = false;
}
} catch (IOException e) {
LOGGER.severe(e.toString());
connected = false;
}
}
public void sendAuthEvent(Contact contact) {
Event authEvent = new Event(Event.AUTH, new JSONObject().put("login", contact.getLogin()));
sendEvent(authEvent);
}
@Override @Override
public void receiveLoop() { public void receiveLoop() {
LOGGER.info(RED + "Boucle de réception de messages..." + RST); LOGGER.info(RED + "Boucle de réception de messages..." + RST);

View file

@ -2,6 +2,7 @@ package rtgre.server;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import rtgre.modeles.Contact;
import rtgre.modeles.ContactMap; import rtgre.modeles.ContactMap;
import rtgre.modeles.Event; import rtgre.modeles.Event;
@ -101,7 +102,7 @@ public class ChatServer {
*/ */
private void handleNewClient(Socket sock) throws IOException { private void handleNewClient(Socket sock) throws IOException {
ChatClientHandler client = new ChatClientHandler(sock); ChatClientHandler client = new ChatClientHandler(sock);
Thread clientLoop = new Thread(client::echoLoop); Thread clientLoop = new Thread(client::eventReceiveLoop);
clientLoop.start(); clientLoop.start();
clientList.add(client); clientList.add(client);
LOGGER.fine("Ajout du client [%s] dans la liste (%d clients connectés)" LOGGER.fine("Ajout du client [%s] dans la liste (%d clients connectés)"
@ -181,9 +182,8 @@ public class ChatServer {
break; break;
} }
LOGGER.info("[%s] Réception de : %s".formatted(ipPort, message)); LOGGER.info("[%s] Réception de : %s".formatted(ipPort, message));
LOGGER.info("[%s] Envoi de : %s".formatted(ipPort, message));
try { try {
if (handleEvent(message)) { if (!handleEvent(message)) {
break; break;
} }
} catch (Exception e) { } catch (Exception e) {
@ -201,23 +201,33 @@ public class ChatServer {
switch (event.getType()) { switch (event.getType()) {
case Event.AUTH: case Event.AUTH:
doLogin(event.getContent()); doLogin(event.getContent());
return false; LOGGER.finest("Login successful");
default:
return true; return true;
default:
LOGGER.warning("Unhandled event type: " + event.getType());
return false;
} }
} }
private void doLogin(JSONObject content) { private void doLogin(JSONObject content) {
String login = content.getString("login"); String login = content.getString("login");
if (login.equals("")) { if (login.isEmpty()) {
LOGGER.warning("Aucun login fourni");
throw new JSONException("Aucun login fourni"); throw new JSONException("Aucun login fourni");
} else if (!contactMap.containsKey(login)) { } else if (!contactMap.containsKey(login)) {
LOGGER.warning("Login non-authorisé");
throw new IllegalStateException("Login non-authorisé"); throw new IllegalStateException("Login non-authorisé");
} else { } else {
LOGGER.info("Connexion de " + login);
contactMap.getContact(login).setConnected(true); contactMap.getContact(login).setConnected(true);
} }
} }
public ChatClientHandler findClient(Contact contact) {
String login = contact.getLogin();
Contact contactRes = contactMap.get()
}
public void send(String message) throws IOException { public void send(String message) throws IOException {
LOGGER.finest("send: %s".formatted(message)); LOGGER.finest("send: %s".formatted(message));
out.println(message); out.println(message);
@ -247,7 +257,7 @@ public class ChatServer {
public String receive() throws IOException { public String receive() throws IOException {
String message = in.readLine(); String message = in.readLine();
LOGGER.finest("receive: %s".formatted(message)); LOGGER.info("receive: %s".formatted(message));
if (message == null) { if (message == null) {
throw new IOException("End of the stream has been reached"); throw new IOException("End of the stream has been reached");
} }