Avancée majeur, nouvelle structure du projet
This commit is contained in:
parent
c16185994e
commit
306a10c441
@ -4,5 +4,5 @@
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK" />
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK" />
|
||||
</project>
|
57
app/src/main/java/lightcontainer/domains/ClientHandler.java
Normal file
57
app/src/main/java/lightcontainer/domains/ClientHandler.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package lightcontainer.domains;
|
||||
|
||||
public class FileFrontEnd {
|
||||
|
||||
}
|
58
app/src/main/java/lightcontainer/domains/Server.java
Normal file
58
app/src/main/java/lightcontainer/domains/Server.java
Normal file
@ -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) { }
|
||||
}
|
||||
}
|
||||
}
|
37
app/src/main/java/lightcontainer/domains/StoreProcessor.java
Normal file
37
app/src/main/java/lightcontainer/domains/StoreProcessor.java
Normal file
@ -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 <j.nihart@student.helmo.be>
|
||||
*/
|
||||
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() {
|
||||
|
||||
}
|
||||
}
|
17
app/src/main/java/lightcontainer/domains/Task.java
Normal file
17
app/src/main/java/lightcontainer/domains/Task.java
Normal file
@ -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() {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package lightcontainer.enumerations;
|
||||
|
||||
public enum TaskStatus {
|
||||
PENDING,
|
||||
PROCESSING,
|
||||
ERROR,
|
||||
SUCCESS
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package lightcontainer.enumerations;
|
||||
|
||||
public enum TaskType {
|
||||
SEND,
|
||||
RECEIVE,
|
||||
DELETE
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package lightcontainer.interfaces;
|
||||
|
||||
public interface ClientHandlerFFE {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package lightcontainer.interfaces;
|
||||
|
||||
public interface StoreProcessorFFE {
|
||||
}
|
@ -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<ClientHandler> handlers = new ArrayList<>();
|
||||
|
||||
// Constructor
|
||||
public ClientHandlerRepository() {
|
||||
|
||||
}
|
||||
|
||||
public void addClient(ClientHandler client) {
|
||||
this.handlers.add(client);
|
||||
}
|
||||
}
|
@ -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<Task> tasks = new LinkedList<>();
|
||||
|
||||
// Constructor
|
||||
public FileFrontEnd() {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package lightcontainer.repository;
|
||||
|
||||
public class StoreProcessingRepository {
|
||||
|
||||
}
|
37
app/src/main/java/lightcontainer/utils/FileReceiver.java
Normal file
37
app/src/main/java/lightcontainer/utils/FileReceiver.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
35
app/src/main/java/lightcontainer/utils/FileSender.java
Normal file
35
app/src/main/java/lightcontainer/utils/FileSender.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
69
app/src/main/java/lightcontainer/utils/NetChooser.java
Normal file
69
app/src/main/java/lightcontainer/utils/NetChooser.java
Normal file
@ -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<NetworkInterface> 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<NetworkInterface> discoveredInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
while (discoveredInterfaces.hasMoreElements()) {
|
||||
NetworkInterface currentInterface = discoveredInterfaces.nextElement();
|
||||
Enumeration<InetAddress> 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user