Lise en place du système permettant à un reader de choisir entre renvoyer la commande au client OU la mettre dans la file d'attente pour les storbackends

This commit is contained in:
Benjamin Lejeune 2022-02-28 16:53:11 +01:00
parent 6574918650
commit 59ce081d8c
7 changed files with 67 additions and 10 deletions

View File

@ -5,7 +5,6 @@ package lightcontainer;
import lightcontainer.domains.server.MulticastServerListener; import lightcontainer.domains.server.MulticastServerListener;
import lightcontainer.domains.server.UnicastServerListener; import lightcontainer.domains.server.UnicastServerListener;
import lightcontainer.interfaces.MulticastSPR;
import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.rules.reader.HelloRule; import lightcontainer.protocol.rules.reader.HelloRule;
import lightcontainer.repository.ClientHandlerRepository; import lightcontainer.repository.ClientHandlerRepository;
@ -28,10 +27,11 @@ public class App {
ProtocolRepository protocolRep = new ProtocolRepositoryImpl(); ProtocolRepository protocolRep = new ProtocolRepositoryImpl();
protocolRep.addReader(new HelloRule()); 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); 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. // close repo et client et server.

View File

@ -5,13 +5,20 @@ import lightcontainer.enumerations.TaskType;
public class Task { public class Task {
// Variables // Variables
private TaskType type; private TaskType type; // TODO : Supprimer car innutile ?
private TaskStatus status; private TaskStatus status;
private String command; private String command;
private String client; private String client;
private String storeDomain; 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;
} }
} }

View File

@ -1,6 +1,8 @@
package lightcontainer.domains.client; package lightcontainer.domains.client;
import lightcontainer.interfaces.ClientHandlerFFE; import lightcontainer.interfaces.ClientHandlerFFE;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
@ -25,15 +27,17 @@ public class ClientHandler implements Runnable, AutoCloseable {
// Variables // Variables
private ClientHandlerFFE fileFrontEnd; private ClientHandlerFFE fileFrontEnd;
private final Socket client; private final Socket client;
private ProtocolRepository protocolRep;
private boolean client_run; private boolean client_run;
private BufferedReader reader; private BufferedReader reader;
private PrintWriter writer; private PrintWriter writer;
// Constructor // Constructor
public ClientHandler(Socket client, ClientHandlerFFE ffe) { public ClientHandler(Socket client, ClientHandlerFFE ffe, ProtocolRepository protocolRep) {
this.fileFrontEnd = ffe; this.fileFrontEnd = ffe;
this.client = client; this.client = client;
this.protocolRep = protocolRep;
this.client_run = false; this.client_run = false;
initClient(); initClient();
} }
@ -75,6 +79,14 @@ public class ClientHandler implements Runnable, AutoCloseable {
String command = this.reader.readLine(); String command = this.reader.readLine();
// TODO gestion de la réception de commandes, fichier, ... // TODO gestion de la réception de commandes, fichier, ...
if (command != null) System.out.println("Client: " + command); 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) { } } catch (IOException ignore) { }
} }
} }

View File

@ -1,6 +1,7 @@
package lightcontainer.domains.server; package lightcontainer.domains.server;
import lightcontainer.domains.client.ClientHandler; import lightcontainer.domains.client.ClientHandler;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.interfaces.UnicastCHR; import lightcontainer.interfaces.UnicastCHR;
import java.io.IOException; import java.io.IOException;
@ -11,12 +12,14 @@ public class UnicastServerListener {
// Variables // Variables
private ServerSocket server; private ServerSocket server;
private final UnicastCHR repository; private final UnicastCHR repository;
private ProtocolRepository protocolRep;
private final int server_port; private final int server_port;
private boolean server_run; private boolean server_run;
// Constructor // Constructor
public UnicastServerListener(UnicastCHR repository, int port) { public UnicastServerListener(UnicastCHR repository, ProtocolRepository protocolRep, int port) {
this.repository = repository; this.repository = repository;
this.protocolRep = protocolRep;
this.server_port = port; this.server_port = port;
this.server_run = false; this.server_run = false;
repository.setServerListener(this); repository.setServerListener(this);
@ -39,7 +42,7 @@ public class UnicastServerListener {
// Accepting connection requests (blocking) // Accepting connection requests (blocking)
Socket client = this.server.accept(); Socket client = this.server.accept();
// Create a new Handler client by passing these dependencies to it // 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) // Add the client handler to its repository (clienthandlerrepository)
// this.repository.add(clientHandler); TODO REPOSITORY // this.repository.add(clientHandler); TODO REPOSITORY
// Start the thread // Start the thread

View File

@ -8,4 +8,12 @@ import lightcontainer.repository.FileFrontEnd;
*/ */
public interface ClientHandlerFFE { public interface ClientHandlerFFE {
// functions // 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);
} }

View File

@ -12,19 +12,36 @@ public abstract class ProtocolReader {
this.rulePattern = Pattern.compile(pattern); this.rulePattern = Pattern.compile(pattern);
} }
public enum ResultCmdReceiver {
CLIENT,
STOREBACKEND
}
public abstract class ProtocolResult { public abstract class ProtocolResult {
/** /**
* Command qui sera renvoyée par exemple au client * Command qui sera renvoyée par exemple au client
*/ */
private String resultCommand; 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() { public String getResultCommand() {
return resultCommand; return resultCommand;
} }
public void setResultCommand(String resultCommand) { public void setResultCommand(String resultCommand, ResultCmdReceiver receiver) {
this.resultCommand = resultCommand; this.resultCommand = resultCommand;
this.receiver = receiver;
} }
} }

View File

@ -3,6 +3,7 @@ package lightcontainer.repository;
import lightcontainer.domains.client.StoreProcessor; import lightcontainer.domains.client.StoreProcessor;
import lightcontainer.domains.Task; import lightcontainer.domains.Task;
import lightcontainer.interfaces.ClientHandlerFFE; import lightcontainer.interfaces.ClientHandlerFFE;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.interfaces.StoreProcessorFFE; import lightcontainer.interfaces.StoreProcessorFFE;
import java.util.Deque; import java.util.Deque;
@ -13,11 +14,13 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE {
private Deque<Task> tasks = new ConcurrentLinkedDeque<>(); private Deque<Task> tasks = new ConcurrentLinkedDeque<>();
private ClientHandlerRepository clientRepository; // TODO -> pourquoi pas une interface ? end private ClientHandlerRepository clientRepository; // TODO -> pourquoi pas une interface ? end
private StoreProcessorRepository storeRepository; // TODO -> pourquoi pas une interface ? end private StoreProcessorRepository storeRepository; // TODO -> pourquoi pas une interface ? end
private ProtocolRepository protocolRepository;
// Constructor // Constructor
public FileFrontEnd(ClientHandlerRepository clientRepo, StoreProcessorRepository storeRepo) { public FileFrontEnd(ClientHandlerRepository clientRepo, StoreProcessorRepository storeRepo, ProtocolRepository protocolRepository) {
this.clientRepository = clientRepo; this.clientRepository = clientRepo;
this.storeRepository = storeRepo; this.storeRepository = storeRepo;
this.protocolRepository = protocolRepository;
} }
/** /**
@ -36,4 +39,11 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE {
public void onStoreAvailable(StoreProcessor store) { 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);
}
} }