Ajout possibilité d'écrire un fichier fichier sur le réseau et ajout de celle-ci pour le store backend

This commit is contained in:
Benjamin 2022-03-05 18:32:55 +01:00
parent f0e9815844
commit 38a403d6a1
7 changed files with 121 additions and 22 deletions

View File

@ -25,6 +25,36 @@ public class App {
private static final String MULTICAST_IP = "226.66.66.1"; private static final String MULTICAST_IP = "226.66.66.1";
private static final int MULTICAST_PORT = 42500; private static final int MULTICAST_PORT = 42500;
/*
private static Thread t1 = null;
public static void main(String[] args) throws InterruptedException {
t1 = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while (true) {
System.out.println("HEY " + i++);
try {
synchronized (t1) {
System.out.println("SUSPEND");
t1.wait();
}
} catch (InterruptedException e) {}
}
}
});
t1.start();
System.out.println("START");
Thread.sleep(1000);
synchronized (t1) {
t1.notify();
}
}
*/
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
// Create all repository // Create all repository
ClientHandlerRepository clientRep = new ClientHandlerRepository(); ClientHandlerRepository clientRep = new ClientHandlerRepository();
@ -50,4 +80,5 @@ public class App {
clientRep.close(); clientRep.close();
storeRep.close(); storeRep.close();
} }
} }

View File

@ -3,6 +3,7 @@ package lightcontainer.domains.client;
import lightcontainer.interfaces.ClientHandlerFFE; import lightcontainer.interfaces.ClientHandlerFFE;
import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.ProtocolWriter;
import lightcontainer.protocol.rules.reader.SigninRule; import lightcontainer.protocol.rules.reader.SigninRule;
import lightcontainer.protocol.rules.writer.SignErrorRule; import lightcontainer.protocol.rules.writer.SignErrorRule;
import lightcontainer.protocol.rules.writer.SignOkRule; import lightcontainer.protocol.rules.writer.SignOkRule;
@ -114,13 +115,15 @@ public class ClientHandler implements Runnable, AutoCloseable {
SigninRule.Result signinResult = (SigninRule.Result) ruleResult; SigninRule.Result signinResult = (SigninRule.Result) ruleResult;
if (signinResult.checkCredentials()) { if (signinResult.checkCredentials()) {
this.login = signinResult.getLogin(); this.login = signinResult.getLogin();
writer.write(protocolRep.executeWriter(SignOkRule.NAME)); ProtocolWriter.ProtocolResult signokResult = protocolRep.executeWriter(SignOkRule.NAME);
writer.write(signokResult.getCommand());
writer.flush(); writer.flush();
return; return;
} }
} catch (ClassCastException ignored) {} } catch (ClassCastException ignored) {}
writer.write(protocolRep.executeWriter(SignErrorRule.NAME)); // Envoie SignError car echec de la connection ProtocolWriter.ProtocolResult signErrorResult = protocolRep.executeWriter(SignErrorRule.NAME);
writer.write(signErrorResult.getCommand()); // Envoie SignError car echec de la connection
writer.flush(); writer.flush();
this.close(); // Fermeture de la connection this.close(); // Fermeture de la connection
} }

View File

