diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index ec37202..c3ee754 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -5,8 +5,13 @@ package lightcontainer; import lightcontainer.domains.server.MulticastServerListener; import lightcontainer.domains.server.UnicastServerListener; +import lightcontainer.interfaces.MulticastSPR; import lightcontainer.interfaces.ProtocolRepository; +import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.reader.HelloRule; +import lightcontainer.protocol.rules.reader.SigninRule; +import lightcontainer.protocol.rules.writer.SignError; +import lightcontainer.protocol.rules.writer.SignOk; import lightcontainer.repository.ClientHandlerRepository; import lightcontainer.repository.FileFrontEnd; import lightcontainer.repository.ProtocolRepositoryImpl; @@ -29,6 +34,10 @@ public class App { protocolRep.addReader(new HelloRule()); protocolRep.addReader(new SigninRule()); + protocolRep.addWriter(new SignOk()); + protocolRep.addWriter(new SignError()); + + new UnicastServerListener(clientRep, protocolRep, UNICAST_PORT); new MulticastServerListener(storeRep, protocolRep, MULTICAST_IP, MULTICAST_PORT); FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep, protocolRep); diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index 256d068..7d0b6ab 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -3,11 +3,13 @@ package lightcontainer.domains.client; import lightcontainer.interfaces.ClientHandlerFFE; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; +import lightcontainer.protocol.rules.reader.SigninRule; +import lightcontainer.protocol.rules.writer.SignError; +import lightcontainer.protocol.rules.writer.SignOk; import java.io.*; import java.net.Socket; import java.nio.charset.StandardCharsets; -import java.util.Objects; /** * ClientHandler @@ -30,6 +32,12 @@ public class ClientHandler implements Runnable, AutoCloseable { private ProtocolRepository protocolRep; private boolean client_run; + /** + * Login of client. + * State : NULL if not connected and Login string if connected + */ + private String login; + private BufferedReader reader; private PrintWriter writer; @@ -82,15 +90,47 @@ public class ClientHandler implements Runnable, AutoCloseable { ProtocolReader.ProtocolResult ruleResult = protocolRep.executeReader(command + "\r\n"); - if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND) { - fileFrontEnd.newCommand(command, ruleResult.getResultCommand()); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd + + if (isConnected()) { + + if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND) { + fileFrontEnd.newCommand(command, ruleResult.getResultCommand()); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd + } else { + writer.write(ruleResult.getResultCommand()); // Renvoye au client + } + } else { - writer.write(ruleResult.getResultCommand()); // Renvoye au client + authentificate(ruleResult); } + } catch (IOException ignore) { } } } + private void authentificate(ProtocolReader.ProtocolResult ruleResult) { + try { + SigninRule.Result signinResult = (SigninRule.Result) ruleResult; + if (signinResult.checkCredentials()) { + this.login = signinResult.getLogin(); + writer.write(protocolRep.executeWriter(SignOk.NAME)); + writer.flush(); + return; + } + } catch (ClassCastException castException) {} + + writer.write(protocolRep.executeWriter(SignError.NAME)); // Envoie SignError car echec de la connection + writer.flush(); + this.close(); // Fermeture de la connection + } + + /** + * Permet de savoir si l'utilisateur s'est connecté (Avec login et mdp) + * @return + */ + private boolean isConnected() { + return login != null; + } + /** * AutoClosable Function * Close the Client thread and resources. diff --git a/app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java b/app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java index c772ba4..af435d1 100644 --- a/app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java +++ b/app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java @@ -7,7 +7,7 @@ public interface ProtocolRepository { T executeReader(String data); - String executeWriter(String... datas); + String executeWriter(String cmdName, String... datas); void addReader(ProtocolReader reader); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SigninRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SigninRule.java new file mode 100644 index 0000000..bbfc660 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SigninRule.java @@ -0,0 +1,59 @@ +package lightcontainer.protocol.rules.reader; + +import lightcontainer.protocol.ProtocolReader; + +public class SigninRule extends ProtocolReader { + + private static final String PATTERN = "^SIGNIN ([A-Za-z0-9]{2,20}) ([^ !]{5,50})\r\n$"; + + // Index du domain dans le tableau de donnée + private static final int LOGIN = 0; + + //Index du port dans le tableau de donnée + private static final int PASSWORD = 1; + + public SigninRule() { + super(PATTERN); + } + + + public class Result extends ProtocolResult { + + private final String login; + + private final String password; + + public Result(String login, String password) { + + this.login = login; + this.password = password; + } + + public String getLogin() { + return login; + } + + public String getPassword() { + return password; + } + + public boolean checkCredentials() { + return getLogin().equals("aa") && getPassword().equals("aaaaa"); + } + } + + + @Override + protected SigninRule.Result onExecuted(String... data) { + SigninRule.Result result = new SigninRule.Result(data[LOGIN], data[PASSWORD]); + + // TODO : Création d'une règle d'écriture SIGN_OK et SIGN_ERROR proprement + if (result.checkCredentials()) { + result.setResultCommand("SIGN_OK\r\n", ResultCmdReceiver.CLIENT); + } else { + result.setResultCommand("SIGN_ERROR\r\n", ResultCmdReceiver.CLIENT); + } + + return result; + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SignError.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SignError.java new file mode 100644 index 0000000..0daa349 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SignError.java @@ -0,0 +1,14 @@ +package lightcontainer.protocol.rules.writer; + +import lightcontainer.protocol.ProtocolWriter; + +public class SignError extends ProtocolWriter { + + private static final String PATTERN = "^SIGN_ERROR\r\n$"; + + public static final String NAME = "SIGN_ERROR"; + + public SignError() { + super(NAME, PATTERN); + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SignOk.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SignOk.java new file mode 100644 index 0000000..848561c --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SignOk.java @@ -0,0 +1,14 @@ +package lightcontainer.protocol.rules.writer; + +import lightcontainer.protocol.ProtocolWriter; + +public class SignOk extends ProtocolWriter { + + private static final String PATTERN = "^SIGN_OK\r\n$"; + + public static final String NAME = "SIGN_OK"; + + public SignOk() { + super(NAME, PATTERN); + } +} diff --git a/app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java b/app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java index dad49c8..4bb1c84 100644 --- a/app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java +++ b/app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java @@ -23,10 +23,10 @@ public class ProtocolRepositoryImpl implements ProtocolRepository { } @Override - public String executeWriter(String... data) { + public String executeWriter(String cmdName, String... data) { for (ProtocolWriter writer : writers) { - String command = writer.execute(data); - if (command != null) { + String command; + if (cmdName.equals(writer.getCmdName()) && (command = writer.execute(data)) != null) { return command; } }