Mise en place d'un système pour autoclose le ClientHandler et StoreProcessor + quelques corrections + avancée mineures

This commit is contained in:
Jérémi N ‘EndMove’ 2022-02-22 12:32:03 +01:00
parent 306a10c441
commit 083586682f
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
6 changed files with 102 additions and 15 deletions

View File

@ -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 <j.nihart@student.helmo.be>
*/
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() {
}
}

View File

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

View File

@ -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 <j.nihart@student.helmo.be>
*/
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() {
}
}

View File

@ -10,6 +10,8 @@ import java.util.LinkedList;
public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE {
// Variables
private Deque<Task> tasks = new LinkedList<>();
private ClientHandlerRepository clientRepository; // TODO -> pourquoi pas une interface ? end
private StoreProcessorRepository storeRepository; // TODO -> pourquoi pas une interface ? end
// Constructor
public FileFrontEnd() {

View File

@ -1,5 +0,0 @@
package lightcontainer.repository;
public class StoreProcessingRepository {
}

View File

@ -0,0 +1,5 @@
package lightcontainer.repository;
public class StoreProcessorRepository {
}