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.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.

View File

@ -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;
}
}

View File

@ -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) { }
}
}

View File

@ -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

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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<Task> 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);
}
}