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:
parent
f0e9815844
commit
38a403d6a1
@ -25,6 +25,36 @@ public class App {
|
||||
private static final String MULTICAST_IP = "226.66.66.1";
|
||||
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 {
|
||||
// Create all repository
|
||||
ClientHandlerRepository clientRep = new ClientHandlerRepository();
|
||||
@ -50,4 +80,5 @@ public class App {
|
||||
clientRep.close();
|
||||
storeRep.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package lightcontainer.domains.client;
|
||||
import lightcontainer.interfaces.ClientHandlerFFE;
|
||||
import lightcontainer.interfaces.ProtocolRepository;
|
||||
import lightcontainer.protocol.ProtocolReader;
|
||||
import lightcontainer.protocol.ProtocolWriter;
|
||||
import lightcontainer.protocol.rules.reader.SigninRule;
|
||||
import lightcontainer.protocol.rules.writer.SignErrorRule;
|
||||
import lightcontainer.protocol.rules.writer.SignOkRule;
|
||||
@ -114,13 +115,15 @@ public class ClientHandler implements Runnable, AutoCloseable {
|
||||
SigninRule.Result signinResult = (SigninRule.Result) ruleResult;
|
||||
if (signinResult.checkCredentials()) {
|
||||
this.login = signinResult.getLogin();
|
||||
writer.write(protocolRep.executeWriter(SignOkRule.NAME));
|
||||
ProtocolWriter.ProtocolResult signokResult = protocolRep.executeWriter(SignOkRule.NAME);
|
||||
writer.write(signokResult.getCommand());
|
||||
writer.flush();
|
||||
return;
|
||||
}
|
||||
} 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();
|
||||
this.close(); // Fermeture de la connection
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package lightcontainer.domains.client;
|
||||
|
||||
import lightcontainer.interfaces.ProtocolRepository;
|
||||
import lightcontainer.interfaces.StoreProcessorFFE;
|
||||
import lightcontainer.protocol.ProtocolReader;
|
||||
import lightcontainer.protocol.ProtocolWriter;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
@ -21,7 +24,7 @@ import java.util.Objects;
|
||||
* @see AutoCloseable
|
||||
* @author Jérémi NIHART <j.nihart@student.helmo.be>
|
||||
*/
|
||||
public class StoreProcessor implements Runnable, AutoCloseable {
|
||||
public class StoreProcessor extends Thread implements AutoCloseable {
|
||||
// Variables
|
||||
private final StoreProcessorFFE fileFrontEnd;
|
||||
private final Socket store;
|
||||
@ -30,12 +33,15 @@ public class StoreProcessor implements Runnable, AutoCloseable {
|
||||
|
||||
private BufferedReader reader;
|
||||
private PrintWriter writer;
|
||||
private String currentCommand;
|
||||
private ProtocolRepository protocolRep;
|
||||
|
||||
// Constructor
|
||||
public StoreProcessor(Socket socket, String domain, StoreProcessorFFE ffe) {
|
||||
public StoreProcessor(Socket socket, String domain, StoreProcessorFFE ffe, ProtocolRepository protocolRep) {
|
||||
this.domain = domain;
|
||||
this.fileFrontEnd = ffe;
|
||||
this.store = socket;
|
||||
this.protocolRep = protocolRep;
|
||||
this.client_run = false;
|
||||
initStore();
|
||||
}
|
||||
@ -73,13 +79,52 @@ public class StoreProcessor implements Runnable, AutoCloseable {
|
||||
this.client_run = true;
|
||||
while (this.client_run) {
|
||||
try {
|
||||
String command = this.reader.readLine();
|
||||
// TODO gestion de la réception de commandes, fichier, ...
|
||||
if (command != null) System.out.println("StoreBackEnd: " + command);
|
||||
alertAvalaible();
|
||||
|
||||
// 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) { }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Close the Storage thread and resources.
|
||||
|
@ -69,7 +69,7 @@ public class MulticastServerListener implements Runnable {
|
||||
|
||||
Socket socket = new Socket(packet.getAddress(), readerResult.getPort());
|
||||
// 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
|
||||
this.repository.addStore(storeProcessor);
|
||||
} catch (IOException ignore) {
|
||||
|
@ -7,7 +7,7 @@ public interface ProtocolRepository {
|
||||
|
||||
<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);
|
||||
|
||||
|
@ -25,25 +25,16 @@ public abstract class ProtocolWriter {
|
||||
return cmdName;
|
||||
}
|
||||
|
||||
public class ProtocolResult {
|
||||
|
||||
/**
|
||||
* 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");
|
||||
private String command;
|
||||
|
||||
for (String param : data)
|
||||
builder.add(param);
|
||||
public String getCommand() {
|
||||
return command;
|
||||
}
|
||||
|
||||
command = builder.toString();
|
||||
|
||||
// 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;
|
||||
private void setCommand(String command) {
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -52,4 +43,33 @@ public abstract class ProtocolWriter {
|
||||
* @param writer Buffer à remplir qui sera envoyer via le réseau
|
||||
*/
|
||||
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();
|
||||
|
||||
}
|
||||
|
@ -23,9 +23,9 @@ public class ProtocolRepositoryImpl implements ProtocolRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeWriter(String cmdName, String... data) {
|
||||
public <T extends ProtocolWriter.ProtocolResult> T executeWriter(String cmdName, String... data) {
|
||||
for (ProtocolWriter writer : writers) {
|
||||
String command;
|
||||
T command;
|
||||
if (cmdName.equals(writer.getCmdName()) && (command = writer.execute(data)) != null) {
|
||||
return command;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user