From ebb5ee9b15b61939381edeef08a754736b8a686c Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 15 Mar 2022 19:18:35 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20suppression=20d'un=20SBE=20de=20son=20r?= =?UTF-8?q?epository=20lorsqu'il=20est=20d=C3=A9connect=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domains/client/StoreProcessor.java | 39 +++++++++++----- .../server/MulticastServerListener.java | 45 +++++++++++-------- .../interfaces/MulticastSPR.java | 11 +++++ .../interfaces/StoreProcessorFFE.java | 6 +++ .../protocol/ProtocolWriter.java | 3 +- .../protocol/rules/writer/GetFileOkRule.java | 4 +- .../protocol/rules/writer/SendfileRule.java | 3 +- .../repository/FileFrontEnd.java | 6 ++- .../repository/StoreProcessorRepository.java | 28 ++++++++++++ .../java/lightcontainer/utils/FileSender.java | 10 ++--- .../main/java/lightcontainer/utils/Log.java | 10 +++++ app/src/main/resources/appdata.json | 2 +- 12 files changed, 124 insertions(+), 43 deletions(-) create mode 100644 app/src/main/java/lightcontainer/utils/Log.java diff --git a/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java b/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java index cc4fc98..d7158f6 100644 --- a/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java +++ b/app/src/main/java/lightcontainer/domains/client/StoreProcessor.java @@ -8,6 +8,7 @@ import lightcontainer.protocol.ProtocolWriter; import java.io.*; import java.net.Socket; +import java.net.SocketException; import java.nio.charset.StandardCharsets; import java.util.Objects; @@ -82,18 +83,21 @@ public class StoreProcessor extends Thread implements AutoCloseable { this.client_run = true; while (this.client_run) { try { - if (!store.isConnected()) { - // TODO : Gérer déconnection - break; - } waitAction(); System.out.println("[SBE] Envoie commande : " + protocolResult.getCommand()); // Request this.writer.write(protocolResult.getCommand()); this.writer.flush(); - protocolResult.write(this.store.getOutputStream()); + try { + protocolResult.write(this.store.getOutputStream()); + } catch (IOException writeException) { // Si SBE fermé + System.out.println("STOPPER"); + // Envoie au client que la requête n'a pu être traitée + alertAvalaible(null); + break; + } // Response String responseCommand = this.reader.readLine(); @@ -116,10 +120,21 @@ public class StoreProcessor extends Thread implements AutoCloseable { alertAvalaible(null); } - } catch (IOException ignore) { - // TODO : Gérer déconnection + } catch (IOException exception) { + System.out.println("[ERROR] Problem with SBE (" + domain + ") : " + exception.getMessage()); + this.close(); } } + + // Fermeture du SBE + try { + this.reader.close(); + this.writer.close(); + this.store.close(); + this.fileFrontEnd.onStoreDisconnect(this.domain); + } catch (IOException ioException) { + System.out.println("[ERROR] Error while closing SBE (" + domain + ") : " + ioException.getMessage()); + } } /** @@ -140,6 +155,9 @@ public class StoreProcessor extends Thread implements AutoCloseable { private void alertAvalaible(ProtocolWriter.ProtocolResult responseCommand) { synchronized (this) { this.protocolResult = null; + if (responseCommand == null) { + this.close(); + } fileFrontEnd.onStoreAvailable(this, responseCommand); } } @@ -164,11 +182,8 @@ public class StoreProcessor extends Thread implements AutoCloseable { @Override public void close() { if (this.client_run) { - try { - this.client_run = false; - this.store.close(); - // TODO : Gérer déconnection (enlever du repo et prévenir client et FileFrontEnd) - } catch (IOException ignored) { } + this.client_run = false; + // TODO : Gérer déconnection (enlever du repo et prévenir client et FileFrontEnd) } } diff --git a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java index c3d8b2d..ba40f10 100644 --- a/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java +++ b/app/src/main/java/lightcontainer/domains/server/MulticastServerListener.java @@ -10,6 +10,7 @@ import lightcontainer.utils.NetChooser; import java.io.IOException; import java.net.*; +import java.util.logging.Logger; /** * StoreMulticastRunnable @@ -69,27 +70,32 @@ public class MulticastServerListener implements Runnable { while (true) { // Read the packet received and build a string of characters this.listener.receive(packet); - String data = new String(packet.getData(), 0, packet.getLength()); - // Create a new StoreBacked (try used in the case of an error to maintain the listening loop) - try { - // TODO Récupérer le port du message du packet et le setup (add description of the line). - HelloRule.Result readerResult = protocolRep.executeReader(null, data); - System.out.printf("Nouveau SBE : Domain=%s | Port=%d\n", readerResult.getDomain(), readerResult.getPort()); - if (!this.repository.hasDomain(readerResult.getDomain())){ - Socket socket = new Socket(packet.getAddress(), readerResult.getPort()); - - // Create the store processor - StoreProcessor storeProcessor = new StoreProcessor(socket, readerResult.getDomain(), ffe, protocolRep); // TODO : Voir comment on procède get via repo ou ici ?! - - // Add the store processor to its repository - this.repository.addStore(storeProcessor); - } - } catch (IOException ignore) { - ignore.printStackTrace(); - } + onNewSbe(packet); } - } catch (Exception ignore) { + } catch (IOException ioException) { + System.out.println("[ERREUR] Multicast server can't start : " + ioException.getMessage()); + } + } + + private void onNewSbe(DatagramPacket packet) { + try { + String data = new String(packet.getData(), 0, packet.getLength()); + HelloRule.Result readerResult = protocolRep.executeReader(null, data); + + System.out.printf("Nouveau SBE : Domain=%s | Port=%d\n", readerResult.getDomain(), readerResult.getPort()); + + if (!this.repository.hasDomain(readerResult.getDomain())){ + Socket socket = new Socket(packet.getAddress(), readerResult.getPort()); + + // Create the store processor + StoreProcessor storeProcessor = new StoreProcessor(socket, readerResult.getDomain(), ffe, protocolRep); // TODO : Voir comment on procède get via repo ou ici ?! + + // Add the store processor to its repository + this.repository.addStore(storeProcessor); + } + } catch (IOException | ClassCastException exception) { + System.out.println("[ERREUR] Une SBE essaye de se connecter avec une mauvaise configuration : " + exception.getMessage()); } } @@ -117,6 +123,7 @@ public class MulticastServerListener implements Runnable { * @since 1.0 */ public void stop() { + repository.disconnectDomains(); this.listener.close(); } } diff --git a/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java b/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java index 385d556..5727721 100644 --- a/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java +++ b/app/src/main/java/lightcontainer/interfaces/MulticastSPR.java @@ -28,4 +28,15 @@ public interface MulticastSPR { boolean hasDomain(String domain); int domainCount(); + + /** + * Déconnecte tous les SBE + */ + void disconnectDomains(); + + /** + * Permet de déconnecter un SBE + * @param domain Le domaine du SBE à déconnecter + */ + void closeStore(String domain); } diff --git a/app/src/main/java/lightcontainer/interfaces/StoreProcessorFFE.java b/app/src/main/java/lightcontainer/interfaces/StoreProcessorFFE.java index 26be68b..15e3561 100644 --- a/app/src/main/java/lightcontainer/interfaces/StoreProcessorFFE.java +++ b/app/src/main/java/lightcontainer/interfaces/StoreProcessorFFE.java @@ -14,4 +14,10 @@ public interface StoreProcessorFFE { * @param responseCommand */ void onStoreAvailable(StoreProcessor store, ProtocolWriter.ProtocolResult response); + + /** + * Permet de déconnecter un SBE + * @param domain Le domaine du SBE à déconnecter + */ + void onStoreDisconnect(String domain); } diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java index cdcaa56..6449229 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolWriter.java @@ -2,6 +2,7 @@ package lightcontainer.protocol; import lightcontainer.domains.client.Context; +import java.io.IOException; import java.io.OutputStream; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -51,7 +52,7 @@ public abstract class ProtocolWriter { * Redéfinissez cette méthode pour l'utiliser. * @param writer Buffer à remplir qui sera envoyer via le réseau */ - public void write(OutputStream writer) {} + public void write(OutputStream writer) throws IOException {} /** * Accesseur au contexte courant sur lequel opère la commande diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java index 08390bb..5b258f0 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java @@ -4,6 +4,7 @@ import lightcontainer.domains.client.Context; import lightcontainer.protocol.ProtocolWriter; import lightcontainer.utils.FileSender; +import java.io.IOException; import java.io.OutputStream; public class GetFileOkRule extends ProtocolWriter { @@ -40,8 +41,7 @@ public class GetFileOkRule extends ProtocolWriter { * @param writer Buffer à remplir qui sera envoyer via le réseau */ @Override - public void write(OutputStream writer) { - super.write(writer); + public void write(OutputStream writer) throws IOException { System.out.printf("Sauvegarde du fichier : %s %d\n", this.filename, this.filesize); FileSender fileSender = new FileSender(storagePath); diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java index 36eabff..43616be 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java @@ -4,6 +4,7 @@ import lightcontainer.domains.client.Context; import lightcontainer.protocol.ProtocolWriter; import lightcontainer.utils.FileSender; +import java.io.IOException; import java.io.OutputStream; /** @@ -40,7 +41,7 @@ public class SendfileRule extends ProtocolWriter { } @Override - public void write(OutputStream writer) { + public void write(OutputStream writer) throws IOException { super.write(writer); System.out.println("Envoie du fichier au SBE"); diff --git a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java index fa1a129..acde83e 100644 --- a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java +++ b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java @@ -63,6 +63,11 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { assignOtherTask(store); } + @Override + public void onStoreDisconnect(String domain) { + this.storeRepository.closeStore(domain); + } + private void assignOtherTask(StoreProcessor store) { Iterator it = tasks.iterator(); @@ -86,7 +91,6 @@ public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { @Override public boolean canExecuteCommand(String 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 f5219aa..163ee96 100644 --- a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java +++ b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java @@ -6,6 +6,7 @@ import lightcontainer.domains.server.MulticastServerListener; import lightcontainer.interfaces.MulticastSPR; import java.util.HashSet; +import java.util.Iterator; import java.util.Set; // TODO : C'est genre un ClientHandlerManager quoi hein, normal qu'il fasse blinder de chose ;) /** @@ -97,6 +98,33 @@ public class StoreProcessorRepository implements AutoCloseable, MulticastSPR { return handlers.size(); } + @Override + public void disconnectDomains() { + for (StoreProcessor handler : handlers) { + handler.close(); + } + } + + /** + * Permet de déconnecter un SBE + * + * @param domain Le domaine du SBE à déconnecter + */ + @Override + public void closeStore(String domain) { + Iterator it = this.handlers.iterator(); + + System.out.println("1 Nombre de SBE : " + handlers.size()); + while (it.hasNext()) { + StoreProcessor storeProcessor = it.next(); + if (storeProcessor.getDomain().equals(domain)) { + storeProcessor.close(); + it.remove(); + return; + } + } + } + /** * AutoClosable Function * Closes all StoreProcessor stored in this repository and deallocates all resources. diff --git a/app/src/main/java/lightcontainer/utils/FileSender.java b/app/src/main/java/lightcontainer/utils/FileSender.java index bc58fc5..67cbee9 100644 --- a/app/src/main/java/lightcontainer/utils/FileSender.java +++ b/app/src/main/java/lightcontainer/utils/FileSender.java @@ -8,7 +8,7 @@ public class FileSender { public FileSender(String path) { this.path = path; } - public boolean sendFile(String filename, OutputStream out, int fileSize, String aesKey, String iv) { + public boolean sendFile(String filename, OutputStream out, int fileSize, String aesKey, String iv) throws IOException { BufferedInputStream bisFile; System.out.printf("Envoie fichier : %s - %s - %s \n", filename, aesKey, iv); try { @@ -23,12 +23,11 @@ public class FileSender { } else return false; } catch(IOException | AES_GCM.AesGcmException ex) { - ex.printStackTrace(); - return false; + throw new IOException(); } } - public boolean sendFile(String filename, OutputStream out) { + public boolean sendFile(String filename, OutputStream out) throws IOException { BufferedInputStream bisFile; int bytesReaded = 0; @@ -48,8 +47,7 @@ public class FileSender { } else return false; } catch(IOException ex) { - ex.printStackTrace(); - return false; + throw ex; } } diff --git a/app/src/main/java/lightcontainer/utils/Log.java b/app/src/main/java/lightcontainer/utils/Log.java new file mode 100644 index 0000000..0aa9e26 --- /dev/null +++ b/app/src/main/java/lightcontainer/utils/Log.java @@ -0,0 +1,10 @@ +package lightcontainer.utils; + +import java.util.logging.Level; +import java.util.logging.LogManager; +import java.util.logging.Logger; + +public class Log { + + +} diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index 2baebe3..1b71617 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":"","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"$2a$10$nDCEDVwbNO/YDQ4qdRcxfuES4.aboluLzWouXXsk6vDoaWocv516W","aes_key":"kYtwHy9qJBg30WS6axWTFGVE0Ge5kpYiJJlC+COIEI4=","files":[]}]} \ No newline at end of file +{"unicast_port":8000,"multicast_ip":"224.66.66.1","multicast_port":15502,"network_interface":"","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"$2a$10$nDCEDVwbNO/YDQ4qdRcxfuES4.aboluLzWouXXsk6vDoaWocv516W","aes_key":"kYtwHy9qJBg30WS6axWTFGVE0Ge5kpYiJJlC+COIEI4=","files":[{"name":"README.md","fileNameSalt":"QnjgVrrW2KADUYUdeD/KcQ==","size":17,"iv":"E5JtY/JrH1B447F/my8Hkg==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file