From ba60809d2f9c8f84aeaf6ae06e8de681d3196add Mon Sep 17 00:00:00 2001 From: EndMove Date: Sun, 20 Mar 2022 12:47:17 +0100 Subject: [PATCH] Fix autoclosable client handler and comment into UnicastThread.java --- .../domains/client/ClientHandler.java | 68 ++++++--------- .../domains/client/StoreProcessor.java | 17 ++-- .../domains/client/UnicastThread.java | 85 +++++++++++++++---- 3 files changed, 97 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index bdee567..16f7dff 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -10,9 +10,8 @@ import lightcontainer.protocol.rules.reader.SignoutRule; import lightcontainer.protocol.rules.reader.SignupRule; import lightcontainer.protocol.rules.writer.SignErrorRule; -import java.io.*; +import java.io.IOException; import java.net.Socket; -import java.nio.charset.StandardCharsets; /** * ClientHandler @@ -42,7 +41,6 @@ public class ClientHandler extends UnicastThread implements AutoCloseable { this.protocolRep = protocolRep; } - /** * Thread Function * Start the dialogue with the client. @@ -51,23 +49,24 @@ public class ClientHandler extends UnicastThread implements AutoCloseable { */ @Override public void run() { + // Set process to true this.setRunning(true); + // Process while while (this.isRunning()) { // Signifie le démarrage d'une nouvelle rquête getContext().newBundle(); - try { String command = this.readLine(); if (command != null) { System.out.println("Client: " + command); } else { - repository.disconnect(this); + this.repository.disconnect(this); break; } - ProtocolReader.ProtocolResult ruleResult = protocolRep.executeReader(getContext(), command + "\r\n"); + ProtocolReader.ProtocolResult ruleResult = this.protocolRep.executeReader(getContext(), command + "\r\n"); if (ruleResult == null) { - repository.disconnect(this); + this.repository.disconnect(this); break; } @@ -80,33 +79,25 @@ public class ClientHandler extends UnicastThread implements AutoCloseable { if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND && !fileFrontEnd.canExecuteCommand(ruleResult.getRequestDomain())) { this.print(ruleResult.onNotExecutable(getContext())); // Renvoie au client } else if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND) { - fileFrontEnd.newCommand(getContext(), writerCommand, ruleResult.getRequestDomain()); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd - + this.fileFrontEnd.newCommand(getContext(), writerCommand, ruleResult.getRequestDomain()); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd // Attend la fin de la réalisation de la tâche waitTaskResponse(); - if (response != null) { - this.write(response.getCommand()); // Renvoie au client - response.write(this.getOutputStream()); // Ecrit au client si nécessaire + if (this.response != null) { + this.write(this.response.getCommand()); // Renvoie au client + this.response.write(this.getOutputStream()); // Ecrit au client si nécessaire } else { this.print(ruleResult.onNotExecutable(getContext())); // Renvoie au client } } else { this.print(writerCommand.getCommand()); // Renvoie au client } - } else { accessDenied(); } } catch (IOException e) { - repository.disconnect(this); - break; + this.close(); } } - - try { - this.close(); - System.out.printf("[CLIENT] %s s'est déconnecté\n", getContext().getLogin()); - } catch (Exception ignored) {} } /** @@ -119,7 +110,6 @@ public class ClientHandler extends UnicastThread implements AutoCloseable { checkSignout(ruleResult); if (getContext().isConnected()) return true; - try { ruleResult .getClass() @@ -129,10 +119,8 @@ public class ClientHandler extends UnicastThread implements AutoCloseable { try { ruleResult.getClass().asSubclass(SigninRule.Result.class); return true; - } catch (ClassCastException e2) { - } + } catch (ClassCastException ignored) { } } - return false; } @@ -140,8 +128,7 @@ public class ClientHandler extends UnicastThread implements AutoCloseable { * Lorsque l'accès à été refusé. */ private void accessDenied() { - System.out.println("AIEAIEAIE"); - ProtocolWriter.ProtocolResult signErrorResult = protocolRep.executeWriter(getContext(), SignErrorRule.NAME); + ProtocolWriter.ProtocolResult signErrorResult = this.protocolRep.executeWriter(getContext(), SignErrorRule.NAME); this.write(signErrorResult.getCommand()); // Envoie SignError car echec de la connection } @@ -153,21 +140,10 @@ public class ClientHandler extends UnicastThread implements AutoCloseable { private void checkSignout(ProtocolReader.ProtocolResult ruleResult) { try { ruleResult.getClass().asSubclass(SignoutRule.Result.class); - repository.disconnect(this); - } catch (ClassCastException e2) {} + this.repository.disconnect(this); + } catch (ClassCastException ignored) {} } - /** - * Vérifie s'il s'âgit d'une demande de déconnexion - * @param ruleResult - private void checkSignError(ProtocolWriter.ProtocolResult ruleResult) { - if (ruleResult.getCommand().startsWith(SignErrorRule.NAME)) { - System.out.println("Pas pu connecter"); - repository.disconnect(this); - } - } - */ - /** * Permet au Client d'attendre la fin de la réalisation de sa tâche */ @@ -201,11 +177,15 @@ public class ClientHandler extends UnicastThread implements AutoCloseable { */ @Override public void close() { - try { - super.close(); - } catch (Exception e) {} - System.out.println("Call close"); - this.setRunning(false); + if (this.isRunning()) { + try { + super.close(); + this.repository.disconnect(this); + System.out.printf("[CLIENT] %s s'est déconnecté\n", getContext().getLogin()); + } catch (IOException e) { + System.out.println("[CH] Error while closing client."); + } + } } public String getLogin() { diff --git a/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java b/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java index 718eb37..1dc2b15 100644 --- a/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java +++ b/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java @@ -1,15 +1,12 @@ package lightcontainer.domains.client; -import lightcontainer.domains.Task; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.interfaces.StoreProcessorFFE; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolWriter; -import java.io.*; +import java.io.IOException; import java.net.Socket; -import java.net.SocketException; -import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.Objects; @@ -35,7 +32,6 @@ public class StoreProcessor extends UnicastThread implements AutoCloseable { private ProtocolWriter.ProtocolResult protocolResult; private final ProtocolRepository protocolRep; - // Constructor public StoreProcessor(Socket socket, String domain, StoreProcessorFFE ffe, ProtocolRepository protocolRep) { super(socket); @@ -44,7 +40,6 @@ public class StoreProcessor extends UnicastThread implements AutoCloseable { this.protocolRep = protocolRep; } - /** * Thread Function * Start the dialogue with the storebackend. @@ -53,8 +48,9 @@ public class StoreProcessor extends UnicastThread implements AutoCloseable { */ @Override public void run() { + // Set process to true this.setRunning(true); - + // Process while while (this.isRunning()) { try { if (protocolResult == null) { // Si on n'a pas encore la commande à envoyer @@ -72,7 +68,6 @@ public class StoreProcessor extends UnicastThread implements AutoCloseable { alertAvailable(null); break; } - // Response String responseCommand = this.readLine(); if (responseCommand != null && !responseCommand.isBlank()) { @@ -100,12 +95,12 @@ public class StoreProcessor extends UnicastThread implements AutoCloseable { } } - // Fermeture du SBE + // Closing SBE try { super.close(); this.fileFrontEnd.onStoreDisconnect(this.domain); - } catch (Exception ioException) { - System.out.println("[ERROR] Error while closing SBE (" + domain + ") : " + ioException.getMessage()); + } catch (Exception e) { + System.out.println("[ERROR] Error while closing SBE (" + domain + ") : " + e.getMessage()); } } diff --git a/app/src/main/java/lightcontainer/domains/client/UnicastThread.java b/app/src/main/java/lightcontainer/domains/client/UnicastThread.java index b363c10..186c455 100644 --- a/app/src/main/java/lightcontainer/domains/client/UnicastThread.java +++ b/app/src/main/java/lightcontainer/domains/client/UnicastThread.java @@ -5,21 +5,28 @@ import java.net.Socket; import java.nio.charset.StandardCharsets; public abstract class UnicastThread extends Thread implements AutoCloseable { - + // Variables private final Socket socket; - private Context context; - private BufferedReader reader; - private PrintWriter writer; - private boolean isRunning = false; + /** + * UnicastThread Constructor + * + * @param socket Unicast Socket + */ public UnicastThread(Socket socket) { this(socket, null); } + /** + * UnicastThread Constructor + * + * @param socket Unicast Socket + * @param context Connection context + */ public UnicastThread(Socket socket, Context context) { this.socket = socket; this.context = context; @@ -49,49 +56,91 @@ public abstract class UnicastThread extends Thread implements AutoCloseable { } } - + /** + * Read from the socket. + * + * @return Returns the line that was read + */ protected String readLine() throws IOException { - return reader.readLine(); + return this.reader.readLine(); } + /** + * Write to the socket. + * + * @param str Content to write. + */ protected void write(String str) { - writer.write(str); - writer.flush(); + this.writer.write(str); + this.writer.flush(); } + /** + * Print to the socket. + * + * @param str Content to print. + */ protected void print(String str) { - writer.print(str); - writer.flush(); + this.writer.print(str); + this.writer.flush(); } + /** + * Getter, allow to retrieve an inputstream of the socket. + * + * @return Socket inputstream. + */ protected InputStream getInputStream() throws IOException { - return socket.getInputStream(); + return this.socket.getInputStream(); } + /** + * Getter, allow to retrieve an outputstream of the socket. + * + * @return Socket outputstream. + */ protected OutputStream getOutputStream() throws IOException { - return socket.getOutputStream(); + return this.socket.getOutputStream(); } - + /** + * Close all connection stream and resources. + */ @Override - public void close() throws Exception { + public void close() throws IOException { this.reader.close(); this.writer.close(); this.socket.close(); } + /** + * Allow to verify if the thread is running. + * @return True; yes, False; no. + */ protected boolean isRunning() { - return isRunning; + return this.isRunning; } + /** + * Setter, define the running status. + * @param running True; is running, False; is not running. + */ protected void setRunning(boolean running) { - isRunning = running; + this.isRunning = running; } + /** + * Retrieve connection context. + * @return Context. + */ protected Context getContext() { - return context; + return this.context; } + /** + * Set connection context. + * @param context context. + */ protected void setContext(Context context) { this.context = context; }