From 083586682f162a3c4469a41e6c297c63d0a927b0 Mon Sep 17 00:00:00 2001 From: EndMove Date: Tue, 22 Feb 2022 12:32:03 +0100 Subject: [PATCH] =?UTF-8?q?Mise=20en=20place=20d'un=20syst=C3=A8me=20pour?= =?UTF-8?q?=20autoclose=20le=20ClientHandler=20et=20StoreProcessor=20+=20q?= =?UTF-8?q?uelques=20corrections=20+=20avanc=C3=A9e=20mineures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lightcontainer/domains/ClientHandler.java | 44 ++++++++++++-- .../domains/StoreMulticastRunnable.java | 1 + .../domains/StoreProcessor.java | 60 +++++++++++++++++-- .../repository/FileFrontEnd.java | 2 + .../repository/StoreProcessingRepository.java | 5 -- .../repository/StoreProcessorRepository.java | 5 ++ 6 files changed, 102 insertions(+), 15 deletions(-) delete mode 100644 app/src/main/java/lightcontainer/repository/StoreProcessingRepository.java create mode 100644 app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java diff --git a/app/src/main/java/lightcontainer/domains/ClientHandler.java b/app/src/main/java/lightcontainer/domains/ClientHandler.java index 6c968b9..8c1fb15 100644 --- a/app/src/main/java/lightcontainer/domains/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/ClientHandler.java @@ -6,13 +6,24 @@ import java.io.*; import java.net.Socket; import java.nio.charset.StandardCharsets; -/**TODO - * Connexion avec le client. (type server) +/** + * ClientHandler + * + * Class communicating with the client, and + * intercepting and sending files to the client. + * + * @version 1.0 + * @since 1.0 + * + * @see Runnable + * @see AutoCloseable + * @author Jérémi NIHART */ -public class ClientHandler implements Runnable { +public class ClientHandler implements Runnable, AutoCloseable { // Variables private ClientHandlerFFE fileFrontEnd; private final Socket client; + private BufferedReader reader; private PrintWriter writer; @@ -24,7 +35,12 @@ public class ClientHandler implements Runnable { } /** - * Initializes socket input/output + * Initialise the Client's Reader and Writer. + * + * @since 1.0 + * + * @see BufferedReader + * @see PrintWriter */ private void initClient() { try { @@ -41,17 +57,33 @@ public class ClientHandler implements Runnable { } } - // Thread Function + /** + * Thread Function + * Start the dialogue with the client. + * + * @since 1.0 + */ @Override public void run() { while (true) { try { String command = this.reader.readLine(); // TODO gestion de la réception de commandes, fichier, ... - if (command != null) System.out.println(command); + if (command != null) System.out.println("Client: " + command); } catch (IOException e) { e.printStackTrace(); } } } + + /** + * AutoClosable Function + * Close the Client thread and resources. + * + * @since 1.0 + */ + @Override + public void close() { + + } } diff --git a/app/src/main/java/lightcontainer/domains/StoreMulticastRunnable.java b/app/src/main/java/lightcontainer/domains/StoreMulticastRunnable.java index f6aede3..835b0dd 100644 --- a/app/src/main/java/lightcontainer/domains/StoreMulticastRunnable.java +++ b/app/src/main/java/lightcontainer/domains/StoreMulticastRunnable.java @@ -52,6 +52,7 @@ public class StoreMulticastRunnable implements Runnable { while(true) { listener.receive(packet); String data = new String(packet.getData(), 0, packet.getLength()); + System.out.println(data); // TODO ajouter un controller et lui signaler qu'il y a un nouveau StoreBackEnd } } catch (Exception ignore) { } diff --git a/app/src/main/java/lightcontainer/domains/StoreProcessor.java b/app/src/main/java/lightcontainer/domains/StoreProcessor.java index 8861929..fb8ea2c 100644 --- a/app/src/main/java/lightcontainer/domains/StoreProcessor.java +++ b/app/src/main/java/lightcontainer/domains/StoreProcessor.java @@ -2,7 +2,9 @@ package lightcontainer.domains; import lightcontainer.interfaces.StoreProcessorFFE; +import java.io.*; import java.net.Socket; +import java.nio.charset.StandardCharsets; /** * StoreProcessor @@ -14,24 +16,74 @@ import java.net.Socket; * @since 1.0 * * @see Runnable + * @see AutoCloseable * @author Jérémi NIHART */ -public class StoreProcessor implements Runnable { +public class StoreProcessor implements Runnable, AutoCloseable { // Variables - private StoreProcessorFFE fileFrontEnd; + private final StoreProcessorFFE fileFrontEnd; + private final Socket store; + + private BufferedReader reader; + private PrintWriter writer; // Constructor - public StoreProcessor(Socket socket, StoreProcessorFFE fileFrontEnd) { - this.fileFrontEnd = fileFrontEnd; + public StoreProcessor(Socket socket, StoreProcessorFFE ffe) { + this.fileFrontEnd = ffe; + this.store = socket; + initStore(); } /** + * Initialise the Store's Reader and Writer. + * + * @since 1.0 + * + * @see BufferedReader + * @see PrintWriter + */ + private void initStore() { + try { + this.reader = new BufferedReader(new InputStreamReader( + this.store.getInputStream(), + StandardCharsets.UTF_8 + )); + this.writer = new PrintWriter(new OutputStreamWriter( + this.store.getOutputStream(), + StandardCharsets.UTF_8 + ), true); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Thread Function * Start the dialogue with the storebackend. * * @since 1.0 */ @Override public void run() { + while (true) { + try { + String command = this.reader.readLine(); + // TODO gestion de la réception de commandes, fichier, ... + if (command != null) System.out.println("StoreBackEnd: " + command); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + /** + * AutoClosable Function + * Close the Storage thread and resources. + * + * @since 1.0 + */ + @Override + public void close() { } } diff --git a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java index 1f5b17d..b4db479 100644 --- a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java +++ b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java @@ -10,6 +10,8 @@ import java.util.LinkedList; public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { // Variables private Deque tasks = new LinkedList<>(); + private ClientHandlerRepository clientRepository; // TODO -> pourquoi pas une interface ? end + private StoreProcessorRepository storeRepository; // TODO -> pourquoi pas une interface ? end // Constructor public FileFrontEnd() { diff --git a/app/src/main/java/lightcontainer/repository/StoreProcessingRepository.java b/app/src/main/java/lightcontainer/repository/StoreProcessingRepository.java deleted file mode 100644 index 39912ef..0000000 --- a/app/src/main/java/lightcontainer/repository/StoreProcessingRepository.java +++ /dev/null @@ -1,5 +0,0 @@ -package lightcontainer.repository; - -public class StoreProcessingRepository { - -} diff --git a/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java new file mode 100644 index 0000000..a330360 --- /dev/null +++ b/app/src/main/java/lightcontainer/repository/StoreProcessorRepository.java @@ -0,0 +1,5 @@ +package lightcontainer.repository; + +public class StoreProcessorRepository { + +}