From b94720a797a0d59fccf03d21c50dd36821a3758d Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sun, 13 Mar 2022 21:47:23 +0100 Subject: [PATCH] =?UTF-8?q?-=20Translation=20de=20la=20suppresion=20du=20f?= =?UTF-8?q?ichier=20temporaire=20sur=20le=20FFE=20de=20Send(OK/ERROR)=20)?= =?UTF-8?q?=20Save(OK/ERROR),=20pour=20supprimer=20le=20fichier=20cr=C3=A9?= =?UTF-8?q?=C3=A9=20par=20la=20m=C3=A9thode=20read=20de=20SAVEFILE,=20et?= =?UTF-8?q?=20ce=20m=C3=AAme=20si=20on=20ne=20passe=20pas=20par=20le=20sys?= =?UTF-8?q?t=C3=A8me=20de=20t=C3=A2che=20(exemple,=20pas=20de=20SBE=20disp?= =?UTF-8?q?onible)=20-=20Ajout=20possibilit=C3=A9=20de=20choisir=20un=20SB?= =?UTF-8?q?E=20appropri=C3=A9=20=C3=A0=20la=20requ=C3=AAte=20et=20donc=20d?= =?UTF-8?q?e=20renvoyer=20un=20message=20d'erreur=20convenable=20si=20le?= =?UTF-8?q?=20SBE=20n'est=20pas=20connect=C3=A9=20pour=20le=20moment=20(Vi?= =?UTF-8?q?a=20son=20ID)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/lightcontainer/App.java | 6 ++-- .../domains/client/ClientHandler.java | 27 +++++++++--------- .../domains/client/Context.java | 4 +-- .../interfaces/MulticastSPR.java | 2 ++ .../protocol/ProtocolWriter.java | 2 +- .../protocol/rules/reader/GetFileRule.java | 4 +-- .../protocol/rules/reader/RemoveFileRule.java | 22 +++++++++++++-- .../protocol/rules/reader/SavefileRule.java | 2 ++ .../protocol/rules/reader/SendErrorRule.java | 7 +---- .../protocol/rules/reader/SendOkRule.java | 13 +-------- .../rules/writer/SaveFileErrorRule.java | 28 ++++++++++++++++++- .../protocol/rules/writer/SaveFileOkRule.java | 25 ++++++++++++++++- .../repository/FileFrontEnd.java | 3 +- .../repository/StoreProcessorRepository.java | 5 ++++ app/src/main/resources/appdata.json | 2 +- 15 files changed, 105 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index 97607d2..ad2fbdf 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -59,7 +59,7 @@ public class App { protocolRep.addReader(new SignoutRule()); protocolRep.addReader(new FilelistRule(protocolRep)); protocolRep.addReader(new SavefileRule(protocolRep, repositoryStorage.getStoragePath())); - protocolRep.addReader(new SendOkRule(protocolRep, repositoryStorage.getStoragePath())); + protocolRep.addReader(new SendOkRule(protocolRep)); protocolRep.addReader(new SendErrorRule(protocolRep, repositoryStorage.getStoragePath())); protocolRep.addReader(new GetFileRule(protocolRep)); protocolRep.addReader(new EraseErrorRule(protocolRep)); @@ -73,8 +73,8 @@ public class App { protocolRep.addWriter(new SignOkRule()); protocolRep.addWriter(new SignErrorRule()); protocolRep.addWriter(new FilesRule()); - protocolRep.addWriter(new SaveFileOkRule()); - protocolRep.addWriter(new SaveFileErrorRule()); + protocolRep.addWriter(new SaveFileOkRule(repositoryStorage.getStoragePath())); + protocolRep.addWriter(new SaveFileErrorRule(repositoryStorage.getStoragePath())); protocolRep.addWriter(new SendfileRule(repositoryStorage.getStoragePath())); protocolRep.addWriter(new GetFileErrorRule()); protocolRep.addWriter(new EraseFileRule()); diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index 3508101..9d924bf 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -110,26 +110,25 @@ public class ClientHandler implements Runnable, AutoCloseable { } if (checkAccess(ruleResult)) { + // Lecture du fichier client ruleResult.read(this.client.getInputStream()); 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("ICI"); + writer.print(ruleResult.onNotExecutable(context)); // Renvoie au client + writer.flush(); + } else if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND) { - if (ruleResult.getReceiver() == ProtocolReader.ResultCmdReceiver.STOREBACKEND) { - // TODO : Vérifier que le StorBackEnd demandé (Pas toujours demandé) est disponible - if (ruleResult.getRequestDomain() != null && !fileFrontEnd.canExecuteCommand(ruleResult.getRequestDomain())) { - writer.print(ruleResult.onNotExecutable(context)); // Renvoie au client - writer.flush(); - } else { - fileFrontEnd.newCommand(context, writerCommand); // Envoie dans la file de tâche FileFrontEnd en attente d'un traitement d'un StorBackEnd + 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(); - - writer.write(response.getCommand()); // Renvoie au client - writer.flush(); - response.write(this.client.getOutputStream()); // Ecrit au client si nécessaire - } + // Attend la fin de la réalisation de la tâche + waitTaskResponse(); + writer.write(response.getCommand()); // Renvoie au client + writer.flush(); + response.write(this.client.getOutputStream()); // Ecrit au client si nécessaire } else { writer.print(writerCommand.getCommand()); // Renvoie au client writer.flush(); diff --git a/app/src/main/java/lightcontainer/domains/client/Context.java b/app/src/main/java/lightcontainer/domains/client/Context.java index c002c9f..f577891 100644 --- a/app/src/main/java/lightcontainer/domains/client/Context.java +++ b/app/src/main/java/lightcontainer/domains/client/Context.java @@ -178,8 +178,8 @@ public class Context { return null; } - public ReadOnlyFile getFileOf(String fileName, String userName) { - return this.repository.getFileOf(fileName, userName); + public ReadOnlyFile getFile(String fileName) { + return this.repository.getFileOf(fileName, login); } public List getStringifiedFilesOf() { diff --git a/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java b/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java index 5f352e7..5565835 100644 --- a/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java +++ b/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java @@ -26,4 +26,6 @@ public interface MulticastSPR { void assignTask(String stor, Task task); boolean hasDomain(String domain); + + int domainCount(); } diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java index 8a772a0..cdcaa56 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java @@ -77,7 +77,7 @@ public abstract class ProtocolWriter { String command = builder + "\r\n"; 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 - System.out.println("Essaye : " + command); + System.out.println("Crée la commande : " + command); if (ruleMatcher.matches()) { ProtocolResult result = onExecuted(context, data); result.setCommand(command); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java index 4b4f056..e3e9a3c 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java @@ -4,8 +4,6 @@ import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.GetFileErrorRule; -import lightcontainer.protocol.rules.writer.GetFileOkRule; -import lightcontainer.storage.File; import lightcontainer.storage.ReadOnlyFile; import java.util.Iterator; @@ -51,7 +49,7 @@ public class GetFileRule extends ProtocolReader { protected GetFileRule.Result onExecuted(Context context, String... data) { GetFileRule.Result result = new GetFileRule.Result(context, data[FILE_NAME]); - ReadOnlyFile file = context.getFileOf(data[FILE_NAME], context.getLogin()); + ReadOnlyFile file = context.getFile(data[FILE_NAME]); // Précision du store back end demandé pour traiter cette commande. String requestDomain = extractRequestDomain(file.getStorageIterator()); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java index 412c8da..7024478 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java @@ -5,6 +5,9 @@ import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.EraseFileRule; import lightcontainer.protocol.rules.writer.RemoveFileErrorRule; +import lightcontainer.storage.ReadOnlyFile; + +import java.util.Iterator; public class RemoveFileRule extends ProtocolReader { @@ -39,11 +42,16 @@ public class RemoveFileRule extends ProtocolReader { @Override protected RemoveFileRule.Result onExecuted(Context context, String... data) { RemoveFileRule.Result result = new RemoveFileRule.Result(context, data[FILENAME]); - String extendedFileName = context.getHashedFileName(result.getFileName()); + ReadOnlyFile file = context.getFile(result.getFileName()); - if (extendedFileName != null) { + if (file != null) { + String extendedFileName = context.getHashedFileName(result.getFileName()); context.putDataString("fileName", result.getFileName()); result.setResultCommand(protocolRep.executeWriter(context, EraseFileRule.NAME, extendedFileName), ResultCmdReceiver.STOREBACKEND); + + // Précision du store back end demandé pour traiter cette commande. + String requestDomain = extractRequestDomain(file.getStorageIterator()); + result.setRequestDomain(requestDomain); } else { result.setResultCommand(protocolRep.executeWriter(context, RemoveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); } @@ -57,4 +65,14 @@ public class RemoveFileRule extends ProtocolReader { result.setResultCommand(protocolRep.executeWriter(context, RemoveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); return result; } + + /** + * TODO : But futur est de pouvoir en avoir plusieurs + * Cette méthode permet de choisir le domaine voulu. + * @param storageIterator Les domaines + * @return Le domain choisi + */ + private String extractRequestDomain(Iterator storageIterator) { + return storageIterator.next(); + } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java index 8ffde95..2d089d0 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java @@ -85,6 +85,8 @@ public class SavefileRule extends ProtocolReader { this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(encryptedFileSize), fileHash), ResultCmdReceiver.STOREBACKEND); } catch (IOException | SHA.ShaException e) { + // TODO : Supprimer le fichier + System.out.println("HEYHEYHEYHEYHEY OHOHOH"); this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); e.printStackTrace(); } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java index 3c77552..ced3fd0 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java @@ -4,6 +4,7 @@ import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.SaveFileErrorRule; +import lightcontainer.utils.ShaHasher; import java.io.IOException; import java.nio.file.Files; @@ -36,12 +37,6 @@ public class SendErrorRule extends ProtocolReader { ProtocolResult result = new ProtocolResult(context); result.setResultCommand(protocolRep.executeWriter(context, SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); - // Suppression du fichier temporaire dans le stockage du FFE - String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); - try { - Files.deleteIfExists(Path.of(String.format("%s/%s", this.storagePath, hashedFileName))); - } catch (IOException e) {} - return result; } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java index 7298bf0..3c32bca 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java @@ -20,13 +20,11 @@ public class SendOkRule extends ProtocolReader { private static final String NAME = "SEND_OK"; private final ProtocolRepository protocolRep; - private final String storagePath; // Constructor - public SendOkRule(ProtocolRepository protocolRep, String storagePath) { + public SendOkRule(ProtocolRepository protocolRep) { super(NAME, PATTERN); this.protocolRep = protocolRep; - this.storagePath = storagePath; } @@ -35,15 +33,6 @@ public class SendOkRule extends ProtocolReader { ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context); result.setResultCommand(protocolRep.executeWriter(context, SaveFileOkRule.NAME), ResultCmdReceiver.CLIENT); - // Sauvegarder dans JSON - context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataInt("size"), context.getDataString("iv"), context.getDomain()); - - // Suppression du fichier temporaire dans le stockage du FFE - String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); - try { - Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName))); - } catch (IOException e) {} - return result; } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java index 4d1f1f3..090564d 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java @@ -1,6 +1,12 @@ package lightcontainer.protocol.rules.writer; +import lightcontainer.domains.client.Context; import lightcontainer.protocol.ProtocolWriter; +import lightcontainer.utils.ShaHasher; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; /** * Règle signifiant que la sauvegarde d'un fichier a échoué @@ -11,8 +17,28 @@ public class SaveFileErrorRule extends ProtocolWriter { public static final String NAME = "SAVEFILE_ERROR"; - public SaveFileErrorRule() { + private final String storagePath; + + public SaveFileErrorRule(String storagePath) { super(NAME, PATTERN); + this.storagePath = storagePath; } + + @Override + protected ProtocolResult onExecuted(Context context, String... data) { + ProtocolResult result = new ProtocolResult(context); + + // 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) {} + + return result; + } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java index 7734605..fac890f 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java @@ -1,7 +1,12 @@ package lightcontainer.protocol.rules.writer; +import lightcontainer.domains.client.Context; import lightcontainer.protocol.ProtocolWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + /** * Règle signifiant que la sauvegarde d'un fichier fût un succès */ @@ -11,8 +16,26 @@ public class SaveFileOkRule extends ProtocolWriter { public static final String NAME = "SAVEFILE_OK"; - public SaveFileOkRule() { + private final String storagePath; + + public SaveFileOkRule(String storagePath) { super(NAME, PATTERN); + this.storagePath = storagePath; } + @Override + protected ProtocolWriter.ProtocolResult onExecuted(Context context, String... data) { + ProtocolWriter.ProtocolResult result = new ProtocolWriter.ProtocolResult(context); + + // Sauvegarder dans JSON + context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataInt("size"), context.getDataString("iv"), context.getDomain()); + + // Suppression du fichier temporaire dans le stockage du FFE + String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); + try { + Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName))); + } catch (IOException e) {} + + return result; + } } diff --git a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java index e61dcde..55394a4 100644 --- a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java +++ b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java @@ -85,6 +85,7 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { @Override public boolean canExecuteCommand(String domain) { - return storeRepository.hasDomain(domain); + System.out.println("Peut on exécuter la commande ? " + domain); + return domain == null ? storeRepository.domainCount() > 0 : storeRepository.hasDomain(domain); } } diff --git a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java index 769208a..e798e27 100644 --- a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java +++ b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java @@ -89,6 +89,11 @@ public class StoreProcessorRepository implements AutoCloseable, MulticastSPR { return false; } + @Override + public int domainCount() { + return handlers.size(); + } + /** * AutoClosable Function * Closes all StoreProcessor stored in this repository and deallocates all resources. diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index f9171fe..a887a1b 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1 +1 @@ -{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[{"name":"main.py","fileNameSalt":"IJNYL681pFqbF9OHzRuHIg==","size":854,"iv":"bPCnwYbenKvFfwbhq+HI5A==","storage":["lightcontainerSB01"]},{"name":"README.md","fileNameSalt":"/jo0zYyQs96gWI9OgBXiPQ==","size":17,"iv":"rvOFhgEvgFMISO44jqlSRg==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file +{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[{"name":"README.md","fileNameSalt":"cDY6LMq13iqKknRS9OVBPw==","size":17,"iv":"jxc7hkIAoH64M8JF7FvNFw==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file