mirror of
https://github.com/Akomry/sae302_applicom.git
synced 2025-12-06 11:43:53 +00:00
feat(Event): 2.6.3 - findClient => mappage inexistant ??
This commit is contained in:
parent
d2ad689b48
commit
df7442f7a6
3 changed files with 49 additions and 7 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue