diff --git a/app/src/main/java/lightcontainer/domains/Task.java b/app/src/main/java/lightcontainer/domains/Task.java index d073814..42ce548 100644 --- a/app/src/main/java/lightcontainer/domains/Task.java +++ b/app/src/main/java/lightcontainer/domains/Task.java @@ -18,10 +18,11 @@ public class Task { */ private final Context context; - public Task(Context context, TaskStatus status, ProtocolWriter.ProtocolResult command) { + public Task(Context context, TaskStatus status, ProtocolWriter.ProtocolResult command, String requestDomain) { this.context = context; this.status = status; this.command = command; + context.setDomain(requestDomain); // Domaine requis } /** @@ -31,8 +32,8 @@ public class Task { * @param command Commande à exécuter * @return L'instance de la tâche créée */ - public static Task newInstance(Context context, ProtocolWriter.ProtocolResult command) { - return new Task(context, TaskStatus.PENDING, command); + public static Task newInstance(Context context, ProtocolWriter.ProtocolResult command, String requestDomain) { + return new Task(context, TaskStatus.PENDING, command, requestDomain); } /** @@ -83,6 +84,10 @@ public class Task { return this.context; } + /** + * Le domaine actuellement utilisé OU requis, null si aucune domaine requis/associé + * @return + */ public String getDomain() { return context.getDomain(); } diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index dc9136f..54fa0f0 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -110,28 +110,22 @@ 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 + fileFrontEnd.newCommand(context, writerCommand, ruleResult.getRequestDomain()); // 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(); } diff --git a/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java b/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java index 5e5074d..466649c 100644 --- a/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java +++ b/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java @@ -17,7 +17,7 @@ public interface ClientHandlerFFE { * @param context Context de la requête * @param command Commande à traiter */ - void newCommand(Context context, ProtocolWriter.ProtocolResult command); + void newCommand(Context context, ProtocolWriter.ProtocolResult command, String requestDomain); boolean canExecuteCommand(String domain); } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java index 43f819d..7e9c278 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java @@ -66,7 +66,7 @@ public class RetrieveOkRule extends ProtocolReader { if (!fileReceiver.receiveFile(reader, this.filename, this.filesize)) { throw new IllegalStateException("Bad file reception"); } - if (SHA.hashFile(storagePath, this.filename).equals(this.hashedFileContent)) { + if (!SHA.hashFile(storagePath, this.filename).equals(this.hashedFileContent)) { throw new IllegalStateException("File hash content are not equals"); } } catch (IllegalStateException|SHA.ShaException e) { diff --git a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java index a35e4f6..66d1df5 100644 --- a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java +++ b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java @@ -51,7 +51,7 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { Iterator it = tasks.iterator(); while (it.hasNext()) { Task task = it.next(); - if (task.getDomain() == null) { + if (task.getDomain() == null || task.getDomain().equals(storeDomain)) { // Si tâche sans domaine ou si domaine requis pour la tâche est le domaine task.setDomain(storeDomain); storeRepository.assignTask(storeDomain, task); return; @@ -62,6 +62,14 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { @Override public void onStoreDisconnect(String domain) { responseToClient(domain, null); + Iterator it = tasks.iterator(); + while (it.hasNext()) { + Task task = it.next(); + if (storeRepository.domainCount() == 0 || (task.getDomain() != null && task.getDomain().equals(domain))) { + clientRepository.respondToClient(it.next().getClient(), null); + it.remove(); // Suppression de la tâche + } + } this.storeRepository.closeStore(domain); } @@ -79,8 +87,8 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { @Override - public void newCommand(Context context, ProtocolWriter.ProtocolResult command) { - Task task = Task.newInstance(context, command); + public void newCommand(Context context, ProtocolWriter.ProtocolResult command, String requestDomain) { + Task task = Task.newInstance(context, command, requestDomain); tasks.add(task); alertStoreProcessors(task); } diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java index 4913a4d..d3c07c9 100644 --- a/app/src/main/java/lightcontainer/storage/JsonAdapter.java +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -13,7 +13,7 @@ public class JsonAdapter implements Adapter { * @return A Json String containing AppData properties */ @Override - public synchronized String toString(AppData appData) { + public String toString(AppData appData) { return addData(appData); } diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index a9a3b60..ea65159 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -28,7 +28,7 @@ public class Repository { /** * Saves configuration file */ - public void save() { + public synchronized void save() { if (filePath != null) { String jsonAppData = adapter.toString(appData); try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) { diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index d2ba8b2..2819776 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":"wlp1s0","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"benjamin","password":"$2a$10$I4vHt83CTYuQCP7xvZ04Ne7Vb0cswBiVZhV0n23k9FCxoH0ny9fZG","aes_key":"mAP6izUBUhBxIkakH2yB/TplhRz1OQV5Fp6HQmhywns=","files":[]},{"name":"aaaaa","password":"$2a$10$nDCEDVwbNO/YDQ4qdRcxfuES4.aboluLzWouXXsk6vDoaWocv516W","aes_key":"kYtwHy9qJBg30WS6axWTFGVE0Ge5kpYiJJlC+COIEI4=","files":[{"name":"README.md","fileNameSalt":"4m84wYD79s9FoFq7Tqjzow==","size":17,"iv":"8EaBv4WGO++knjUbLXSFUA==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file +{"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":"5rB5fhj09F6ukJPRoJgTGQ==","size":17,"iv":"hY2yWRgIxB0dRettv/vPJw==","storage":["lightcontainerSB01"]}]},{"name":"aaaaa","password":"$2a$10$nDCEDVwbNO/YDQ4qdRcxfuES4.aboluLzWouXXsk6vDoaWocv516W","aes_key":"kYtwHy9qJBg30WS6axWTFGVE0Ge5kpYiJJlC+COIEI4=","files":[{"name":"main.py","fileNameSalt":"XKxG99LVODAXohvCwvMRww==","size":854,"iv":"G9nULidPTDRxBp+Yp+IaCQ==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file