diff --git a/.idea/misc.xml b/.idea/misc.xml index 3e79c5f..5821b2f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,5 +4,5 @@ - + \ No newline at end of file diff --git a/app/src/main/java/lightcontainer/domains/ClientHandler.java b/app/src/main/java/lightcontainer/domains/ClientHandler.java new file mode 100644 index 0000000..6c968b9 --- /dev/null +++ b/app/src/main/java/lightcontainer/domains/ClientHandler.java @@ -0,0 +1,57 @@ +package lightcontainer.domains; + +import lightcontainer.interfaces.ClientHandlerFFE; + +import java.io.*; +import java.net.Socket; +import java.nio.charset.StandardCharsets; + +/**TODO + * Connexion avec le client. (type server) + */ +public class ClientHandler implements Runnable { + // Variables + private ClientHandlerFFE fileFrontEnd; + private final Socket client; + private BufferedReader reader; + private PrintWriter writer; + + // Constructor + public ClientHandler(Socket client, ClientHandlerFFE ffe) { + this.fileFrontEnd = ffe; + this.client = client; + initClient(); + } + + /** + * Initializes socket input/output + */ + private void initClient() { + try { + this.reader = new BufferedReader(new InputStreamReader( + this.client.getInputStream(), + StandardCharsets.UTF_8 + )); + this.writer = new PrintWriter(new OutputStreamWriter( + this.client.getOutputStream(), + StandardCharsets.UTF_8 + ), true); + } catch (IOException e) { + e.printStackTrace(); + } + } + + // Thread Function + @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); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/app/src/main/java/lightcontainer/domains/FileFrontEnd.java b/app/src/main/java/lightcontainer/domains/FileFrontEnd.java deleted file mode 100644 index 6916615..0000000 --- a/app/src/main/java/lightcontainer/domains/FileFrontEnd.java +++ /dev/null @@ -1,5 +0,0 @@ -package lightcontainer.domains; - -public class FileFrontEnd { - -} diff --git a/app/src/main/java/lightcontainer/domains/Server.java b/app/src/main/java/lightcontainer/domains/Server.java new file mode 100644 index 0000000..5c32cd1 --- /dev/null +++ b/app/src/main/java/lightcontainer/domains/Server.java @@ -0,0 +1,58 @@ +package lightcontainer.domains; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + +public class Server { + // Variables + private ServerSocket server; + private final int server_port; + private boolean server_run; + + // Constructor + public Server(int port) { + this.server_port = port; + this.server_run = false; + } + + /** + * Initializes the server and starts it on the previously selected port. + * + * @since 1.0 + * + * @see Thread#start() + * @see ClientHandler + */ + public void start() { + try { + this.server = new ServerSocket(this.server_port); + while (server_run) { + // Accepting connection requests (blocking) + Socket client = this.server.accept(); + // Create a new Handler client by passing these dependencies to it + ClientHandler clientHandler = new ClientHandler(client, null); // TODO passer FileFrontEnd ou faire ca dans le repository ?! + // Add the client handler to its repository (clienthandlerrepository) + // this.repository.add(clientHandler); TODO REPOSITORY + // Start the thread + (new Thread(clientHandler)).start(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Stops the server and terminates the new connection. + * + * @since 1.0 + */ + public void stop() { + if (this.server_run) { + try { + this.server_run = false; + this.server.close(); + } catch (IOException ignored) { } + } + } +} diff --git a/app/src/main/java/lightcontainer/domains/StoreProcessor.java b/app/src/main/java/lightcontainer/domains/StoreProcessor.java new file mode 100644 index 0000000..8861929 --- /dev/null +++ b/app/src/main/java/lightcontainer/domains/StoreProcessor.java @@ -0,0 +1,37 @@ +package lightcontainer.domains; + +import lightcontainer.interfaces.StoreProcessorFFE; + +import java.net.Socket; + +/** + * StoreProcessor + * + * Class communicating with the storebackend and + * processing the tasks in the FileFrontEnd + * + * @version 1.0 + * @since 1.0 + * + * @see Runnable + * @author Jérémi NIHART + */ +public class StoreProcessor implements Runnable { + // Variables + private StoreProcessorFFE fileFrontEnd; + + // Constructor + public StoreProcessor(Socket socket, StoreProcessorFFE fileFrontEnd) { + this.fileFrontEnd = fileFrontEnd; + } + + /** + * Start the dialogue with the storebackend. + * + * @since 1.0 + */ + @Override + public void run() { + + } +} diff --git a/app/src/main/java/lightcontainer/domains/Task.java b/app/src/main/java/lightcontainer/domains/Task.java new file mode 100644 index 0000000..6cd8fc0 --- /dev/null +++ b/app/src/main/java/lightcontainer/domains/Task.java @@ -0,0 +1,17 @@ +package lightcontainer.domains; + +import lightcontainer.enumerations.TaskStatus; +import lightcontainer.enumerations.TaskType; + +public class Task { + // Variables + private TaskType type; + private TaskStatus status; + private String command; + private String client; + private String store; + + public Task() { + + } +} diff --git a/app/src/main/java/lightcontainer/enumerations/TaskStatus.java b/app/src/main/java/lightcontainer/enumerations/TaskStatus.java new file mode 100644 index 0000000..64261f9 --- /dev/null +++ b/app/src/main/java/lightcontainer/enumerations/TaskStatus.java @@ -0,0 +1,8 @@ +package lightcontainer.enumerations; + +public enum TaskStatus { + PENDING, + PROCESSING, + ERROR, + SUCCESS +} diff --git a/app/src/main/java/lightcontainer/enumerations/TaskType.java b/app/src/main/java/lightcontainer/enumerations/TaskType.java new file mode 100644 index 0000000..3d4054c --- /dev/null +++ b/app/src/main/java/lightcontainer/enumerations/TaskType.java @@ -0,0 +1,7 @@ +package lightcontainer.enumerations; + +public enum TaskType { + SEND, + RECEIVE, + DELETE +} diff --git a/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java b/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java new file mode 100644 index 0000000..a1783f7 --- /dev/null +++ b/app/src/main/java/lightcontainer/interfaces/ClientHandlerFFE.java @@ -0,0 +1,4 @@ +package lightcontainer.interfaces; + +public interface ClientHandlerFFE { +} diff --git a/app/src/main/java/lightcontainer/interfaces/StoreProcessorFFE.java b/app/src/main/java/lightcontainer/interfaces/StoreProcessorFFE.java new file mode 100644 index 0000000..e6fde95 --- /dev/null +++ b/app/src/main/java/lightcontainer/interfaces/StoreProcessorFFE.java @@ -0,0 +1,4 @@ +package lightcontainer.interfaces; + +public interface StoreProcessorFFE { +} diff --git a/app/src/main/java/lightcontainer/repository/ClientHandlerRepository.java b/app/src/main/java/lightcontainer/repository/ClientHandlerRepository.java new file mode 100644 index 0000000..66f183b --- /dev/null +++ b/app/src/main/java/lightcontainer/repository/ClientHandlerRepository.java @@ -0,0 +1,20 @@ +package lightcontainer.repository; + +import lightcontainer.domains.ClientHandler; + +import java.util.ArrayList; +import java.util.List; + +public class ClientHandlerRepository { + // Variable + private final List handlers = new ArrayList<>(); + + // Constructor + public ClientHandlerRepository() { + + } + + public void addClient(ClientHandler client) { + this.handlers.add(client); + } +} diff --git a/app/src/main/java/lightcontainer/repository/FileFrontEnd.java b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java new file mode 100644 index 0000000..1f5b17d --- /dev/null +++ b/app/src/main/java/lightcontainer/repository/FileFrontEnd.java @@ -0,0 +1,18 @@ +package lightcontainer.repository; + +import lightcontainer.domains.Task; +import lightcontainer.interfaces.ClientHandlerFFE; +import lightcontainer.interfaces.StoreProcessorFFE; + +import java.util.Deque; +import java.util.LinkedList; + +public class FileFrontEnd implements ClientHandlerFFE, StoreProcessorFFE { + // Variables + private Deque tasks = new LinkedList<>(); + + // Constructor + public FileFrontEnd() { + + } +} diff --git a/app/src/main/java/lightcontainer/repository/StoreProcessingRepository.java b/app/src/main/java/lightcontainer/repository/StoreProcessingRepository.java new file mode 100644 index 0000000..39912ef --- /dev/null +++ b/app/src/main/java/lightcontainer/repository/StoreProcessingRepository.java @@ -0,0 +1,5 @@ +package lightcontainer.repository; + +public class StoreProcessingRepository { + +} diff --git a/app/src/main/java/lightcontainer/utils/FileReceiver.java b/app/src/main/java/lightcontainer/utils/FileReceiver.java new file mode 100644 index 0000000..c59452f --- /dev/null +++ b/app/src/main/java/lightcontainer/utils/FileReceiver.java @@ -0,0 +1,37 @@ +package lightcontainer.utils; + +import java.io.BufferedOutputStream; +import java.io.FileOutputStream; +import java.io.InputStream; + +public class FileReceiver { + private static final int DEFAULT_BUFFER = 8000; + private String path; + + public FileReceiver(String path) { this.path = path; } + + public boolean receiveFile(InputStream input, String fileName, long fileSize) { + int bytesReceived = 0; + BufferedOutputStream bosFile = null; + + try { + byte[] buffer = new byte[DEFAULT_BUFFER]; + bosFile = new BufferedOutputStream(new FileOutputStream(String.format("%s/%s", path, fileName))); + long currentOffset = 0; + + while((currentOffset < fileSize) && ((bytesReceived = input.read(buffer)) > 0)) { + bosFile.write(buffer, 0, bytesReceived); + currentOffset += bytesReceived; + } + bosFile.flush(); + bosFile.close(); + + return true; + } catch(Exception ex) { + ex.printStackTrace(); + if(bosFile != null) { try { bosFile.close(); } catch(Exception e) {} } + return false; + } + } + +} diff --git a/app/src/main/java/lightcontainer/utils/FileSender.java b/app/src/main/java/lightcontainer/utils/FileSender.java new file mode 100644 index 0000000..92f93f3 --- /dev/null +++ b/app/src/main/java/lightcontainer/utils/FileSender.java @@ -0,0 +1,35 @@ +package lightcontainer.utils; + +import java.io.*; + +public class FileSender { + private static final int DEFAULT_BUFFER=8000; + private String path; + + public FileSender(String path) { this.path = path; } + + public boolean sendFile(String filename, OutputStream out) { + BufferedInputStream bisFile = null; + int bytesReaded = 0; + + try { + File f = new File(String.format("%s/%s", path, filename)); + long fileSize = f.length(); + if(f.exists()) { + byte[] buffer = new byte[DEFAULT_BUFFER]; + bisFile = new BufferedInputStream(new FileInputStream(f)); + long currentOffset = 0; + while((currentOffset < fileSize) && (bytesReaded = bisFile.read(buffer)) > 0) { + out.write(buffer, 0, bytesReaded); out.flush(); + currentOffset+= bytesReaded; + } + bisFile.close(); + return true; + } else + return false; + } catch(IOException ex) { + ex.printStackTrace(); + return false; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/lightcontainer/utils/NetChooser.java b/app/src/main/java/lightcontainer/utils/NetChooser.java new file mode 100644 index 0000000..f9ecd98 --- /dev/null +++ b/app/src/main/java/lightcontainer/utils/NetChooser.java @@ -0,0 +1,69 @@ +package lightcontainer.utils; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.Scanner; + +public class NetChooser { + private List interfaces; + + public NetChooser() { + loadInterfaces(); + Scanner console = new Scanner(System.in); + String[] allInterfaceNames = getInterfaces(); + for(int index=0; index < allInterfaceNames.length; ++index) { + System.out.printf("%d. %s\n", index, allInterfaceNames[index]); + } + System.out.printf("Select your interface :"); + NetworkInterface selected = getInterfacesByIndex(console.nextInt()); + System.out.printf("Selected interface: %s\n", selected.getDisplayName()); + + } + + private void loadInterfaces() { + try { + interfaces = new ArrayList<>(); + Enumeration discoveredInterfaces = NetworkInterface.getNetworkInterfaces(); + while (discoveredInterfaces.hasMoreElements()) { + NetworkInterface currentInterface = discoveredInterfaces.nextElement(); + Enumeration inetAddresses = currentInterface.getInetAddresses(); + int ipCount = 0; + while(inetAddresses.hasMoreElements()) { + InetAddress currentAddress = inetAddresses.nextElement(); + ipCount++; + } + if(ipCount > 0) + interfaces.add(currentInterface); + } + } catch(SocketException ex) { + ex.printStackTrace(); + } + + } + + public NetworkInterface getInterfacesByIndex(int i) { + if(i >= 0) + return interfaces.get(i); + else + return null; + } + + public String[] getInterfaces() { + if(interfaces.size() > 0) { + String[] result = new String[interfaces.size()]; + for (int i = 0; i < interfaces.size(); ++i) { + result[i] = interfaces.get(i).getDisplayName(); + } + return result; + } else + return null; + } + + public static void main(String[] args) { + new NetChooser(); + } +} \ No newline at end of file