Suppression du fichier temporaire du FFE lorsque le SBE nous renvoie le status (OK ou ERROR)

This commit is contained in:
Benjamin 2022-03-13 17:05:08 +01:00
parent 8c2ad17bd8
commit 0e3cd23d48
9 changed files with 76 additions and 50 deletions

View File

@ -28,30 +28,12 @@ public class App {
// Create all repository // Create all repository
ClientHandlerRepository clientRep = new ClientHandlerRepository(); ClientHandlerRepository clientRep = new ClientHandlerRepository();
StoreProcessorRepository storeRep = new StoreProcessorRepository(); StoreProcessorRepository storeRep = new StoreProcessorRepository();
// Initialisation des protocoles
ProtocolRepository protocolRep = new ProtocolRepositoryImpl(); ProtocolRepository protocolRep = new ProtocolRepositoryImpl();
protocolRep.addReader(new HelloRule()); initProtocols(repositoryStorage, protocolRep);
protocolRep.addReader(new SigninRule(protocolRep));
protocolRep.addReader(new SignupRule(protocolRep));
protocolRep.addReader(new SignoutRule());
protocolRep.addReader(new FilelistRule(protocolRep));
protocolRep.addReader(new SavefileRule(protocolRep, repositoryStorage.getStoragePath()));
protocolRep.addReader(new SendOkRule(protocolRep));
protocolRep.addReader(new GetFileRule(protocolRep));
protocolRep.addReader(new EraseErrorRule(protocolRep));
protocolRep.addReader(new EraseOkRule(protocolRep));
protocolRep.addReader(new RemoveFileRule(protocolRep));
protocolRep.addWriter(new SignOkRule());
protocolRep.addWriter(new SignErrorRule());
protocolRep.addWriter(new FilesRule());
protocolRep.addWriter(new SaveFileOkRule());
protocolRep.addWriter(new SaveFileErrorRule());
protocolRep.addWriter(new SendfileRule(repositoryStorage.getStoragePath()));
protocolRep.addWriter(new GetFileErrorRule());
protocolRep.addWriter(new EraseFileRule());
protocolRep.addWriter(new RemoveFileErrorRule());
protocolRep.addWriter(new RemoveFileOkRule());
// Initialisation du dispatcher et des servers
FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep, protocolRep); FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep, protocolRep);
new UnicastServerListener(ffe, clientRep, protocolRep, repositoryStorage, repositoryStorage.getUnicastPort()); new UnicastServerListener(ffe, clientRep, protocolRep, repositoryStorage, repositoryStorage.getUnicastPort());
new MulticastServerListener(ffe, storeRep, protocolRep, repositoryStorage.getMulticastIp(), repositoryStorage.getMulticastPort()); new MulticastServerListener(ffe, storeRep, protocolRep, repositoryStorage.getMulticastIp(), repositoryStorage.getMulticastPort());
@ -64,6 +46,44 @@ public class App {
// storeRep.close(); // storeRep.close();
} }
private static void initProtocols(Repository repositoryStorage, ProtocolRepository protocolRep) {
initReadersProtocols(repositoryStorage, protocolRep);
initWritersProtocols(repositoryStorage, protocolRep);
}
private static void initReadersProtocols(Repository repositoryStorage, ProtocolRepository protocolRep) {
protocolRep.addReader(new HelloRule());
protocolRep.addReader(new SigninRule(protocolRep));
protocolRep.addReader(new SignupRule(protocolRep));
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 SendErrorRule(protocolRep, repositoryStorage.getStoragePath()));
protocolRep.addReader(new GetFileRule(protocolRep));
protocolRep.addReader(new EraseErrorRule(protocolRep));
protocolRep.addReader(new EraseOkRule(protocolRep));
protocolRep.addReader(new RemoveFileRule(protocolRep));
protocolRep.addReader(new RetrieveErrorRule());
protocolRep.addReader(new RetrieveOkRule());
}
private static void initWritersProtocols(Repository repositoryStorage, ProtocolRepository protocolRep) {
protocolRep.addWriter(new SignOkRule());
protocolRep.addWriter(new SignErrorRule());
protocolRep.addWriter(new FilesRule());
protocolRep.addWriter(new SaveFileOkRule());
protocolRep.addWriter(new SaveFileErrorRule());
protocolRep.addWriter(new SendfileRule(repositoryStorage.getStoragePath()));
protocolRep.addWriter(new GetFileErrorRule());
protocolRep.addWriter(new EraseFileRule());
protocolRep.addWriter(new RemoveFileErrorRule());
protocolRep.addWriter(new RemoveFileOkRule());
protocolRep.addWriter(new GetFileOkRule());
protocolRep.addWriter(new RetrieveFileRule());
}
private static Repository prepareStorage() { private static Repository prepareStorage() {
AppData appData = AppData.getInstance(); AppData appData = AppData.getInstance();
Repository repository = new Repository( Repository repository = new Repository(

View File

@ -9,7 +9,7 @@ public class RetrieveErrorRule extends ProtocolReader {
private static final String NAME = "RETRIEVE_ERROR"; private static final String NAME = "RETRIEVE_ERROR";
// Constructor // Constructor
protected RetrieveErrorRule() { public RetrieveErrorRule() {
super(NAME, PATTERN); super(NAME, PATTERN);
} }

View File

@ -13,7 +13,7 @@ public class RetrieveOkRule extends ProtocolReader {
private static final int HASHED_FILE_CONTENT = 2; // Index hashed file content private static final int HASHED_FILE_CONTENT = 2; // Index hashed file content
// Variables // Variables
protected RetrieveOkRule() { public RetrieveOkRule() {
super(NAME, PATTERN); super(NAME, PATTERN);
} }

View File

@ -5,6 +5,10 @@ import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.writer.SaveFileErrorRule; import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
/** /**
* Règle permettant de de confirmer la sauvegrade d'un fichier. * Règle permettant de de confirmer la sauvegrade d'un fichier.
*/ */
@ -17,11 +21,13 @@ public class SendErrorRule extends ProtocolReader {
private static final String NAME = "SEND_ERROR"; private static final String NAME = "SEND_ERROR";
private ProtocolRepository protocolRep; private ProtocolRepository protocolRep;
private final String storagePath;
// Constructor // Constructor
public SendErrorRule(ProtocolRepository protocolRep) { public SendErrorRule(ProtocolRepository protocolRep, String storagePath) {
super(NAME, PATTERN); super(NAME, PATTERN);
this.protocolRep = protocolRep; this.protocolRep = protocolRep;
this.storagePath = storagePath;
} }
@ -29,7 +35,13 @@ public class SendErrorRule extends ProtocolReader {
protected ProtocolResult onExecuted(Context context, String... data) { protected ProtocolResult onExecuted(Context context, String... data) {
ProtocolResult result = new ProtocolResult(context); ProtocolResult result = new ProtocolResult(context);
result.setResultCommand(protocolRep.executeWriter(context, SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); result.setResultCommand(protocolRep.executeWriter(context, SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
// TODO : Supprimer le fichier du FFE
// 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; return result;
} }
} }

View File

@ -5,6 +5,10 @@ import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.writer.SaveFileOkRule; import lightcontainer.protocol.rules.writer.SaveFileOkRule;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
/** /**
* Règle permettant de de confirmer la sauvegrade d'un fichier. * Règle permettant de de confirmer la sauvegrade d'un fichier.
*/ */
@ -16,11 +20,13 @@ public class SendOkRule extends ProtocolReader {
private static final String NAME = "SEND_OK"; private static final String NAME = "SEND_OK";
private final ProtocolRepository protocolRep; private final ProtocolRepository protocolRep;
private final String storagePath;
// Constructor // Constructor
public SendOkRule(ProtocolRepository protocolRep) { public SendOkRule(ProtocolRepository protocolRep, String storagePath) {
super(NAME, PATTERN); super(NAME, PATTERN);
this.protocolRep = protocolRep; this.protocolRep = protocolRep;
this.storagePath = storagePath;
} }
@ -30,10 +36,14 @@ public class SendOkRule extends ProtocolReader {
result.setResultCommand(protocolRep.executeWriter(context, SaveFileOkRule.NAME), ResultCmdReceiver.CLIENT); result.setResultCommand(protocolRep.executeWriter(context, SaveFileOkRule.NAME), ResultCmdReceiver.CLIENT);
// Sauvegarder dans JSON // Sauvegarder dans JSON
System.out.println("Save en json du fichier");
context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataInt("size"), context.getDataString("iv"), context.getDomain()); context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataInt("size"), context.getDataString("iv"), context.getDomain());
// TODO : Supprimer le fichier du FFE // 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; return result;
} }
} }

View File

@ -4,14 +4,14 @@ import lightcontainer.protocol.ProtocolWriter;
public class GetFileOkRule extends ProtocolWriter { public class GetFileOkRule extends ProtocolWriter {
// Constants // Constants
private static final String PATTERN = "^GETFILE_OK (^ !]{1,20}) ([0-9]{1,10})\r\n)$"; private static final String PATTERN = "^GETFILE_OK (^ !]{1,20}) ([0-9]{1,10})\r\n$";
public static final String NAME = "GETFILE_OK"; public static final String NAME = "GETFILE_OK";
// -- params // -- params
private static final int FILE_NAME = 0; // Index file name hashed private static final int FILE_NAME = 0; // Index file name hashed
private static final int FILE_SIZE = 1; // Index file size private static final int FILE_SIZE = 1; // Index file size
// Constructors // Constructors
protected GetFileOkRule() { public GetFileOkRule() {
super(NAME, PATTERN); super(NAME, PATTERN);
} }
} }

View File

@ -10,7 +10,7 @@ public class RetrieveFileRule extends ProtocolWriter {
private static final int HASHED_FILE_NAME = 0; // Index hashed filename private static final int HASHED_FILE_NAME = 0; // Index hashed filename
// Constructor // Constructor
protected RetrieveFileRule() { public RetrieveFileRule() {
super(NAME, PATTERN); super(NAME, PATTERN);
} }
} }

View File

@ -44,7 +44,7 @@ public class SHA {
* *
* @param in InputStream to the input, flux to hash. * @param in InputStream to the input, flux to hash.
* @param fileSize Stream/file size. * @param fileSize Stream/file size.
* *ichier, utilisé
* @return Borrowing of the full current flux. * @return Borrowing of the full current flux.
* *
* @throws ShaException if an error occur. * @throws ShaException if an error occur.

View File

@ -1,17 +1 @@
{ {"unicast_port":8000,"multicast_ip":"226.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":"/jo0zYyQs96gWI9OgBXiPQ==","size":17,"iv":"rvOFhgEvgFMISO44jqlSRg==","storage":["lightcontainerSB01"]}]}]}
"unicast_port": 8000,
"multicast_ip": "226.66.66.1",
"multicast_port": 15502,
"network_interface": "My network interface",
"tls": true,
"storagePath": "C:\\Users\\ledou\\Documents\\ffe",
"users": [
{
"name": "aaaaa",
"password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617",
"aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=",
"passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==",
"files": []
}
]
}