diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index 837b242..fba84bc 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -3,7 +3,6 @@ */ package lightcontainer; -import lightcontainer.domains.client.Context; import lightcontainer.domains.server.MulticastServerListener; import lightcontainer.domains.server.UnicastServerListener; import lightcontainer.interfaces.ProtocolRepository; @@ -13,11 +12,8 @@ import lightcontainer.repository.ClientHandlerRepository; import lightcontainer.repository.FileFrontEnd; import lightcontainer.repository.ProtocolRepositoryImpl; import lightcontainer.repository.StoreProcessorRepository; -import lightcontainer.storage.AppConfig; import lightcontainer.storage.AppData; -import java.io.File; - public class App { // Constant config server // -- Unicast client port @@ -40,13 +36,13 @@ public class App { protocolRep.addReader(new HelloRule()); protocolRep.addReader(new SigninRule(protocolRep)); protocolRep.addReader(new SignupRule(protocolRep)); + protocolRep.addReader(new SignoutRule()); protocolRep.addReader(new FilelistRule(protocolRep)); protocolRep.addReader(new SavefileRule(protocolRep)); protocolRep.addReader(new SendOkRule(protocolRep)); protocolRep.addWriter(new SignOkRule()); protocolRep.addWriter(new SignErrorRule()); - protocolRep.addWriter(new SignoutRule()); protocolRep.addWriter(new FilesRule()); protocolRep.addWriter(new SaveFileOkRule()); protocolRep.addWriter(new SaveFileErrorRule()); diff --git a/app/src/main/java/lightcontainer/domains/Task.java b/app/src/main/java/lightcontainer/domains/Task.java index 91a0efc..0164cea 100644 --- a/app/src/main/java/lightcontainer/domains/Task.java +++ b/app/src/main/java/lightcontainer/domains/Task.java @@ -2,13 +2,16 @@ package lightcontainer.domains; import lightcontainer.domains.client.Context; import lightcontainer.enumerations.TaskStatus; -import lightcontainer.enumerations.TaskType; import lightcontainer.protocol.ProtocolWriter; +/** + * Une tâche exécutable + */ public class Task { // Variables private TaskStatus status; private ProtocolWriter.ProtocolResult command; + private String storeDomain; /** @@ -22,6 +25,12 @@ public class Task { this.command = command; } + /** + * Permet de créer une instance de la class {@link Task} + * @param context Context à utiliser pour cette tâche + * @param command Commande à exécuter + * @return L'instance de la tâche créée + */ public static Task newInstance(Context context, ProtocolWriter.ProtocolResult command) { Task task = new Task(context, TaskStatus.PENDING, command); return task; @@ -44,10 +53,18 @@ public class Task { return context.getLogin(); } + /** + * Permet de récupérer la commande à executer + * @return Commande à exécuter + */ public ProtocolWriter.ProtocolResult getCommand() { return this.command; } + /** + * Permet de définir le StorBackEnd à utiliser pour cette tâche + * @param storeDomain Le StorBackEnd à utiliser + */ public void setDomain(String storeDomain) { this.storeDomain = storeDomain; if (storeDomain != null) { 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/client/Context.java b/app/src/main/java/lightcontainer/domains/client/Context.java index 05c36f2..fef239e 100644 --- a/app/src/main/java/lightcontainer/domains/client/Context.java +++ b/app/src/main/java/lightcontainer/domains/client/Context.java @@ -7,6 +7,10 @@ import lightcontainer.utils.AES_GCM; import java.security.NoSuchAlgorithmException; import java.util.LinkedList; +/** + * Contexte associé à la requête d'un utilisateur. + * Permet de récupérer les données à celui-ci et d'effectuer des actions sur le context courant. + */ public class Context { private AppData appData; @@ -16,12 +20,18 @@ public class Context { */ private String login; - + // Constructeur public Context(AppData appData) { this.appData = appData; } + /** + * Permet de créer un utilisateur. + * @param login Login de l'utilisateur + * @param password Mot de passe de l'utilisateur + * @return TRUE si l'utilisateur a pu être créé + */ public boolean createUser(String login, String password) { try { String key = AES_GCM.generateSecretKey(); 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/ProtocolReader.java b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java index d49f39c..ebace44 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java @@ -6,6 +6,9 @@ import java.io.InputStream; import java.util.regex.Matcher; import java.util.regex.Pattern; +/** + * Permet de construire des règles ayant pour but de parser une commande en un résultat utilisable par celle-ci + */ public abstract class ProtocolReader { private final String name; // Variables diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java index 87f6682..7cc3c15 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java @@ -6,6 +6,9 @@ import java.util.StringJoiner; import java.util.regex.Matcher; import java.util.regex.Pattern; +/** + * Représente une construction de tâche. + */ public abstract class ProtocolWriter { // Variables private final Pattern rulePattern; diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java index b0b132a..94b46f4 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/FilelistRule.java @@ -7,6 +7,9 @@ import lightcontainer.protocol.ProtocolWriter; import lightcontainer.protocol.rules.writer.FilesRule; import lightcontainer.protocol.rules.writer.SignOkRule; +/** + * Règle permettant de récupérer la liste des fichiers d'un utilisateur + */ public class FilelistRule extends ProtocolReader { // Constants private static final String PATTERN = "^FILELIST\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java index 885e95e..2440a97 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java @@ -7,6 +7,9 @@ import java.io.BufferedReader; import java.util.ArrayList; import java.util.List; +/** + * Règle permettant d'être alerter de l'annoncement d'un SBE + */ public class HelloRule extends ProtocolReader { private static final String PATTERN = "^HELLO ([A-Za-z0-9]{5,20}) ([0-9]{1,5})\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java index 0ba4f79..5f1bfa8 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java @@ -13,6 +13,10 @@ import java.io.IOException; import java.io.InputStream; import java.security.NoSuchAlgorithmException; +/** + * Règle permettant de sauvegarder un fichier sur le SBE. + * Celui-ci va chiffre le contenu du fichier à l'aide de AES. + */ public class SavefileRule extends ProtocolReader { // Constants private static final String PATTERN = "^SAVEFILE ([^ !]{1,20}) ([0-9]{1,10})\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java index 52a400c..3cd1e66 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java @@ -6,6 +6,9 @@ import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolWriter; import lightcontainer.protocol.rules.writer.SaveFileOkRule; +/** + * Règle permettant de de confirmer la sauvegrade d'un fichier. + */ public class SendOkRule extends ProtocolReader { diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SigninRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SigninRule.java index c6963f7..814138b 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SigninRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SigninRule.java @@ -10,6 +10,9 @@ import lightcontainer.protocol.rules.writer.SignOkRule; import java.io.InputStream; +/** + * Règle permettant de gérer la connection d'un utilisateur + */ public class SigninRule extends ProtocolReader { // Constants private static final String PATTERN = "^SIGNIN ([A-Za-z0-9]{5,20}) ([^ !]{5,50})\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SignoutRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SignoutRule.java new file mode 100644 index 0000000..ee6936a --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SignoutRule.java @@ -0,0 +1,31 @@ +package lightcontainer.protocol.rules.reader; + +import lightcontainer.domains.client.Context; +import lightcontainer.protocol.ProtocolReader; +import lightcontainer.protocol.ProtocolWriter; + +/** + * Règle demandant la déconnexion du client + */ +public class SignoutRule extends ProtocolReader { + + private static final String PATTERN = "^SIGNOUT\r\n$"; + + public static final String NAME = "SIGNOUT"; + + public SignoutRule() { + 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/protocol/rules/reader/SignupRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SignupRule.java index 391e3b8..f8efa4a 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SignupRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SignupRule.java @@ -6,6 +6,9 @@ import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.SignErrorRule; import lightcontainer.protocol.rules.writer.SignOkRule; +/** + * Règle permettant de gérer la création d'un utilisateur + */ public class SignupRule extends ProtocolReader { // Constants private static final String PATTERN = "^SIGNUP ([A-Za-z0-9]{5,20}) ([^ !]{5,50})\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/FilesRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/FilesRule.java index a611163..e0f50c4 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/FilesRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/FilesRule.java @@ -2,6 +2,9 @@ package lightcontainer.protocol.rules.writer; import lightcontainer.protocol.ProtocolWriter; +/** + * Règle permettant de construire une commande contenant la liste des fichiers d'un utilisateur + */ public class FilesRule extends ProtocolWriter { private static final String PATTERN = "^FILES( ([^ !]{1,20})!([0-9]{1,10})){0,50}\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java index 8dda3a7..4d1f1f3 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java @@ -2,6 +2,9 @@ package lightcontainer.protocol.rules.writer; import lightcontainer.protocol.ProtocolWriter; +/** + * Règle signifiant que la sauvegarde d'un fichier a échoué + */ public class SaveFileErrorRule extends ProtocolWriter { private static final String PATTERN = "^SAVEFILE_ERROR\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java index 1bd90d3..7734605 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java @@ -2,6 +2,9 @@ package lightcontainer.protocol.rules.writer; import lightcontainer.protocol.ProtocolWriter; +/** + * Règle signifiant que la sauvegarde d'un fichier fût un succès + */ public class SaveFileOkRule extends ProtocolWriter { private static final String PATTERN = "^SAVEFILE_OK\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java index c12ddb9..3d0bac0 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java @@ -6,6 +6,9 @@ import lightcontainer.utils.FileSender; import java.io.OutputStream; +/** + * Règle envoyée au SBE, demandant la sauvegarde d'un fichier. + */ public class SendfileRule extends ProtocolWriter { private static final String PATTERN = "^SENDFILE [A-Za-z0-9.]{0,200} [0-9]{1,10} [A-Za-z0-9.]{50,200}\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SignErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SignErrorRule.java index b78a66f..096bb9a 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SignErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SignErrorRule.java @@ -2,6 +2,9 @@ package lightcontainer.protocol.rules.writer; import lightcontainer.protocol.ProtocolWriter; +/** + * Règle renvoyée au client lorsque l'authentification a échoué. + */ public class SignErrorRule extends ProtocolWriter { private static final String PATTERN = "^SIGN_ERROR\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SignOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SignOkRule.java index 44b3f3c..6d6ef04 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SignOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SignOkRule.java @@ -2,6 +2,9 @@ package lightcontainer.protocol.rules.writer; import lightcontainer.protocol.ProtocolWriter; +/** + * Règle renvoyée au client lorsque l'authentification a réusie. + */ public class SignOkRule extends ProtocolWriter { private static final String PATTERN = "^SIGN_OK\r\n$"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SignoutRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SignoutRule.java deleted file mode 100644 index ff42d2c..0000000 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SignoutRule.java +++ /dev/null @@ -1,14 +0,0 @@ -package lightcontainer.protocol.rules.writer; - -import lightcontainer.protocol.ProtocolWriter; - -public class SignoutRule extends ProtocolWriter { - - private static final String PATTERN = "^SIGNOUT\r\n$"; - - public static final String NAME = "SIGNOUT"; - - public SignoutRule() { - super(NAME, PATTERN); - } -} 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) { diff --git a/app/src/test/java/lightcontainer/protocol/rules/writer/SignoutRuleTest.java b/app/src/test/java/lightcontainer/protocol/rules/writer/SignoutRuleTest.java index 31bf799..cb3af1e 100644 --- a/app/src/test/java/lightcontainer/protocol/rules/writer/SignoutRuleTest.java +++ b/app/src/test/java/lightcontainer/protocol/rules/writer/SignoutRuleTest.java @@ -1,6 +1,7 @@ package lightcontainer.protocol.rules.writer; import lightcontainer.protocol.ProtocolWriter; +import lightcontainer.protocol.rules.reader.SignoutRule; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -9,13 +10,6 @@ class SignoutRuleTest { @Test public void whenRuleIsRightThenReturnCommand() { - //GIVEN - ProtocolWriter protocolWriter = new SignoutRule(); - String[] datas = {}; - - //EXPECT - assertNotNull(protocolWriter.execute(datas)); - assertEquals("SIGNOUT\r\n", protocolWriter.execute(datas)); } } \ No newline at end of file