@ -1,6 +1,9 @@
package lightcontainer.domains.client; package lightcontainer.domains.client;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.interfaces.StoreProcessorFFE; import lightcontainer.interfaces.StoreProcessorFFE;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.ProtocolWriter;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
@ -21,7 +24,7 @@ import java.util.Objects;
* @see AutoCloseable * @see AutoCloseable
* @author Jérémi NIHART <j.nihart@student.helmo.be> * @author Jérémi NIHART <j.nihart@student.helmo.be>
*/ */
public class StoreProcessor implements Runnable, AutoCloseable { public class StoreProcessor extends Thread implements AutoCloseable {
// Variables // Variables
private final StoreProcessorFFE fileFrontEnd; private final StoreProcessorFFE fileFrontEnd;
private final Socket store; private final Socket store;
@ -30,12 +33,15 @@ public class StoreProcessor implements Runnable, AutoCloseable {
private BufferedReader reader; private BufferedReader reader;
private PrintWriter writer; private PrintWriter writer;
private String currentCommand;
private ProtocolRepository protocolRep;
// Constructor // Constructor
public StoreProcessor(Socket socket, String domain, StoreProcessorFFE ffe) { public StoreProcessor(Socket socket, String domain, StoreProcessorFFE ffe, ProtocolRepository protocolRep) {
this.domain = domain; this.domain = domain;
this.fileFrontEnd = ffe; this.fileFrontEnd = ffe;
this.store = socket; this.store = socket;
this.protocolRep = protocolRep;
this.client_run = false; this.client_run = false;
initStore(); initStore();
} }
@ -73,13 +79,52 @@ public class StoreProcessor implements Runnable, AutoCloseable {
this.client_run = true; this.client_run = true;
while (this.client_run) { while (this.client_run) {
try { try {
String command = this.reader.readLine(); alertAvalaible();
// TODO gestion de la réception de commandes, fichier, ...
if (command != null) System.out.println("StoreBackEnd: " + command); // Request
ProtocolWriter.ProtocolResult requestResult = protocolRep.executeWriter(this.currentCommand);
this.writer.write(requestResult.getCommand());
requestResult.write(this.store.getOutputStream());
// Response
String responseCommand = this.reader.readLine();
if (responseCommand != null)
System.out.println("StoreBackEnd: " + responseCommand);
ProtocolReader.ProtocolResult responseResult = protocolRep.executeReader(responseCommand);
System.out.println("StoreBackEnd response to client: " + responseResult.getResultCommand());
} catch (IOException ignore) { } } catch (IOException ignore) { }
} }
} }
/**
* Permet de demander au StoreBackEnd d'effectuer une commande
* @param command La commande à effectuer
*/
public void executeCommand(String command) {
synchronized (this) {
this.currentCommand = command;
this.notify();
}
}
/**
* Permet de déclarer le StoreBackEnd disponible
*/
private void alertAvalaible() {
synchronized (this) {
this.currentCommand = null;
fileFrontEnd.onStoreAvailable(this);
try {
this.wait();
} catch (InterruptedException e) { e.printStackTrace(); }
}
}
/** /**
* AutoClosable Function * AutoClosable Function
* Close the Storage thread and resources. * Close the Storage thread and resources.

View File

@ -69,7 +69,7 @@ public class MulticastServerListener implements Runnable {
Socket socket = new Socket(packet.getAddress(), readerResult.getPort()); Socket socket = new Socket(packet.getAddress(), readerResult.getPort());
// Create the store processor // Create the store processor
StoreProcessor storeProcessor = new StoreProcessor(socket, readerResult.getDomain(), null); // TODO <!!!> : Voir comment on procède get via repo ou ici ?! StoreProcessor storeProcessor = new StoreProcessor(socket, readerResult.getDomain(), null, protocolRep); // TODO <!!!> : Voir comment on procède get via repo ou ici ?!
// Add the store processor to its repository // Add the store processor to its repository
this.repository.addStore(storeProcessor); this.repository.addStore(storeProcessor);
} catch (IOException ignore) { } catch (IOException ignore) {

View File

@ -7,7 +7,7 @@ public interface ProtocolRepository {
<T extends ProtocolReader.ProtocolResult> T executeReader(String data); <T extends ProtocolReader.ProtocolResult> T executeReader(String data);
String executeWriter(String cmdName, String... datas); <T extends ProtocolWriter.ProtocolResult> T executeWriter(String cmdName, String... data);
void addReader(ProtocolReader reader); void addReader(ProtocolReader reader);

View File

@ -25,25 +25,16 @@ public abstract class ProtocolWriter {
return cmdName; return cmdName;
} }
public class ProtocolResult {
/** private String command;
* Permet de contruire une commande selon une règle établie.
* @param data Les données à ajouter dans la commande; L'ordre défini leur position dans la commande
* @return La commande construite
*/
public final String execute(String... data) {
// Concatatène le nom de la commande avec les données (trim), avec un espace entre chaque
String command;
StringJoiner builder = new StringJoiner(" ", this.cmdName, "\r\n");
for (String param : data) public String getCommand() {
builder.add(param); return command;
}
command = builder.toString(); private void setCommand(String command) {
this.command = command;
// Vérifie que tout match (cf. Matcher). Si match alors on retourne la commande build, sinon on retourne NULL
Matcher ruleMatcher = this.rulePattern.matcher(command);
return ruleMatcher.matches() ? command : null;
} }
/** /**
@ -52,4 +43,33 @@ public abstract class ProtocolWriter {
* @param writer Buffer à remplir qui sera envoyer via le réseau * @param writer Buffer à remplir qui sera envoyer via le réseau
*/ */
public void write(OutputStream writer) {} public void write(OutputStream writer) {}
}
/**
* Permet de contruire une commande selon une règle établie.
* @param data Les données à ajouter dans la commande; L'ordre défini leur position dans la commande
* @return La commande construite
*/
public final <T extends ProtocolResult> T execute(String... data) {
// Concatatène le nom de la commande avec les données (trim), avec un espace entre chaque
StringJoiner builder = new StringJoiner(" ", this.cmdName, "\r\n");
for (String param : data)
builder.add(param);
String command = builder.toString();
Matcher ruleMatcher = this.rulePattern.matcher(command); // Vérifie que tout match (cf. Matcher). Si match alors on retourne la commande build, sinon on retourne NULL
if (ruleMatcher.matches()) {
ProtocolResult result = onExecuted();
result.setCommand(command);
return (T) result;
}
return null;
}
/**
* Cette méthode est appelée lors de l'exécution de la règle
*/
protected abstract <T extends ProtocolResult> T onExecuted();
} }

View File

@ -23,9 +23,9 @@ public class ProtocolRepositoryImpl implements ProtocolRepository {
} }
@Override @Override
public String executeWriter(String cmdName, String... data) { public <T extends ProtocolWriter.ProtocolResult> T executeWriter(String cmdName, String... data) {
for (ProtocolWriter writer : writers) { for (ProtocolWriter writer : writers) {
String command; T command;
if (cmdName.equals(writer.getCmdName()) && (command = writer.execute(data)) != null) { if (cmdName.equals(writer.getCmdName()) && (command = writer.execute(data)) != null) {
return command; return command;
} }