Merge branch 'dev' into maximilien

This commit is contained in:
Maximilien LEDOUX 2022-02-26 14:32:14 +01:00
commit 067d9d661d
8 changed files with 128 additions and 34 deletions

View File

@ -67,12 +67,18 @@ public class StoreProcessor implements Runnable, AutoCloseable {
public void run() { public void run() {
while (true) { while (true) {
try { try {
// Envoie donnée au StoreBackEnd
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("StoreBackEnd: " + command); if (command != null) System.out.println("StoreBackEnd: " + command);
// Réception donnée du StoreBackEnd
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
// Alerter le FileFrontEnd que ce store processor est disponible pour gérer de nouvelle demande
fileFrontEnd.onStoreAvailable(this);
} }
} }

View File

@ -9,7 +9,7 @@ public class Task {
private TaskStatus status; private TaskStatus status;
private String command; private String command;
private String client; private String client;
private String store; private String storeDomain;
public Task() { public Task() {

View File

@ -1,4 +1,13 @@
package lightcontainer.interfaces; package lightcontainer.interfaces;
import lightcontainer.domains.StoreProcessor;
public interface StoreProcessorFFE { public interface StoreProcessorFFE {
/**
* Permet à un {@link StoreProcessor} d'avertir le FFE qu'il est disponible
* @param store
*/
void onStoreAvailable(StoreProcessor store);
} }

View File

@ -1,31 +0,0 @@
package lightcontainer.protocol;
public class HelloRule extends Protocol {
// Variables
// Constructor
protected HelloRule() {
super("HELLO", "HELLO "); // TODO : add the regex here (sbe_hello = "HELLO bl domain bl port line")
}
/**
* Execute the rule on a command.
*
* This function allows you to check a command and process those groups (parameters)
* use the utility functions of {@link Protocol} to facilitate processing, see @see.
*
* @param cmd Command on which to execute the rule.
* @see Protocol#execute(String)
* @see #matcherCheck(String)
* @see #matcherGetGroups()
* @since 1.0
*/
@Override
public void execute(String cmd) {
if (matcherCheck(cmd)) {
System.out.println("Good rule ;-) !");
} else {
System.out.println("OUPPS unknown rule !");
}
}
}

View File

@ -0,0 +1,40 @@
package lightcontainer.protocol;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class ProtocolReader {
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
*/
public boolean execute(String data) {
Matcher ruleMatcher = this.rulePattern.matcher(data);
if (ruleMatcher.matches()) {
String[] groups = new String[ruleMatcher.groupCount()];
for (int i = 1; i <= groups.length; ++i)
groups[i - 1] = ruleMatcher.group(i);
onExecuted(groups);
return true;
}
return false;
}
/**
* Cette méthode est appelée lors de l'exécution de la règle
* @param data
*/
protected abstract void onExecuted(String... data);
}

View File

@ -0,0 +1,27 @@
package lightcontainer.protocol.rules;
import lightcontainer.protocol.ProtocolReader;
public class HelloRule extends ProtocolReader {
private static final String PATTERN = "^HELLO ([A-Za-z0-9]{5,20}) ([0-9]{1,5})\r\n$";
// Index du domain dans le tableau de donnée
private static final int DOMAIN = 0;
//Index du port dans le tableau de donnée
private static final int PORT = 1;
public HelloRule() {
super(PATTERN);
}
@Override
protected void onExecuted(String... data) {
String domain = data[DOMAIN], port = data[PORT];
System.out.printf("Règle Hello avec domain=%s et port=%s\n", domain, port);
}
}

View File

@ -1,20 +1,43 @@
package lightcontainer.repository; package lightcontainer.repository;
import lightcontainer.domains.ClientHandler;
import lightcontainer.domains.StoreProcessor;
import lightcontainer.domains.Task; import lightcontainer.domains.Task;
import lightcontainer.interfaces.ClientHandlerFFE; import lightcontainer.interfaces.ClientHandlerFFE;
import lightcontainer.interfaces.StoreProcessorFFE; import lightcontainer.interfaces.StoreProcessorFFE;
import java.util.Collections;
import java.util.Deque; import java.util.Deque;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.concurrent.ConcurrentLinkedDeque;
public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE {
// Variables // Variables
private Deque<Task> tasks = new LinkedList<>(); 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
// Constructor // Constructor
public FileFrontEnd() { public FileFrontEnd(ClientHandlerRepository clientRepo, StoreProcessorRepository storeRepo) {
this.clientRepository = clientRepo;
this.storeRepository = storeRepo;
}
/**
* Appelé quand nouvelle tâche
*/
public void alertStoreProcessors(Task task) {
// On avertit les stor processors d'une nouvelle tâche
}
/**
* Permet à un {@link StoreProcessor} d'avertir le FFE qu'il est disponible
*
* @param store
*/
@Override
public void onStoreAvailable(StoreProcessor store) {
} }
} }

View File

@ -0,0 +1,20 @@
package lightcontainer.protocol.rules;
import lightcontainer.protocol.ProtocolReader;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class HelloRuleTest {
@Test
public void whenRuleIsRightThenIsExecute() {
// GIVEN
ProtocolReader protocolReader = new HelloRule();
String request = "HELLO bento 42890\r\n";
// EXPECT
assertTrue(protocolReader.execute(request));
}
}