- Translation de la suppresion du fichier temporaire sur le FFE de Send(OK/ERROR) ) Save(OK/ERROR), pour supprimer le fichier créé par la méthode read de SAVEFILE, et ce même si on ne passe pas par le système de tâche (exemple, pas de SBE disponible)

- Ajout possibilité de choisir un SBE approprié à la requête et donc de renvoyer un message d'erreur convenable si le SBE n'est pas connecté pour le moment (Via son ID)
This commit is contained in:
Benjamin 2022-03-13 21:47:23 +01:00
parent e5628942a2
commit b94720a797
15 changed files with 105 additions and 47 deletions

View File

@ -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());

View File

@ -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();

View File

@ -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<String> getStringifiedFilesOf() {

View File

@ -26,4 +26,6 @@ public interface MulticastSPR {
void assignTask(String stor, Task task);
boolean hasDomain(String domain);
int domainCount();
}

View File

@ -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);

View File

@ -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());

View File

@ -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<String> storageIterator) {
return storageIterator.next();
}
}

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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.

View File

@ -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"]}]}]}
{"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"]}]}]}