From e7b4d7a5c37e0c2fe1b391ae84a3a94a5688be2d Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 12 Jan 2020 22:04:49 +0100 Subject: [PATCH] Implement protocol TODO : send method --- src/exception/ProtocolError.java | 2 + src/server/Server.java | 31 +++++- src/server/ServerManagement.java | 34 ------- src/server/ServerManagementUDP.java | 151 ++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+), 36 deletions(-) create mode 100644 src/exception/ProtocolError.java delete mode 100644 src/server/ServerManagement.java create mode 100644 src/server/ServerManagementUDP.java diff --git a/src/exception/ProtocolError.java b/src/exception/ProtocolError.java new file mode 100644 index 0000000..4b86a8c --- /dev/null +++ b/src/exception/ProtocolError.java @@ -0,0 +1,2 @@ +package exception; +public class ProtocolError extends Exception {} diff --git a/src/server/Server.java b/src/server/Server.java index e3b1757..4c6c533 100644 --- a/src/server/Server.java +++ b/src/server/Server.java @@ -1,12 +1,39 @@ package Server; +import java.io.File; +import server.ServerManagementUDP; public class Server { - + private int port; + private String directory; public Server() { - + port = 40000; + String d; + /* Follow XDG Base Directory Specification + * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html + */ + if (System.getProperty("os.name").equals("Linux")) { + d = System.getenv().get("XDG_DATA_HOME"); + if (d == null || f.equals("")) { + d = System.getenv().get("HOME"); + if (d != null && (!f.equals(""))) { + d += "/.local/share"; + } else { + d += "." + } + } + } else { + d = "."; + } + d += "P2P_JAVA_PROJECT_SERVER/"; + // create directory if not already exists + new File(d).mkdirs(); } public static void main(String [] args) { Server s = new Server("server"); + ServerManagementUDP sm = new ServerManagementUDP(s.directory, s.port); + Thread t = new Thread(sm); + t.setName("server P2P-JAVA-PROJECT"); + t.start(); } } diff --git a/src/server/ServerManagement.java b/src/server/ServerManagement.java deleted file mode 100644 index 641fe25..0000000 --- a/src/server/ServerManagement.java +++ /dev/null @@ -1,34 +0,0 @@ -package server; -import exception.NotFound; -import java.util.Vector; - -public class ServerManagement { - - private Vector fileList; - private String baseDirectory; - - public ServerManagement(String baseDirectory) { - this.baseDirectory = baseDirectory; - fileList = new Vector; - initFileList(); - } - - private void initFileList() { - File folder = new File(baseDirectory); - File[] files = folder.listFiles(); - /* Add non-recursively files's names to fileList */ - for (int i = 0; i < files.length; i++) { - if (files[i].isFile()) { - fileList.add(files[i].getName()); - } - } - - } - - public void upload(String filename) throws NotFound { - } - - public String listDirectory() { - - } -} diff --git a/src/server/ServerManagementUDP.java b/src/server/ServerManagementUDP.java new file mode 100644 index 0000000..b4a8d00 --- /dev/null +++ b/src/server/ServerManagementUDP.java @@ -0,0 +1,151 @@ +package server; +import java.util.Vector; +import java.io.File; +import java.io.IOException; +import java.net.Datagram.Packet; +import java.net.Datagram.Socket; +import exception.ProtocolError; +import exception.NotFound; + + +/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ +public class ServerManagementUDP implements Runnable { + + private Vector fileList; + private String baseDirectory; + private int UDPPort; + private final String protocolID = "P2P-JAVA-PROJECT VERSION 1.0"; + + /** Constructor for UDP implementation, with baseDirectory and UDPPort parameters. + * @param baseDirectory the root directory where files are stored + * @param UDPPort the server will listen on this port + */ + public ServerManagementUDP(String baseDirectory, int UDPPort) { + this.baseDirectory = baseDirectory; + this.UDPPort = UDPPort; + fileList = new Vector; + initFileList(); + } + + /** Implementation of runnable. This methods allows to run the server. + */ + public void run() { + try { + // socket creation on port UDPPort + DatagramSocket socket = new DatagramSocket(UDPPort); + // buffer to receive UDP Datagram + final byte[] buffer = new byte[1024]; + while(true) { + // java object to receive Datagram + DatagramPacket dgram = new DatagramPacket(tampon, tampon.length); + // wait and receive datagram + socket.receive(dgram); + // extract data + String str = new String(dgram.getData(), 0, dgram.getLength()); + // process request + str = gestionProtocole.processRequest(str); + dgram.setData(chaine.getBytes()); + dgram.setLength(chaine.getBytes().length); + // send response + socket.send(dgram); + } + } catch (Exception e) { + // TODO: treat exceptions + } + } + + /** Process the request received. + * @param request the request received + * @return data to be send as response + */ + String processRequest(String request) { + String res = protocolID + '\n'; + String formattedRequest[] = request.split('\n'); + try { + try { + checkProtocolID(formattedRequest[0]); + switch (formattedRequest[1]) { + "LIST": + res += sendFileList(); + break; + "DOWNLOAD": + res += upload(formattedRequest[2]); + break; + default: + throw ProtocolError; + } + } catch (java.lang.ArrayIndexOutOfBoundsException e) { + throw ProtocolError; + } + } catch (ProtocolError e) { + // wrong version or wrong implementation + res += sendProtocolError(); + } catch (NotFound e) { + res += sendNotFound(); + } + } + + /** Initialize local list of all files allowed to be shared. + */ + private void initFileList() { + File folder = new File(baseDirectory); + File[] files = folder.listFiles(); + /* Add non-recursively files's names to fileList */ + for (int i = 0; i < files.length; i++) { + if (files[i].isFile()) { + fileList.add(files[i].getName()); + } + } + } + + /** Check server's protocol identifier matches message's protocol identifier. + * Throws a ProtocolError if mismatched. + * @param msgProtocolID part of the request containing protocol identifier + * @throws ProtocolError + */ + private void checkProtocolID(String msgProtocolID) throws ProtocolError { + if (protocolID != msgProtocolID) { + throw ProtocolError; + } + } + + /** Prepare the data to be send if a file is requested + * @param filename name of the file to be send + * @return data to be send + * @throws NotFound + */ + private String upload(String filename) throws NotFound { + // TODO + } + + /** Prepare the data to be send if file list is requested + * @return data to be send + */ + private String sendFileList() { + String res = "LIST\n"; + for (String f : fileList) { + res += (f + '\n'); + } + return res + '\n'; + } + + /** Prepare data to be send if protocol error is detected + * @return data to be send + */ + private String sendProtocolError() { + return "PROTOCOL ERROR\n" + } + + /** Prepare data to be send if file is not found + * @return data to be send + */ + private String sendNotFound() { + return "NOT FOUND\n" + } + +}