From 58c8d7f51408f65b2f457503c9246cf5e9dce823 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Fri, 11 Mar 2022 17:02:52 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20possibilit=C3=A9=20de=20d=C3=A9connecte?= =?UTF-8?q?r=20un=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domains/client/ClientHandler.java | 27 ++++++++++++++++--- .../domains/server/UnicastServerListener.java | 2 +- .../lightcontainer/interfaces/UnicastCHR.java | 6 +++++ .../protocol/rules/reader/SignoutRule.java | 6 +++++ .../repository/ClientHandlerRepository.java | 6 +++++ 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index 90725fc..9da2ee0 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -1,10 +1,13 @@ package lightcontainer.domains.client; +import lightcontainer.domains.server.UnicastServerListener; import lightcontainer.interfaces.ClientHandlerFFE; import lightcontainer.interfaces.ProtocolRepository; +import lightcontainer.interfaces.UnicastCHR; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolWriter; import lightcontainer.protocol.rules.reader.SigninRule; +import lightcontainer.protocol.rules.reader.SignoutRule; import lightcontainer.protocol.rules.reader.SignupRule; import lightcontainer.protocol.rules.writer.SignErrorRule; import lightcontainer.protocol.rules.writer.SignOkRule; @@ -34,6 +37,7 @@ public class ClientHandler implements Runnable, AutoCloseable { private ProtocolRepository protocolRep; private Context context; private boolean client_run; + private UnicastCHR repository; private BufferedReader reader; @@ -41,7 +45,8 @@ public class ClientHandler implements Runnable, AutoCloseable { private ProtocolWriter.ProtocolResult response; // Constructor - public ClientHandler(Socket client, ClientHandlerFFE ffe, ProtocolRepository protocolRep, Context context) { + public ClientHandler(UnicastCHR repository, Socket client, ClientHandlerFFE ffe, ProtocolRepository protocolRep, Context context) { + this.repository = repository; this.fileFrontEnd = ffe; this.client = client; this.protocolRep = protocolRep; @@ -87,11 +92,13 @@ public class ClientHandler implements Runnable, AutoCloseable { String command = this.reader.readLine(); if (command != null) { System.out.println("Client: " + command); - } else this.client.close(); + } else { + repository.disconnect(this); + } ProtocolReader.ProtocolResult ruleResult = protocolRep.executeReader(context, command + "\r\n"); if (ruleResult == null) { - this.close(); + repository.disconnect(this); return; } @@ -120,6 +127,7 @@ public class ClientHandler implements Runnable, AutoCloseable { } } catch (IOException ignore) { ignore.printStackTrace(); + repository.disconnect(this); } } @@ -131,6 +139,7 @@ public class ClientHandler implements Runnable, AutoCloseable { * @return TRUE si le client possède l'accès demandé */ private boolean checkAccess(ProtocolReader.ProtocolResult ruleResult) { + checkSignout(ruleResult); if (context.isConnected()) return true; @@ -158,6 +167,17 @@ public class ClientHandler implements Runnable, AutoCloseable { writer.flush(); } + /** + * Vérifie s'il s'âgit d'une demande de déconnexion + * @param ruleResult + */ + private void checkSignout(ProtocolReader.ProtocolResult ruleResult) { + try { + ruleResult.getClass().asSubclass(SignoutRule.Result.class); + repository.disconnect(this); + } catch (ClassCastException e2) { } + } + /** * Permet au Client d'attendre la fin de la réalisation de sa tâche */ @@ -194,6 +214,7 @@ public class ClientHandler implements Runnable, AutoCloseable { try { this.client_run = false; this.client.close(); + System.out.printf("[CLIENT] %s s'est déconnecté\n", context.getLogin()); } catch (IOException ignored) { } } } diff --git a/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java b/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java index 9beddcd..d0bac9d 100644 --- a/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java +++ b/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java @@ -51,7 +51,7 @@ public class UnicastServerListener implements Runnable { Socket client = this.server.accept(); System.out.println("New Client"); // Create a new Handler client by passing these dependencies to it - ClientHandler clientHandler = new ClientHandler(client, ffe, protocolRep, new Context(AppData.getInstance())); // TODO passer FileFrontEnd ou faire ca dans le repository ?! + ClientHandler clientHandler = new ClientHandler(this.repository, client, ffe, protocolRep, new Context(AppData.getInstance())); // Add the client handler to its repository (clienthandlerrepository) this.repository.addClient(clientHandler); // Start the thread diff --git a/app/src/main/java/lightcontainer/interfaces/UnicastCHR.java b/app/src/main/java/lightcontainer/interfaces/UnicastCHR.java index 160b533..53af817 100644 --- a/app/src/main/java/lightcontainer/interfaces/UnicastCHR.java +++ b/app/src/main/java/lightcontainer/interfaces/UnicastCHR.java @@ -22,5 +22,11 @@ public interface UnicastCHR { */ void addClient(ClientHandler client); + /** + * Permet de demander la déconnection d'un client + * @param client Le client à déconnecter + */ + void disconnect(ClientHandler client); + void respondToClient(String client, ProtocolWriter.ProtocolResult response); } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SignoutRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SignoutRule.java index 4309f28..ee6936a 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SignoutRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SignoutRule.java @@ -17,9 +17,15 @@ public class SignoutRule extends ProtocolReader { super(NAME, PATTERN); } + public class Result extends ProtocolResult { + public Result(Context context) { + super(context); + } + } @Override protected T onExecuted(Context context, String... data) { return null; } + } diff --git a/app/src/main/java/lightcontainer/repository/ClientHandlerRepository.java b/app/src/main/java/lightcontainer/repository/ClientHandlerRepository.java index 1f288ea..ff17247 100644 --- a/app/src/main/java/lightcontainer/repository/ClientHandlerRepository.java +++ b/app/src/main/java/lightcontainer/repository/ClientHandlerRepository.java @@ -55,6 +55,12 @@ public class ClientHandlerRepository implements AutoCloseable, UnicastCHR { this.handlers.add(client); } + @Override + public void disconnect(ClientHandler client) { + if (handlers.remove(client)) + client.close(); + } + @Override public void respondToClient(String login, ProtocolWriter.ProtocolResult response) { for (ClientHandler client : handlers) {