From 572a75e3ae27fed51d652d4ca0a7ff8c1adf77f1 Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 14 Jan 2020 11:10:11 +0100 Subject: [PATCH] correction syntaxiques de clientP2P --- src/clientP2P/ClientManagementUDP.class | Bin 0 -> 3676 bytes .../ClientManagementUDP.java | 69 ++++--- src/clientP2P/ClientP2P.class | Bin 0 -> 1552 bytes .../Client.java => clientP2P/ClientP2P.java} | 18 +- src/exception/TransmissionError.class | Bin 0 -> 221 bytes src/exception/TransmissionError.java | 2 +- src/server/Server.java | 39 ---- src/server/ServerManagementUDP.java | 172 ------------------ 8 files changed, 52 insertions(+), 248 deletions(-) create mode 100644 src/clientP2P/ClientManagementUDP.class rename src/{client => clientP2P}/ClientManagementUDP.java (67%) create mode 100644 src/clientP2P/ClientP2P.class rename src/{client/Client.java => clientP2P/ClientP2P.java} (75%) create mode 100644 src/exception/TransmissionError.class delete mode 100644 src/server/Server.java delete mode 100644 src/server/ServerManagementUDP.java diff --git a/src/clientP2P/ClientManagementUDP.class b/src/clientP2P/ClientManagementUDP.class new file mode 100644 index 0000000000000000000000000000000000000000..e4928ecaca96f1b6a29ab87d3ef3e18ad66c7359 GIT binary patch literal 3676 zcmaJ^`F|7F5&y0&c^74Eg0PKUAW?t@TefU)2_eQI^??%+U$u-~2MAfD#TJ%#<<-hS z+oaI6Nsrt|?>4kxq@#1@hjvak*gavIza@>mRU=CqJALe6UVh>DMj#K%IY!=pkT zla-GPIj7>hhEJ$?T*D_-d`iO;DxTEvX%(N*@L3g~3*k0oR6M2OXWC9 z7ZWdNcu^i+((rN!jmU)Xd3-@;z8FHi=zb}L4t!b2SA=|3#n)7PUBfFHz7fJV@huVl zwu>plz9;1SDt;g{KNRvKQTVZnpQw0E#YF|RCybmqkao8 zMka46sOd>tY4?DFmX@^!*UrRSCuz6ePEmFJP}(v_^7AK5XTms<;Y_uYw@6z=s5apm z$+=-8>#Z>dOaRd*^u%4k#++%T2JF*T#x_#p=0e^SSMRLgMC)2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Q06PjVx&QzG literal 0 HcmV?d00001 diff --git a/src/exception/TransmissionError.java b/src/exception/TransmissionError.java index 67a01c7..5c64987 100644 --- a/src/exception/TransmissionError.java +++ b/src/exception/TransmissionError.java @@ -1,2 +1,2 @@ package exception; -public class TransmissionError extends Exceptions {} +public class TransmissionError extends Exception {} diff --git a/src/server/Server.java b/src/server/Server.java deleted file mode 100644 index 4c6c533..0000000 --- a/src/server/Server.java +++ /dev/null @@ -1,39 +0,0 @@ -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/ServerManagementUDP.java b/src/server/ServerManagementUDP.java deleted file mode 100644 index 1bddbd0..0000000 --- a/src/server/ServerManagementUDP.java +++ /dev/null @@ -1,172 +0,0 @@ -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; -import exception.InternalError; - - -/** 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 (InternalError e) { - res += sendInternalError(); - } 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 - * @throws InternalError - */ - private String upload(String filename) throws NotFound, InternalError { - File file = new File(filename); - if (!file.exists() || !file.isFile()) { - throw NotFound; - } - String res = "LOAD " + file.length(); + "\n" - try { - res += new String(Files.readAllBytes(Paths.get(filename))); - } catch (IOException e) { - throw InternalError; - } - return res; - } - - /** 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" - } - - /** Prepare data to be send if internal error encounterred - * @return data to be send - */ - private String sendInternalError() { - return "INTERNAL ERROR\n" - } - -}