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 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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.
|
||||||
|
@ -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) {
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
@ -25,31 +25,51 @@ public abstract class ProtocolWriter {
|
|||||||
return cmdName;
|
return cmdName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ProtocolResult {
|
||||||
|
|
||||||
|
private String command;
|
||||||
|
|
||||||
|
public String getCommand() {
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setCommand(String command) {
|
||||||
|
this.command = command;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet d'écrire un fichier sur le réseau. Cad envoyer le contenu d'un fichier sur le réseau.
|
||||||
|
* Redéfinissez cette méthode pour l'utiliser.
|
||||||
|
* @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.
|
* 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
|
* @param data Les données à ajouter dans la commande; L'ordre défini leur position dans la commande
|
||||||
* @return La commande construite
|
* @return La commande construite
|
||||||
*/
|
*/
|
||||||
public final String execute(String... data) {
|
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
|
// 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");
|
StringJoiner builder = new StringJoiner(" ", this.cmdName, "\r\n");
|
||||||
|
|
||||||
for (String param : data)
|
for (String param : data)
|
||||||
builder.add(param);
|
builder.add(param);
|
||||||
|
|
||||||
command = builder.toString();
|
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
|
||||||
|
|
||||||
// Vérifie que tout match (cf. Matcher). Si match alors on retourne la commande build, sinon on retourne NULL
|
if (ruleMatcher.matches()) {
|
||||||
Matcher ruleMatcher = this.rulePattern.matcher(command);
|
ProtocolResult result = onExecuted();
|
||||||
return ruleMatcher.matches() ? command : null;
|
result.setCommand(command);
|
||||||
|
return (T) result;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Permet d'écrire un fichier sur le réseau. Cad envoyer le contenu d'un fichier sur le réseau.
|
* Cette méthode est appelée lors de l'exécution de la règle
|
||||||
* Redéfinissez cette méthode pour l'utiliser.
|
|
||||||
* @param writer Buffer à remplir qui sera envoyer via le réseau
|
|
||||||
*/
|
*/
|
||||||
public void write(OutputStream writer) {}
|
protected abstract <T extends ProtocolResult> T onExecuted();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user