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();
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);

View file

@ -1,8 +1,13 @@
package rtgre.chat.net;
import org.json.JSONObject;
import rtgre.chat.ChatController;
import rtgre.modeles.Contact;
import rtgre.modeles.Event;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Logger;
import static rtgre.chat.ChatApplication.LOGGER;
@ -26,6 +31,32 @@ public class ChatClient extends ClientTCP {
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
public void receiveLoop() {
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.JSONObject;
import rtgre.modeles.Contact;
import rtgre.modeles.ContactMap;
import rtgre.modeles.Event;
@ -101,7 +102,7 @@ public class ChatServer {
*/
private void handleNewClient(Socket sock) throws IOException {
ChatClientHandler client = new ChatClientHandler(sock);
Thread clientLoop = new Thread(client::echoLoop);
Thread clientLoop = new Thread(client::eventReceiveLoop);
clientLoop.start();
clientList.add(client);
LOGGER.fine("Ajout du client [%s] dans la liste (%d clients connectés)"
@ -181,9 +182,8 @@ public class ChatServer {
break;
}
LOGGER.info("[%s] Réception de : %s".formatted(ipPort, message));
LOGGER.info("[%s] Envoi de : %s".formatted(ipPort, message));
try {
if (handleEvent(message)) {
if (!handleEvent(message)) {
break;
}
} catch (Exception e) {
@ -201,23 +201,33 @@ public class ChatServer {
switch (event.getType()) {
case Event.AUTH:
doLogin(event.getContent());
return false;
default:
LOGGER.finest("Login successful");
return true;
default:
LOGGER.warning("Unhandled event type: " + event.getType());
return false;
}
}
private void doLogin(JSONObject content) {
String login = content.getString("login");
if (login.equals("")) {
if (login.isEmpty()) {
LOGGER.warning("Aucun login fourni");
throw new JSONException("Aucun login fourni");
} else if (!contactMap.containsKey(login)) {
LOGGER.warning("Login non-authorisé");
throw new IllegalStateException("Login non-authorisé");
} else {
LOGGER.info("Connexion de " + login);
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 {
LOGGER.finest("send: %s".formatted(message));
out.println(message);
@ -247,7 +257,7 @@ public class ChatServer {
public String receive() throws IOException {
String message = in.readLine();
LOGGER.finest("receive: %s".formatted(message));
LOGGER.info("receive: %s".formatted(message));
if (message == null) {
throw new IOException("End of the stream has been reached");
}