From c1fe6a0a966422604f09259c808ee091f375f694 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sat, 26 Feb 2022 13:37:38 +0100 Subject: [PATCH] =?UTF-8?q?Modification=20du=20syst=C3=A8me=20de=20Protoco?= =?UTF-8?q?lReader.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../protocol/ProtocolReader.java | 36 +++++++++++++------ .../protocol/rules/HelloRule.java | 5 ++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java index 307847d..472dc0a 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java @@ -1,24 +1,40 @@ package lightcontainer.protocol; -public interface ProtocolReader { +import java.util.regex.Matcher; +import java.util.regex.Pattern; +public abstract class ProtocolReader { - /** - * Permet de récupérer le nom de la commande (ex. FILELIST, SENDRESULT) - */ - String getCmd(); + 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 */ - void execute(String data); + public boolean execute(String data) { + Matcher ruleMatcher = this.rulePattern.matcher(data); + + if (ruleMatcher.matches()) { + String[] groups = new String[ruleMatcher.groupCount()]; + + for (int i= 0; i < groups.length; ++i) + groups[i] = ruleMatcher.group(i); + + onExecuted(groups); + return true; + } + + return false; + } /** - * Permet de demander au protocol s'il capable de décomposer/gérer cette commande - * @param data Commande ("GETFILE" bl filename line) - * @return TRUE si ce protocol peut gérer + * Cette méthode est appelée lors de l'exécution de la règle + * @param data */ - boolean match(String 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 index 6edb27d..529f8f7 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/HelloRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/HelloRule.java @@ -11,8 +11,11 @@ public class HelloRule extends ProtocolReader { @Override - protected void onExecuted(String[] data) { + protected void onExecuted(String... data) { + String domain = data[0]; + String port = data[1]; + System.out.printf("Règle Hello avec domain=%s et port=%s", domain, port); } }