diff --git a/app/src/main/java/lightcontainer/domains/StoreProcessor.java b/app/src/main/java/lightcontainer/domains/StoreProcessor.java index fb8ea2c..fc4f721 100644 --- a/app/src/main/java/lightcontainer/domains/StoreProcessor.java +++ b/app/src/main/java/lightcontainer/domains/StoreProcessor.java @@ -67,12 +67,18 @@ public class StoreProcessor implements Runnable, AutoCloseable { public void run() { while (true) { try { + // Envoie donnée au StoreBackEnd String command = this.reader.readLine(); // TODO gestion de la réception de commandes, fichier, ... if (command != null) System.out.println("StoreBackEnd: " + command); + + // Réception donnée du StoreBackEnd } catch (IOException e) { e.printStackTrace(); } + + // Alerter le FileFrontEnd que ce store processor est disponible pour gérer de nouvelle demande + fileFrontEnd.onStoreAvailable(this); } } diff --git a/app/src/main/java/lightcontainer/domains/Task.java b/app/src/main/java/lightcontainer/domains/Task.java index 6cd8fc0..45494c8 100644 --- a/app/src/main/java/lightcontainer/domains/Task.java +++ b/app/src/main/java/lightcontainer/domains/Task.java @@ -9,7 +9,7 @@ public class Task { private TaskStatus status; private String command; private String client; - private String store; + private String storeDomain; public Task() { diff --git a/app/src/main/java/lightcontainer/interfaces/StoreProcessorFFE.java b/app/src/main/java/lightcontainer/interfaces/StoreProcessorFFE.java index e6fde95..7a5ffb6 100644 --- a/app/src/main/java/lightcontainer/interfaces/StoreProcessorFFE.java +++ b/app/src/main/java/lightcontainer/interfaces/StoreProcessorFFE.java @@ -1,4 +1,13 @@ package lightcontainer.interfaces; +import lightcontainer.domains.StoreProcessor; + public interface StoreProcessorFFE { + + /** + * Permet à un {@link StoreProcessor} d'avertir le FFE qu'il est disponible + * @param store + */ + void onStoreAvailable(StoreProcessor store); + } diff --git a/app/src/main/java/lightcontainer/protocol/HelloRule.java b/app/src/main/java/lightcontainer/protocol/HelloRule.java deleted file mode 100644 index c9c2b94..0000000 --- a/app/src/main/java/lightcontainer/protocol/HelloRule.java +++ /dev/null @@ -1,31 +0,0 @@ -package lightcontainer.protocol; - -public class HelloRule extends Protocol { - // Variables - - // Constructor - protected HelloRule() { - super("HELLO", "HELLO "); // TODO : add the regex here (sbe_hello = "HELLO bl domain bl port line") - } - - /** - * Execute the rule on a command. - * - * This function allows you to check a command and process those groups (parameters) - * use the utility functions of {@link Protocol} to facilitate processing, see @see. - * - * @param cmd Command on which to execute the rule. - * @see Protocol#execute(String) - * @see #matcherCheck(String) - * @see #matcherGetGroups() - * @since 1.0 - */ - @Override - public void execute(String cmd) { - if (matcherCheck(cmd)) { - System.out.println("Good rule ;-) !"); - } else { - System.out.println("OUPPS unknown rule !"); - } - } -} diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java new file mode 100644 index 0000000..1d45c83 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java @@ -0,0 +1,40 @@ +package lightcontainer.protocol; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public abstract class ProtocolReader { + + private final Pattern rulePattern; + + protected ProtocolReader(String pattern) { + this.rulePattern = Pattern.compile(pattern); + } + + /** + * Permet de lancer la décomposition d'une commande pour en extraire les données + * @param data Contenu de la commande + */ + public boolean execute(String data) { + Matcher ruleMatcher = this.rulePattern.matcher(data); + + if (ruleMatcher.matches()) { + String[] groups = new String[ruleMatcher.groupCount()]; + + for (int i = 1; i <= groups.length; ++i) + groups[i - 1] = ruleMatcher.group(i); + + onExecuted(groups); + return true; + } + + return false; + } + + /** + * Cette méthode est appelée lors de l'exécution de la règle + * @param data + */ + protected abstract void onExecuted(String... data); + +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/HelloRule.java b/app/src/main/java/lightcontainer/protocol/rules/HelloRule.java new file mode 100644 index 0000000..30acdb9 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/HelloRule.java @@ -0,0 +1,27 @@ +package lightcontainer.protocol.rules; + +import lightcontainer.protocol.ProtocolReader; + +public class HelloRule extends ProtocolReader { + + private static final String PATTERN = "^HELLO ([A-Za-z0-9]{5,20}) ([0-9]{1,5})\r\n$"; + + // Index du domain dans le tableau de donnée + private static final int DOMAIN = 0; + + //Index du port dans le tableau de donnée + private static final int PORT = 1; + + public HelloRule() { + super(PATTERN); + } + + + @Override + protected void onExecuted(String... data) { + String domain = data[DOMAIN], port = data[PORT]; + + System.out.printf("Règle Hello avec domain=%s et port=%s\n", domain, port); + } + +} diff --git a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java index b4db479..d7dfde2 100644 --- a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java +++ b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java @@ -1,20 +1,43 @@ package lightcontainer.repository; +import lightcontainer.domains.ClientHandler; +import lightcontainer.domains.StoreProcessor; import lightcontainer.domains.Task; import lightcontainer.interfaces.ClientHandlerFFE; import lightcontainer.interfaces.StoreProcessorFFE; +import java.util.Collections; import java.util.Deque; import java.util.LinkedList; +import java.util.concurrent.ConcurrentLinkedDeque; public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { // Variables - private Deque tasks = new LinkedList<>(); + private Deque tasks = new ConcurrentLinkedDeque<>(); private ClientHandlerRepository clientRepository; // TODO -> pourquoi pas une interface ? end private StoreProcessorRepository storeRepository; // TODO -> pourquoi pas une interface ? end // Constructor - public FileFrontEnd() { + public FileFrontEnd(ClientHandlerRepository clientRepo, StoreProcessorRepository storeRepo) { + this.clientRepository = clientRepo; + this.storeRepository = storeRepo; + } + + /** + * Appelé quand nouvelle tâche + */ + public void alertStoreProcessors(Task task) { + // On avertit les stor processors d'une nouvelle tâche + + } + + /** + * Permet à un {@link StoreProcessor} d'avertir le FFE qu'il est disponible + * + * @param store + */ + @Override + public void onStoreAvailable(StoreProcessor store) { } } diff --git a/app/src/test/java/lightcontainer/protocol/rules/HelloRuleTest.java b/app/src/test/java/lightcontainer/protocol/rules/HelloRuleTest.java new file mode 100644 index 0000000..8305c90 --- /dev/null +++ b/app/src/test/java/lightcontainer/protocol/rules/HelloRuleTest.java @@ -0,0 +1,20 @@ +package lightcontainer.protocol.rules; + +import lightcontainer.protocol.ProtocolReader; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class HelloRuleTest { + + @Test + public void whenRuleIsRightThenIsExecute() { + // GIVEN + ProtocolReader protocolReader = new HelloRule(); + String request = "HELLO bento 42890\r\n"; + + // EXPECT + assertTrue(protocolReader.execute(request)); + } + +} \ No newline at end of file