From 9fdc69d75fa4c50e438c186ee8e9a1f3ea6ecaaa Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 26 Feb 2022 16:55:50 +0100 Subject: [PATCH] =?UTF-8?q?Syst=C3=A8me=20permettant=20de=20retourner=20de?= =?UTF-8?q?s=20r=C3=A9sultats=20divers=20apr=C3=A8s=20l'ex=C3=A9cution=20d?= =?UTF-8?q?'un=20ProtocolReader=20sans=20cast=20(g=C3=A9n=C3=A9rique)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/lightcontainer/App.java | 8 +++- .../server/MulticastServerListener.java | 42 +++++++++++-------- .../interfaces/ProtocolRepository.java | 2 +- .../protocol/ProtocolReader.java | 13 +++--- .../protocol/rules/reader/HelloRule.java | 33 +++++++++++++-- .../repository/ProtocolRepositoryImpl.java | 13 +++--- .../protocol/rules/reader/HelloRuleTest.java | 8 +++- 7 files changed, 82 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index 47a4788..397587f 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -6,8 +6,11 @@ package lightcontainer; import lightcontainer.domains.server.MulticastServerListener; import lightcontainer.domains.server.UnicastServerListener; import lightcontainer.interfaces.MulticastSPR; +import lightcontainer.interfaces.ProtocolRepository; +import lightcontainer.protocol.rules.reader.HelloRule; import lightcontainer.repository.ClientHandlerRepository; import lightcontainer.repository.FileFrontEnd; +import lightcontainer.repository.ProtocolRepositoryImpl; import lightcontainer.repository.StoreProcessorRepository; public class App { @@ -22,9 +25,12 @@ public class App { // Create all repository ClientHandlerRepository clientRep = new ClientHandlerRepository(); StoreProcessorRepository storeRep = new StoreProcessorRepository(); + ProtocolRepository protocolRep = new ProtocolRepositoryImpl(); + + protocolRep.addReader(new HelloRule()); new UnicastServerListener(clientRep, UNICAST_PORT); - new MulticastServerListener(storeRep, MULTICAST_IP, MULTICAST_PORT); + new MulticastServerListener(storeRep, protocolRep, MULTICAST_IP, MULTICAST_PORT); FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep); diff --git a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java index e54371b..33dee9a 100644 --- a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java +++ b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java @@ -2,34 +2,38 @@ package lightcontainer.domains.server; import lightcontainer.domains.client.StoreProcessor; import lightcontainer.interfaces.MulticastSPR; +import lightcontainer.interfaces.ProtocolRepository; +import lightcontainer.protocol.ProtocolReader; +import lightcontainer.protocol.rules.reader.HelloRule; import java.io.IOException; import java.net.*; /** * StoreMulticastRunnable - * + *

* Class listening to the announcement of new StoreBackEnd. * Allowing it to be used as a storage unit. * - * @version 1.0 - * @since 1.0 - * - * @see Runnable - * @author Jérémi NIHART + * @author Jérémi NIHART + * @version 1.0 + * @see Runnable + * @since 1.0 */ public class MulticastServerListener implements Runnable { // Variable private final String multicast_address; private final int multicast_port; private final MulticastSPR repository; + private final ProtocolRepository protocolRep; private final byte[] buffer = new byte[256]; private MulticastSocket listener; // Constructor - public MulticastServerListener(MulticastSPR repository, String multicast_address, int multicast_port) { + public MulticastServerListener(MulticastSPR repository, ProtocolRepository protocolRep, String multicast_address, int multicast_port) { this.repository = repository; + this.protocolRep = protocolRep; this.multicast_address = multicast_address; this.multicast_port = multicast_port; repository.setServerListener(this); @@ -38,10 +42,9 @@ public class MulticastServerListener implements Runnable { /** * Start Multicast listening on indicated port and IP group. * - * @since 1.0 - * - * @see MulticastSocket#receive(DatagramPacket) - * @see DatagramPacket + * @see MulticastSocket#receive(DatagramPacket) + * @see DatagramPacket + * @since 1.0 */ @Override public void run() { @@ -54,29 +57,32 @@ public class MulticastServerListener implements Runnable { DatagramPacket packet = new DatagramPacket(this.buffer, this.buffer.length); // Add the listener to the multicast group this.listener.joinGroup(listener_group); - while(true) { + while (true) { // Read the packet received and build a string of characters this.listener.receive(packet); String data = new String(packet.getData(), 0, packet.getLength()); // Create a new StoreBacked (try used in the case of an error to maintain the listening loop) try { // TODO Récupérer le port du message du packet et le setup (add description of the line). - Socket socket = new Socket(packet.getAddress(), 2500); + HelloRule.Result readerResult = protocolRep.executeReader(data); + + Socket socket = new Socket(packet.getAddress(), readerResult.getPort()); // Create the store processor StoreProcessor storeProcessor = new StoreProcessor(socket, null); // TODO : Voir comment on procède get via repo ou ici ?! // Add the store processor to its repository this.repository.addStore(storeProcessor); - } catch (IOException ignore) { } + } catch (IOException ignore) { + } } - } catch (Exception ignore) { } + } catch (Exception ignore) { + } } /** * Closes the MulticastSocket connection and aborts the listening and infinite listening loop. * - * @since 1.0 - * - * @see MulticastServerListener#run() + * @see MulticastServerListener#run() + * @since 1.0 */ public void stop() { this.listener.close(); diff --git a/app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java b/app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java index 2ee0743..c772ba4 100644 --- a/app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java +++ b/app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java @@ -5,7 +5,7 @@ import lightcontainer.protocol.ProtocolWriter; public interface ProtocolRepository { - boolean executeReader(String data); + T executeReader(String data); String executeWriter(String... datas); diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java index 1d45c83..a61d3e7 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java @@ -11,11 +11,14 @@ public abstract class ProtocolReader { this.rulePattern = Pattern.compile(pattern); } + + public abstract class ProtocolResult {} + /** * 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) { + public T execute(String data) { Matcher ruleMatcher = this.rulePattern.matcher(data); if (ruleMatcher.matches()) { @@ -24,17 +27,17 @@ public abstract class ProtocolReader { for (int i = 1; i <= groups.length; ++i) groups[i - 1] = ruleMatcher.group(i); - onExecuted(groups); - return true; + + return onExecuted(groups); } - return false; + return null; } /** * Cette méthode est appelée lors de l'exécution de la règle * @param data */ - protected abstract void onExecuted(String... data); + protected abstract T onExecuted(String... data); } 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 a39019b..ba9567b 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java @@ -2,6 +2,9 @@ package lightcontainer.protocol.rules.reader; import lightcontainer.protocol.ProtocolReader; +import java.util.ArrayList; +import java.util.List; + public class HelloRule extends ProtocolReader { private static final String PATTERN = "^HELLO ([A-Za-z0-9]{5,20}) ([0-9]{1,5})\r\n$"; @@ -17,11 +20,33 @@ public class HelloRule extends ProtocolReader { } - @Override - protected void onExecuted(String... data) { - String domain = data[DOMAIN], port = data[PORT]; + public class Result extends ProtocolResult { - System.out.printf("Règle Hello avec domain=%s et port=%s\n", domain, port); + private final String domain; + private final int port; + + public Result(String domain, int port) { + this.domain = domain; + this.port = port; + } + + public String getDomain() { + return domain; + } + + public int getPort() { + return port; + } + } + + + @Override + protected HelloRule.Result onExecuted(String... data) { + String domain = data[DOMAIN]; + int port = Integer.parseInt(data[PORT]); + + System.out.printf("Regle Hello avec domain=%s et port=%s\n", domain, port); + return new HelloRule.Result(domain, port); } } diff --git a/app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java b/app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java index 52071f6..7735ef0 100644 --- a/app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java +++ b/app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java @@ -8,17 +8,18 @@ import java.util.HashSet; import java.util.Set; public class ProtocolRepositoryImpl implements ProtocolRepository { - private Set readers = new HashSet<>(); - private Set writers = new HashSet<>(); + private final Set readers = new HashSet<>(); + private final Set writers = new HashSet<>(); @Override - public boolean executeReader(String data) { + public T executeReader(String data) { for (ProtocolReader reader : readers) { - if (reader.execute(data)) { - return true; + T readerResult = reader.execute(data); + if (readerResult != null) { + return readerResult; } } - return false; + return null; } @Override diff --git a/app/src/test/java/lightcontainer/protocol/rules/reader/HelloRuleTest.java b/app/src/test/java/lightcontainer/protocol/rules/reader/HelloRuleTest.java index c2a1b04..7b82f38 100644 --- a/app/src/test/java/lightcontainer/protocol/rules/reader/HelloRuleTest.java +++ b/app/src/test/java/lightcontainer/protocol/rules/reader/HelloRuleTest.java @@ -14,8 +14,12 @@ class HelloRuleTest { ProtocolReader protocolReader = new HelloRule(); String request = "HELLO bento 42890\r\n"; - // EXPECT - assertTrue(protocolReader.execute(request)); + // WHEN + HelloRule.Result ruleResult = protocolReader.execute(request); + + // THEN + assertEquals("bento", ruleResult.getDomain()); + assertEquals(42890, ruleResult.getPort()); } } \ No newline at end of file