From 59ce081d8cd3850fc18a7b3b804ed606ea78919e Mon Sep 17 00:00:00 2001 From: Benjamin Date: Mon, 28 Feb 2022 16:53:11 +0100 Subject: [PATCH] =?UTF-8?q?Lise=20en=20place=20du=20syst=C3=A8me=20permett?= =?UTF-8?q?ant=20=C3=A0=20un=20reader=20de=20choisir=20entre=20renvoyer=20?= =?UTF-8?q?la=20commande=20au=20client=20OU=20la=20mettre=20dans=20la=20fi?= =?UTF-8?q?le=20d'attente=20pour=20les=20storbackends?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/lightcontainer/App.java | 6 +++--- .../java/lightcontainer/domains/Task.java | 11 +++++++++-- .../domains/client/ClientHandler.java | 14 +++++++++++++- .../domains/server/UnicastServerListener.java | 7 +++++-- .../interfaces/ClientHandlerFFE.java | 8 ++++++++ .../protocol/ProtocolReader.java | 19 ++++++++++++++++++- .../repository/FileFrontEnd.java | 12 +++++++++++- 7 files changed, 67 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index 3aae4e2..ec37202 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -5,7 +5,6 @@ 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; @@ -28,10 +27,11 @@ public class App { ProtocolRepository protocolRep = new ProtocolRepositoryImpl(); protocolRep.addReader(new HelloRule()); + protocolRep.addReader(new SigninRule()); - new UnicastServerListener(clientRep, UNICAST_PORT); + new UnicastServerListener(clientRep, protocolRep, UNICAST_PORT); new MulticastServerListener(storeRep, protocolRep, MULTICAST_IP, MULTICAST_PORT); - FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep); + FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep, protocolRep); // close repo et client et server. diff --git a/app/src/main/java/lightcontainer/domains/Task.java b/app/src/main/java/lightcontainer/domains/Task.java index 45494c8..9ff6369 100644 --- a/app/src/main/java/lightcontainer/domains/Task.java +++ b/app/src/main/java/lightcontainer/domains/Task.java @@ -5,13 +5,20 @@ import lightcontainer.enumerations.TaskType; public class Task { // Variables - private TaskType type; + private TaskType type; // TODO : Supprimer car innutile ? private TaskStatus status; private String command; private String client; private String storeDomain; - public Task() { + public Task(TaskStatus pending, String command, String client) { + status = pending; + this.command = command; + this.client = client; + } + public static Task newInstance(String command, String client) { + Task task = new Task(TaskStatus.PENDING, command, client); + return task; } } diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index 53f7ba0..256d068 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -1,6 +1,8 @@ package lightcontainer.domains.client; import lightcontainer.interfaces.ClientHandlerFFE; +import lightcontainer.interfaces.ProtocolRepository; +import lightcontainer.protocol.ProtocolReader; import java.io.*; import java.net.Socket; @@ -25,15 +27,17 @@ public class ClientHandler implements Runnable, AutoCloseable { // Variables private ClientHandlerFFE fileFrontEnd; private final Socket client; + private ProtocolRepository protocolRep; private boolean client_run; private BufferedReader reader; private PrintWriter writer; // Constructor - public ClientHandler(Socket client, ClientHandlerFFE ffe) { + public ClientHandler(Socket client, ClientHandlerFFE ffe, ProtocolRepository protocolRep) { this.fileFrontEnd = ffe; this.client = client; + this.protocolRep = protocolRep; this.client_run = false; initClient(); } @@ -75,6 +79,14 @@ public class ClientHandler implements Runnable, AutoCloseable { String command = this.reader.readLine(); // TODO gestion de la réception de commandes, fichier, ... if (command != null) System.out.println("Client: " + command); + + 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 + } else { + writer.write(ruleResult.getResultCommand()); // Renvoye au client + } } catch (IOException ignore) { } } } diff --git a/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java b/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java index c5ca8cd..c604dee 100644 --- a/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java +++ b/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java @@ -1,6 +1,7 @@ package lightcontainer.domains.server; import lightcontainer.domains.client.ClientHandler; +import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.interfaces.UnicastCHR; import java.io.IOException; @@ -11,12 +12,14 @@ public class UnicastServerListener { // Variables private ServerSocket server; private final UnicastCHR repository; + private ProtocolRepository protocolRep; private final int server_port; private boolean server_run; // Constructor - public UnicastServerListener(UnicastCHR repository, int port) { + public UnicastServerListener(UnicastCHR repository, ProtocolRepository protocolRep, int port) { this.repository = repository; + this.protocolRep = protocolRep; this.server_port = port; this.server_run = false; repository.setServerListener(this); @@ -39,7 +42,7 @@ public class UnicastServerListener { // Accepting connection requests (blocking) Socket client = this.server.accept(); // Create a new Handler client by passing these dependencies to it - ClientHandler clientHandler = new ClientHandler(client, null); // TODO passer FileFrontEnd ou faire ca dans le repository ?! + ClientHandler clientHandler = new ClientHandler(client, null, protocolRep); // TODO passer FileFrontEnd ou faire ca dans le repository ?! // Add the client handler to its repository (clienthandlerrepository) // this.repository.add(clientHandler); TODO REPOSITORY // Start the thread diff --git a/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java b/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java index a3a303f..99536f1 100644 --- a/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java +++ b/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java @@ -8,4 +8,12 @@ import lightcontainer.repository.FileFrontEnd; */ public interface ClientHandlerFFE { // functions + + /** + * Demande le traitement d'une commande + * @param command Commande à traiter + * @param client identifiant du client à qui est affilié cette commande + */ + void newCommand(String command, String client); + } diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java index 295212c..b1102bf 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java @@ -12,19 +12,36 @@ public abstract class ProtocolReader { this.rulePattern = Pattern.compile(pattern); } + public enum ResultCmdReceiver { + CLIENT, + STOREBACKEND + } + public abstract class ProtocolResult { /** * Command qui sera renvoyée par exemple au client */ private String resultCommand; + /** + * Désigne vers ou cette commande est envoyée. + * ResultCmdReceiver.CLIENT : Signifie que cette commande va être directement revoyée au client. + * ResultCmdReceiver.STOREBACKEND : Signifie que cette commande va être envoyée dans la file de tâche du server et êre en attente d'envoie à un StorBackEnd + */ + private ResultCmdReceiver receiver; + + + public ResultCmdReceiver getReceiver() { + return receiver; + } public String getResultCommand() { return resultCommand; } - public void setResultCommand(String resultCommand) { + public void setResultCommand(String resultCommand, ResultCmdReceiver receiver) { this.resultCommand = resultCommand; + this.receiver = receiver; } } diff --git a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java index b7028c5..ce47bea 100644 --- a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java +++ b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java @@ -3,6 +3,7 @@ package lightcontainer.repository; import lightcontainer.domains.client.StoreProcessor; import lightcontainer.domains.Task; import lightcontainer.interfaces.ClientHandlerFFE; +import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.interfaces.StoreProcessorFFE; import java.util.Deque; @@ -13,11 +14,13 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { private Deque tasks = new ConcurrentLinkedDeque<>(); private ClientHandlerRepository clientRepository; // TODO -> pourquoi pas une interface ? end private StoreProcessorRepository storeRepository; // TODO -> pourquoi pas une interface ? end + private ProtocolRepository protocolRepository; // Constructor - public FileFrontEnd(ClientHandlerRepository clientRepo, StoreProcessorRepository storeRepo) { + public FileFrontEnd(ClientHandlerRepository clientRepo, StoreProcessorRepository storeRepo, ProtocolRepository protocolRepository) { this.clientRepository = clientRepo; this.storeRepository = storeRepo; + this.protocolRepository = protocolRepository; } /** @@ -36,4 +39,11 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { public void onStoreAvailable(StoreProcessor store) { } + + @Override + public void newCommand(String command, String client) { + // TODO : Ajouter la tâche - Alerter les StorBackEnds + Task task = Task.newInstance(command, client); + tasks.add(task); + } }