Merge branch 'benjamin' into dev

# Conflicts:
#	app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java
#	app/src/main/resources/appdata.json
This commit is contained in:
Benjamin 2022-03-19 14:15:18 +01:00
commit a287bbdb59
11 changed files with 53 additions and 47 deletions

View File

@ -42,6 +42,7 @@ public class Task {
* @return TRUE si le client doit recevoir cette réponse.
*/
public boolean isResponseOfClient(String storeDomain) {
System.out.println(status + " - " + context.getDomain() + " | " + storeDomain);
return (status == TaskStatus.PROCESSING && context.getDomain().equals(storeDomain));
}

View File

@ -110,30 +110,33 @@ public class ClientHandler implements Runnable, AutoCloseable {
ProtocolWriter.ProtocolResult writerCommand = ruleResult.getResultCommand();
// TODO : Vérifier que le StorBackEnd demandé (Pas toujours demandé) est disponible
if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND && !fileFrontEnd.canExecuteCommand(ruleResult.getRequestDomain())) {
System.out.println("[1] 1");
writer.print(ruleResult.onNotExecutable(context)); // Renvoie au client
writer.flush();
} else if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND) {
System.out.println("[1] 2");
fileFrontEnd.newCommand(context, writerCommand); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd
// Attend la fin de la réalisation de la tâche
waitTaskResponse();
System.out.println("[1] 4");
if (response != null) {
System.out.println("[1] 5");
writer.write(response.getCommand()); // Renvoie au client
writer.flush();
response.write(this.client.getOutputStream()); // Ecrit au client si nécessaire
} else {
System.out.println("[1] 6");
writer.print(ruleResult.onNotExecutable(context)); // Renvoie au client
writer.flush();
}
} else {
System.out.println("[1] 3");
writer.print(writerCommand.getCommand()); // Renvoie au client
writer.flush();
}
} else {
System.out.println(4);
accessDenied();
}
} catch (IOException e) {

View File

@ -79,9 +79,12 @@ public class StoreProcessor extends Thread implements AutoCloseable {
@Override
public void run() {
this.client_run = true;
while (this.client_run) {
try {
waitAction();
if (protocolResult == null) { // Si on n'a pas encore la commande à envoyer
waitAction();
}
System.out.println("[SBE] Envoie commande : " + protocolResult.getCommand());
// Request
@ -92,6 +95,7 @@ public class StoreProcessor extends Thread implements AutoCloseable {
protocolResult.write(this.store.getOutputStream());
} catch (IOException writeException) { // Si SBE fermé
System.out.println("STOPPER");
writeException.printStackTrace();
// Envoie au client que la requête n'a pu être traitée
alertAvailable(null);
break;
@ -103,7 +107,7 @@ public class StoreProcessor extends Thread implements AutoCloseable {
responseCommand += "\r\n";
ProtocolReader.ProtocolResult responseResult = protocolRep.executeReader(context, responseCommand);
if (responseResult != null) {
System.out.println("StoreBackEnd response to client: " + responseResult.getResultCommand());
System.out.println("StoreBackEnd (" + domain + ") response to client: " + responseResult.getResultCommand());
responseResult.read(
this.store.getInputStream()
);
@ -207,7 +211,7 @@ public class StoreProcessor extends Thread implements AutoCloseable {
return Objects.hash(domain);
}
public boolean canProcessTask(Task task) {
public boolean canProcessTask() {
return this.protocolResult == null; // Vérifier si tâche veut ce SBE
}

View File

@ -23,7 +23,7 @@ public interface MulticastSPR {
*/
boolean addStore(StoreProcessor store);
String findDomain(Task task);
String findDomain();
void assignTask(String stor, Task task);

View File

@ -16,7 +16,7 @@ public class SendErrorRule extends ProtocolReader {
//"^SEND_ERROR\r\n$"
private static final String PATTERN = SBE_SEND_RESULT_ERROR;
private static final String NAME = "SEND_ERROR";
private static final String NAME = "SENDERROR";
private final ProtocolRepository protocolRep;

View File

@ -31,16 +31,21 @@ public class SaveFileErrorRule extends ProtocolWriter {
@Override
protected ProtocolResult onExecuted(Context context, String... data) {
ProtocolResult result = new ProtocolResult(context);
String fileName = context.getDataString("fileName");
String fileNameSalt = context.getDataString("fileNameSalt");
// Suppression du fichier temporaire dans le stockage du FFE
ShaHasher hasher = new ShaHasher(context.getLogin() + "_" + context.getDataString("fileName"));
try {
Files.deleteIfExists(Path.of(
String.format("%s/%s", this.storagePath,
hasher.fromSalt(hasher.saltToByte(context.getDataString("fileNameSalt")))
)
));
} catch (IOException e) {
if (fileName != null && fileNameSalt != null) {
// Suppression du fichier temporaire dans le stockage du FFE
ShaHasher hasher = new ShaHasher(context.getLogin() + "_" + fileName);
try {
Files.deleteIfExists(Path.of(
String.format("%s/%s", this.storagePath,
hasher.fromSalt(hasher.saltToByte(fileNameSalt))
)
));
} catch (IOException e) {
}
}
return result;

View File

@ -30,6 +30,7 @@ public class SaveFileOkRule extends ProtocolWriter {
protected ProtocolWriter.ProtocolResult onExecuted(Context context, String... data) {
ProtocolWriter.ProtocolResult result = new ProtocolWriter.ProtocolResult(context);
System.out.println("===> Save en json " + context.getDomain() + " - " + context.getLogin());
// Sauvegarder dans JSON
context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataInt("size"), context.getDataString("iv"), context.getDomain());

View File

@ -32,7 +32,7 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE {
*/
public void alertStoreProcessors(Task task) {
// On avertit les stor processors d'une nouvelle tâche
String stor = storeRepository.findDomain(task);
String stor = storeRepository.findDomain();
if (stor != null) {
storeRepository.assignTask(stor, task);
task.setDomain(stor);
@ -51,9 +51,7 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE {
Iterator<Task> it = tasks.iterator();
while (it.hasNext()) {
Task task = it.next();
System.out.println("Cherche");
if (task.isResponseOfClient(store.getDomain())) {
System.out.println("Task trouvée");
clientRepository.respondToClient(task.getClient(), response);
it.remove(); // Suppression de la tâche
break;
@ -70,12 +68,11 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE {
private void assignOtherTask(StoreProcessor store) {
Iterator<Task> it = tasks.iterator();
while (it.hasNext()) {
Task task = it.next();
if (store.canProcessTask(task)) {
storeRepository.assignTask(store.getDomain(), task);
if (task.getDomain() == null && store.canProcessTask()) {
task.setDomain(store.getDomain());
store.executeCommand(task.getContext(), task.getCommand());
return;
}
}

View File

@ -31,7 +31,6 @@ public class ProtocolRepositoryImpl implements ProtocolRepository {
return command;
}
}
System.out.println("COMMANDE NULL");
return null;
}

View File

@ -60,14 +60,24 @@ public class StoreProcessorRepository implements AutoCloseable, MulticastSPR {
}
@Override
public String findDomain(Task task) {
StoreProcessor handler = findSBE(task);
public String findDomain() {
StoreProcessor handler = findSBE();
return handler == null ? null : handler.getDomain();
}
private StoreProcessor findSBE(Task task) {
private StoreProcessor findSBE() {
for (StoreProcessor handler : handlers) {
if (handler.canProcessTask(task)) {
if (handler.canProcessTask()) {
return handler;
}
}
return null;
}
private StoreProcessor getSBE(String stor) {
for (StoreProcessor handler : handlers) {
if (handler.getDomain().equals(stor)) {
return handler;
}
}
@ -77,9 +87,10 @@ public class StoreProcessorRepository implements AutoCloseable, MulticastSPR {
@Override
public void assignTask(String stor, Task task) {
StoreProcessor handler = findSBE(task);
System.out.println("Find stor : " + handler);
handler.executeCommand(task.getContext(), task.getCommand());
StoreProcessor handler = getSBE(stor);
if (handler != null) {
handler.executeCommand(task.getContext(), task.getCommand());
}
}
@Override

View File

@ -1,16 +1 @@
{
"unicast_port": 8000,
"multicast_ip": "224.66.66.1",
"multicast_port": 15502,
"network_interface": "lo",
"tls": true,
"storagePath": "C:\\Users\\ledou\\Documents\\ffe",
"users": [
{
"name": "aaaaa",
"password": "$2a$10$bRdd0/8R60TNUbwtq2dMbun.TAPOZlqxJ/UB7qL2rgxHBuZownH0.",
"aes_key": "FwwA+Mkg94TsXeBuReE6wD3QEsVnrnuIXPtzCLFtdJM=",
"files": []
}
]
}
{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"wlp1s0","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"benjamin","password":"$2a$10$I4vHt83CTYuQCP7xvZ04Ne7Vb0cswBiVZhV0n23k9FCxoH0ny9fZG","aes_key":"mAP6izUBUhBxIkakH2yB/TplhRz1OQV5Fp6HQmhywns=","files":[{"name":"README.md","fileNameSalt":"atK0eGo/J8IBOTCpIZpWow==","size":17,"iv":"fXPzefxl0cO4Hf02Qwi+BA==","storage":["lightcontainerSB01"]}]},{"name":"aaaaa","password":"$2a$10$nDCEDVwbNO/YDQ4qdRcxfuES4.aboluLzWouXXsk6vDoaWocv516W","aes_key":"kYtwHy9qJBg30WS6axWTFGVE0Ge5kpYiJJlC+COIEI4=","files":[{"name":"ca.crt","fileNameSalt":"qz6FoPGyJJ8Hy+1LIouvZA==","size":4207,"iv":"8v2H+cOzztD++NXHXw5Ofw==","storage":["lightcontainerSB01"]}]}]}