From 49ece737ea99c5218535fd2335661d25fb0a7919 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 12 Dec 2019 11:56:46 +0100 Subject: [PATCH 01/48] Update diagram sequence --- doc/classdiagram.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/classdiagram.xml b/doc/classdiagram.xml index 04352dc..e4d97ca 100644 --- a/doc/classdiagram.xml +++ b/doc/classdiagram.xml @@ -1 +1 @@ -7VrbcqM4EP0aV808eMqAr4+x4+zsrjObGk/tbB4VUEAbgVxCjs18/bZA3CzFOAlOdmp4SaGDJKM+3ac7DT1nEe5/42gTXDMP05498PY957Jn29PxFP5KIMmA4WyWAT4nXgZZJbAmP7ACBwrdEg/HtYmCMSrIpg66LIqwK2oY4pzt6tPuGa3/6gb5WAPWLqI6+p14IlDHsicl/hkTP8h/2Rqr84Uon6xOEgfIY7sK5Cx7zoIzJrKrcL/AVNout0u27uqJu8WDcRyJUxb8FXu3qP9HePHF36HF9nf0fbru22P1cCLJT4w9MIAaMi4C5rMI0WWJzjnbRh6W2w5gVM5ZMbYB0ALwXyxEothEW8EACkRI1V28J+IfufzTSI1uK3cu92rndJDkg0jwRC1yRjlwmwKDwTQHysXpqLb6BnMSYoF5AXoX0kVgGLEIZ8gVoVTdz0wj7fGkyRUUsy131ayEUX8pPPfrjv75bZncPXwmq/5IuS7iPhZH+LBKz4CIwgwelyewkGOKBHmsPwhSvu0X80r64UJ5gNkbjj3lI6Jb9UtrzB/BXoc+Eu9ISFFqsnsWidxdJIOIEj+CaxdsJC09h/WCQEBdqBtCOsncDQj1VihhW2mOWCD3IR/NA8bJD9gW5f4Ct7lQ3gQeW52xlisVXxzHMOcmp8c6gK7RvjZxhWKhAJdRijYxuSuOEQJTJJozIVhYcwcD+/KEeF+BdP7UXXuoxCDJVU6Nd6W2WGOFBRVdybHWKZ9olGtkU5ISHQvOHgoRlDa6h1BZMMp4GUA5+xTfCwP3IfG8VELiDXJJ5H9LBaNvlcgqXXjplMh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TUQUyeDuV46jd8pndTUe8kwBcp95/BxurpCKv3UpHJp7GAdAE6vk+5PWyeJg3w37+AEXdt1ib6OP3+ZTG/pMm6xc9E68VbH9+fhe3v0gXxvht3uDnXTQ/0KvD/+UBuzuV4pX+y4SN/xqZz+SKg8UrKSoc4cGGmFjdrXJPmbkdrXJGUVkFdk3i4BmtZT8vd0b1UFJlDfqRScA6VgA/DFeZtYGIPapc6tF3RqSWtBtzCCPgq7qlB7iWVyKDGnemKxh6YnSKeFupC5vqvX9fuiueJMcLq4eIQxusYMuZyqfzNUEEo00zSzJBgcfdyXqUAutuE0VuSWI6C5u4nQO1JtPZ4AzDCcrTqnp4Auv9cRVuP8PmwI7+hc8g2MDPCW6nMe3UaEQu9TfuwEpiiCIfosU1+QfHliDe4oV4UdsZ69u0p8VrqclQ8uwTHPD64s/pUGdnXAdgMsf+SvizAwPBePmyKs3YJQU1HXemFmND0sv03fKrcOE7Bhqfbk1olvcgyfxXRyyyS3apL9abkFhvV1W/MxPZvcMryGk3Lr74SmBUcTiUjG4SM/Za0kGywFn5dI/JRI65i+UfC86pu5VypF52yVf6tTiueDd9KQIM5VCTI8jAtZ10jTleTkZi3FpLh4i57sGKPlkJo0fPiwXh1Toll88Z0KlOKzeXvxHw== \ No newline at end of file From 2f202812c8af7781bd9c5b3c1777a83e521ab51e Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 12 Dec 2019 11:57:02 +0100 Subject: [PATCH 02/48] Class stub --- src/client/Client.java | 10 ++++++++++ src/client/ClientManagement.java | 17 +++++++++++++++++ src/exception/NotFound.java | 2 ++ src/server/Server.java | 12 ++++++++++++ src/server/ServerManagement.java | 22 ++++++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 src/client/Client.java create mode 100644 src/client/ClientManagement.java create mode 100644 src/exception/NotFound.java create mode 100644 src/server/Server.java create mode 100644 src/server/ServerManagement.java diff --git a/src/client/Client.java b/src/client/Client.java new file mode 100644 index 0000000..3005e2a --- /dev/null +++ b/src/client/Client.java @@ -0,0 +1,10 @@ +package client; + +public class Client { + public Client() { + + } + public static void main(String [] args) { + Client c = new Client("client"); + } +} diff --git a/src/client/ClientManagement.java b/src/client/ClientManagement.java new file mode 100644 index 0000000..967d31a --- /dev/null +++ b/src/client/ClientManagement.java @@ -0,0 +1,17 @@ +package client; +import exception.NotFound; + +public class ClientManagment { + private String baseDirectory; + public ClientManagment(String baseDirectory) { + this.baseDirectory = baseDirectory; + } + + public void download(String filename) throws NotFound { + } + + public String listDirectory() { + + } + +} diff --git a/src/exception/NotFound.java b/src/exception/NotFound.java new file mode 100644 index 0000000..172a0bc --- /dev/null +++ b/src/exception/NotFound.java @@ -0,0 +1,2 @@ +package exception; +public class NotFound extends Exception {} diff --git a/src/server/Server.java b/src/server/Server.java new file mode 100644 index 0000000..e3b1757 --- /dev/null +++ b/src/server/Server.java @@ -0,0 +1,12 @@ +package Server; + +public class Server { + + public Server() { + + } + public static void main(String [] args) { + Server s = new Server("server"); + } + +} diff --git a/src/server/ServerManagement.java b/src/server/ServerManagement.java new file mode 100644 index 0000000..dd09361 --- /dev/null +++ b/src/server/ServerManagement.java @@ -0,0 +1,22 @@ +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(); + } + + public void upload(String filename) throws NotFound { + } + + public String listDirectory() { + + } +} From 91ebd05063c2d8a55021ced40643ba9f2fe54e14 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 12 Dec 2019 14:21:35 +0100 Subject: [PATCH 03/48] Add `initFileList()` in `ServerManagement.java` --- src/server/ServerManagement.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/server/ServerManagement.java b/src/server/ServerManagement.java index dd09361..641fe25 100644 --- a/src/server/ServerManagement.java +++ b/src/server/ServerManagement.java @@ -13,6 +13,18 @@ public class ServerManagement { 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 { } From 1911c143eb1356aed9e83fbf89d4264835a41ccf Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 12 Jan 2020 22:04:15 +0100 Subject: [PATCH 04/48] Update documentation --- doc/classdiagram.xml | 2 +- doc/protocol.md | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 doc/protocol.md diff --git a/doc/classdiagram.xml b/doc/classdiagram.xml index e4d97ca..0cc7494 100644 --- a/doc/classdiagram.xml +++ b/doc/classdiagram.xml @@ -1 +1 @@ -7VrbcqM4EP0aV808eMoC48tj7Dg7u+tkU+Opnc2jAgpoI5BLyLE9X78SiJslG5LgeKeKFxdqXYz6tE4fGnr2PNz9xuA6uKUeIj1r4O169nXPsiajifiVhn1qGE6nqcFn2EtNoDCs8E+kjANl3WAPxZWBnFLC8bpqdGkUIZdXbJAxuq0Oe6Kk+q9r6CPNsHIh0a0/sMcDtS1rXNi/IuwH2T+DkdpfCLPBaidxAD26LZnsRc+eM0p5ehXu5ohI32V+SefdHOnNb4yhiDeZ8FfsPcD+H+HVnb+F883v8Mdk1bdG6ub4Ptsx8oQDVJMyHlCfRpAsCuuM0U3kIbnsQLSKMUtK18IIhPFfxPleoQk3nApTwEOietEO83/k9C+Oaj2Ueq53auWksc8aEWd7Ncl2MsNDYhgMJpmhmJy0KrPvEcMh4ojlRu9KhohoRjRCqeUGE6L6U9dIfxx1uTLFdMNcNWpPib/gnvttS/78vtg/Pn/Fy76jQhcyH/ETeIAiMsSJQlTcLtuLiQwRyPFL9Uagim0/H1fALy5UBJij4dRdvkCyUf+0QuxF+OswRuItDglMXPZEI56Fi0QQEuxH4toVPpKenon5HIsDdaU6uAySmRtg4i3hnm6kO2IO3eesNQsowz/FsjCLF9HNuIomEbHlESs5U+HFUCzG3G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XhLyCvbXKW+vRFGtiefiRK6lqjCmYj9FTQ9HSlh/0ScNZb5BSNVSC1+i51FV/B16juy+tF7LZwWOsQ7SvU6Wc430/rhu6rOU8/rzA/8OXlumVS5hsMHyyj4S7NFoxXZohPs1CvZoJD+TuSnapnlFf1R8JJNTtLjCNzF5dxkj+1TZoXmsqyQbdNPZYHDF3XXqYG++jblExA1zRdwVN9epwc0+tf/Dd9cVfeJYnpnjjHsOdDXsmevpyag3gue5c1/hpgfKvCKp7gWu2p96/3H1qJWWj5em61fJx/Tm7J6ZLx/T/YN2vfIxWy143DmOR9oZHbEGW3G4peQFcScRr1wQhfYHoR2WQGAV94Qgm8xSrL+QabqSFD1IrbHitbmuehA//6U5b0ZfuPvLH/2fAGP6Ew== \ No newline at end of file diff --git a/doc/protocol.md b/doc/protocol.md new file mode 100644 index 0000000..06d6a7f --- /dev/null +++ b/doc/protocol.md @@ -0,0 +1,11 @@ +# P2P-JAVA-PROJECT version 1 (Protocol for step 1) +All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the protocol). + +## Client messages +- `LIST\n`: ask the server to list files from server root directory +- `DOWNLOAD\n\n`: ask the server to download file from server root directory. Only one filename is allowed per request. + +## Server responses +- The response to `LIST` request is in the format `LIST\n\n\n[…]\n\n` +- The response to `DOWNLOAD` request is `LOAD \n` or `NOT FOUND\n` if the file doesn't exists. +- The server send a `PROTOCOL ERROR\n` message if it doesn't understands what the client sent. From e7b4d7a5c37e0c2fe1b391ae84a3a94a5688be2d Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 12 Jan 2020 22:04:49 +0100 Subject: [PATCH 05/48] 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" + } + +} From acaa363153c09d25ce42670d8f0f877ca415f076 Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 12 Jan 2020 22:20:52 +0100 Subject: [PATCH 06/48] Add send method + update documentation --- doc/classdiagram.xml | 2 +- doc/protocol.md | 1 + src/exception/InternalError.java | 2 ++ src/server/ServerManagementUDP.java | 25 +++++++++++++++++++++++-- 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 src/exception/InternalError.java diff --git a/doc/classdiagram.xml b/doc/classdiagram.xml index 0cc7494..e6dfb8c 100644 --- a/doc/classdiagram.xml +++ b/doc/classdiagram.xml @@ -1 +1 @@ -7VzZduI4EP0azul+IAcvbI9hSXfPkAwn6XVe5ghbgDrCYmQRoL++JVvehW2CTc/ip1hlWVi6Vbeuy3JaxnhzeEfBdn1PbIhbesc+tIxJS9c10+zzP8Jy9C0DQ/cNK4ps2SkyPKEfUBo70rpDNnQTHRkhm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TtsD+81CrlZEfIaDu9ph/eDDtZXOQmvZPQOfsot0+nsl9wSfnZq1pDPCuwyFaQG7k0qWuopLUe5HHFcff4QjeC1yULtEsbH/M6sI/FJgYvMOYHk1MCBkwL5yxpXhw4LnlEfNVc1SrbOZ7iZUTbKTCRQNr8t/6+F/ztKZtjun/GMl97Y2xsap0T7QS3svandnSYf95Of8Tp00a66Z8563GOga+DkVtfMW73VUz7zVP2QqHZUSNTX9xlRr0/d/zh7NCrTx1PD9avoo3pEuGuX08dCfesc9LG4rXxY+37otAs8YgFW4nFFyQviSiIS/4jCKAU9AwKrrCbE0WSieP0nGaZrCdF9/ZplHYOp27HV8OlWv6H8hej4+QxVc4/nZzJU5fCIY/fKDVWt3z2HoRbv8SmfplrWX4tL6XUUdM95k08PfXtd6xTWH1nOT0v7Nfd2GqT98fNRCZvGaP+r7nocBXIDG4ReT9kgDBTDr3yxQ+nI6dhXncw/ZWSv3i7wYvoXYqLq6Z/Zscb/Ag== \ No newline at end of file diff --git a/doc/protocol.md b/doc/protocol.md index 06d6a7f..51fed50 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -9,3 +9,4 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p - The response to `LIST` request is in the format `LIST\n\n\n[…]\n\n` - The response to `DOWNLOAD` request is `LOAD \n` or `NOT FOUND\n` if the file doesn't exists. - The server send a `PROTOCOL ERROR\n` message if it doesn't understands what the client sent. +- The server send a `INTERNAL ERROR\n` message if it encounters an internal error. diff --git a/src/exception/InternalError.java b/src/exception/InternalError.java new file mode 100644 index 0000000..c738615 --- /dev/null +++ b/src/exception/InternalError.java @@ -0,0 +1,2 @@ +package exception; +public class InternalError extends Exception {} diff --git a/src/server/ServerManagementUDP.java b/src/server/ServerManagementUDP.java index b4a8d00..1bddbd0 100644 --- a/src/server/ServerManagementUDP.java +++ b/src/server/ServerManagementUDP.java @@ -6,6 +6,7 @@ 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. @@ -85,6 +86,8 @@ public class ServerManagementUDP implements Runnable { } catch (ProtocolError e) { // wrong version or wrong implementation res += sendProtocolError(); + } catch (InternalError e) { + res += sendInternalError(); } catch (NotFound e) { res += sendNotFound(); } @@ -118,9 +121,20 @@ public class ServerManagementUDP implements Runnable { * @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 { - // TODO + 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 @@ -147,5 +161,12 @@ public class ServerManagementUDP implements Runnable { 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" + } } From 192f0a2c9470cc34910c694a43e888c5a3bb1f1e Mon Sep 17 00:00:00 2001 From: Louis Date: Sun, 12 Jan 2020 23:29:49 +0100 Subject: [PATCH 07/48] Add client implementation + update doc --- doc/classdiagram.xml | 2 +- src/client/Client.java | 31 +++++- src/client/ClientManagement.java | 17 ---- src/client/ClientManagementUDP.java | 140 +++++++++++++++++++++++++++ src/exception/TransmissionError.java | 2 + 5 files changed, 173 insertions(+), 19 deletions(-) delete mode 100644 src/client/ClientManagement.java create mode 100644 src/client/ClientManagementUDP.java create mode 100644 src/exception/TransmissionError.java diff --git a/doc/classdiagram.xml b/doc/classdiagram.xml index e6dfb8c..9bc3e48 100644 --- a/doc/classdiagram.xml +++ b/doc/classdiagram.xml @@ -1 +1 @@ -7Vxbc9o4FP41mWkfksE3Lo+BkLa7JMskve7LjmIL0EZYrCwC9NevZMs3WRgTbNruug8d61gSkr5z+XQs5cIaLbfvKFgt7og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yKJPx4dRdzObKcvp+5QvU2LWjojCdqC6kH2DVpfU92R3tUPp05bVG3/1+sT7MZb8I7KWNlp/nvLCXvLf7XavvgX \ No newline at end of file diff --git a/src/client/Client.java b/src/client/Client.java index 3005e2a..3d60b9d 100644 --- a/src/client/Client.java +++ b/src/client/Client.java @@ -1,10 +1,39 @@ package client; public class Client { + private String host; + private int port; + private String directory; public Client() { - + host = "localhost"; + 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_CLIENT/"; + // create directory if not already exists + new File(d).mkdirs(); + } } public static void main(String [] args) { Client c = new Client("client"); + ClientManagementUDP cm = new ClientManagementUDP(c.directory, c.host, c.port); + Thread t = new Thread(cm); + t.setName("client P2P-JAVA-PROJECT"); + t.start(); } } diff --git a/src/client/ClientManagement.java b/src/client/ClientManagement.java deleted file mode 100644 index 967d31a..0000000 --- a/src/client/ClientManagement.java +++ /dev/null @@ -1,17 +0,0 @@ -package client; -import exception.NotFound; - -public class ClientManagment { - private String baseDirectory; - public ClientManagment(String baseDirectory) { - this.baseDirectory = baseDirectory; - } - - public void download(String filename) throws NotFound { - } - - public String listDirectory() { - - } - -} diff --git a/src/client/ClientManagementUDP.java b/src/client/ClientManagementUDP.java new file mode 100644 index 0000000..6dc1a4f --- /dev/null +++ b/src/client/ClientManagementUDP.java @@ -0,0 +1,140 @@ +package client; +import exception.NotFound; +import exception.ProtocolError; +import exception.InternalError; +import exception.TransmissionError; +import java.utils.Scanner; +import java.net.InetAddress; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.io.File; +import java.io.IOException; + +/** 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 ClientManagmentUDP implements Runnable { + private String baseDirectory; + private int UDPPort; + private String host; + 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 ClientManagmentUDP(String baseDirectory, String host, int UDPPort) { + this.baseDirectory = baseDirectory; + this.host = host; + this.UDPPort = UDPPort; + } + + /** Implementation of Runnable + */ + public void run() { + System.out.println("Files present on the server:"); + try { + String msg = sendMsg(sendFileList()); + System.out.println(listDirectory(msg)); + System.out.println("Name of the file to download:"); + Scanner scanner = new Scanner(System.in); + f = scanner.nextLine(); + msg = sendMsg(sendDownloadRequest(f)); + download(msg, f); + } catch (ProtocolError e) { + System.out.println("Protocol error"); + } catch (NotFound e) { + System.out.println("File not found"); + } catch (InternalError e) { + System.out.println("Server internal error"); + } catch (IOException e) { + System.out.println("Cannot write to file"); + } + } + + /** Prepare request to download file + * @param filename name of the file to be downloaded + * @return request to be send + */ + private String sendDownloadRequest(String filename) { + return protocolID + "\nDOWNLOAD\n" + filename + "\n"; + } + + /** Download file. + * @param response Servers's response + * @throws NotFound + * @throws ProtocolError + * @throws InternalError + */ + private void download(String response, String filename) throws NotFound, ProtocolError, InternalError, IOException { + try { + String r[] = r.split('\n', 3); + checkProtocolID(r[0]); + String r2[] = r[1].split(' '); + if (r2[0] != "LOAD") { + throw ProtocolError; + } + int size = Integer.parseInt(r2[1]); + if (r[2].length() != size) { + throws TransmissionError; + } + FileWriter fileWriter = new FileWriter(baseDirectory + f); + fileWriter.write(r[2]); + fileWriter.close(); + + } catch (java.lang.ArrayIndexOutOfBoundsException e) { + throw ProtocolError; + } catch (ParseException e) { + throw ProtocolError; + } + } + + /** Prepare request to list files on server + * @return request to be send + */ + private String sendListDirectoryRequest() { + return protocolID + "\nLIST\n"; + } + + /** Parse list of directory response from server + * @param response server's response + * @return list of files, separated by CRLF + * @throws ProtocolError + */ + private String listDirectory(String response) throws ProtocolError { + try { + String r[] = r.split('\n'); + checkProtocolID(r[0]); + return r.split(protocolID + "\nLOAD \n")[1]; + } catch (java.lang.ArrayIndexOutOfBoundsException e) { + throw ProtocolError; + } + } + + /** Check client'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; + } + } + + /** Send message to server + * @param msg message to be send + * @return server's response + */ + private String sendMsg(String msg) { + InetAddress dst = InetAddress.getByName(host); + byte [] bytesString = msg.getBytes(); + DatagramPacket emission = new DatagramPacket(bytesString, bytesString.length, dst, UDPPort); + socket.send(emission); + socket.receive(reception); + return new String(reception.getData(), 0, reception.getLength()); +} diff --git a/src/exception/TransmissionError.java b/src/exception/TransmissionError.java new file mode 100644 index 0000000..67a01c7 --- /dev/null +++ b/src/exception/TransmissionError.java @@ -0,0 +1,2 @@ +package exception; +public class TransmissionError extends Exceptions {} From a326c33124544c48ac72c9e5b34179c855936f89 Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 14 Jan 2020 10:32:55 +0100 Subject: [PATCH 08/48] fix syntax in serverP2P --- src/exception/InternalError.class | Bin 0 -> 213 bytes src/exception/NotFound.class | Bin 0 -> 203 bytes src/exception/ProtocolError.class | Bin 0 -> 213 bytes src/serverP2P/ServerManagementUDP.class | Bin 0 -> 3973 bytes src/serverP2P/ServerManagementUDP.java | 175 ++++++++++++++++++++++++ src/serverP2P/ServerP2P.class | Bin 0 -> 1480 bytes src/serverP2P/ServerP2P.java | 39 ++++++ 7 files changed, 214 insertions(+) create mode 100644 src/exception/InternalError.class create mode 100644 src/exception/NotFound.class create mode 100644 src/exception/ProtocolError.class create mode 100644 src/serverP2P/ServerManagementUDP.class create mode 100644 src/serverP2P/ServerManagementUDP.java create mode 100644 src/serverP2P/ServerP2P.class create mode 100644 src/serverP2P/ServerP2P.java diff --git a/src/exception/InternalError.class b/src/exception/InternalError.class new file mode 100644 index 0000000000000000000000000000000000000000..90c81e92c94dd5c44f78aef8852685f973f2391b GIT binary patch literal 213 zcmX^0Z`VEs1_l!bUM>b^1}=66ZgvJ9Mg}&U%)HDJJ4Oa(4b3n{1{UZ1lvG9rexJ;| zRKL>Pq|~C2#H1Xc2v=}^X;E^jTPBDjN1nDYDk!v zDmJyw7iV!30c<#gp}c4ES}CKWzfBl5fv6Dr6m9=DCg|`B%@8k_8C_=MmIKcG33{}) O$LO*Qt`NHI?fn4swI|vD literal 0 HcmV?d00001 diff --git a/src/exception/ProtocolError.class b/src/exception/ProtocolError.class new file mode 100644 index 0000000000000000000000000000000000000000..ab341e7cc91cbdf461e98c54e5161a85350dd7a3 GIT binary patch literal 213 zcmX^0Z`VEs1_l!bUM>b^1}=66ZgvJ9Mg}&U%)HDJJ4Oa(4b3n{1{UZ1lvG9rexJ;| zRKL>Pq|~C2#H1Xc2v=}^X;E^jTPBDj6i}33lAoNP<62adU!<3nSeD4cz{0@F$RM6t zk(^pkl9`{UkD`W=K^UY;KPNFSUEdX^hCz{m3FsIQU<5*-2|$t!$dd*085mf#wlgqp V1WU66Nj9J`15gVi0|%Jq1OU|KEJ6SP literal 0 HcmV?d00001 diff --git a/src/serverP2P/ServerManagementUDP.class b/src/serverP2P/ServerManagementUDP.class new file mode 100644 index 0000000000000000000000000000000000000000..da550c6b95102e326c3259e0dd5310b31fec014e GIT binary patch literal 3973 zcmaJ^d301~75}}=WZvZQl8^zy-Ug@;vJe_D&;+U3XcH$hX%d2gTKkebl7Zn(I`f7_ ztxG|KwyxczDk_Spw$)I}gpv|y>(**rDkxf)y6+y(>G2PHwDEV}yveQ%bHC-@@2d8dkZY4G8IEbi1u-mT(28Z^9D#rrhqxJwp? zRUDDU`-L3UkdF_D>@k@gm%Y1%+#{m*s`#La5Bc$7KRzP-kBa2SWOkn)_sjTz9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lroJM+WmaH5pDfp66+hQe>aFNYc<4;mLAmt^7?PDf#14~*;14~m_7Ah(CR1RZ#xwo>~n^l!9 z&R+8<578Eja`@7@V;oDc>XDNimn`-Mu1%rn1PVP>*@KwN^$KV0^Hyaq$d-d4ImfU3 zTg5X01P9vAp>mhmdzw=%xUw{W^RJ5k7Bwh?tVk<*L< zqIZC}J<6bu^8s>-s69=zKF-n%5vNZOr{|ge3oO$`mgx%TSFwvfV7KBQoWIH%{S&t- zb66lL;s)f7vA#amPKDw8?mgoWHg815w?BLt%5csm!K*D+5;zlw;m>^0t$EO~{H%I)0DQDBfC$SOUJ@mfxp z$a605e)-c0c{t>#^gzi&l&8E5sF>&Z7IF1mcJHDkp_MYO7J{kj1T+pC9u@=Z2$oG3 zU1$V0v%F;8kV3TpT~H8mc>)T#uce*~$UldgbJ#MBH@Pw{*vhEv#*5`c^rMEA>c&?3 zAs2Kta!BtYJ&c7o!oG2o62~ZW52cQy7k4v)d!2BK;QcoiN`}Tz2XCgVOqd~e)5>vT z^-=RWVl7MFU~VCgb0vjr3A{Ndl}e$urR*v2clrd1IjL803Hh0TC z?1~$!urk|2RG1E;&}nP;u>$wAAwIy8K8PFekmGcobdos^`0jX(T_{VQajsiSIm&(Z s1R6zIx}*A>t&4K*z$d}&?!JDQFK1PwG 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(buffer, buffer.length); + // wait and receive datagram + socket.receive(dgram); + // extract data + String str = new String(dgram.getData(), 0, dgram.getLength()); + // process request + str = processRequest(str); + dgram.setData(str.getBytes()); + dgram.setLength(str.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]) { + case "LIST": + res += sendFileList(); + break; + case "DOWNLOAD": + res += upload(formattedRequest[2]); + break; + default: + throw new ProtocolError(); + } + } catch (java.lang.ArrayIndexOutOfBoundsException e) { + throw new ProtocolError(); + } + } catch (ProtocolError e) { + // wrong version or wrong implementation + res += sendProtocolError(); + } catch (InternalError e) { + res += sendInternalError(); + } catch (NotFound e) { + res += sendNotFound(); + } + return res; + } + + /** 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 new 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 new NotFound(); + } + String res = "LOAD " + file.length() + "\n"; + try { + res += new String(Files.readAllBytes(Paths.get(filename))); + } catch (IOException e) { + throw new 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"; + } + +} diff --git a/src/serverP2P/ServerP2P.class b/src/serverP2P/ServerP2P.class new file mode 100644 index 0000000000000000000000000000000000000000..aa96009f76e95a0a2105943677aecb57f41c0647 GIT binary patch literal 1480 zcmZuxU31e`5Ixs6mZB(M3W<|IXu_ux#}>GhwuI7{BtU{48`}g7ZC%B_I09R8WjRx( zGyDkN$}seiXJ%TPHZz?*^?{$nblUFq2crZ=ld&%0 zx`GMZ5aye;cxQyUEg{PgsN0Uqz&02L%BI6huI)Tw z(2|dgJwvY=Rz=Ubj%ihv8G=_#%XF_Y_`?g^v~<-jbB2+mY4NRQZHGH~W2Z`*vOp9SM{NO`u0jK2brc>hAO#v~SBp!&MaVJ^cy)X5G?4C9S{fBO4Q8DgKl z3qQ>*)J2#hN>M>6+M}f6p{QL!_`urq&Li6K1FqbYhRbUVgB9*(9J|gPm-KKp{EIKQxVMgJHtEqFZNq;XWl0F1Pj=#=;9-fTn9!by5o{Y^XD@zJr8D$%l&fp*+QV zF2XkIb#BpkqTb>?{r0MPB;DIzsw=y8H;dtDpOz<)G7hxUMd6H1HFkT-Oy8+wkJ zeGC`c7>Tqns*RCp(OvKgvtIZ3dv{P9PkJLqdy#!i6e4X*wh+4ex?I75l@Y)I z#Pt=z2uXSrag1RF8d4ZXneMQkFpZ};hG%q3y~GJbXw@_V|52h(!jgn2aN!*${}G>2 z0*gWK&V){XfF59JA5+xEG_jj0M1I9F`uW>9p4!K3VX2K1TT$(k7UrTY%nO1Hg5V5- ZWao%tcMs-i{^w{1B1DtnJjHyt@IR6iQdj^0 literal 0 HcmV?d00001 diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java new file mode 100644 index 0000000..33338e3 --- /dev/null +++ b/src/serverP2P/ServerP2P.java @@ -0,0 +1,39 @@ +package serverP2P; +import java.io.File; +import serverP2P.ServerManagementUDP; + +public class ServerP2P { + private int port; + private String directory; + public ServerP2P() { + 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 || d.equals("")) { + d = System.getenv().get("HOME"); + if (d != null && (!d.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) { + ServerP2P s = new ServerP2P(); + ServerManagementUDP sm = new ServerManagementUDP(s.directory, s.port); + Thread t = new Thread(sm); + t.setName("server P2P-JAVA-PROJECT"); + t.start(); + } + +} From 572a75e3ae27fed51d652d4ca0a7ff8c1adf77f1 Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 14 Jan 2020 11:10:11 +0100 Subject: [PATCH 09/48] 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)2F6lkfUs)C&rbk?f0 zPO7-X91~+dH^k-OnWUL@(*#68llU`~&bccPuQ*lTQqhIlOu01SNY0wcxv?^=8!C7v z(5SN4aBha#r56c1?lntkf-PfRW3fZMlfALA z@zFyE`zIok2geif(UHjRj$Jw)#Qi!3g&Y#{0OBex>-Z^Nr>g9bi)0;>ySowFinz08 zBxgE{rqiwBijF1B=tv@^V-^qTF!7Lrts};~8L>}#!Y64V;@XiC_T36Lctx0Z)0xgh z(y%Pk(NVxvarb9B-VkHcV(bWxD%fn60Pmb|3@bOE&gF>yLC3KjnyN6jX;q^HY*D(d z^c*pL_3*0bi0uyAc`K!$PJ)hDwi`L=Pm~#qTdwI?My6=6A>lz0Nf)O}b~kzvrtQx7 z=t{jPsPAX6G<(`fyB;Qjje<=#p2@y^I+HRP-PWX+S>~^^LXKJaH_L2jV03C^XtZ}A zq+=OB=jkT%3jy~UuIo678BRsy>6bj6BB z!-#}b{6WVb@s^H1;q4N@mg#m57_Kqn81o4`IcKt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zD%Ng(xn!A2UoumU{tOo@wbs zA7bKI_q+xQVjel4NInSVOWs#eY@^ZOc9kLSPu}NwKbw#G{r*y3*>iec3{wMGK1cF6 zR?Z+I(Ku8;q>-U|X-KHL$Qu&-koGrji2<1PyY94XTTIiMut-hrWr^X$?51b99?sK% zU!c1sh=3Rb`U;3y=&r5ScF#qr+WK;OA%uEUE>bAsE$$o?%!EJ%^;pZH)YY z+H)w{=Lr~}$rHe0&1x!#`8m?gyo0viY(>S2} z?^~S3cVzvZZn&pdM1ppn!O(x?7?7Y#SOG5ojhTPM?+OLIj4Qpb^1}=66ZgvJ9Mg}&U%)HDJJ4Oa(4b3n{1{UZ1lvG9rexJ;| zRKL>Pq|~C2#H1Xc2v=}^X;E^jTPBDj7E+X$SDc$!T%4Jo=UP;hU!<3nSeD4cz{0@F z$RM3sk(^pk0+iCnP{+t13{t6|lbDyT?+R1Lpvb@kbQB0M0wK^8Ajt;g$%6R|46It) Y85lQ$rP+Zb8&H@5sD+V%159%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" - } - -} From 71a9b23ce405c50f966879c4dcb07b32d2718a2e Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 14 Jan 2020 11:14:30 +0100 Subject: [PATCH 10/48] maj classdiagram --- doc/classdiagram.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/classdiagram.xml b/doc/classdiagram.xml index 9bc3e48..6848251 100644 --- a/doc/classdiagram.xml +++ b/doc/classdiagram.xml @@ -1 +1 @@ -7Vxbd6M2EP41Pmf7kBzuth/XdvbSJlufzV770qMYxdYGI1fIib2/vhKImyQIjsHJtuQlMAghaWa++TQMHtjT9e4tAZvVFfZhMLAMfzewZwPLMh1nyP5xyT6RjGwrESwJ8kWjXHCNfkIhNIR0i3wYlRpSjAOKNmXhAochXNCSDBCCH8rNbnFQfuoGLKEiuF6AQJV+RT5diVlYw1z+DqLlKn2y6Y2TK2uQNhYziVbAxw8FkX0xsKcEY5ocrXdTGPDFS9clue9NxdVsYASGtMkNexwsL6i/+PgQ/PHpYn9z9w5dnpliHvcg2IoZf2ADsow3eBv6YuB0n65G9IDWAQjZ2eQWh/RaXDHZOQjQMmTHCzYcSJjgHhKK2EK+Fhco3jDpYoUC/xLs8ZYPOqJgcZeeTVaYoJ+sWxCIPtllQoVNWF6pxTW/k4kNJiUwYm3m6UqYkugK7EoNL0FEhWCBgwBsInSTTWMNyBKFE0wpXotG6kKLteczhLuCSCz8W4jXkJI9ayKuurYwAuEFVnr+kNuU6QnZqmBPtiNMWZjxMus61zQ7EMrWK/7PyP8Ozn5fv/6wfADT7XvwdXR9ZjqK4hVtw9B/zV2InYU4VvqKrlPdQJ/5iWiLCV3hJQ5BcJFLJ2y5yP6bWMT45Ds/Obfc9Hy2K16d7SsXvNZ2mZEsYZOGfMi12iIwABTdl11ft/Ti1jlGIc21fOaWtZxiWNpDhLdkAcVNRU+V+jHN+n6SCSv9xHaQzaaRaeiXa/y4aQQotoeIEnyXASM3i1sUBFMcYJIbTYoMAbylGlxYI9+P7SXagAUKl584TszOzFxyGd84s3PJR+EhXEQwBRQkHsytJwA3MJj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F4sNaulYy1lDtiuwL3QQGliZVLTlykJXqhk+rj+7SFbw1GSh9kzT0tQT60ha3TIo1LZklS4HqluycpbCqJrqlu70cdxUH+229fF5NHB8HlgQ4rswLg3DLstwaDkHpcju5pAgdgY80ji9aGuflJ4ZaoaWFGqMvVqoUfqLlHGnUKNWOktJlz7ubKceVnoi4YxOWA+rdzLqA4k+sDhgJS+xHFYvXDWt8MLrYXPvcmQ9bNEd1AnvHFWungTpdhpwHvsAW55IeUDU3uNrvT5556EnXVEJs3HY0jpNfBKXkF+n8Jz6sEXpb58gbLHPE9oKEDIHR4Qt7EblrTkw5cw3LtW/NA5V65+YCevU90RU+HluSU143WzDMHbrCtNcgQ2/3BD8gJiG8EeSiMDk4dg94OdTVIOUb1xLvOBOELlWSNxIb1VF12HpckktFJXos18/UaKgM1vQvAT7oqFcKmtznWE9lMv9vVNA+eg8/j6DYNMyJQi2XhoGN375+mWkI8aOrHf16Qi5v+Pa3eud5tcYpGKNPh/RiivLypRTbTjl7zHoRd+/dN84H/GiX7rXM5WTJiSOigXaTkg86QXdWjGfI3UxHEqpi7Fk+I1fz5Umcg3n0ij8SSvrOJFhm6q+nfo5i1dSM0Z1xi+L2Vg/aXRpqdGl5n2onkC0QiA8+ZUazUvYpyUQlsodewJxyFR+GgKR8tMzEIha+vB8GvBTP5gYetJEtjRR1/7cUtQi+emNHug7+rUNzTuvWiAwza6A3tbXTMs/bqG+C9ewnrovpm7oRTLja92LuJ3pTl9O3aF4mxa1dEYStAXVg+wdtL6muiO5qy9Onbao2v6P1SfYjUPwU5Wxstv8l5oT9pb/3LV99S8= \ No newline at end of file From ecc92f79d1b4ab7d00d1933643516d599cf927b6 Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 14 Jan 2020 11:55:02 +0100 Subject: [PATCH 11/48] erreurs --- src/P2P_JAVA_PROJECT_SERVER/bonjour | 1 + src/clientP2P/ClientManagementUDP.class | Bin 3676 -> 3771 bytes src/clientP2P/ClientManagementUDP.java | 7 +++++-- src/clientP2P/ClientP2P.class | Bin 1552 -> 1562 bytes src/clientP2P/ClientP2P.java | 3 ++- src/serverP2P/ServerManagementUDP.class | Bin 3973 -> 4214 bytes src/serverP2P/ServerManagementUDP.java | 17 +++++++++++------ src/serverP2P/ServerP2P.class | Bin 1480 -> 1490 bytes src/serverP2P/ServerP2P.java | 3 ++- 9 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 src/P2P_JAVA_PROJECT_SERVER/bonjour diff --git a/src/P2P_JAVA_PROJECT_SERVER/bonjour b/src/P2P_JAVA_PROJECT_SERVER/bonjour new file mode 100644 index 0000000..1cd909e --- /dev/null +++ b/src/P2P_JAVA_PROJECT_SERVER/bonjour @@ -0,0 +1 @@ +bonjour diff --git a/src/clientP2P/ClientManagementUDP.class b/src/clientP2P/ClientManagementUDP.class index e4928ecaca96f1b6a29ab87d3ef3e18ad66c7359..de712c00f5a6c19210acfb75c1b0f12011b056f4 100644 GIT binary patch delta 1325 zcmY+D`%_e97{@2;Y<77+3G&vMYH;TX#+`JS)bU|Bji4bwc?r5ZT zQBjX5Y1zH(jzC~;LD^L+D=WJ>(=`3kAJB}yWc55pCa2kRzVGus&+~n5=h+T!g7T!g%uIfthDKNSjF9rXzo$CSK&UJ`wb7M$Ab=2=uud$ zmNg0w8G3CVHmtQ-XIO9Zh~ZJ2#|)3#Z0Ip;bU2F+!zP#(Q~Uq6`sb}X49{i z?KV3c^679G;0e|2bjVfiE{6+vQsF6u-G-+P&)7U`^PKuU@9+XIs`QfKWrqqS?Qy8Q zkXHnGRaLKPcdx7ThT%6Lz@8R$gtRkh(` zmrwZAN$S&!ysZp@FE&7kp_r?D7>~yL`hD zuOwVvk>FeP{?6rlj=KDylpi^2ahe~ty``hIY(cs!)zRwm6GvQr=AaUO8SW4NW}O{Q zE??QwzQQ7JX5A@5Vo_Uiak}!nCNFIdheMr0PkS>WWtBBp)RIL&XD53a(8>9yj8V=g zGFb7M>+1s8A}6?BaGuu}DY52znMko&z(Qa1dt|?8b~~*zZxH_i!pEsK+$f)-LEf9> zZIrjkmNYVFpHi9~%bPFGVYxxISY-Hu^St)#JrXR>oztVi&Lwn$nBg{g{-V(Ohf;!u z+YK%9U>O$8HY~QeL!4bgvbSf*Do$;479rzQ$UXAR0>aVqyxag?Rd++t}a zC_gAFiF&`f+Guu8p+R|OTX9-}&L#G}Bdiy5rX zkSn1}C;U+@k*QpBvhpU%$&pGnNIe@x+9b{QaSofgh%Lm~D*t3pHffvG-Y?y37wQff Y&Vb*d?9Opo4R`+6XRah@_s4SAzwPY;!T{K62em02Vvt{7m1X-2u>(guY@?;+zj zO*6e{kCyiSMoE@J7#Lenmc1C2z5P_b^e^;_{ZQ69PdLp!_j~R=_j|wR?78Qbmi?B@ zpFA@?1Jv=0M;Bha5o_Ow4xL@G`mj?|GDdS;}3La5- zRCk6Hb{K};JzJK^;(UsRCtRM?=~FIGtKIFff<4N7#>LmgUYC6yl@vXmhF&3FE@N;`I?iKZ76jYc;xp>-H@BbOEF*nc}c&fc=@%-v_U(_c2Dq^Q;GPE}vHpsY9Mu&`> zTnQs(3y$=Zr53Fha!gi8M~AeQf-U}N*+CI&war5+b|KNfNf|cF_ZLf@f5;Fv++x@w zACBSH)rKyY+k|Y1N(U~HraX_Nh{+{tFxgouRs)3-bz@>2{*S#)h;qjoTXr@PmLF87 z6MnKhJ*2!!kwxB(VY|GU0T#>aTEZZjpXA>mIRxP&qFX6Ca`W?Q4PryWT962(M<^>% z**QgOc3fPcAccw`*bb2y7U`XVWEiL7tlm%{D1A6Dctn^+PH#>fC3=kdTzZnr)|F@w z-OIC)64}m7W@d`z+35WI5@E)sxFQ>?id4lWxl&AYlB-Ir=+w#8Va1hGTr-v{u~2j} svq7oaXy;mQKF<`MpRcI7xShRhU|*1@^u%e}4TZDGO6?tbg8z2szmG59H~;_u diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 63d4374..3cd83c3 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -125,7 +125,7 @@ public class ClientManagementUDP implements Runnable { * @throws ProtocolError */ private void checkProtocolID(String msgProtocolID) throws ProtocolError { - if (protocolID != msgProtocolID) { + if (!protocolID.equals(msgProtocolID)) { throw new ProtocolError(); } } @@ -143,8 +143,11 @@ public class ClientManagementUDP implements Runnable { DatagramPacket reception = new DatagramPacket(buffer, buffer.length); DatagramPacket emission = new DatagramPacket(buffer, buffer.length, dst, UDPPort); socket.send(emission); + System.out.print(msg); socket.receive(reception); - return new String(reception.getData(), 0, reception.getLength()); + String tmp = new String(reception.getData(), 0, reception.getLength()); + System.out.print(tmp); + return tmp; } catch (Exception e){ throw new TransmissionError(); } diff --git a/src/clientP2P/ClientP2P.class b/src/clientP2P/ClientP2P.class index c0f85c682277a534fa42010e9a81951cb7f3fb42..bec0ac804cb39ec7ef021b6570724dae73f832a8 100644 GIT binary patch delta 174 zcmbQhGmB?}2)8tYIWq&p90yJY>51|I!a$0_kDbAv3rL%?GX!uk$WCl(XY`-U#rU2{ zQh)LB#L2GCsQ*WFxh=Fi1^4#Huf^!ob0x${@p_1|-!P Sv>7xQ^nuneG8lsmF#!OhejmI5 delta 162 zcmbQmGl6G<2)7i2IWq&p90yJYsfqFd!t4xwTnthSrtA#VrzN{?}u~0%6DJ)LF{V;Vrfarq?54o|#jinM=CYzSIv0RK5Zage{r5lg9@uMLJ!jq!cDXbU$lw9?+!ZSKL;#m0ue#$$qkL$dZR4jXt6 zdkwrVTQ9IKv$poSWqfd2Bve?S@PUCN_)y^^10SQ%9PP**@CiOOP=G=WNmHg5M}l60 zEQol+L2vQRp~(2j20p_94Gw?bL4yr^jxRL0d-fli=kMRWr-t*wEJM&cAsAss!Jvj@ zbE8UbXy8kHWuO=l17C}{5fjYc9KG!~A!2rOHnHDAJ;s?l>Sl3%BexqC;80$cE@h zCN$AXEf)&C$y*<~`;ikrykveRSuZ8)E6MpPGQ5?q!Zwy=CppVJq6W6VNwFc z(uK_lLqimAA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8M=&sP6neIAz3l@8iX7~@<9x( z!u0{0z{JHe*bQ}Yl}sd=V9miWCL(jt3&Er-NLPaWFGUwlKr+f`w2o$LFb`{4!+NWr zNI3pRd+AXFQs8KWmMzPXD_Rh2RU5?6eK~M8c@+a#uHj0sOn2XyCSdt7JU#XhQZ$q! zioysDN8k%-EJ?0`lohJ`F_NXGRW#HGsufk^b|oWTDWjd$O_LyOL}zRwteY_yTdb^G z$&1p$n!wvHA&wq=n*(`;lgqWXyGK~?21y1_b_OplAZ^6X;LXJ#J+Z2R(QD%GdrXr0lW#E^vhpyf zGN?`FX0q7q!StAsQF5{liz+8S12Y2?0|Ud7$x$rxgtWA`GDvI$a+w&kfTR@z-(*%+ zQ|84C@{=uD&G{rFw=+oj>}HUT)ZW4%F}aRaTV9cYgF%Twf#*@e}NPa<+V ugQU-H2B}ExEez6=J6N^l6&N@e6oHl~0ZC;Bbp{m%Z3a~aMg{}00fqonW*b!i diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 33338e3..5517180 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -24,9 +24,10 @@ public class ServerP2P { } else { d = "."; } - d += "P2P_JAVA_PROJECT_SERVER/"; + d += "/P2P_JAVA_PROJECT_SERVER/"; // create directory if not already exists new File(d).mkdirs(); + directory = d; } public static void main(String [] args) { ServerP2P s = new ServerP2P(); From 789c0c0257a1440172e7141a29786e009f5106a5 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 14 Jan 2020 12:33:21 +0100 Subject: [PATCH 12/48] Remove debugs print --- src/clientP2P/ClientManagementUDP.java | 7 ++----- src/clientP2P/ClientP2P.java | 1 + src/serverP2P/ServerManagementUDP.java | 7 ++----- src/serverP2P/ServerP2P.java | 1 + 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 3cd83c3..19756fb 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -48,7 +48,7 @@ public class ClientManagementUDP implements Runnable { msg = sendMsg(sendDownloadRequest(f)); download(msg, f); } catch (TransmissionError e) { - System.out.println("TransmissionError"); + System.out.println("Transmission error"); } catch (ProtocolError e) { System.out.println("Protocol error"); } catch (NotFound e) { @@ -143,11 +143,8 @@ public class ClientManagementUDP implements Runnable { DatagramPacket reception = new DatagramPacket(buffer, buffer.length); DatagramPacket emission = new DatagramPacket(buffer, buffer.length, dst, UDPPort); socket.send(emission); - System.out.print(msg); socket.receive(reception); - String tmp = new String(reception.getData(), 0, reception.getLength()); - System.out.print(tmp); - return tmp; + return new String(reception.getData(), 0, reception.getLength()); } catch (Exception e){ throw new TransmissionError(); } diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 862b2d6..7c9e7de 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -30,6 +30,7 @@ public class ClientP2P { // create directory if not already exists new File(d).mkdirs(); directory = d; + System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directory); } public static void main(String [] args) { diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index 6397248..c9506dd 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -67,19 +67,18 @@ public class ServerManagementUDP implements Runnable { * @return data to be send as response */ String processRequest(String request) { - System.out.print(request); String res = protocolID + "\n"; String formattedRequest[] = request.split("\n"); try { try { checkProtocolID(formattedRequest[0]); - System.out.print("juste avant le switch"); switch (formattedRequest[1]) { case "LIST": - System.out.print("liste detectee"); + System.out.println("List request"); res += sendFileList(); break; case "DOWNLOAD": + System.out.println("Download request: " + formattedRequest[2]); res += upload(formattedRequest[2]); break; default: @@ -102,7 +101,6 @@ public class ServerManagementUDP implements Runnable { /** Initialize local list of all files allowed to be shared. */ private void initFileList() { - System.out.println(baseDirectory); File folder = new File(baseDirectory); File[] files = folder.listFiles(); /* Add non-recursively files's names to fileList */ @@ -119,7 +117,6 @@ public class ServerManagementUDP implements Runnable { * @throws ProtocolError */ private void checkProtocolID(String msgProtocolID) throws ProtocolError { - System.out.print(msgProtocolID + "\n" + protocolID); if (!protocolID.equals(msgProtocolID)) { throw new ProtocolError(); } diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 5517180..399ad31 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -28,6 +28,7 @@ public class ServerP2P { // create directory if not already exists new File(d).mkdirs(); directory = d; + System.out.println("Server will listen on port " + port + " and serve files from " + directory); } public static void main(String [] args) { ServerP2P s = new ServerP2P(); From f2fecdca5499393e94bbd52e919b130b78a181ad Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 14 Jan 2020 12:34:25 +0100 Subject: [PATCH 13/48] Update documentation --- doc/classdiagram.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/classdiagram.xml b/doc/classdiagram.xml index 6848251..c99baab 100644 --- a/doc/classdiagram.xml +++ b/doc/classdiagram.xml @@ -1 +1 @@ -7Vxbd9o4EP41nLN9SI7vwGOBtNvdtOUkvWz3ZY+CFdDWWKwsEuivX8mWb5JsTGJDuus85NhCEpJm5ptvxmMG9nS9e0vAZvUe+zAYWIa/G9izgWWNHY/95w37pMEdj5OGJUF+0mTmDbfoBxSNhmjdIh9GpY4U44CiTblxgcMQLmipDRCCH8vd7nFQ/tYNWEKl4XYBArX1K/LpKmkdWcO8/VeIlqv0m01P7G8N0s5iJ9EK+Pix0GRfDewpwZgmV+vdFAb87NJzSca9qfg0WxiBIW0yYI+D5RX1FzePwe+frvZ3339F1xem2McDCLZixx/YgizjDd6Gvlg43aenET2idQBCdje5xyG9FZ+Y7B4EaBmy6wVbDiSs4QESithBvhYfULxhrYsVCvxrsMdbvuiIgsX39G6ywgT9YNOCQMzJPiZU6ITllXrc8pGs2WCtBEaszz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aanRpeaVqI5ANEIgXPmtGs172MclEJbKHTsCsc9UXgyBSPjpCQhEJX14Og140Q8mBq40kS1N1LY/txS1iH99oyuabg7rlTcoa0K9abaF9ba+bFr+iQv1jbiaJdVdPXVNR5LaX+OOpN+a7nQV1S2Kt25dS2s8QVtT3UvfROvKqluSu/r61HHrqu3/WImCXTsKP1YlK7vNfq85JnDZj17bl/8C \ No newline at end of file From 7e50b3874a77b9cd2a3a2d523b28eebc2e28d6c9 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 14 Jan 2020 12:37:46 +0100 Subject: [PATCH 14/48] Remove .class files and add it to .gitignore --- .gitignore | 1 + src/clientP2P/ClientManagementUDP.class | Bin 3771 -> 0 bytes src/clientP2P/ClientP2P.class | Bin 1562 -> 0 bytes src/serverP2P/ServerManagementUDP.class | Bin 4214 -> 0 bytes src/serverP2P/ServerP2P.class | Bin 1490 -> 0 bytes 5 files changed, 1 insertion(+) delete mode 100644 src/clientP2P/ClientManagementUDP.class delete mode 100644 src/clientP2P/ClientP2P.class delete mode 100644 src/serverP2P/ServerManagementUDP.class delete mode 100644 src/serverP2P/ServerP2P.class diff --git a/.gitignore b/.gitignore index 36020d3..d8aea1c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store *.swp +*.class bin project.geany diff --git a/src/clientP2P/ClientManagementUDP.class b/src/clientP2P/ClientManagementUDP.class deleted file mode 100644 index de712c00f5a6c19210acfb75c1b0f12011b056f4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3771 zcmaJ^`(G5-75@&q%Z!^5;|eHMl8xF33oA;DNr2e$Ox6NRfnZTnGb{tFEHmruENEL3 zZQ7(s^L{sdq_5Pzt2VMWwAy!@v`yQzO`rcqe(CFbW|oJtA3wmkXYM`c+;hI?+;cB) z{`d7u0QTTtVf4V#V6Q(kdFxYsD_WJI4KhMhf#qC zgq)I<(?UM3;z13cQ1M9(52<)q!>3d{qTx{$pVsi0ipRsa0hW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p`P94KZ0_eluL$JZB z)ClN0x?aO%Y$dQUf^`c<2>St6>L|u>kdPjtu0bpJBF|7y67>5C`a?{fK)VB>{~)H~ z2!H>L&C36n!hnkVR2ck)qGIfB6)BCz0G$<5$(&cS5~K$BiYh|CnWG>Yi0ru<RVcZ3uqmU$1fGo))Ff6jg%}e;7+DKC`sSP zV@jp>@hdcs_b;G*RG5x{(9Qz7T2xBZs3M{YEPhpauuKrp?xN#+v6g;3MsFS`_XIDQL~e= diff --git a/src/clientP2P/ClientP2P.class b/src/clientP2P/ClientP2P.class deleted file mode 100644 index bec0ac804cb39ec7ef021b6570724dae73f832a8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1562 zcmZux?^7FP5PseSa>sELIDQE&mRgD-X>zm{tx~aRfELUzNlF`AjYskt4wAcgxpeGD zXZ$17@ndHk{pO4$Rc0JN`N2QQanyY;N#~%QO!mFo-P?V3_u2Qae}DTOzzx(DEMis0 z+e0{ycY?t8x5d*<8Sg514{I`RDHy@~;$>aN2gA6HgoHaXk}@_VY$_PTst{W;?uzrC zjBOb^5gdou>?;Tc%~Y>kPs0@*a65?FwfYO_>(YwCelZ zDH!`T(v0L?qkNP$8s0lWqFTtkZkQIsbokNBDMdIXZ?~K>-!esrbIF>?EjM>P7Z>24tFB-rwe^#3| zEThWn)S&wtIT6G>RY~E%;YLO152=Xm#|s20CLqOnkW?Ir+%?fxUl+3bhcxa}s{GTY z%j*ooRqo~-yTKio$gmWC#TZ{!5o`DtEu+?CnDT$o&9k;l(+W{sZarZb4=;BEv|O{M z6Mu+`BVNDy3Zfo4pGn>iKcbzXdTC3jy6`I!`;hiN+!h-!<9FR{S(aF*HDQsO+|3ZhiP}xqa9muX zC10e=C5V971bPaHW$;d7l6Nw(G{oksMCn=26Zk380gGsXV+14Gz%d53&~r$d+{Vzi zs6L0H4gU%?fIl#jiv9^Ts-5#pSz#LWn=W@v-(l_qW5o`}qitx~1c^2#Q$Jwdubz6L z4rx;mH+0W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class b/src/serverP2P/ServerP2P.class deleted file mode 100644 index d2f34a8bcbd09eb89cb14996ccd25e09200849ca..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1490 zcmZuxTW=Fr5dMyByqnGDwjps68X%zsCyu?vrO*-zF-d@=cI?_FU~Xn(pE?WnuDf1G zMV0y^`qrxIOOg6q32bPjiYFfWld7sP#}~8-XytQ!X6DR%GxMGK=l{R{25=6$3g!{_ z1HY~eU>cvvIH%xqoR@Jy!63d6hc9JZ9K~_INQ*rqmTx3v83J|NaT(YK!$8?|c*(V$ zCk$Hhk+Em!Rl}<2IoC0*$}&Unl4+UlWd?tEVVkb5+GWl#k~A&8)vWDsCvWUjNivjk zjndcZhEfu#>AOyG9t{2x5`9$$XIz{!lqv9^^F^nG0iUEX7 z7%_wnE3Y=qYMDDK?&AUZ)MHh5Y$uADYETh6}+;VTOWrUORflTtd z4mZj|PK66~FP|hxQ2{C1gQVhH5xau$fxGFQN7V8IqTG{)%WDjS74BvnyUrb#2yr_6 zfv&wTB9id$nntz3FyXz?jkCOPhYZLsxAqvu!V6soP1mgIL>HpiP^Vsf0Wpn?4+ZbL z@&xaR$hT3ibBoFo^#<<|xEIaC>0W~=uI$=fD~6+eSRO;NIFL@?BhfZkdra?!@});X z*-d^R@P?pw$g9XUEla$WWucK8+}$EV6OkLP;kcNir#nxh!bcxbzVnax(%G4K3B=h2 zBJK$MWC1)~Y=bRozUT03ffqQU1zV7QM&$)$ZQvyoA6{WF8F>vgqNyEKTBwG+s#9In zFPPcKaG{NnNDHId7%eRt27h4ID;|F*4r=2`ujXh^vyX{Fq>afILbnzp&oT992mKVMJL&iP{ zOA?~M*?%$lpEyRzVGMe=I5Y Date: Tue, 14 Jan 2020 12:39:18 +0100 Subject: [PATCH 15/48] Remove test folders --- .gitignore | 2 ++ src/P2P_JAVA_PROJECT_SERVER/bonjour | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) delete mode 100644 src/P2P_JAVA_PROJECT_SERVER/bonjour diff --git a/.gitignore b/.gitignore index d8aea1c..983da03 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ *.class bin project.geany +P2P_JAVA_PROJECT_SERVER +P2P_JAVA_PROJECT_CLIENT diff --git a/src/P2P_JAVA_PROJECT_SERVER/bonjour b/src/P2P_JAVA_PROJECT_SERVER/bonjour deleted file mode 100644 index 1cd909e..0000000 --- a/src/P2P_JAVA_PROJECT_SERVER/bonjour +++ /dev/null @@ -1 +0,0 @@ -bonjour From b4927e19949c658e569e539ffc4925f1a338d865 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 15 Jan 2020 21:57:48 +0100 Subject: [PATCH 16/48] Fix part of protocol but still bugged --- src/clientP2P/ClientManagementUDP.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 19756fb..8561a74 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -113,7 +113,7 @@ public class ClientManagementUDP implements Runnable { try { String r[] = response.split("\n"); checkProtocolID(r[0]); - return response.split(protocolID + "\nLOAD \n")[1]; + return response.split(protocolID + "\nLIST\n")[1]; } catch (java.lang.ArrayIndexOutOfBoundsException e) { throw new ProtocolError(); } From bb1df852736f62928de6d8e1a1eeac3a81a39b6f Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 15 Jan 2020 21:58:11 +0100 Subject: [PATCH 17/48] Add default user data folder for mac --- src/clientP2P/ClientP2P.java | 6 +++++- src/serverP2P/ServerP2P.java | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 7c9e7de..b32aa63 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -23,7 +23,11 @@ public class ClientP2P { d += "."; } } - } else { + } else if (os.equals("Mac")||os.equals("Mac OS X") { + /* Apple MacOS X User Data Directory + * https://developer.apple.com/library/archive/qa/qa1170/_index.html */ + d = System.getProperty("user.home") + "/Library/"; + } else { d = "."; } d += "/P2P_JAVA_PROJECT_CLIENT/"; diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 399ad31..6587423 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -11,7 +11,8 @@ public class ServerP2P { /* Follow XDG Base Directory Specification * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html */ - if (System.getProperty("os.name").equals("Linux")) { + String os = System.getProperty("os.name"); + if (os.equals("Linux")) { d = System.getenv().get("XDG_DATA_HOME"); if (d == null || d.equals("")) { d = System.getenv().get("HOME"); @@ -21,6 +22,10 @@ public class ServerP2P { d += "."; } } + } else if (os.equals("Mac")||os.equals("Mac OS X") { + /* Apple MacOS X User Data Directory + * https://developer.apple.com/library/archive/qa/qa1170/_index.html */ + d = System.getProperty("user.home") + "/Library/"; } else { d = "."; } From 14b7a2b814cb43dfa101fcb9065fbf67f27c0bc7 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 15 Jan 2020 22:20:27 +0100 Subject: [PATCH 18/48] Fix last commit --- src/clientP2P/ClientP2P.java | 2 +- src/serverP2P/ServerP2P.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index b32aa63..7a74036 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -26,7 +26,7 @@ public class ClientP2P { } else if (os.equals("Mac")||os.equals("Mac OS X") { /* Apple MacOS X User Data Directory * https://developer.apple.com/library/archive/qa/qa1170/_index.html */ - d = System.getProperty("user.home") + "/Library/"; + d = System.getProperty("user.home") + "/Library"; } else { d = "."; } diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 6587423..7725619 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -25,7 +25,7 @@ public class ServerP2P { } else if (os.equals("Mac")||os.equals("Mac OS X") { /* Apple MacOS X User Data Directory * https://developer.apple.com/library/archive/qa/qa1170/_index.html */ - d = System.getProperty("user.home") + "/Library/"; + d = System.getProperty("user.home") + "/Library"; } else { d = "."; } From 0e1a451214fa84674914ed0e292b124b1391834d Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 16 Jan 2020 12:35:06 +0100 Subject: [PATCH 19/48] Avoid duplication of code --- src/clientP2P/ClientP2P.java | 33 +++++------------------------ src/serverP2P/ServerP2P.java | 34 +++++------------------------- src/tools/Directories.java | 40 ++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 57 deletions(-) create mode 100644 src/tools/Directories.java diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 7a74036..396d7ec 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -1,45 +1,22 @@ package clientP2P; import java.io.File; import clientP2P.ClientManagementUDP; +import tools.Directories; public class ClientP2P { private String host; private int port; - private String directory; + private String dataHomeDirectory; public ClientP2P() { host = "localhost"; 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 || d.equals("")) { - d = System.getenv().get("HOME"); - if (d != null && (!d.equals(""))) { - d += "/.local/share"; - } else { - d += "."; - } - } - } else if (os.equals("Mac")||os.equals("Mac OS X") { - /* Apple MacOS X User Data Directory - * https://developer.apple.com/library/archive/qa/qa1170/_index.html */ - d = System.getProperty("user.home") + "/Library"; - } else { - d = "."; - } - d += "/P2P_JAVA_PROJECT_CLIENT/"; - // create directory if not already exists - new File(d).mkdirs(); - directory = d; - System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directory); + dataHomeDirectory = new Directories("P2P_JAVA_PROJECT_CLIENT").getDataHomeDirectory(); + System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + dataHomeDirectory); } public static void main(String [] args) { ClientP2P c = new ClientP2P(); - ClientManagementUDP cm = new ClientManagementUDP(c.directory, c.host, c.port); + ClientManagementUDP cm = new ClientManagementUDP(c.dataHomeDirectory, c.host, c.port); Thread t = new Thread(cm); t.setName("client P2P-JAVA-PROJECT"); t.start(); diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 7725619..c147ad8 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -1,43 +1,19 @@ package serverP2P; import java.io.File; import serverP2P.ServerManagementUDP; +import tools.Directories; public class ServerP2P { private int port; - private String directory; + private String dataHomeDirectory; public ServerP2P() { port = 40000; - String d; - /* Follow XDG Base Directory Specification - * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html - */ - String os = System.getProperty("os.name"); - if (os.equals("Linux")) { - d = System.getenv().get("XDG_DATA_HOME"); - if (d == null || d.equals("")) { - d = System.getenv().get("HOME"); - if (d != null && (!d.equals(""))) { - d += "/.local/share"; - } else { - d += "."; - } - } - } else if (os.equals("Mac")||os.equals("Mac OS X") { - /* Apple MacOS X User Data Directory - * https://developer.apple.com/library/archive/qa/qa1170/_index.html */ - d = System.getProperty("user.home") + "/Library"; - } else { - d = "."; - } - d += "/P2P_JAVA_PROJECT_SERVER/"; - // create directory if not already exists - new File(d).mkdirs(); - directory = d; - System.out.println("Server will listen on port " + port + " and serve files from " + directory); + dataHomeDirectory = new Directories("P2P_JAVA_PROJECT_SERVER").getDataHomeDirectory(); + System.out.println("Server will listen on port " + port + " and serve files from " + dataHomeDirectory); } public static void main(String [] args) { ServerP2P s = new ServerP2P(); - ServerManagementUDP sm = new ServerManagementUDP(s.directory, s.port); + ServerManagementUDP sm = new ServerManagementUDP(s.dataHomeDirectory, s.port); Thread t = new Thread(sm); t.setName("server P2P-JAVA-PROJECT"); t.start(); diff --git a/src/tools/Directories.java b/src/tools/Directories.java new file mode 100644 index 0000000..f4e8ead --- /dev/null +++ b/src/tools/Directories.java @@ -0,0 +1,40 @@ +package tools; + +public class Directories { + private String projectName; + private String dataHomeDirectory; + + public Directories(String projectName) { + this.projectName = projectName; + setDataHomeDirectory(); + } + private void setDataHomeDirectory() { + /* Follow XDG Base Directory Specification + * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html + */ + String os = System.getProperty("os.name"); + if (os.equals("Linux")) { + d = System.getenv().get("XDG_DATA_HOME"); + if (d == null || d.equals("")) { + d = System.getProperty("user.home") + "/.local/share"; + } + } else if (os.equals("Mac")||os.equals("Mac OS X") { + /* Apple MacOS X User Data Directory + * https://developer.apple.com/library/archive/qa/qa1170/_index.html */ + d = System.getProperty("user.home") + "/Library"; + } else { + d = "."; + } + d += "/" + projectName + "/"; + // create directory if not already exists + new File(d).mkdirs(); + dataHomeDirectory = d; + } + + public String getDataHomeDirectory() { + return dataHomeDirectory(); + } + + + +} From 20fef41d7166c997a827db9f23105b2bccd99879 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 16 Jan 2020 12:40:42 +0100 Subject: [PATCH 20/48] Add doc to Directories.java --- src/tools/Directories.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/tools/Directories.java b/src/tools/Directories.java index f4e8ead..3782a2e 100644 --- a/src/tools/Directories.java +++ b/src/tools/Directories.java @@ -1,13 +1,25 @@ package tools; +/** Helper to get application directories. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ public class Directories { private String projectName; private String dataHomeDirectory; - + + /** Constructor with projectName parameter. + * @param projectName name of the project + */ public Directories(String projectName) { this.projectName = projectName; setDataHomeDirectory(); } + + /** Setter for dataHomeDirectory. Will create the directory if not already exists. + */ private void setDataHomeDirectory() { /* Follow XDG Base Directory Specification * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html @@ -31,6 +43,9 @@ public class Directories { dataHomeDirectory = d; } + /** Getter for dataHomeDirectory. + * @return path to the application home directory + */ public String getDataHomeDirectory() { return dataHomeDirectory(); } From c1ac428fc67cfe13f9991026f7debac4e8075b01 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 16 Jan 2020 12:43:12 +0100 Subject: [PATCH 21/48] Fix errors on Directories.java --- src/clientP2P/ClientP2P.java | 1 - src/serverP2P/ServerP2P.java | 1 - src/tools/Directories.java | 6 ++++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 396d7ec..8d7e678 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -1,5 +1,4 @@ package clientP2P; -import java.io.File; import clientP2P.ClientManagementUDP; import tools.Directories; diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index c147ad8..18c11f4 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -1,5 +1,4 @@ package serverP2P; -import java.io.File; import serverP2P.ServerManagementUDP; import tools.Directories; diff --git a/src/tools/Directories.java b/src/tools/Directories.java index 3782a2e..959bad9 100644 --- a/src/tools/Directories.java +++ b/src/tools/Directories.java @@ -1,4 +1,5 @@ package tools; +import java.io.File; /** Helper to get application directories. * @author Louis Royer @@ -24,13 +25,14 @@ public class Directories { /* Follow XDG Base Directory Specification * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html */ + String d; String os = System.getProperty("os.name"); if (os.equals("Linux")) { d = System.getenv().get("XDG_DATA_HOME"); if (d == null || d.equals("")) { d = System.getProperty("user.home") + "/.local/share"; } - } else if (os.equals("Mac")||os.equals("Mac OS X") { + } else if (os.equals("Mac")||os.equals("Mac OS X")) { /* Apple MacOS X User Data Directory * https://developer.apple.com/library/archive/qa/qa1170/_index.html */ d = System.getProperty("user.home") + "/Library"; @@ -47,7 +49,7 @@ public class Directories { * @return path to the application home directory */ public String getDataHomeDirectory() { - return dataHomeDirectory(); + return dataHomeDirectory; } From 019d8b321f9040782a01473b418170aca7111401 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 16 Jan 2020 13:04:22 +0100 Subject: [PATCH 22/48] Fix listing of files --- src/clientP2P/ClientManagementUDP.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 8561a74..36cb5e5 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -139,13 +139,15 @@ public class ClientManagementUDP implements Runnable { try{ InetAddress dst = InetAddress.getByName(host); byte [] buffer = msg.getBytes(); + byte [] buffer2 = new byte[1500]; DatagramSocket socket = new DatagramSocket(); - DatagramPacket reception = new DatagramPacket(buffer, buffer.length); + DatagramPacket reception = new DatagramPacket(buffer2, 1500); DatagramPacket emission = new DatagramPacket(buffer, buffer.length, dst, UDPPort); socket.send(emission); socket.receive(reception); return new String(reception.getData(), 0, reception.getLength()); } catch (Exception e){ + System.out.println(e); throw new TransmissionError(); } From d261bd1fa034a13e31bebb5a04071edd9adc14fe Mon Sep 17 00:00:00 2001 From: js Date: Tue, 21 Jan 2020 10:11:42 +0100 Subject: [PATCH 23/48] Update .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 983da03..84658cf 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ bin project.geany P2P_JAVA_PROJECT_SERVER P2P_JAVA_PROJECT_CLIENT +.classpath +.project From f1013e4042664357c888b9bac89a55806e4f6ed0 Mon Sep 17 00:00:00 2001 From: js Date: Tue, 21 Jan 2020 10:22:33 +0100 Subject: [PATCH 24/48] added eclipse gitignore --- .gitignore | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/.gitignore b/.gitignore index 84658cf..4da5402 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,102 @@ P2P_JAVA_PROJECT_SERVER P2P_JAVA_PROJECT_CLIENT .classpath .project + +# Created by https://www.gitignore.io/api/java,eclipse +# Edit at https://www.gitignore.io/?templates=java,eclipse + +### Eclipse ### +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.settings/ +.loadpath +.recommenders + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# PyDev specific (Python IDE for Eclipse) +*.pydevproject + +# CDT-specific (C/C++ Development Tooling) +.cproject + +# CDT- autotools +.autotools + +# Java annotation processor (APT) +.factorypath + +# PDT-specific (PHP Development Tools) +.buildpath + +# sbteclipse plugin +.target + +# Tern plugin +.tern-project + +# TeXlipse plugin +.texlipse + +# STS (Spring Tool Suite) +.springBeans + +# Code Recommenders +.recommenders/ + +# Annotation Processing +.apt_generated/ + +# Scala IDE specific (Scala & Java development for Eclipse) +.cache-main +.scala_dependencies +.worksheet + +### Eclipse Patch ### +# Eclipse Core +.project + +# JDT-specific (Eclipse Java Development Tools) +.classpath + +# Annotation Processing +.apt_generated + +.sts4-cache/ + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# End of https://www.gitignore.io/api/java,eclipse From 72ce99fd6ffa8bd5cea693517a3597e7200bd7f5 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 10:37:21 +0100 Subject: [PATCH 25/48] Fix NOT FOUND --- src/clientP2P/ClientManagementUDP.java | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 36cb5e5..68796ec 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -78,18 +78,22 @@ public class ClientManagementUDP implements Runnable { try { String r[] = response.split("\n", 3); checkProtocolID(r[0]); - String r2[] = r[1].split(" "); - if (r2[0] != "LOAD") { + String r2[] = r[1].split("\n"); + switch (r2[0]) { + case "LOAD": + int size = Integer.parseInt(r2[1]); + if (r[2].length() != size) { + throw new TransmissionError(); + } + FileWriter fileWriter = new FileWriter(baseDirectory + filename); + fileWriter.write(r[2]); + fileWriter.close(); + break; + case "NOT FOUND": + throw new NotFound(); + default: throw new ProtocolError(); } - int size = Integer.parseInt(r2[1]); - if (r[2].length() != size) { - throw new TransmissionError(); - } - FileWriter fileWriter = new FileWriter(baseDirectory + filename); - fileWriter.write(r[2]); - fileWriter.close(); - } catch (java.lang.ArrayIndexOutOfBoundsException e) { throw new ProtocolError(); } catch (NumberFormatException e) { @@ -113,7 +117,7 @@ public class ClientManagementUDP implements Runnable { try { String r[] = response.split("\n"); checkProtocolID(r[0]); - return response.split(protocolID + "\nLIST\n")[1]; + return response.split(protocolID + "\nLIST\n")[1].split("\n\n")[0]; } catch (java.lang.ArrayIndexOutOfBoundsException e) { throw new ProtocolError(); } From f3d47433e17bb004db47b5db146f9af62d4bfa59 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 10:55:20 +0100 Subject: [PATCH 26/48] Update project --- doc/protocol.md | 2 +- src/clientP2P/ClientManagementUDP.java | 16 ++++++++++------ src/serverP2P/ServerManagementUDP.java | 7 ++++--- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/doc/protocol.md b/doc/protocol.md index 51fed50..addb917 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -7,6 +7,6 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p ## Server responses - The response to `LIST` request is in the format `LIST\n\n\n[…]\n\n` -- The response to `DOWNLOAD` request is `LOAD \n` or `NOT FOUND\n` if the file doesn't exists. +- The response to `DOWNLOAD` request is `LOAD\n\n` or `NOT FOUND\n` if the file doesn't exists. - The server send a `PROTOCOL ERROR\n` message if it doesn't understands what the client sent. - The server send a `INTERNAL ERROR\n` message if it encounters an internal error. diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 68796ec..486c541 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -76,27 +76,31 @@ public class ClientManagementUDP implements Runnable { */ private void download(String response, String filename) throws TransmissionError, NotFound, ProtocolError, InternalError, IOException { try { - String r[] = response.split("\n", 3); + String r[] = response.split("\n"); checkProtocolID(r[0]); - String r2[] = r[1].split("\n"); - switch (r2[0]) { + switch (r[1]) { case "LOAD": - int size = Integer.parseInt(r2[1]); - if (r[2].length() != size) { + int size = Integer.parseInt(r[2]); + if (r[3].length() != size + 1) { throw new TransmissionError(); } FileWriter fileWriter = new FileWriter(baseDirectory + filename); - fileWriter.write(r[2]); + fileWriter.write(r[3]); fileWriter.close(); break; case "NOT FOUND": throw new NotFound(); + case "INTERNAL ERROR": + throw new InternalError(); default: + System.out.println("Error: Unknow format `" + r[1] + "`"); throw new ProtocolError(); } } catch (java.lang.ArrayIndexOutOfBoundsException e) { + System.out.println("Error: IndexOutOfBonds"); throw new ProtocolError(); } catch (NumberFormatException e) { + System.out.println("Error: NumberFormat"); throw new ProtocolError(); } } diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index c9506dd..7f09964 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -129,13 +129,14 @@ public class ServerManagementUDP implements Runnable { * @throws InternalError */ private String upload(String filename) throws NotFound, InternalError { - File file = new File(filename); + File file = new File(baseDirectory + filename); + System.out.println("Uploading `" + baseDirectory + filename + "`"); if (!file.exists() || !file.isFile()) { throw new NotFound(); } - String res = "LOAD " + file.length() + "\n"; + String res = "LOAD\n" + file.length() + "\n"; try { - res += new String(Files.readAllBytes(Paths.get(filename))); + res += new String(Files.readAllBytes(Paths.get(baseDirectory + filename))); } catch (IOException e) { throw new InternalError(); } From db3796c384a3f210b3fe9587a9979fda8b2ed5d5 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 10:57:27 +0100 Subject: [PATCH 27/48] Fix upload --- src/clientP2P/ClientManagementUDP.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 486c541..a64aa48 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -81,7 +81,7 @@ public class ClientManagementUDP implements Runnable { switch (r[1]) { case "LOAD": int size = Integer.parseInt(r[2]); - if (r[3].length() != size + 1) { + if (r[3].length() != size - 1) { throw new TransmissionError(); } FileWriter fileWriter = new FileWriter(baseDirectory + filename); From de96d7b661e3f37cf3ef6d4c8ee943f043064ecb Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 11:21:34 +0100 Subject: [PATCH 28/48] Add functionnality openning in finder --- src/clientP2P/ClientP2P.java | 16 ++++++++++++++-- src/tools/Directories.java | 18 +++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 8d7e678..e409d48 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -1,16 +1,28 @@ package clientP2P; import clientP2P.ClientManagementUDP; import tools.Directories; +import java.util.Scanner; public class ClientP2P { private String host; private int port; private String dataHomeDirectory; public ClientP2P() { + Directories d = new Directories("P2P_JAVA_PROJECT_CLIENT"); + String os = System.getProperty("os.name"); + host = "localhost"; port = 40000; - dataHomeDirectory = new Directories("P2P_JAVA_PROJECT_CLIENT").getDataHomeDirectory(); - System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + dataHomeDirectory); + dataHomeDirectory = d.getDataHomeDirectory(); + System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + dataHomeDirectory); + if (os.equals("Linux")||os.equals("Mac")||os.equals("Mac OS X")) { + System.out.println("Do you want to open this directory? (y/N)"); + Scanner scanner = new Scanner(System.in); + String resp = scanner.nextLine(); + if (resp == "y" || resp == "Y") { + d.openDataHomeDirectory(); + } + } } public static void main(String [] args) { diff --git a/src/tools/Directories.java b/src/tools/Directories.java index 959bad9..42543a8 100644 --- a/src/tools/Directories.java +++ b/src/tools/Directories.java @@ -1,5 +1,8 @@ package tools; import java.io.File; +import java.lang.Runtime; +import java.io.IOException; + /** Helper to get application directories. * @author Louis Royer @@ -51,7 +54,20 @@ public class Directories { public String getDataHomeDirectory() { return dataHomeDirectory; } - + + public void openDataHomeDirectory() { + try { + String os = System.getProperty("os.name"); + + if (os.equals("Linux")) { + Runtime runtime = Runtime.getRuntime(); + runtime.exec(new String[] { "xdg-open", dataHomeDirectory }); + } else if (os.equals("Mac")||os.equals("Mac OS X")) { + Runtime runtime = Runtime.getRuntime(); + runtime.exec(new String[] { "open", dataHomeDirectory }); + } + } catch (IOException e) {} + } } From a4bb32bf440e682372fb60bbe0f92fd10802ffd3 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 11:23:55 +0100 Subject: [PATCH 29/48] Fix last commit --- src/clientP2P/ClientP2P.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index e409d48..e94299a 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -19,7 +19,8 @@ public class ClientP2P { System.out.println("Do you want to open this directory? (y/N)"); Scanner scanner = new Scanner(System.in); String resp = scanner.nextLine(); - if (resp == "y" || resp == "Y") { + if (resp.equals("y") || resp.equals("Y")) { + System.out.println("Openning"); d.openDataHomeDirectory(); } } From 3a27d84690b81efd2b168417478969c39da99f62 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 11:32:01 +0100 Subject: [PATCH 30/48] Add openning functionnality to server --- src/serverP2P/ServerP2P.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 18c11f4..601c54e 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -1,14 +1,27 @@ package serverP2P; import serverP2P.ServerManagementUDP; import tools.Directories; +import java.util.Scanner; public class ServerP2P { private int port; private String dataHomeDirectory; public ServerP2P() { + Directories d = new Directories("P2P_JAVA_PROJECT_SERVER"); + String os = System.getProperty("os.name"); + port = 40000; - dataHomeDirectory = new Directories("P2P_JAVA_PROJECT_SERVER").getDataHomeDirectory(); + dataHomeDirectory = d.getDataHomeDirectory(); System.out.println("Server will listen on port " + port + " and serve files from " + dataHomeDirectory); + if (os.equals("Linux")||os.equals("Mac")||os.equals("Mac OS X")) { + System.out.println("Do you want to open this directory? (y/N)"); + Scanner scanner = new Scanner(System.in); + String resp = scanner.nextLine(); + if (resp.equals("y") || resp.equals("Y")) { + System.out.println("Openning"); + d.openDataHomeDirectory(); + } + } } public static void main(String [] args) { ServerP2P s = new ServerP2P(); From 8f239e23f5775d3e55131974f673d502dd1e9901 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 12:43:02 +0100 Subject: [PATCH 31/48] Proposition version 1.1 en mode binaire MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ça sera plus facile à parser et plus facile de découper en blocs de 4KB plus tard. --- doc/protocol.md | 53 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/doc/protocol.md b/doc/protocol.md index addb917..29622e2 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -7,6 +7,57 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p ## Server responses - The response to `LIST` request is in the format `LIST\n\n\n[…]\n\n` -- The response to `DOWNLOAD` request is `LOAD\n\n` or `NOT FOUND\n` if the file doesn't exists. +- The response to `DOWNLOAD` request is `LOAD\n\n\n` or `NOT FOUND\n` if the file doesn't exists. - The server send a `PROTOCOL ERROR\n` message if it doesn't understands what the client sent. - The server send a `INTERNAL ERROR\n` message if it encounters an internal error. + +# P2P-JAVA-PROJECT version 1.1 (Binary protocol for step 1) + +```Datagram format + +[0-7: VERSION(0x11, first quartet is major version, second is minor)] +[8-15: REQUEST/RESPONSE CODE] +[16-31: RESERVED FOR FUTURE USE] +[32-63: PAYLOAD SIZE IN BYTES] +[64-xx: PAYLOAD] + +``` + +## Requests and responses codes +- REQUESTS (msb is 0): + - `LIST` (0x00) + - `DOWNLOAD` (0x01) + +- RESPONSES (msb is 1): + - `LIST` (0x80) + - `LOAD` (0x81) + - `NOT FOUND` (0x82) + - `PROTOCOL ERROR` (0x83) + - `INTERNAL ERROR` (0x84) + +### List +Payload size for list request is always zero. +Payload for list response is filenames separated by `\n`. + +### Download +#### Not found +Response when the file requested is not found on the server. +Payload size for Not found is zero + +#### Protocol error +Response when the request cannot be interpreted. +Payload size for Protocol error is zero + +#### Internal error +Response in internal failure case. +Payload size for Internal error is zero + +#### Load response +Payload contains + +``` +[64-127: OFFSET OF FILE CONTENT IN BYTES] +[128-159: TOTAL FILESIZE] +[\n] +[FILE CONTENT] +``` From b3cc441d9ec5d2b3904219f17291ead7ba744a2d Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 19:48:46 +0100 Subject: [PATCH 32/48] Factorize code for opening directory --- src/clientP2P/ClientP2P.java | 21 ++++------------- src/serverP2P/ServerP2P.java | 18 ++++----------- src/tools/Directories.java | 45 ++++++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 45 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index e94299a..1d09a0c 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -1,34 +1,23 @@ package clientP2P; import clientP2P.ClientManagementUDP; import tools.Directories; -import java.util.Scanner; + public class ClientP2P { private String host; private int port; private String dataHomeDirectory; public ClientP2P() { - Directories d = new Directories("P2P_JAVA_PROJECT_CLIENT"); - String os = System.getProperty("os.name"); - + Directories directories = new Directories("P2P_JAVA_PROJECT_CLIENT"); host = "localhost"; port = 40000; - dataHomeDirectory = d.getDataHomeDirectory(); - System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + dataHomeDirectory); - if (os.equals("Linux")||os.equals("Mac")||os.equals("Mac OS X")) { - System.out.println("Do you want to open this directory? (y/N)"); - Scanner scanner = new Scanner(System.in); - String resp = scanner.nextLine(); - if (resp.equals("y") || resp.equals("Y")) { - System.out.println("Openning"); - d.openDataHomeDirectory(); - } - } + System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directories.getDataHomeDirectory()); + directories.askOpenDataHomeDirectory(); } public static void main(String [] args) { ClientP2P c = new ClientP2P(); - ClientManagementUDP cm = new ClientManagementUDP(c.dataHomeDirectory, c.host, c.port); + ClientManagementUDP cm = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.host, c.port); Thread t = new Thread(cm); t.setName("client P2P-JAVA-PROJECT"); t.start(); diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 601c54e..c681d66 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -1,31 +1,21 @@ package serverP2P; import serverP2P.ServerManagementUDP; import tools.Directories; -import java.util.Scanner; public class ServerP2P { private int port; private String dataHomeDirectory; public ServerP2P() { - Directories d = new Directories("P2P_JAVA_PROJECT_SERVER"); - String os = System.getProperty("os.name"); + Directories directories = new Directories("P2P_JAVA_PROJECT_SERVER"); port = 40000; dataHomeDirectory = d.getDataHomeDirectory(); - System.out.println("Server will listen on port " + port + " and serve files from " + dataHomeDirectory); - if (os.equals("Linux")||os.equals("Mac")||os.equals("Mac OS X")) { - System.out.println("Do you want to open this directory? (y/N)"); - Scanner scanner = new Scanner(System.in); - String resp = scanner.nextLine(); - if (resp.equals("y") || resp.equals("Y")) { - System.out.println("Openning"); - d.openDataHomeDirectory(); - } - } + System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory()); + directories.askOpenDataHomeDirectory(); } public static void main(String [] args) { ServerP2P s = new ServerP2P(); - ServerManagementUDP sm = new ServerManagementUDP(s.dataHomeDirectory, s.port); + ServerManagementUDP sm = new ServerManagementUDP(s.directories.getDataHomeDirectory(), s.port); Thread t = new Thread(sm); t.setName("server P2P-JAVA-PROJECT"); t.start(); diff --git a/src/tools/Directories.java b/src/tools/Directories.java index 42543a8..92a18b7 100644 --- a/src/tools/Directories.java +++ b/src/tools/Directories.java @@ -1,4 +1,5 @@ package tools; +import java.util.Scanner; import java.io.File; import java.lang.Runtime; import java.io.IOException; @@ -13,12 +14,14 @@ import java.io.IOException; public class Directories { private String projectName; private String dataHomeDirectory; + private String os; /** Constructor with projectName parameter. * @param projectName name of the project */ public Directories(String projectName) { this.projectName = projectName; + os = System.getProperty("os.name"); setDataHomeDirectory(); } @@ -28,24 +31,21 @@ public class Directories { /* Follow XDG Base Directory Specification * https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html */ - String d; - String os = System.getProperty("os.name"); if (os.equals("Linux")) { - d = System.getenv().get("XDG_DATA_HOME"); - if (d == null || d.equals("")) { - d = System.getProperty("user.home") + "/.local/share"; + dataHomeDirectory = System.getenv().get("XDG_DATA_HOME"); + if (dataHomeDirectory == null || dataHomeDirectory.equals("")) { + dataHomeDirectory = System.getProperty("user.home") + "/.local/share"; } } else if (os.equals("Mac")||os.equals("Mac OS X")) { /* Apple MacOS X User Data Directory * https://developer.apple.com/library/archive/qa/qa1170/_index.html */ - d = System.getProperty("user.home") + "/Library"; + dataHomeDirectory = System.getProperty("user.home") + "/Library"; } else { - d = "."; + dataHomeDirectory = "."; } - d += "/" + projectName + "/"; + dataHomeDirectory += "/" + projectName + "/"; // create directory if not already exists - new File(d).mkdirs(); - dataHomeDirectory = d; + new File(dataHomeDirectory).mkdirs(); } /** Getter for dataHomeDirectory. @@ -55,10 +55,10 @@ public class Directories { return dataHomeDirectory; } - public void openDataHomeDirectory() { + /** Opens dataHomeDirectory if supported. + */ + private void openDataHomeDirectory() { try { - String os = System.getProperty("os.name"); - if (os.equals("Linux")) { Runtime runtime = Runtime.getRuntime(); runtime.exec(new String[] { "xdg-open", dataHomeDirectory }); @@ -66,8 +66,23 @@ public class Directories { Runtime runtime = Runtime.getRuntime(); runtime.exec(new String[] { "open", dataHomeDirectory }); } - } catch (IOException e) {} + } catch (IOException e) { + System.out.println("Error encountered while trying to open directory"); + } } - + + /** Asks the user to choose opening dataHomeDirectory or not. + */ + public void askOpenDataHomeDirectory() { + if (os.equals("Linux") || os.equals("Mac") || os.equals("Mac OS X")) { + System.out.println("Do you want to open this directory? (y/N)"); + Scanner scanner = new Scanner(System.in); + String resp = scanner.nextLine(); + if (resp.equals("y") || resp.equals("Y")) { + System.out.println("Openning"); + openDataHomeDirectory(); + } + } + } From f4e318dfe5ad021b2df005cb2c049122f9b888f5 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 19:56:50 +0100 Subject: [PATCH 33/48] Fix last commit --- src/clientP2P/ClientP2P.java | 4 ++-- src/serverP2P/ServerP2P.java | 7 +++---- src/tools/Directories.java | 1 + 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 1d09a0c..32c2641 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -6,9 +6,9 @@ import tools.Directories; public class ClientP2P { private String host; private int port; - private String dataHomeDirectory; + private Directories directories; public ClientP2P() { - Directories directories = new Directories("P2P_JAVA_PROJECT_CLIENT"); + directories = new Directories("P2P_JAVA_PROJECT_CLIENT"); host = "localhost"; port = 40000; System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directories.getDataHomeDirectory()); diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index c681d66..00be061 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -4,12 +4,11 @@ import tools.Directories; public class ServerP2P { private int port; - private String dataHomeDirectory; - public ServerP2P() { - Directories directories = new Directories("P2P_JAVA_PROJECT_SERVER"); + private Directories directories; + public ServerP2P() { + directories = new Directories("P2P_JAVA_PROJECT_SERVER"); port = 40000; - dataHomeDirectory = d.getDataHomeDirectory(); System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory()); directories.askOpenDataHomeDirectory(); } diff --git a/src/tools/Directories.java b/src/tools/Directories.java index 92a18b7..da6363f 100644 --- a/src/tools/Directories.java +++ b/src/tools/Directories.java @@ -83,6 +83,7 @@ public class Directories { openDataHomeDirectory(); } } + } } From 026008473ac6eaf0cb74bd2a901e35b0ad5ce95f Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 23:47:43 +0100 Subject: [PATCH 34/48] =?UTF-8?q?D=C3=A9but=20de=20l=E2=80=99impl=C3=A9men?= =?UTF-8?q?tation=20du=20protocole=20binaire?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reste des TODO dans FileList.java et dans FilePart.java + la documentation à faire. Ça ne compile pas encore, et c’est normal (à cause des TODOs). --- doc/protocol.md | 11 +++-- src/protocolP2P/CodeType.java | 5 ++ src/protocolP2P/FileList.java | 36 ++++++++++++++ src/protocolP2P/FilePart.java | 41 ++++++++++++++++ src/protocolP2P/Payload.java | 34 +++++++++++++ src/protocolP2P/ProtocolP2PDatagram.java | 53 ++++++++++++++++++++ src/protocolP2P/RequestResponseCode.java | 62 ++++++++++++++++++++++++ 7 files changed, 238 insertions(+), 4 deletions(-) create mode 100644 src/protocolP2P/CodeType.java create mode 100644 src/protocolP2P/FileList.java create mode 100644 src/protocolP2P/FilePart.java create mode 100644 src/protocolP2P/Payload.java create mode 100644 src/protocolP2P/ProtocolP2PDatagram.java create mode 100644 src/protocolP2P/RequestResponseCode.java diff --git a/doc/protocol.md b/doc/protocol.md index 29622e2..99817d0 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -26,7 +26,7 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p ## Requests and responses codes - REQUESTS (msb is 0): - `LIST` (0x00) - - `DOWNLOAD` (0x01) + - `LOAD` (0x01) - RESPONSES (msb is 1): - `LIST` (0x80) @@ -39,10 +39,10 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p Payload size for list request is always zero. Payload for list response is filenames separated by `\n`. -### Download +### Load #### Not found Response when the file requested is not found on the server. -Payload size for Not found is zero +Payload size for Not found is zero. #### Protocol error Response when the request cannot be interpreted. @@ -50,7 +50,7 @@ Payload size for Protocol error is zero #### Internal error Response in internal failure case. -Payload size for Internal error is zero +Payload size for Internal error is zero. #### Load response Payload contains @@ -61,3 +61,6 @@ Payload contains [\n] [FILE CONTENT] ``` + +#### Load request +Payload contains only the name of the file to load. diff --git a/src/protocolP2P/CodeType.java b/src/protocolP2P/CodeType.java new file mode 100644 index 0000000..6fa1198 --- /dev/null +++ b/src/protocolP2P/CodeType.java @@ -0,0 +1,5 @@ +package protocolP2P; +public enum CodeType { + REQUEST, + RESPONSE +} diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java new file mode 100644 index 0000000..60fd6b8 --- /dev/null +++ b/src/protocolP2P/FileList.java @@ -0,0 +1,36 @@ +package protocolP2P; +import java.util.ArrayList; +import protocolP2P.Payload; +import protocolP2P.RequestResponseCode; +import exception.ProtocolError; + +public class FileList extends Payload { + private final static RequestResponseCode requestResponseCode = RequestResponseCode.LIST_RESPONSE; + private ArrayList content; + + public FileList(ArrayList content) { + this.content = content; + } + + public FileList(byte[] datagram) throws ProtocolError { + //TODO + assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) == RequestResponseCode.LIST_RESPONSE : "FileList subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; + int size = (datagram[PAYLOAD_SIZE_POSITON] << (8*3)) | (datagram[PAYLOAD_SIZE_POSITON+1] << (8*2)) | (datagram[PAYLOAD_SIZE_POSITON+2] << 8) | datagram[PAYLOAD_SIZE_POSITON+3]; + + } + + /** To datagram with padding */ + public byte[] toDatagram() { + byte[] datagram; // java initialize all to zero + // size is keep blank (ProtocolP2PDatagram will handle it) + // set request/response code + datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; + // bits 16-31 are reserved for future use + //TODO size + int size = ; + for(i=0;i<4;i++) { + datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) (size >> (8 * (3 - i)) & 0xFF); + } + //TODO content + } +} diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java new file mode 100644 index 0000000..6351a72 --- /dev/null +++ b/src/protocolP2P/FilePart.java @@ -0,0 +1,41 @@ +package protocolP2P; +import protocolP2P.Payload; +import protocolP2P.RequestResponseCode; +import exception.ProtocolError; + +public class FilePart extends Payload { + private static final RequestResponseCode requestResponseCode = RequestResponseCode.LOAD_RESPONSE; + private String filename; + private int totalSize; + private int offset; + private byte[] partialContent; + + public FilePart(String filename, int totalSize, int offset, byte[] partialContent) { + this.filename = filename; + this.totalSize = totalSize; + this.offset = offset; + this.partialContent = partialContent; + } + + public FilePart(byte[] datagram) throws ProtocolError { + //TODO + assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) == RequestResponseCode.LOAD_RESPONSE : "FilePart subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; + int size = (datagram[PAYLOAD_SIZE_POSITON] << (8*3)) | (datagram[PAYLOAD_SIZE_POSITON+1] << (8*2)) | (datagram[PAYLOAD_SIZE_POSITON+2] << 8) | datagram[PAYLOAD_SIZE_POSITON+3]; + } + + /** To datagram with padding */ + public byte[] toDatagram() { + byte[] datagram; // java initialize all to zero + // size is keep blank (ProtocolP2PDatagram will handle it) + // set request/response code + datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; + // bits 16-31 are reserved for future use + //TODO size + int size = ; + for(i=0;i<4;i++) { + datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) (size >> (8 * (3 - i)) & 0xFF); + } + //TODO content + } + +} diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java new file mode 100644 index 0000000..3526cd6 --- /dev/null +++ b/src/protocolP2P/Payload.java @@ -0,0 +1,34 @@ +package protocolP2P; +import protocolP2P.RequestResponseCode; +import protocolP2P.FilePart; +import protocolP2P.FileList; +import exception.ProtocolError; + +public class Payload { + private RequestResponseCode requestResponseCode; + protected final static int PAYLOAD_SIZE_POSITON = 4; + protected final static int PAYLOAD_START_POSITION = 8; + + /** To create request/response with a payload size of zero */ + public Payload(RequestResponseCode requestResponseCode) { + assert requestResponseCode != requestResponseCode.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class"; + assert requestResponseCode != requestResponseCode.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class"; + this.requestResponseCode = requestResponseCode; + } + + public Payload(byte[] datagram) throws ProtocolError { + assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class"; + assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class"; + requestResponseCode = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]); + } + + /** Payload with padding */ + public byte[] toDatagram() { + byte [] datagram = new byte[8]; // java initialize all to zero + // size is keep blank (ProtocolP2PDatagram will handle it) + // set request/response code + datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; + // bits 16-31 are reserved for future use + // payload size is 0 (this is what java have initialized datagram) + } +} diff --git a/src/protocolP2P/ProtocolP2PDatagram.java b/src/protocolP2P/ProtocolP2PDatagram.java new file mode 100644 index 0000000..19ab192 --- /dev/null +++ b/src/protocolP2P/ProtocolP2PDatagram.java @@ -0,0 +1,53 @@ +package protocolP2P; +import exception.ProtocolError; +import protocolP2P.Payload; +import protocolP2P.RequestResponseCode; +import java.util.ArrayList; +import java.lang.Byte; + +public class ProtocolP2PDatagram { + private final static byte PROTOCOL_VERSION = 0x11; + private final static int VERSION_POSITON = 0; + private byte version; + private Payload payload; + + public ProtocolP2PDatagram(Payload payload) { + version = PROTOCOL_VERSION; + this.payload = payload; + } + + public ProtocolP2PDatagram(byte[] datagram) throws ProtocolError { + // unwrap version + version = datagram[VERSION_POSITON]; + checkProtocolVersion(); // this can throw ProtocolError + RequestResponseCode r = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]); // this can throw ProtocolError + switch (r) { + case RequestResponseCode.LIST_RESPONSE: + payload = (Payload) new FileList(datagram); + break; + case RequestResponseCode.LOAD_RESPONSE: + payload = (Payload) new FilePart(datagram); + break; + default: + payload = new Payload(datagram); + break; + } + } + + public byte[] toDatagram() { + byte[] datagram = payload.toDatagram(); + datagram[VERSION_POSITON] = version; + return datagram; + } + + public Payload getPayload() { + return payload; + } + + private void checkProtocolVersion() throws ProtocolError { + if (PROTOCOL_VERSION != version) { + throw new ProtocolError(); + } + } +} + diff --git a/src/protocolP2P/RequestResponseCode.java b/src/protocolP2P/RequestResponseCode.java new file mode 100644 index 0000000..da6a923 --- /dev/null +++ b/src/protocolP2P/RequestResponseCode.java @@ -0,0 +1,62 @@ +package protocolP2P; +import protocolP2P.CodeType; +import exception.ProtocolError; +import java.util.HashMap; +import java.util.Map; +import java.lang.Byte; + +/** Request/Response code enum. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ +public enum RequestResponseCode { + LIST_REQUEST(CodeType.REQUEST, (byte)0x00), + LOAD_REQUEST(CodeType.REQUEST, (byte)0x01), + LIST_RESPONSE(CodeType.RESPONSE, (byte)0x80), + LOAD_RESPONSE(CodeType.RESPONSE, (byte)0x81), + NOT_FOUND(CodeType.RESPONSE, (byte)0x82), + PROTOCOL_ERROR(CodeType.RESPONSE, (byte)0x83), + INTERNAL_ERROR(CodeType.RESPONSE, (byte)0x84); + + public final CodeType codeType; + public final byte codeValue; + protected final static int RRCODE_POSITION = 1; + + /* To be able to convert code to enum */ + private static final Map BY_CODE = new HashMap<>(); + /* Initialization of HashMap */ + static { + for (RequestResponseCode r: values()) { + assert !BY_CODE.containsKey(Byte.valueOf(r.codeValue)) : "Duplicate in " + RequestResponseCode.class.getCanonicalName(); + BY_CODE.put(Byte.valueOf(r.codeValue), r); + } + } + + /** Private constructor + * @param codeType type of code (request or response) + * @param codeValue value of the element in datagram + * @return enum element + */ + private RequestResponseCode(CodeType codeType, byte codeValue) { + this.codeType = codeType; + this.codeValue = codeValue; + } + + /** Gives enum from datagram code. + * @param code value of the element in datagram + * @return enum element + */ + public static RequestResponseCode fromCode(byte code) throws ProtocolError { + byte code = BY_CODE.get(Byte.valueOf(code)); + if (code == null) { + throw new ProtocolError(); + } + return code; + } + + +} + + From 855e4ed73e8454ad7100c87d930b37bc040c53a6 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 22 Jan 2020 13:02:03 +0100 Subject: [PATCH 35/48] Update RequestResponse codes --- doc/protocol.md | 34 +++++++++++++++-------- src/exception/InternalError.class | Bin 213 -> 0 bytes src/exception/NotFound.class | Bin 203 -> 0 bytes src/exception/ProtocolError.class | Bin 213 -> 0 bytes src/exception/TransmissionError.class | Bin 221 -> 0 bytes src/exception/VersionError.java | 2 ++ src/protocolP2P/CodeType.java | 3 +- src/protocolP2P/FileList.java | 8 +++++- src/protocolP2P/FilePart.java | 8 +++++- src/protocolP2P/Payload.java | 16 +++++++++-- src/protocolP2P/ProtocolP2PDatagram.java | 9 +++--- src/protocolP2P/RequestResponseCode.java | 8 ++++-- 12 files changed, 65 insertions(+), 23 deletions(-) delete mode 100644 src/exception/InternalError.class delete mode 100644 src/exception/NotFound.class delete mode 100644 src/exception/ProtocolError.class delete mode 100644 src/exception/TransmissionError.class create mode 100644 src/exception/VersionError.java diff --git a/doc/protocol.md b/doc/protocol.md index 99817d0..1d11573 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -31,26 +31,26 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p - RESPONSES (msb is 1): - `LIST` (0x80) - `LOAD` (0x81) - - `NOT FOUND` (0x82) - - `PROTOCOL ERROR` (0x83) - - `INTERNAL ERROR` (0x84) + - `VERSION ERROR` (0xC0) + - `PROTOCOL ERROR` (0xC1) + - `INTERNAL ERROR` (0xC2) + - `EMPTY DIRECTORY` (0xC3) + - `NOT FOUND` (0xC4) ### List Payload size for list request is always zero. -Payload for list response is filenames separated by `\n`. +Payload for list response is filenames separated by `\n`. Payload size for list response is never zero. + +#### Empty directory +When directory is empty. +Payload size for empty directory is always zero. + ### Load #### Not found Response when the file requested is not found on the server. Payload size for Not found is zero. -#### Protocol error -Response when the request cannot be interpreted. -Payload size for Protocol error is zero - -#### Internal error -Response in internal failure case. -Payload size for Internal error is zero. #### Load response Payload contains @@ -64,3 +64,15 @@ Payload contains #### Load request Payload contains only the name of the file to load. + +### Other response code (errors) +#### Version error +Response when datagram received use wrong version code. + +#### Protocol error +Response when the request cannot be interpreted (but version is correct). +Payload size for Protocol error is zero + +#### Internal error +Response in internal failure case. +Payload size for Internal error is zero. diff --git a/src/exception/InternalError.class b/src/exception/InternalError.class deleted file mode 100644 index 90c81e92c94dd5c44f78aef8852685f973f2391b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 213 zcmX^0Z`VEs1_l!bUM>b^1}=66ZgvJ9Mg}&U%)HDJJ4Oa(4b3n{1{UZ1lvG9rexJ;| zRKL>Pq|~C2#H1Xc2v=}^X;E^jTPBDjN1nDYDk!v zDmJyw7iV!30c<#gp}c4ES}CKWzfBl5fv6Dr6m9=DCg|`B%@8k_8C_=MmIKcG33{}) O$LO*Qt`NHI?fn4swI|vD diff --git a/src/exception/ProtocolError.class b/src/exception/ProtocolError.class deleted file mode 100644 index ab341e7cc91cbdf461e98c54e5161a85350dd7a3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 213 zcmX^0Z`VEs1_l!bUM>b^1}=66ZgvJ9Mg}&U%)HDJJ4Oa(4b3n{1{UZ1lvG9rexJ;| zRKL>Pq|~C2#H1Xc2v=}^X;E^jTPBDj6i}33lAoNP<62adU!<3nSeD4cz{0@F$RM6t zk(^pkl9`{UkD`W=K^UY;KPNFSUEdX^hCz{m3FsIQU<5*-2|$t!$dd*085mf#wlgqp V1WU66Nj9J`15gVi0|%Jq1OU|KEJ6SP diff --git a/src/exception/TransmissionError.class b/src/exception/TransmissionError.class deleted file mode 100644 index ec43b93a1e274798862b55580370b6769717de13..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 221 zcmX^0Z`VEs1_l!bUM>b^1}=66ZgvJ9Mg}&U%)HDJJ4Oa(4b3n{1{UZ1lvG9rexJ;| zRKL>Pq|~C2#H1Xc2v=}^X;E^jTPBDj7E+X$SDc$!T%4Jo=UP;hU!<3nSeD4cz{0@F z$RM3sk(^pk0+iCnP{+t13{t6|lbDyT?+R1Lpvb@kbQB0M0wK^8Ajt;g$%6R|46It) Y85lQ$rP+Zb8&H@5sD+V%159%Q06PjVx&QzG diff --git a/src/exception/VersionError.java b/src/exception/VersionError.java new file mode 100644 index 0000000..c271a81 --- /dev/null +++ b/src/exception/VersionError.java @@ -0,0 +1,2 @@ +package exception; +public class VersionError extends Exceptions {} diff --git a/src/protocolP2P/CodeType.java b/src/protocolP2P/CodeType.java index 6fa1198..f13e805 100644 --- a/src/protocolP2P/CodeType.java +++ b/src/protocolP2P/CodeType.java @@ -1,5 +1,6 @@ package protocolP2P; public enum CodeType { REQUEST, - RESPONSE + RESPONSE, + ERROR } diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index 60fd6b8..978bf74 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import exception.ProtocolError; +import exception.InternalError; public class FileList extends Payload { private final static RequestResponseCode requestResponseCode = RequestResponseCode.LIST_RESPONSE; @@ -12,9 +13,14 @@ public class FileList extends Payload { this.content = content; } - public FileList(byte[] datagram) throws ProtocolError { + public FileList(byte[] datagram) throws ProtocolError, InternalError { //TODO + /* assert to help debugging */ assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) == RequestResponseCode.LIST_RESPONSE : "FileList subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; + /* InternalErrorException */ + if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE) { + throw new InternalError(); + } int size = (datagram[PAYLOAD_SIZE_POSITON] << (8*3)) | (datagram[PAYLOAD_SIZE_POSITON+1] << (8*2)) | (datagram[PAYLOAD_SIZE_POSITON+2] << 8) | datagram[PAYLOAD_SIZE_POSITON+3]; } diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 6351a72..4d03f6d 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -2,6 +2,7 @@ package protocolP2P; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import exception.ProtocolError; +import exception.InternalError; public class FilePart extends Payload { private static final RequestResponseCode requestResponseCode = RequestResponseCode.LOAD_RESPONSE; @@ -17,9 +18,14 @@ public class FilePart extends Payload { this.partialContent = partialContent; } - public FilePart(byte[] datagram) throws ProtocolError { + public FilePart(byte[] datagram) throws ProtocolError, InternalError { //TODO + /* assert to help debugging */ assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) == RequestResponseCode.LOAD_RESPONSE : "FilePart subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; + /* InternalErrorException */ + if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE) { + throw new InternalError(); + } int size = (datagram[PAYLOAD_SIZE_POSITON] << (8*3)) | (datagram[PAYLOAD_SIZE_POSITON+1] << (8*2)) | (datagram[PAYLOAD_SIZE_POSITON+2] << 8) | datagram[PAYLOAD_SIZE_POSITON+3]; } diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index 3526cd6..3cece81 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -3,6 +3,7 @@ import protocolP2P.RequestResponseCode; import protocolP2P.FilePart; import protocolP2P.FileList; import exception.ProtocolError; +import exception.InternalError; public class Payload { private RequestResponseCode requestResponseCode; @@ -10,16 +11,27 @@ public class Payload { protected final static int PAYLOAD_START_POSITION = 8; /** To create request/response with a payload size of zero */ - public Payload(RequestResponseCode requestResponseCode) { + public Payload(RequestResponseCode requestResponseCode) throws InternalError { + /* asserts to help debugging */ assert requestResponseCode != requestResponseCode.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class"; assert requestResponseCode != requestResponseCode.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class"; this.requestResponseCode = requestResponseCode; + checkRequestResponseCode(); // this can throw InternalError } - public Payload(byte[] datagram) throws ProtocolError { + public Payload(byte[] datagram) throws ProtocolError, InternalError { + /* asserts to help debugging */ assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class"; assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class"; requestResponseCode = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]); + checkRequestResponseCode(); // this can throw InternalError + } + + private void checkRequestResponseCode() throws InternalError { + /* Incorrect use cases (use subclasses instead) */ + if (requestResponseCode == RequestResponseCode.LIST_RESPONSE || requestResponseCode == RequestResponseCode.LOAD_RESPONSE) { + throw new InternalError(); + } } /** Payload with padding */ diff --git a/src/protocolP2P/ProtocolP2PDatagram.java b/src/protocolP2P/ProtocolP2PDatagram.java index 19ab192..5ae58a9 100644 --- a/src/protocolP2P/ProtocolP2PDatagram.java +++ b/src/protocolP2P/ProtocolP2PDatagram.java @@ -1,5 +1,6 @@ package protocolP2P; import exception.ProtocolError; +import exception.VersionError; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import java.util.ArrayList; @@ -16,10 +17,10 @@ public class ProtocolP2PDatagram { this.payload = payload; } - public ProtocolP2PDatagram(byte[] datagram) throws ProtocolError { + public ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError { // unwrap version version = datagram[VERSION_POSITON]; - checkProtocolVersion(); // this can throw ProtocolError + checkProtocolVersion(); // this can throw VersionError RequestResponseCode r = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]); // this can throw ProtocolError switch (r) { case RequestResponseCode.LIST_RESPONSE: @@ -44,9 +45,9 @@ public class ProtocolP2PDatagram { return payload; } - private void checkProtocolVersion() throws ProtocolError { + private void checkProtocolVersion() throws VersionError { if (PROTOCOL_VERSION != version) { - throw new ProtocolError(); + throw new VersionError(); } } } diff --git a/src/protocolP2P/RequestResponseCode.java b/src/protocolP2P/RequestResponseCode.java index da6a923..e23def6 100644 --- a/src/protocolP2P/RequestResponseCode.java +++ b/src/protocolP2P/RequestResponseCode.java @@ -16,9 +16,11 @@ public enum RequestResponseCode { LOAD_REQUEST(CodeType.REQUEST, (byte)0x01), LIST_RESPONSE(CodeType.RESPONSE, (byte)0x80), LOAD_RESPONSE(CodeType.RESPONSE, (byte)0x81), - NOT_FOUND(CodeType.RESPONSE, (byte)0x82), - PROTOCOL_ERROR(CodeType.RESPONSE, (byte)0x83), - INTERNAL_ERROR(CodeType.RESPONSE, (byte)0x84); + VERSION_ERROR(CodeType.ERROR, (byte)0xC0), + PROTOCOL_ERROR(CodeType.ERROR, (byte)0xC1), + INTERNAL_ERROR(CodeType.ERROR, (byte)0xC2), + EMPTY_DIRECTORY(CodeType.ERROR, (byte)0xC3), + NOT_FOUND(CodeType.ERROR, (byte)0xC4); public final CodeType codeType; public final byte codeValue; From 04457fdac5c1e2b3d9c8eb002bd53581158e980e Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 22 Jan 2020 13:13:41 +0100 Subject: [PATCH 36/48] Factorization of setPayloadSize and getPayloadSize --- src/protocolP2P/FileList.java | 7 ++----- src/protocolP2P/FilePart.java | 6 ++---- src/protocolP2P/Payload.java | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index 978bf74..e46ad33 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -21,8 +21,7 @@ public class FileList extends Payload { if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE) { throw new InternalError(); } - int size = (datagram[PAYLOAD_SIZE_POSITON] << (8*3)) | (datagram[PAYLOAD_SIZE_POSITON+1] << (8*2)) | (datagram[PAYLOAD_SIZE_POSITON+2] << 8) | datagram[PAYLOAD_SIZE_POSITON+3]; - + int size = getPayloadSize(datagram); } /** To datagram with padding */ @@ -34,9 +33,7 @@ public class FileList extends Payload { // bits 16-31 are reserved for future use //TODO size int size = ; - for(i=0;i<4;i++) { - datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) (size >> (8 * (3 - i)) & 0xFF); - } + datagram = setPayloadSize(size, datagram); //TODO content } } diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 4d03f6d..984e98a 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -26,7 +26,7 @@ public class FilePart extends Payload { if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE) { throw new InternalError(); } - int size = (datagram[PAYLOAD_SIZE_POSITON] << (8*3)) | (datagram[PAYLOAD_SIZE_POSITON+1] << (8*2)) | (datagram[PAYLOAD_SIZE_POSITON+2] << 8) | datagram[PAYLOAD_SIZE_POSITON+3]; + int size = getPayloadSize(datagram); } /** To datagram with padding */ @@ -38,9 +38,7 @@ public class FilePart extends Payload { // bits 16-31 are reserved for future use //TODO size int size = ; - for(i=0;i<4;i++) { - datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) (size >> (8 * (3 - i)) & 0xFF); - } + datagram = setPayloadSize(size, datagram); //TODO content } diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index 3cece81..4e3cc6c 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -43,4 +43,20 @@ public class Payload { // bits 16-31 are reserved for future use // payload size is 0 (this is what java have initialized datagram) } + + protected static byte[] setPayloadSize(int size, byte[] datagram) { + for(int i=0;i<4;i++) { + datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) (size >> (8 * (3 - i)) & 0xFF); + } + } + return datagram; + } + + protected static int getPayloadSize(byte[] datagram) { + int size = 0; + for(int i=0;i<4;i++) { + size |= ((int)datagram[PAYLOAD_SIZE_POSITON + i]) << (8* i); + } + return size; + } } From 0470a67f0f29a8d59feb387fe637d9567bf1e3f8 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 22 Jan 2020 13:43:25 +0100 Subject: [PATCH 37/48] Add documentation --- src/protocolP2P/CodeType.java | 7 +++++ src/protocolP2P/FileList.java | 31 +++++++++++++++++-- src/protocolP2P/FilePart.java | 23 +++++++++++++- src/protocolP2P/Payload.java | 38 +++++++++++++++++++++--- src/protocolP2P/ProtocolP2PDatagram.java | 24 +++++++++++++++ 5 files changed, 115 insertions(+), 8 deletions(-) diff --git a/src/protocolP2P/CodeType.java b/src/protocolP2P/CodeType.java index f13e805..e38de49 100644 --- a/src/protocolP2P/CodeType.java +++ b/src/protocolP2P/CodeType.java @@ -1,4 +1,11 @@ package protocolP2P; + +/** Request/Response code's type enum. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ public enum CodeType { REQUEST, RESPONSE, diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index e46ad33..7c98455 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -5,14 +5,35 @@ import protocolP2P.RequestResponseCode; import exception.ProtocolError; import exception.InternalError; +/** Representation of payload for list response. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ public class FileList extends Payload { private final static RequestResponseCode requestResponseCode = RequestResponseCode.LIST_RESPONSE; private ArrayList content; - public FileList(ArrayList content) { + /** Constructor (typically used by the server) with an ArrayList parameter containing + * filenames. + * @param content a list of files. Must not be empty. + * @throws InternalError + */ + public FileList(ArrayList content) throws InternalError { + /* assert to help debugging */ + assert !content.isEmpty() : "Payload size of FileList must not be empty, use EmptyDirectory from Payload instead"; + if (content.isEmpty()) { + throws new InternalError(); + } this.content = content; } + /** Constructor (typically used by client) with a byte[] parameter containing the datagram received. + * @param datagram the full datagram received + * @throws ProtocolError + * @throws InternalError + */ public FileList(byte[] datagram) throws ProtocolError, InternalError { //TODO /* assert to help debugging */ @@ -24,8 +45,12 @@ public class FileList extends Payload { int size = getPayloadSize(datagram); } - /** To datagram with padding */ - public byte[] toDatagram() { + /** Returns a byte[] containing datagram with padding. + * This datagram is still incomplete and should not be send directly. + * ProtocolP2PDatagram will use this method to generate the complete datagram. + * @return datagram with padding + */ + protected byte[] toDatagram() { byte[] datagram; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 984e98a..bc05a3e 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -4,6 +4,12 @@ import protocolP2P.RequestResponseCode; import exception.ProtocolError; import exception.InternalError; +/** Representation of payload for load response. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ public class FilePart extends Payload { private static final RequestResponseCode requestResponseCode = RequestResponseCode.LOAD_RESPONSE; private String filename; @@ -11,6 +17,12 @@ public class FilePart extends Payload { private int offset; private byte[] partialContent; + /** Constructor (typically used by server) with informations about file part to send as parameters. + * @param filename name of the file to send + * @param totalSize total size of the file to send + * @param offset where in the file begins the part we are sending + * @param partialContent content of the file we send + */ public FilePart(String filename, int totalSize, int offset, byte[] partialContent) { this.filename = filename; this.totalSize = totalSize; @@ -18,6 +30,11 @@ public class FilePart extends Payload { this.partialContent = partialContent; } + /** Constructor (typically used by client) with datagram received as parameter. + * @param datagram the full datagram received + * @throws ProtocolError + * @throws InternalError + */ public FilePart(byte[] datagram) throws ProtocolError, InternalError { //TODO /* assert to help debugging */ @@ -29,7 +46,11 @@ public class FilePart extends Payload { int size = getPayloadSize(datagram); } - /** To datagram with padding */ + /** Returns a byte[] containing datagram with padding. + * This datagram is still incomplete and should not be send directly. + * ProtocolP2PDatagram will use this method to generate the complete datagram. + * @return datagram with padding + */ public byte[] toDatagram() { byte[] datagram; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index 4e3cc6c..220e323 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -4,13 +4,21 @@ import protocolP2P.FilePart; import protocolP2P.FileList; import exception.ProtocolError; import exception.InternalError; - +/** Representation of payload. If payload has a size, use subclasses instead. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ public class Payload { private RequestResponseCode requestResponseCode; protected final static int PAYLOAD_SIZE_POSITON = 4; protected final static int PAYLOAD_START_POSITION = 8; - /** To create request/response with a payload size of zero */ + /** Consructor used to create Payload with a payload size of zero using a RRCode. + * @param requestResponseCode Request/Response code associated with the payload + * @throws InternalError + */ public Payload(RequestResponseCode requestResponseCode) throws InternalError { /* asserts to help debugging */ assert requestResponseCode != requestResponseCode.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class"; @@ -19,6 +27,12 @@ public class Payload { checkRequestResponseCode(); // this can throw InternalError } + /** Constructor used to create a Payload (when no more specific subclasses exists) using datagram as parameter. + * If payload size is not empty, using subclass is required. + * @param datagram the full datagram received + * @throws ProtocolError + * @throws InternalError + */ public Payload(byte[] datagram) throws ProtocolError, InternalError { /* asserts to help debugging */ assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class"; @@ -27,6 +41,9 @@ public class Payload { checkRequestResponseCode(); // this can throw InternalError } + /** Used to check RRCode used is compatible with this class use, or if a more specific subclass is required. + * @throws InternalError + */ private void checkRequestResponseCode() throws InternalError { /* Incorrect use cases (use subclasses instead) */ if (requestResponseCode == RequestResponseCode.LIST_RESPONSE || requestResponseCode == RequestResponseCode.LOAD_RESPONSE) { @@ -34,7 +51,11 @@ public class Payload { } } - /** Payload with padding */ + /** Returns a byte[] containing datagram with padding. + * This datagram is still incomplete and should not be send directly. + * ProtocolP2PDatagram will use this method to generate the complete datagram. + * @return datagram with padding + */ public byte[] toDatagram() { byte [] datagram = new byte[8]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) @@ -44,6 +65,11 @@ public class Payload { // payload size is 0 (this is what java have initialized datagram) } + /** Set payload’s size in a datagram. + * @param size integer representing payload size + * @param datagram datagram to be completed + * @return datagram with payload’s size set + */ protected static byte[] setPayloadSize(int size, byte[] datagram) { for(int i=0;i<4;i++) { datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) (size >> (8 * (3 - i)) & 0xFF); @@ -51,7 +77,11 @@ public class Payload { } return datagram; } - + + /** Get payload’s size from a datagram. + * @param datagram the full datagram received + * @return integer representing payload size + */ protected static int getPayloadSize(byte[] datagram) { int size = 0; for(int i=0;i<4;i++) { diff --git a/src/protocolP2P/ProtocolP2PDatagram.java b/src/protocolP2P/ProtocolP2PDatagram.java index 5ae58a9..31ae53f 100644 --- a/src/protocolP2P/ProtocolP2PDatagram.java +++ b/src/protocolP2P/ProtocolP2PDatagram.java @@ -6,17 +6,31 @@ import protocolP2P.RequestResponseCode; import java.util.ArrayList; import java.lang.Byte; +/** Representation of datagram. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ public class ProtocolP2PDatagram { private final static byte PROTOCOL_VERSION = 0x11; private final static int VERSION_POSITON = 0; private byte version; private Payload payload; + /** Constructor with payload parameter (typically used when sending datagram). + * @param payload the payload associated with the datagram to send + */ public ProtocolP2PDatagram(Payload payload) { version = PROTOCOL_VERSION; this.payload = payload; } + /** Constructor with datagram as byte[] parameter (typically used when receiving datagram). + * @param datagram the full datagram received + * @throws ProtocolError + * @throws VersionError + */ public ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError { // unwrap version version = datagram[VERSION_POSITON]; @@ -35,16 +49,26 @@ public class ProtocolP2PDatagram { } } + /** Returns a byte[] containing full datagram (typically used when sending datagram). + * This datagram is still complete and ready to be send. + * @return the full datagram to send + */ public byte[] toDatagram() { byte[] datagram = payload.toDatagram(); datagram[VERSION_POSITON] = version; return datagram; } + /** Returns Payload associated with the datagram. + * @return payload associated with the datagram + */ public Payload getPayload() { return payload; } + /** Used to check protocol version when a datagram is constructed from bytes[]. + * @throws VersionError + */ private void checkProtocolVersion() throws VersionError { if (PROTOCOL_VERSION != version) { throw new VersionError(); From 6e2e5451df411f2cca0d16c1fb4e532d9ad4ac71 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 22 Jan 2020 17:05:45 +0100 Subject: [PATCH 38/48] Add algorithm but did not implement it --- doc/protocol.md | 2 +- src/protocolP2P/FileList.java | 8 +++--- src/protocolP2P/FilePart.java | 52 +++++++++++++++++++++++++++++------ src/protocolP2P/Payload.java | 16 +++++++++-- 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/doc/protocol.md b/doc/protocol.md index 1d11573..227e3b8 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -57,7 +57,7 @@ Payload contains ``` [64-127: OFFSET OF FILE CONTENT IN BYTES] -[128-159: TOTAL FILESIZE] +[128-191: TOTAL FILESIZE] [\n] [FILE CONTENT] ``` diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index 7c98455..35b6088 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -51,14 +51,14 @@ public class FileList extends Payload { * @return datagram with padding */ protected byte[] toDatagram() { - byte[] datagram; // java initialize all to zero + //TODO compute size + int size = ; + byte[] datagram = new byte[size]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // bits 16-31 are reserved for future use - //TODO size - int size = ; datagram = setPayloadSize(size, datagram); - //TODO content + //TODO Write content } } diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index bc05a3e..230da36 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -13,8 +13,8 @@ import exception.InternalError; public class FilePart extends Payload { private static final RequestResponseCode requestResponseCode = RequestResponseCode.LOAD_RESPONSE; private String filename; - private int totalSize; - private int offset; + private long totalSize; + private long offset; private byte[] partialContent; /** Constructor (typically used by server) with informations about file part to send as parameters. @@ -22,8 +22,15 @@ public class FilePart extends Payload { * @param totalSize total size of the file to send * @param offset where in the file begins the part we are sending * @param partialContent content of the file we send + * @throws InternalError */ - public FilePart(String filename, int totalSize, int offset, byte[] partialContent) { + public FilePart(String filename, long totalSize, long offset, byte[] partialContent) throws InternalError { + /* asserts to help debugging */ + assert totalSize >= 0 : "totalSize cannot be negative"; + assert offset >= 0 : "offset cannot be negative"; + if (totalSize < 0 || offset < 0) { + throw new InternalError(); + } this.filename = filename; this.totalSize = totalSize; this.offset = offset; @@ -36,14 +43,17 @@ public class FilePart extends Payload { * @throws InternalError */ public FilePart(byte[] datagram) throws ProtocolError, InternalError { - //TODO /* assert to help debugging */ assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) == RequestResponseCode.LOAD_RESPONSE : "FilePart subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; /* InternalErrorException */ if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE) { throw new InternalError(); } - int size = getPayloadSize(datagram); + setOffset(datagram); // this can throw ProtocolError + setTotalFileSize(datagram); // this can throw ProtocolError + int size = getPayloadSize(datagram); // this can throw ProtocolError + int partialContentStart = setFilename(datagram, size) + 1; // this can throw ProtocolError + setPartialContent(datagram, partialContentStart, size); } /** Returns a byte[] containing datagram with padding. @@ -52,15 +62,39 @@ public class FilePart extends Payload { * @return datagram with padding */ public byte[] toDatagram() { - byte[] datagram; // java initialize all to zero + //TODO : calculate payload size + int size = ; + byte[] datagram = new byte[size]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // bits 16-31 are reserved for future use - //TODO size - int size = ; datagram = setPayloadSize(size, datagram); - //TODO content + //TODO : write offset to datagram + //TODO : write totalFileSize to datagram + //TODO : write filename\n to datagram + //TODO : write partialContent to datagram + } + + private setOffset(byte[] datagram) throws ProtocolError { + //TODO: copy from 8 to 15 into long and write into offset. + // throw protocolerror if offset is < 0 + } + private setTotalFileSize(byte[] datagram) throw ProtocolError { + // TODO: convert from byte 16 to 23 (included) + // into long and write into totalFileSize + // throw protocolerror if offset is < 0 + } + private int setFilename(byte[] datagram, size) throws ProtocolError { + // TODO: copy datagram from byte 24 to the first \n (excluded) + // into filename unless we excess size + // (in this case the wrong size has been + // set by the emitter, it is a protocolerror) + // return position of the \n + + } + private setPartialContent(byte[] datagram, int start, size) { + // TODO: copy datagram from start to size into partialContent } } diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index 220e323..6a8fd51 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -69,10 +69,16 @@ public class Payload { * @param size integer representing payload size * @param datagram datagram to be completed * @return datagram with payload’s size set + * @throws InternalError */ - protected static byte[] setPayloadSize(int size, byte[] datagram) { + protected static byte[] setPayloadSize(int size, byte[] datagram) throws InternalError { + /* assert to help debugging */ + assert size >= 0: "Payload size cannont be negative"; + if (size < 0) { + throw new InternalError(); + } for(int i=0;i<4;i++) { - datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) (size >> (8 * (3 - i)) & 0xFF); + datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) ((size >> (8 * (3 - i))) & 0xFF); } } return datagram; @@ -81,12 +87,16 @@ public class Payload { /** Get payload’s size from a datagram. * @param datagram the full datagram received * @return integer representing payload size + * @throws ProtocolError */ - protected static int getPayloadSize(byte[] datagram) { + protected static int getPayloadSize(byte[] datagram) throws ProtocolError { int size = 0; for(int i=0;i<4;i++) { size |= ((int)datagram[PAYLOAD_SIZE_POSITON + i]) << (8* i); } + if (size < 0) { + throw new ProtocolError(); + } return size; } } From c3b8c2c5e0bb7d123f95b0eda116b27526dc37fc Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 23 Jan 2020 11:37:07 +0100 Subject: [PATCH 39/48] Update protocol classes modelisation --- src/exception/SizeError.java | 3 ++ src/protocolP2P/FileList.java | 4 +-- src/protocolP2P/FilePart.java | 20 +++++++------- src/protocolP2P/Payload.java | 21 +++++++------- src/protocolP2P/ProtocolP2PDatagram.java | 35 +++++++++++++++++++++--- 5 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 src/exception/SizeError.java diff --git a/src/exception/SizeError.java b/src/exception/SizeError.java new file mode 100644 index 0000000..0af8f87 --- /dev/null +++ b/src/exception/SizeError.java @@ -0,0 +1,3 @@ +package exception; +/** Used on reception side when size as set in datagram is too big, and we cant store this in a int/long as usual. */ +public class SizeError extends Exception {} diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index 35b6088..f5b6c70 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -31,10 +31,10 @@ public class FileList extends Payload { /** Constructor (typically used by client) with a byte[] parameter containing the datagram received. * @param datagram the full datagram received - * @throws ProtocolError + * @throws SizeError * @throws InternalError */ - public FileList(byte[] datagram) throws ProtocolError, InternalError { + protected FileList(byte[] datagram) throws SizeError, InternalError { //TODO /* assert to help debugging */ assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) == RequestResponseCode.LIST_RESPONSE : "FileList subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 230da36..6a86560 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -39,19 +39,19 @@ public class FilePart extends Payload { /** Constructor (typically used by client) with datagram received as parameter. * @param datagram the full datagram received - * @throws ProtocolError + * @throws SizeError * @throws InternalError */ - public FilePart(byte[] datagram) throws ProtocolError, InternalError { + protected FilePart(byte[] datagram) throws SizeError, ProtocolError, InternalError { /* assert to help debugging */ assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) == RequestResponseCode.LOAD_RESPONSE : "FilePart subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; /* InternalErrorException */ if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE) { throw new InternalError(); } - setOffset(datagram); // this can throw ProtocolError - setTotalFileSize(datagram); // this can throw ProtocolError - int size = getPayloadSize(datagram); // this can throw ProtocolError + setOffset(datagram); // this can throw SizeError + setTotalFileSize(datagram); // this can throw SizeError + int size = getPayloadSize(datagram); // this can throw SizeError int partialContentStart = setFilename(datagram, size) + 1; // this can throw ProtocolError setPartialContent(datagram, partialContentStart, size); } @@ -61,7 +61,7 @@ public class FilePart extends Payload { * ProtocolP2PDatagram will use this method to generate the complete datagram. * @return datagram with padding */ - public byte[] toDatagram() { + protected byte[] toDatagram() { //TODO : calculate payload size int size = ; byte[] datagram = new byte[size]; // java initialize all to zero @@ -76,16 +76,16 @@ public class FilePart extends Payload { //TODO : write partialContent to datagram } - private setOffset(byte[] datagram) throws ProtocolError { + private setOffset(byte[] datagram) throws SizeError { //TODO: copy from 8 to 15 into long and write into offset. // throw protocolerror if offset is < 0 } - private setTotalFileSize(byte[] datagram) throw ProtocolError { + private setTotalFileSize(byte[] datagram) throw SizeError { // TODO: convert from byte 16 to 23 (included) // into long and write into totalFileSize // throw protocolerror if offset is < 0 } - private int setFilename(byte[] datagram, size) throws ProtocolError { + private int setFilename(byte[] datagram, int payloadSize) throws ProtocolError { // TODO: copy datagram from byte 24 to the first \n (excluded) // into filename unless we excess size // (in this case the wrong size has been @@ -93,7 +93,7 @@ public class FilePart extends Payload { // return position of the \n } - private setPartialContent(byte[] datagram, int start, size) { + private setPartialContent(byte[] datagram, int start, int payloadSize) { // TODO: copy datagram from start to size into partialContent } diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index 6a8fd51..c38acf1 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -33,7 +33,7 @@ public class Payload { * @throws ProtocolError * @throws InternalError */ - public Payload(byte[] datagram) throws ProtocolError, InternalError { + protected Payload(byte[] datagram) throws ProtocolError, InternalError { /* asserts to help debugging */ assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class"; assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class"; @@ -56,7 +56,7 @@ public class Payload { * ProtocolP2PDatagram will use this method to generate the complete datagram. * @return datagram with padding */ - public byte[] toDatagram() { + protected byte[] toDatagram() { byte [] datagram = new byte[8]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code @@ -68,34 +68,35 @@ public class Payload { /** Set payload’s size in a datagram. * @param size integer representing payload size * @param datagram datagram to be completed - * @return datagram with payload’s size set * @throws InternalError */ - protected static byte[] setPayloadSize(int size, byte[] datagram) throws InternalError { + protected static setPayloadSize(int size, byte[] datagram) throws InternalError { /* assert to help debugging */ - assert size >= 0: "Payload size cannont be negative"; + assert size >= 0: "Payload size cannot be negative"; if (size < 0) { + // We don't throw SizeError + // because this is only for reception side throw new InternalError(); } for(int i=0;i<4;i++) { datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) ((size >> (8 * (3 - i))) & 0xFF); - } } - return datagram; } /** Get payload’s size from a datagram. * @param datagram the full datagram received * @return integer representing payload size - * @throws ProtocolError + * @throws SizeError */ - protected static int getPayloadSize(byte[] datagram) throws ProtocolError { + protected static int getPayloadSize(byte[] datagram) throws SizeError { int size = 0; for(int i=0;i<4;i++) { size |= ((int)datagram[PAYLOAD_SIZE_POSITON + i]) << (8* i); } if (size < 0) { - throw new ProtocolError(); + // Size in datagram is probably correct + // but we cannot store it into a int (or this will be negative) + throw new SizeError(); } return size; } diff --git a/src/protocolP2P/ProtocolP2PDatagram.java b/src/protocolP2P/ProtocolP2PDatagram.java index 31ae53f..1846258 100644 --- a/src/protocolP2P/ProtocolP2PDatagram.java +++ b/src/protocolP2P/ProtocolP2PDatagram.java @@ -5,6 +5,8 @@ import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import java.util.ArrayList; import java.lang.Byte; +import java.net.DatagramPacket; +import java.net.DatagramSocket; /** Representation of datagram. * @author Louis Royer @@ -26,12 +28,37 @@ public class ProtocolP2PDatagram { this.payload = payload; } - /** Constructor with datagram as byte[] parameter (typically used when receiving datagram). + /** Send datagram on socket + * @param socket DatagramSocket used to send datagram + */ + public void send(DatagramSocket socket) { + //TODO + byte[] datagram = toDatagram(); + // generate DatagramPacket + // send it + } + + /** Receive datagram on socket + * @param socket DatagramSocket used to receive datagram + * @return payload of the datagram + * @throws ProtocolError + * @throws VersionError + */ + public static Payload receive(DatagramSocket socket) throws ProtocolError, VersionError { + //TODO + // reception + //datagram = + // contruction + ProtocolP2PDatagram p = new ProtocolP2PDatagram(datagram); + return p.getPayload(); + + } + /** Private constructor with datagram as byte[] parameter (typically used when receiving datagram). * @param datagram the full datagram received * @throws ProtocolError * @throws VersionError */ - public ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError { + private ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError, SizeError { // unwrap version version = datagram[VERSION_POSITON]; checkProtocolVersion(); // this can throw VersionError @@ -53,7 +80,7 @@ public class ProtocolP2PDatagram { * This datagram is still complete and ready to be send. * @return the full datagram to send */ - public byte[] toDatagram() { + private byte[] toDatagram() { byte[] datagram = payload.toDatagram(); datagram[VERSION_POSITON] = version; return datagram; @@ -62,7 +89,7 @@ public class ProtocolP2PDatagram { /** Returns Payload associated with the datagram. * @return payload associated with the datagram */ - public Payload getPayload() { + private Payload getPayload() { return payload; } From 5eb7e0deabd04839f6486338beb741038e77d9f1 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 23 Jan 2020 12:26:44 +0100 Subject: [PATCH 40/48] Refactoring + implementation of some methods --- doc/protocol.md | 18 +++++----- src/protocolP2P/FileList.java | 3 +- src/protocolP2P/FilePart.java | 20 +++++++---- src/protocolP2P/Payload.java | 17 +++------ src/tools/ByteArrayTools.java | 66 +++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 29 deletions(-) create mode 100644 src/tools/ByteArrayTools.java diff --git a/doc/protocol.md b/doc/protocol.md index 227e3b8..0ea88aa 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -15,11 +15,11 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p ```Datagram format -[0-7: VERSION(0x11, first quartet is major version, second is minor)] -[8-15: REQUEST/RESPONSE CODE] -[16-31: RESERVED FOR FUTURE USE] -[32-63: PAYLOAD SIZE IN BYTES] -[64-xx: PAYLOAD] +1 byte: [0-7: VERSION(0x11, first quartet is major version, second is minor)] +1 byte: [8-15: REQUEST/RESPONSE CODE] +2 bytes: [16-31: RESERVED FOR FUTURE USE] +4 bytes: [32-63: PAYLOAD SIZE IN BYTES] +x bytes: [64-xx: PAYLOAD] ``` @@ -56,10 +56,10 @@ Payload size for Not found is zero. Payload contains ``` -[64-127: OFFSET OF FILE CONTENT IN BYTES] -[128-191: TOTAL FILESIZE] -[\n] -[FILE CONTENT] +8 bytes: [64-127: OFFSET OF FILE CONTENT IN BYTES] +8 bytes: [128-191: TOTAL FILESIZE] +y bytes: [\n] +z bytes: [FILE CONTENT] ``` #### Load request diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index f5b6c70..4372e9f 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -4,6 +4,7 @@ import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import exception.ProtocolError; import exception.InternalError; +import exception.SizeError; /** Representation of payload for list response. * @author Louis Royer @@ -52,7 +53,7 @@ public class FileList extends Payload { */ protected byte[] toDatagram() { //TODO compute size - int size = ; + int size = 8 + ; byte[] datagram = new byte[size]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 6a86560..c63801c 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -3,6 +3,8 @@ import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import exception.ProtocolError; import exception.InternalError; +import exception.SizeError; +import tools.BytesArrayTools; /** Representation of payload for load response. * @author Louis Royer @@ -63,7 +65,7 @@ public class FilePart extends Payload { */ protected byte[] toDatagram() { //TODO : calculate payload size - int size = ; + int size = 24 + filename.length + 1; byte[] datagram = new byte[size]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code @@ -76,15 +78,21 @@ public class FilePart extends Payload { //TODO : write partialContent to datagram } + /** Write from bytes 8 to 15 of datagram into offset. + * @param datagram received datagram + * @throws SizeError + */ private setOffset(byte[] datagram) throws SizeError { - //TODO: copy from 8 to 15 into long and write into offset. - // throw protocolerror if offset is < 0 + offset = BytesArrayTools.readLong(datagram, 8); } + /** Write from bytes 16 to 23 of datagram into totalSize. + * @param datagram received datagram + * @throws SizeError + */ private setTotalFileSize(byte[] datagram) throw SizeError { - // TODO: convert from byte 16 to 23 (included) - // into long and write into totalFileSize - // throw protocolerror if offset is < 0 + totalSize = BytesArrayTools.readLong(datagram, 16); } + private int setFilename(byte[] datagram, int payloadSize) throws ProtocolError { // TODO: copy datagram from byte 24 to the first \n (excluded) // into filename unless we excess size diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index c38acf1..44a1458 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -4,6 +4,8 @@ import protocolP2P.FilePart; import protocolP2P.FileList; import exception.ProtocolError; import exception.InternalError; +import exception.SizeError; +import tools.BytesArrayTools; /** Representation of payload. If payload has a size, use subclasses instead. * @author Louis Royer * @author Flavien Haas @@ -78,9 +80,7 @@ public class Payload { // because this is only for reception side throw new InternalError(); } - for(int i=0;i<4;i++) { - datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) ((size >> (8 * (3 - i))) & 0xFF); - } + BytesArrayTools.write(datagram, PAYLOAD_SIZE_POSITON, size); } /** Get payload’s size from a datagram. @@ -89,15 +89,6 @@ public class Payload { * @throws SizeError */ protected static int getPayloadSize(byte[] datagram) throws SizeError { - int size = 0; - for(int i=0;i<4;i++) { - size |= ((int)datagram[PAYLOAD_SIZE_POSITON + i]) << (8* i); - } - if (size < 0) { - // Size in datagram is probably correct - // but we cannot store it into a int (or this will be negative) - throw new SizeError(); - } - return size; + return BytesArrayTools.readInt(datagram, PAYLOAD_SIZE_POSITON]; } } diff --git a/src/tools/ByteArrayTools.java b/src/tools/ByteArrayTools.java new file mode 100644 index 0000000..176d562 --- /dev/null +++ b/src/tools/ByteArrayTools.java @@ -0,0 +1,66 @@ +package tools; +import exception.SizeError; +/** Helper to manipulate byte[]. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ +public class BytesArrayTools { + /** Write int in a bytearray + * @param array the array to write + * @param start where to begin writting + * @param value int to write + */ + public static void write(byte[] array, int start, int value) { + for(int i=0;i<4;i++) { + array[start + i] = (byte) ((size >> (8 * (3 - i))) & 0xFF); + } + } + /** Write long in a bytearray + * @param array the array to write + * @param start where to begin writting + * @param value long to write + */ + public static void write(byte[] array, int start, long value) { + for(int i=0;i<4;i++) { + array[start + i] = (byte) ((size >> (8 * (4 - i))) & 0xFF); + } + } + + /** Read int from a bytearray + * @param array the array to read + * @param start where to begin reading + * @return value read as int + */ + public static int readInt(byte[] array, int start) throws SizeError { + int size = 0; + for(int i=0;i<4;i++) { + size |= ((int)array[start + i]) << (8* i); + } + if (size < 0) { + // Size in array is probably correct + // but we cannot store it into a int (or this will be negative) + throw new SizeError(); + } + return size; + } + /** Read long from a bytearray + * @param array the array to read + * @param start where to begin reading + * @return value read as long + */ + public static long readLong(byte[] array, int start) throws SizeError { + long size = 0; + for(int i=0;i<8;i++) { + size |= ((int)array[start + i]) << (8* i); + } + if (size < 0) { + // Size in array is probably correct + // but we cannot store it into a int (or this will be negative) + throw new SizeError(); + } + return size; + } + +} From 2afb87303fdd8538ee851aa7b054dfdcf1163e59 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 23 Jan 2020 15:54:27 +0100 Subject: [PATCH 41/48] Fix all errors and all warnings --- doc/protocol.md | 5 +- src/exception/InternalError.java | 4 +- src/exception/NotFound.java | 4 +- src/exception/ProtocolError.java | 4 +- src/exception/SizeError.java | 4 +- src/exception/TransmissionError.java | 4 +- src/exception/VersionError.java | 4 +- src/protocolP2P/FileList.java | 19 ++-- src/protocolP2P/FilePart.java | 105 +++++++++++++----- src/protocolP2P/Payload.java | 26 +++-- src/protocolP2P/ProtocolP2PDatagram.java | 24 ++-- src/protocolP2P/RequestResponseCode.java | 6 +- ...teArrayTools.java => BytesArrayTools.java} | 4 +- 13 files changed, 149 insertions(+), 64 deletions(-) rename src/tools/{ByteArrayTools.java => BytesArrayTools.java} (92%) diff --git a/doc/protocol.md b/doc/protocol.md index 0ea88aa..7a9fb8a 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -13,6 +13,8 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p # P2P-JAVA-PROJECT version 1.1 (Binary protocol for step 1) +All strings in the datagram are utf-8 encoded. + ```Datagram format 1 byte: [0-7: VERSION(0x11, first quartet is major version, second is minor)] @@ -58,7 +60,8 @@ Payload contains ``` 8 bytes: [64-127: OFFSET OF FILE CONTENT IN BYTES] 8 bytes: [128-191: TOTAL FILESIZE] -y bytes: [\n] +4 bytes: [192-223: FILENAME SIZE] (cannot be > to PAYLOAD_SIZE - 20 or be zero) +y bytes: [] z bytes: [FILE CONTENT] ``` diff --git a/src/exception/InternalError.java b/src/exception/InternalError.java index c738615..9a20633 100644 --- a/src/exception/InternalError.java +++ b/src/exception/InternalError.java @@ -1,2 +1,4 @@ package exception; -public class InternalError extends Exception {} +public class InternalError extends Exception { + private static final long serialVersionUID = 11L; +} diff --git a/src/exception/NotFound.java b/src/exception/NotFound.java index 172a0bc..7018d43 100644 --- a/src/exception/NotFound.java +++ b/src/exception/NotFound.java @@ -1,2 +1,4 @@ package exception; -public class NotFound extends Exception {} +public class NotFound extends Exception { + private static final long serialVersionUID = 11L; +} diff --git a/src/exception/ProtocolError.java b/src/exception/ProtocolError.java index 4b86a8c..d5e4811 100644 --- a/src/exception/ProtocolError.java +++ b/src/exception/ProtocolError.java @@ -1,2 +1,4 @@ package exception; -public class ProtocolError extends Exception {} +public class ProtocolError extends Exception { + private static final long serialVersionUID = 11L; +} diff --git a/src/exception/SizeError.java b/src/exception/SizeError.java index 0af8f87..d0cb07d 100644 --- a/src/exception/SizeError.java +++ b/src/exception/SizeError.java @@ -1,3 +1,5 @@ package exception; /** Used on reception side when size as set in datagram is too big, and we cant store this in a int/long as usual. */ -public class SizeError extends Exception {} +public class SizeError extends Exception { + private static final long serialVersionUID = 11L; +} diff --git a/src/exception/TransmissionError.java b/src/exception/TransmissionError.java index 5c64987..d95dd4c 100644 --- a/src/exception/TransmissionError.java +++ b/src/exception/TransmissionError.java @@ -1,2 +1,4 @@ package exception; -public class TransmissionError extends Exception {} +public class TransmissionError extends Exception { + private static final long serialVersionUID = 11L; +} diff --git a/src/exception/VersionError.java b/src/exception/VersionError.java index c271a81..f84c811 100644 --- a/src/exception/VersionError.java +++ b/src/exception/VersionError.java @@ -1,2 +1,4 @@ package exception; -public class VersionError extends Exceptions {} +public class VersionError extends Exception { + private static final long serialVersionUID = 11L; +} diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index 4372e9f..aaa819b 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -13,7 +13,6 @@ import exception.SizeError; * @version 1.0 */ public class FileList extends Payload { - private final static RequestResponseCode requestResponseCode = RequestResponseCode.LIST_RESPONSE; private ArrayList content; /** Constructor (typically used by the server) with an ArrayList parameter containing @@ -22,10 +21,11 @@ public class FileList extends Payload { * @throws InternalError */ public FileList(ArrayList content) throws InternalError { + super(RequestResponseCode.LIST_RESPONSE); /* assert to help debugging */ assert !content.isEmpty() : "Payload size of FileList must not be empty, use EmptyDirectory from Payload instead"; if (content.isEmpty()) { - throws new InternalError(); + throw new InternalError(); } this.content = content; } @@ -34,24 +34,27 @@ public class FileList extends Payload { * @param datagram the full datagram received * @throws SizeError * @throws InternalError + * @throws ProtocolError */ - protected FileList(byte[] datagram) throws SizeError, InternalError { - //TODO + protected FileList(byte[] datagram) throws SizeError, ProtocolError, InternalError { + super(datagram); /* assert to help debugging */ - assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) == RequestResponseCode.LIST_RESPONSE : "FileList subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; + assert requestResponseCode == RequestResponseCode.LIST_RESPONSE : "FileList subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; /* InternalErrorException */ - if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE) { + if (requestResponseCode!= RequestResponseCode.LIST_RESPONSE) { throw new InternalError(); } int size = getPayloadSize(datagram); + //TODO } /** Returns a byte[] containing datagram with padding. * This datagram is still incomplete and should not be send directly. * ProtocolP2PDatagram will use this method to generate the complete datagram. * @return datagram with padding + * @throws InternalError */ - protected byte[] toDatagram() { + /*protected byte[] toDatagram() throws InternalError { //TODO compute size int size = 8 + ; byte[] datagram = new byte[size]; // java initialize all to zero @@ -61,5 +64,5 @@ public class FileList extends Payload { // bits 16-31 are reserved for future use datagram = setPayloadSize(size, datagram); //TODO Write content - } + }*/ } diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index c63801c..d6d6d2c 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -5,6 +5,8 @@ import exception.ProtocolError; import exception.InternalError; import exception.SizeError; import tools.BytesArrayTools; +import java.util.Arrays; +import java.io.UnsupportedEncodingException; /** Representation of payload for load response. * @author Louis Royer @@ -13,7 +15,6 @@ import tools.BytesArrayTools; * @version 1.0 */ public class FilePart extends Payload { - private static final RequestResponseCode requestResponseCode = RequestResponseCode.LOAD_RESPONSE; private String filename; private long totalSize; private long offset; @@ -27,10 +28,15 @@ public class FilePart extends Payload { * @throws InternalError */ public FilePart(String filename, long totalSize, long offset, byte[] partialContent) throws InternalError { + super(RequestResponseCode.LOAD_RESPONSE); /* asserts to help debugging */ assert totalSize >= 0 : "totalSize cannot be negative"; + assert partialContent.length != 0 : "partialContent.length cannot be zero"; + assert totalSize >= partialContent.length : "totalSize must be greater than partialContent.length"; assert offset >= 0 : "offset cannot be negative"; - if (totalSize < 0 || offset < 0) { + assert filename != null : "filename is required"; + if (totalSize < 0 || partialContent.length == 0 || totalSize < partialContent.length + || offset < 0 || filename == null) { throw new InternalError(); } this.filename = filename; @@ -45,64 +51,113 @@ public class FilePart extends Payload { * @throws InternalError */ protected FilePart(byte[] datagram) throws SizeError, ProtocolError, InternalError { + super(datagram); /* assert to help debugging */ - assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) == RequestResponseCode.LOAD_RESPONSE : "FilePart subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; + assert requestResponseCode == RequestResponseCode.LOAD_RESPONSE : "FilePart subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; /* InternalErrorException */ - if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE) { + if (requestResponseCode != RequestResponseCode.LOAD_RESPONSE) { throw new InternalError(); } setOffset(datagram); // this can throw SizeError - setTotalFileSize(datagram); // this can throw SizeError - int size = getPayloadSize(datagram); // this can throw SizeError - int partialContentStart = setFilename(datagram, size) + 1; // this can throw ProtocolError - setPartialContent(datagram, partialContentStart, size); + setTotalSize(datagram); // this can throw SizeError + setFilename(datagram); // this can throw ProtocolError, SizeError + setPartialContent(datagram); // this can throw SizeError } /** Returns a byte[] containing datagram with padding. * This datagram is still incomplete and should not be send directly. * ProtocolP2PDatagram will use this method to generate the complete datagram. * @return datagram with padding + * @throws InternalError */ - protected byte[] toDatagram() { + protected byte[] toDatagram() throws InternalError { //TODO : calculate payload size - int size = 24 + filename.length + 1; + int size = 24 + filename.length() + 1; byte[] datagram = new byte[size]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // bits 16-31 are reserved for future use - datagram = setPayloadSize(size, datagram); - //TODO : write offset to datagram - //TODO : write totalFileSize to datagram - //TODO : write filename\n to datagram + setPayloadSize(size, datagram); + // write offset to datagram (Byte 8) + BytesArrayTools.write(datagram, 8, offset); + // write totalSize to datagram (Byte 16) + BytesArrayTools.write(datagram, 16, totalSize); + // write filename’s size to datagram + BytesArrayTools.write(datagram, 24, filename.length()); + //TODO : write filename to datagram + try { + byte[] bFilename = filename.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new InternalError(); + } //TODO : write partialContent to datagram + + return datagram; } /** Write from bytes 8 to 15 of datagram into offset. * @param datagram received datagram * @throws SizeError */ - private setOffset(byte[] datagram) throws SizeError { + private void setOffset(byte[] datagram) throws SizeError { offset = BytesArrayTools.readLong(datagram, 8); } /** Write from bytes 16 to 23 of datagram into totalSize. * @param datagram received datagram * @throws SizeError */ - private setTotalFileSize(byte[] datagram) throw SizeError { + private void setTotalSize(byte[] datagram) throws SizeError { totalSize = BytesArrayTools.readLong(datagram, 16); } - private int setFilename(byte[] datagram, int payloadSize) throws ProtocolError { - // TODO: copy datagram from byte 24 to the first \n (excluded) - // into filename unless we excess size - // (in this case the wrong size has been - // set by the emitter, it is a protocolerror) - // return position of the \n - + /** Read filename’s size from bytes 24 to 28 of datagram. + * @param datagram received datagram + * @throws ProtocolError + * @throws SizeError + * @return filename’s size + */ + private int getFilenameSize(byte[] datagram) throws SizeError, ProtocolError { + int size = BytesArrayTools.readInt(datagram, 24); // this can throw SizeError + // filename size cannot be zero + if (size == 0) { + throw new ProtocolError(); + } + // offset (8B) + totalSize (8B) + filenameSize (4B) = 20B + if ((20 + size) > getPayloadSize(datagram)) { + throw new ProtocolError(); + } + return size; + } + + /** Write filename from byte 24 to byte (24 + (filenameSize - 1)) of datagram. + * @param datagram received datagram + * @throws ProtocolError + * @throws SizeError + * @throws InternalError + */ + private void setFilename(byte[] datagram) throws ProtocolError, SizeError, InternalError { + int filenameSize = getFilenameSize(datagram); // this can throw ProtocolError or SizeError + try { + filename = new String(datagram, 24, filenameSize, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new InternalError(); + } } - private setPartialContent(byte[] datagram, int start, int payloadSize) { - // TODO: copy datagram from start to size into partialContent + + /** Write partialContent from byte (24 + filenameSize) to byte (8 + payloadSize) of datagram. + * @param datagram received datagram + * @throws SizeError + * @throws ProtocolError + */ + private void setPartialContent(byte[] datagram) throws ProtocolError, SizeError { + int start = 24 + getFilenameSize(datagram); // this can throw SizeError or ProtocolError + int end = 8 + getPayloadSize(datagram); // this can throw SizeError + try { + partialContent = Arrays.copyOfRange(datagram, start, end+1); + } catch (ArrayIndexOutOfBoundsException e) { + throw new ProtocolError(); + } } } diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index 44a1458..ce55f38 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -13,8 +13,8 @@ import tools.BytesArrayTools; * @version 1.0 */ public class Payload { - private RequestResponseCode requestResponseCode; - protected final static int PAYLOAD_SIZE_POSITON = 4; + protected RequestResponseCode requestResponseCode; + protected final static int PAYLOAD_SIZE_POSITION = 4; protected final static int PAYLOAD_START_POSITION = 8; /** Consructor used to create Payload with a payload size of zero using a RRCode. @@ -23,8 +23,8 @@ public class Payload { */ public Payload(RequestResponseCode requestResponseCode) throws InternalError { /* asserts to help debugging */ - assert requestResponseCode != requestResponseCode.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class"; - assert requestResponseCode != requestResponseCode.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class"; + assert requestResponseCode != RequestResponseCode.LIST_RESPONSE || (this instanceof FileList) : "LIST_RESPONSE must use FilePart class"; + assert requestResponseCode != RequestResponseCode.LOAD_RESPONSE || (this instanceof FilePart) : "LOAD_RESPONSE must use FileList class"; this.requestResponseCode = requestResponseCode; checkRequestResponseCode(); // this can throw InternalError } @@ -37,8 +37,8 @@ public class Payload { */ protected Payload(byte[] datagram) throws ProtocolError, InternalError { /* asserts to help debugging */ - assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class"; - assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class"; + assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE || (this instanceof FileList) : "LIST_RESPONSE must use FilePart class"; + assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE || (this instanceof FilePart) : "LOAD_RESPONSE must use FileList class"; requestResponseCode = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]); checkRequestResponseCode(); // this can throw InternalError } @@ -48,7 +48,8 @@ public class Payload { */ private void checkRequestResponseCode() throws InternalError { /* Incorrect use cases (use subclasses instead) */ - if (requestResponseCode == RequestResponseCode.LIST_RESPONSE || requestResponseCode == RequestResponseCode.LOAD_RESPONSE) { + if ((requestResponseCode == RequestResponseCode.LIST_RESPONSE && !(this instanceof FileList)) + || (requestResponseCode == RequestResponseCode.LOAD_RESPONSE && !(this instanceof FilePart))) { throw new InternalError(); } } @@ -57,14 +58,17 @@ public class Payload { * This datagram is still incomplete and should not be send directly. * ProtocolP2PDatagram will use this method to generate the complete datagram. * @return datagram with padding + * @throws InternalError */ - protected byte[] toDatagram() { + protected byte[] toDatagram() throws InternalError { + // InternalError is impossible in this method on Payload class but still on subclasses byte [] datagram = new byte[8]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // bits 16-31 are reserved for future use // payload size is 0 (this is what java have initialized datagram) + return datagram; } /** Set payload’s size in a datagram. @@ -72,7 +76,7 @@ public class Payload { * @param datagram datagram to be completed * @throws InternalError */ - protected static setPayloadSize(int size, byte[] datagram) throws InternalError { + protected static void setPayloadSize(int size, byte[] datagram) throws InternalError { /* assert to help debugging */ assert size >= 0: "Payload size cannot be negative"; if (size < 0) { @@ -80,7 +84,7 @@ public class Payload { // because this is only for reception side throw new InternalError(); } - BytesArrayTools.write(datagram, PAYLOAD_SIZE_POSITON, size); + BytesArrayTools.write(datagram, PAYLOAD_SIZE_POSITION, size); } /** Get payload’s size from a datagram. @@ -89,6 +93,6 @@ public class Payload { * @throws SizeError */ protected static int getPayloadSize(byte[] datagram) throws SizeError { - return BytesArrayTools.readInt(datagram, PAYLOAD_SIZE_POSITON]; + return BytesArrayTools.readInt(datagram, PAYLOAD_SIZE_POSITION); } } diff --git a/src/protocolP2P/ProtocolP2PDatagram.java b/src/protocolP2P/ProtocolP2PDatagram.java index 1846258..a259028 100644 --- a/src/protocolP2P/ProtocolP2PDatagram.java +++ b/src/protocolP2P/ProtocolP2PDatagram.java @@ -1,6 +1,8 @@ package protocolP2P; import exception.ProtocolError; import exception.VersionError; +import exception.SizeError; +import exception.InternalError; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import java.util.ArrayList; @@ -30,8 +32,9 @@ public class ProtocolP2PDatagram { /** Send datagram on socket * @param socket DatagramSocket used to send datagram + * @throws InternalError */ - public void send(DatagramSocket socket) { + public void send(DatagramSocket socket) throws InternalError { //TODO byte[] datagram = toDatagram(); // generate DatagramPacket @@ -43,31 +46,34 @@ public class ProtocolP2PDatagram { * @return payload of the datagram * @throws ProtocolError * @throws VersionError + * @throws InternalError + * @throws SizeError */ - public static Payload receive(DatagramSocket socket) throws ProtocolError, VersionError { + public static Payload receive(DatagramSocket socket) throws ProtocolError, VersionError, InternalError, SizeError { //TODO // reception - //datagram = + byte[] datagram = new byte[5]; // contruction ProtocolP2PDatagram p = new ProtocolP2PDatagram(datagram); return p.getPayload(); - } /** Private constructor with datagram as byte[] parameter (typically used when receiving datagram). * @param datagram the full datagram received * @throws ProtocolError * @throws VersionError + * @throws InternalError + * @throws SizeError */ - private ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError, SizeError { + private ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError, InternalError, SizeError { // unwrap version version = datagram[VERSION_POSITON]; checkProtocolVersion(); // this can throw VersionError RequestResponseCode r = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]); // this can throw ProtocolError switch (r) { - case RequestResponseCode.LIST_RESPONSE: + case LIST_RESPONSE: payload = (Payload) new FileList(datagram); break; - case RequestResponseCode.LOAD_RESPONSE: + case LOAD_RESPONSE: payload = (Payload) new FilePart(datagram); break; default: @@ -79,11 +85,13 @@ public class ProtocolP2PDatagram { /** Returns a byte[] containing full datagram (typically used when sending datagram). * This datagram is still complete and ready to be send. * @return the full datagram to send + * @throws InternalError */ - private byte[] toDatagram() { + private byte[] toDatagram() throws InternalError { byte[] datagram = payload.toDatagram(); datagram[VERSION_POSITON] = version; return datagram; + } /** Returns Payload associated with the datagram. diff --git a/src/protocolP2P/RequestResponseCode.java b/src/protocolP2P/RequestResponseCode.java index e23def6..81f3cad 100644 --- a/src/protocolP2P/RequestResponseCode.java +++ b/src/protocolP2P/RequestResponseCode.java @@ -51,11 +51,11 @@ public enum RequestResponseCode { * @return enum element */ public static RequestResponseCode fromCode(byte code) throws ProtocolError { - byte code = BY_CODE.get(Byte.valueOf(code)); - if (code == null) { + RequestResponseCode r= BY_CODE.get(Byte.valueOf(code)); + if (r == null) { throw new ProtocolError(); } - return code; + return r; } diff --git a/src/tools/ByteArrayTools.java b/src/tools/BytesArrayTools.java similarity index 92% rename from src/tools/ByteArrayTools.java rename to src/tools/BytesArrayTools.java index 176d562..9547f64 100644 --- a/src/tools/ByteArrayTools.java +++ b/src/tools/BytesArrayTools.java @@ -14,7 +14,7 @@ public class BytesArrayTools { */ public static void write(byte[] array, int start, int value) { for(int i=0;i<4;i++) { - array[start + i] = (byte) ((size >> (8 * (3 - i))) & 0xFF); + array[start + i] = (byte) ((value >> (8 * (3 - i))) & 0xFF); } } /** Write long in a bytearray @@ -24,7 +24,7 @@ public class BytesArrayTools { */ public static void write(byte[] array, int start, long value) { for(int i=0;i<4;i++) { - array[start + i] = (byte) ((size >> (8 * (4 - i))) & 0xFF); + array[start + i] = (byte) ((value >> (8 * (4 - i))) & 0xFF); } } From eeb22e74cc7a41a6420b7ddb973839f08aa28ab1 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 23 Jan 2020 16:34:10 +0100 Subject: [PATCH 42/48] FileList is done --- src/protocolP2P/FileList.java | 55 +++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index aaa819b..65c824f 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -1,10 +1,11 @@ package protocolP2P; -import java.util.ArrayList; +import java.util.Arrays; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import exception.ProtocolError; import exception.InternalError; import exception.SizeError; +import java.io.UnsupportedEncodingException; /** Representation of payload for list response. * @author Louis Royer @@ -13,18 +14,18 @@ import exception.SizeError; * @version 1.0 */ public class FileList extends Payload { - private ArrayList content; + private String[] content; /** Constructor (typically used by the server) with an ArrayList parameter containing * filenames. * @param content a list of files. Must not be empty. * @throws InternalError */ - public FileList(ArrayList content) throws InternalError { + public FileList(String[] content) throws InternalError { super(RequestResponseCode.LIST_RESPONSE); /* assert to help debugging */ - assert !content.isEmpty() : "Payload size of FileList must not be empty, use EmptyDirectory from Payload instead"; - if (content.isEmpty()) { + assert content.length != 0 : "Payload size of FileList must not be empty, use EmptyDirectory from Payload instead"; + if (content.length == 0) { throw new InternalError(); } this.content = content; @@ -45,7 +46,11 @@ public class FileList extends Payload { throw new InternalError(); } int size = getPayloadSize(datagram); - //TODO + try { + content = (new String(datagram, 8, size, "UTF-8")).split("\n"); + } catch (UnsupportedEncodingException e) { + throw new InternalError(); + } } /** Returns a byte[] containing datagram with padding. @@ -54,15 +59,41 @@ public class FileList extends Payload { * @return datagram with padding * @throws InternalError */ - /*protected byte[] toDatagram() throws InternalError { - //TODO compute size - int size = 8 + ; + protected byte[] toDatagram() throws InternalError { + // compute size + int size = 8; + for (String s : content) { + size += s.length(); + } byte[] datagram = new byte[size]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // bits 16-31 are reserved for future use - datagram = setPayloadSize(size, datagram); - //TODO Write content - }*/ + setPayloadSize(size, datagram); + // Write content + int bCount = 8; + for(String s : content) { + if (bCount != 8) { // not on first iteration + try { + datagram[bCount] = "\n".getBytes("UTF-8")[0]; // separator + } catch (UnsupportedEncodingException e) { + throw new InternalError(); + } + bCount += 1; + } + // Copy filename + try { + byte[] sb = s.getBytes("UTF-8"); + for(byte b : sb) { + datagram[bCount] = b; + bCount += 1; + } + } catch (UnsupportedEncodingException e) { + throw new InternalError(); + } + + } + return datagram; + } } From 462b376eeef8d3c3acb264f50fcba7857167353a Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 23 Jan 2020 17:21:23 +0100 Subject: [PATCH 43/48] Add send + receive methods --- src/protocolP2P/FileList.java | 4 ++- src/protocolP2P/FilePart.java | 4 ++- src/protocolP2P/Payload.java | 9 ++++- src/protocolP2P/ProtocolP2PDatagram.java | 42 +++++++++++++++++++----- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index 65c824f..6734f38 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -2,6 +2,7 @@ package protocolP2P; import java.util.Arrays; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; +import exception.TransmissionError; import exception.ProtocolError; import exception.InternalError; import exception.SizeError; @@ -36,8 +37,9 @@ public class FileList extends Payload { * @throws SizeError * @throws InternalError * @throws ProtocolError + * @throws TransmissionError */ - protected FileList(byte[] datagram) throws SizeError, ProtocolError, InternalError { + protected FileList(byte[] datagram) throws TransmissionError, SizeError, ProtocolError, InternalError { super(datagram); /* assert to help debugging */ assert requestResponseCode == RequestResponseCode.LIST_RESPONSE : "FileList subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index d6d6d2c..1dd89ce 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -4,6 +4,7 @@ import protocolP2P.RequestResponseCode; import exception.ProtocolError; import exception.InternalError; import exception.SizeError; +import exception.TransmissionError; import tools.BytesArrayTools; import java.util.Arrays; import java.io.UnsupportedEncodingException; @@ -49,8 +50,9 @@ public class FilePart extends Payload { * @param datagram the full datagram received * @throws SizeError * @throws InternalError + * @throws TransmissionError */ - protected FilePart(byte[] datagram) throws SizeError, ProtocolError, InternalError { + protected FilePart(byte[] datagram) throws TransmissionError, SizeError, ProtocolError, InternalError { super(datagram); /* assert to help debugging */ assert requestResponseCode == RequestResponseCode.LOAD_RESPONSE : "FilePart subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index ce55f38..f09010c 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -4,6 +4,7 @@ import protocolP2P.FilePart; import protocolP2P.FileList; import exception.ProtocolError; import exception.InternalError; +import exception.TransmissionError; import exception.SizeError; import tools.BytesArrayTools; /** Representation of payload. If payload has a size, use subclasses instead. @@ -34,9 +35,15 @@ public class Payload { * @param datagram the full datagram received * @throws ProtocolError * @throws InternalError + * @throws TransmissionError + * @throws SizeError */ - protected Payload(byte[] datagram) throws ProtocolError, InternalError { + protected Payload(byte[] datagram) throws SizeError, ProtocolError, InternalError, TransmissionError { /* asserts to help debugging */ + assert getPayloadSize(datagram) + 8 <= datagram.length : "Payload is truncated"; + if (datagram.length < getPayloadSize(datagram) + 8) { + throw new TransmissionError(); + } assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE || (this instanceof FileList) : "LIST_RESPONSE must use FilePart class"; assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE || (this instanceof FilePart) : "LOAD_RESPONSE must use FileList class"; requestResponseCode = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]); diff --git a/src/protocolP2P/ProtocolP2PDatagram.java b/src/protocolP2P/ProtocolP2PDatagram.java index a259028..730eac3 100644 --- a/src/protocolP2P/ProtocolP2PDatagram.java +++ b/src/protocolP2P/ProtocolP2PDatagram.java @@ -3,12 +3,16 @@ import exception.ProtocolError; import exception.VersionError; import exception.SizeError; import exception.InternalError; +import exception.TransmissionError; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import java.util.ArrayList; import java.lang.Byte; import java.net.DatagramPacket; import java.net.DatagramSocket; +import java.net.InetAddress; +import java.io.IOException; +import java.net.UnknownHostException; /** Representation of datagram. * @author Louis Royer @@ -30,41 +34,61 @@ public class ProtocolP2PDatagram { this.payload = payload; } - /** Send datagram on socket - * @param socket DatagramSocket used to send datagram + /** Send datagram on socket (from client) + * @param socket DatagramSocket used to send datagram. + * @param host host to send datagram (null if this is a response) + * @param port port to send datagram (null if this is a response) * @throws InternalError + * @throws UnknownHostException + * @throws IOException */ - public void send(DatagramSocket socket) throws InternalError { - //TODO + public void send(DatagramSocket socket, String host, int port) throws InternalError, UnknownHostException, IOException { + InetAddress dst = InetAddress.getByName(host); byte[] datagram = toDatagram(); // generate DatagramPacket + DatagramPacket datagramPacket = new DatagramPacket(datagram, datagram.length, dst, port); // send it + socket.send(datagramPacket); + } + + /** Send datagram on socket (from server, as a response) + * @param socket DatagramSocket used to send datagram. + * @throws InternalError + * @throws IOException + */ + public void send(DatagramSocket socket) throws InternalError, IOException { + byte[] datagram = toDatagram(); + // generate DatagramPacket + DatagramPacket datagramPacket = new DatagramPacket(datagram, datagram.length); + socket.send(datagramPacket); } /** Receive datagram on socket * @param socket DatagramSocket used to receive datagram * @return payload of the datagram + * @throws TransmissionError * @throws ProtocolError * @throws VersionError * @throws InternalError * @throws SizeError */ - public static Payload receive(DatagramSocket socket) throws ProtocolError, VersionError, InternalError, SizeError { - //TODO + public static Payload receive(DatagramSocket socket) throws TransmissionError, ProtocolError, VersionError, InternalError, SizeError { // reception - byte[] datagram = new byte[5]; + byte[] datagram = new byte[4096]; + DatagramPacket reception = new DatagramPacket(datagram, datagram.length); // contruction ProtocolP2PDatagram p = new ProtocolP2PDatagram(datagram); return p.getPayload(); } /** Private constructor with datagram as byte[] parameter (typically used when receiving datagram). * @param datagram the full datagram received + * @throws TransmissionError * @throws ProtocolError * @throws VersionError * @throws InternalError * @throws SizeError */ - private ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError, InternalError, SizeError { + private ProtocolP2PDatagram(byte[] datagram) throws TransmissionError, ProtocolError, VersionError, InternalError, SizeError { // unwrap version version = datagram[VERSION_POSITON]; checkProtocolVersion(); // this can throw VersionError @@ -77,7 +101,7 @@ public class ProtocolP2PDatagram { payload = (Payload) new FilePart(datagram); break; default: - payload = new Payload(datagram); + payload = new Payload(datagram); // this can throw TransmissionError break; } } From 4fec7446163e3ced894c13340130fa5ac0f069f7 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 23 Jan 2020 17:38:26 +0100 Subject: [PATCH 44/48] End of protocol implementation --- src/protocolP2P/FilePart.java | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 1dd89ce..d4a3fb2 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -73,8 +73,8 @@ public class FilePart extends Payload { * @throws InternalError */ protected byte[] toDatagram() throws InternalError { - //TODO : calculate payload size - int size = 24 + filename.length() + 1; + // compute payload size + int size = 28 + filename.length() + partialContent.length; byte[] datagram = new byte[size]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code @@ -87,14 +87,22 @@ public class FilePart extends Payload { BytesArrayTools.write(datagram, 16, totalSize); // write filename’s size to datagram BytesArrayTools.write(datagram, 24, filename.length()); - //TODO : write filename to datagram + // write filename to datagram try { byte[] bFilename = filename.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new InternalError(); } - //TODO : write partialContent to datagram - + int i = filename.length() + 24; + for (byte b : bFilename) { + datagram[i] = b; + i += 1; + } + // write partialContent to datagram + for (byte b: partialContent) { + datagram[i] = b; + i += 1; + } return datagram; } @@ -113,7 +121,7 @@ public class FilePart extends Payload { totalSize = BytesArrayTools.readLong(datagram, 16); } - /** Read filename’s size from bytes 24 to 28 of datagram. + /** Read filename’s size from bytes 24 to 27 of datagram. * @param datagram received datagram * @throws ProtocolError * @throws SizeError @@ -132,7 +140,7 @@ public class FilePart extends Payload { return size; } - /** Write filename from byte 24 to byte (24 + (filenameSize - 1)) of datagram. + /** Write filename from byte 28 to byte (28 + (filenameSize - 1)) of datagram. * @param datagram received datagram * @throws ProtocolError * @throws SizeError @@ -141,19 +149,19 @@ public class FilePart extends Payload { private void setFilename(byte[] datagram) throws ProtocolError, SizeError, InternalError { int filenameSize = getFilenameSize(datagram); // this can throw ProtocolError or SizeError try { - filename = new String(datagram, 24, filenameSize, "UTF-8"); + filename = new String(datagram, 28, filenameSize, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new InternalError(); } } - /** Write partialContent from byte (24 + filenameSize) to byte (8 + payloadSize) of datagram. + /** Write partialContent from byte (28 + filenameSize) to byte (8 + payloadSize) of datagram. * @param datagram received datagram * @throws SizeError * @throws ProtocolError */ private void setPartialContent(byte[] datagram) throws ProtocolError, SizeError { - int start = 24 + getFilenameSize(datagram); // this can throw SizeError or ProtocolError + int start = 28 + getFilenameSize(datagram); // this can throw SizeError or ProtocolError int end = 8 + getPayloadSize(datagram); // this can throw SizeError try { partialContent = Arrays.copyOfRange(datagram, start, end+1); From 2d20357150ed2713a45ffef45fc5dfea895cd315 Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 25 Jan 2020 17:48:21 +0100 Subject: [PATCH 45/48] Implement ServerManagement and ClientManagement --- src/clientP2P/ClientManagementUDP.java | 223 +++++++++--------- src/clientP2P/ClientP2P.java | 2 +- src/protocolP2P/FileList.java | 30 ++- src/protocolP2P/FilePart.java | 52 +++- src/protocolP2P/LoadRequest.java | 90 +++++++ src/protocolP2P/Payload.java | 15 +- src/protocolP2P/ProtocolP2PDatagram.java | 115 ++++++++- src/protocolP2P/RequestResponseCode.java | 2 +- src/remoteException/EmptyDirectory.java | 4 + src/remoteException/InternalRemoteError.java | 4 + .../NotFound.java | 2 +- src/remoteException/ProtocolRemoteError.java | 4 + src/remoteException/VersionRemoteError.java | 4 + src/serverP2P/ServerManagementUDP.java | 205 +++++++--------- src/serverP2P/ServerP2P.java | 2 +- 15 files changed, 489 insertions(+), 265 deletions(-) create mode 100644 src/protocolP2P/LoadRequest.java create mode 100644 src/remoteException/EmptyDirectory.java create mode 100644 src/remoteException/InternalRemoteError.java rename src/{exception => remoteException}/NotFound.java (79%) create mode 100644 src/remoteException/ProtocolRemoteError.java create mode 100644 src/remoteException/VersionRemoteError.java diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index a64aa48..3b8a811 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -1,18 +1,30 @@ package clientP2P; -import exception.NotFound; -import exception.ProtocolError; import exception.InternalError; +import exception.ProtocolError; +import exception.SizeError; import exception.TransmissionError; +import exception.VersionError; +import remoteException.EmptyDirectory; +import remoteException.InternalRemoteError; +import remoteException.NotFound; +import remoteException.ProtocolRemoteError; +import remoteException.VersionRemoteError; +import java.net.UnknownHostException; import java.util.Scanner; import java.net.InetAddress; -import java.net.DatagramPacket; import java.net.DatagramSocket; -import java.io.File; +import java.net.SocketException; import java.io.IOException; -import java.io.FileWriter; - +import java.nio.file.Files; +import java.io.File; +import protocolP2P.ProtocolP2PDatagram; +import protocolP2P.Payload; +import protocolP2P.RequestResponseCode; +import protocolP2P.FileList; +import protocolP2P.FilePart; +import protocolP2P.LoadRequest; -/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP. +/** Implementation of P2P-JAVA-PROJECT CLIENT * @author Louis Royer * @author Flavien Haas * @author JS Auge @@ -22,142 +34,139 @@ public class ClientManagementUDP implements Runnable { private String baseDirectory; private int UDPPort; private String host; - private final String protocolID = "P2P-JAVA-PROJECT VERSION 1.0"; + private DatagramSocket socket; + /** Constructor for UDP implementation, with baseDirectory and UDPPort parameters. * @param baseDirectory the root directory where files are stored + * @param host hostname of the server * @param UDPPort the server will listen on this port */ public ClientManagementUDP(String baseDirectory, String host, int UDPPort) { this.baseDirectory = baseDirectory; this.host = host; this.UDPPort = UDPPort; + try { + socket = new DatagramSocket(); + } catch (SocketException e) { + System.err.println("Error: No socket available."); + System.exit(-1); + } } /** Implementation of Runnable */ public void run() { - System.out.println("Files present on the server:"); try { - String msg = sendMsg(sendListDirectoryRequest()); - System.out.println(listDirectory(msg)); + String[] list = listDirectory(); + System.out.println("Files present on the server:"); + for(String listItem: list) { + System.out.println(listItem); + } System.out.println("Name of the file to download:"); Scanner scanner = new Scanner(System.in); String f = scanner.nextLine(); - msg = sendMsg(sendDownloadRequest(f)); - download(msg, f); + download(f); + System.out.println("File sucessfully downloaded"); + } catch (EmptyDirectory e) { + System.err.println("Error: Server has no file in directory"); + } catch (InternalError e) { + System.err.println("Error: Client internal error"); + } catch (UnknownHostException e) { + System.err.println("Error: Server host is unknown"); + } catch (IOException e) { + System.err.println("Error: Request cannot be send or response cannot be received"); } catch (TransmissionError e) { - System.out.println("Transmission error"); + System.err.println("Error: Message received is too big"); } catch (ProtocolError e) { - System.out.println("Protocol error"); + System.err.println("Error: Cannot decode server’s response"); + } catch (VersionError e) { + System.err.println("Error: Server’s response use bad version of the protocol"); + } catch (SizeError e) { + System.err.println("Error: Cannot handle this packets because of internal representation limitations of numbers on the client"); + } catch (InternalRemoteError e) { + System.err.println("Error: Server internal error"); + } catch (ProtocolRemoteError e) { + System.err.println("Error: Server cannot decode client’s request"); + } catch (VersionRemoteError e) { + System.err.println("Error: Server cannot decode this version of the protocol"); } catch (NotFound e) { - System.out.println("File not found"); - } catch (InternalError e) { - System.out.println("Server internal error"); - } catch (IOException e) { - System.out.println("Cannot write to file"); + System.err.println("Error: Server have not this file in directory"); } } - /** Prepare request to download file - * @param filename name of the file to be downloaded - * @return request to be send - */ - private String sendDownloadRequest(String filename) { - return protocolID + "\nDOWNLOAD\n" + filename + "\n"; - } - - /** Download file. - * @param response Servers's response - * @throws NotFound - * @throws ProtocolError - * @throws InternalError + /** Try to download a file + * @param filename name of the file to download + * @throws NotFound + * @throws InternalError + * @throws UnknownHostException + * @throws IOException + * @throws TransmissionError + * @throws ProtocolError + * @throws VersionError + * @throws SizeError + * @throws InternalRemoteError + * @throws ProtocolRemoteError + * @throws VersionRemoteError */ - private void download(String response, String filename) throws TransmissionError, NotFound, ProtocolError, InternalError, IOException { + private void download(String filename) throws NotFound, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { + ProtocolP2PDatagram d = new ProtocolP2PDatagram((Payload) new LoadRequest(filename)); + d.send(socket, host, UDPPort); try { - String r[] = response.split("\n"); - checkProtocolID(r[0]); - switch (r[1]) { - case "LOAD": - int size = Integer.parseInt(r[2]); - if (r[3].length() != size - 1) { - throw new TransmissionError(); - } - FileWriter fileWriter = new FileWriter(baseDirectory + filename); - fileWriter.write(r[3]); - fileWriter.close(); - break; - case "NOT FOUND": - throw new NotFound(); - case "INTERNAL ERROR": + Payload p = ProtocolP2PDatagram.receive(socket).getPayload(); + assert p instanceof FilePart : "This payload must be instance of FilePart"; + if (!(p instanceof FilePart)) { throw new InternalError(); - default: - System.out.println("Error: Unknow format `" + r[1] + "`"); - throw new ProtocolError(); + } else { + FilePart fp = (FilePart)p; + if (!fp.getFilename().equals(filename)) { + System.err.println("Error: wrong file received"); + throw new ProtocolError(); + } + if (fp.getOffset() != 0 || fp.getPartialContent().length != fp.getTotalSize()) { + System.err.println("Error: cannot handle partial files (not implemented)"); + throw new InternalError(); + } + try { + Files.write(new File(baseDirectory + filename).toPath(), fp.getPartialContent()); + } catch (IOException e) { + System.err.println("Error: cannot write file (" + baseDirectory + filename + ")"); + } } - } catch (java.lang.ArrayIndexOutOfBoundsException e) { - System.out.println("Error: IndexOutOfBonds"); - throw new ProtocolError(); - } catch (NumberFormatException e) { - System.out.println("Error: NumberFormat"); - throw new ProtocolError(); - } - } - - /** Prepare request to list files on server - * @return request to be send - */ - private String sendListDirectoryRequest() { - return protocolID + "\nLIST\n"; + } catch (EmptyDirectory e) { + throw new ProtocolError(); } - /** Parse list of directory response from server - * @param response server's response - * @return list of files, separated by CRLF - * @throws ProtocolError - */ - private String listDirectory(String response) throws ProtocolError { - try { - String r[] = response.split("\n"); - checkProtocolID(r[0]); - return response.split(protocolID + "\nLIST\n")[1].split("\n\n")[0]; - } catch (java.lang.ArrayIndexOutOfBoundsException e) { - throw new ProtocolError(); - } } - /** Check client's protocol identifier matches message's protocol identifier. - * Throws a ProtocolError if mismatched. - * @param msgProtocolID part of the request containing protocol identifier + /** list server’s directory content + * @return list of files + * @throws InternalError + * @throws UnknowHostException + * @throws IOException + * @throws TransmissionError * @throws ProtocolError + * @throws VersionError + * @throws SizeError + * @throws EmptyDirectory + * @throws InternalRemoteError + * @throws ProtocolRemoteError + * @throws VersionRemoteError */ - private void checkProtocolID(String msgProtocolID) throws ProtocolError { - if (!protocolID.equals(msgProtocolID)) { + private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { + ProtocolP2PDatagram d = new ProtocolP2PDatagram(new Payload(RequestResponseCode.LIST_REQUEST)); + d.send(socket, host, UDPPort); + try { + Payload p = ProtocolP2PDatagram.receive(socket).getPayload(); + assert p instanceof FileList : "This payload must be instance of Filelist"; + if (!(p instanceof FileList)) { + throw new InternalError(); + } else { + return ((FileList)p).getFileList(); + } + } catch (NotFound e) { throw new ProtocolError(); } } - - /** Send message to server - * @param msg message to be send - * @return server's response - */ - private String sendMsg(String msg) throws TransmissionError { - //TODO changer le gros try catch - try{ - InetAddress dst = InetAddress.getByName(host); - byte [] buffer = msg.getBytes(); - byte [] buffer2 = new byte[1500]; - DatagramSocket socket = new DatagramSocket(); - DatagramPacket reception = new DatagramPacket(buffer2, 1500); - DatagramPacket emission = new DatagramPacket(buffer, buffer.length, dst, UDPPort); - socket.send(emission); - socket.receive(reception); - return new String(reception.getData(), 0, reception.getLength()); - } catch (Exception e){ - System.out.println(e); - throw new TransmissionError(); - } - - } } diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 32c2641..8ee0186 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -10,7 +10,7 @@ public class ClientP2P { public ClientP2P() { directories = new Directories("P2P_JAVA_PROJECT_CLIENT"); host = "localhost"; - port = 40000; + port = 40001; System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directories.getDataHomeDirectory()); directories.askOpenDataHomeDirectory(); } diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index 6734f38..9f141c9 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -15,21 +15,21 @@ import java.io.UnsupportedEncodingException; * @version 1.0 */ public class FileList extends Payload { - private String[] content; + private String[] fileList; /** Constructor (typically used by the server) with an ArrayList parameter containing * filenames. - * @param content a list of files. Must not be empty. + * @param fileList a list of files. Must not be empty. * @throws InternalError */ - public FileList(String[] content) throws InternalError { + public FileList(String[] fileList) throws InternalError { super(RequestResponseCode.LIST_RESPONSE); /* assert to help debugging */ - assert content.length != 0 : "Payload size of FileList must not be empty, use EmptyDirectory from Payload instead"; - if (content.length == 0) { + assert fileList.length != 0 : "Payload size of FileList must not be empty, use EmptyDirectory from Payload instead"; + if (fileList.length == 0) { throw new InternalError(); } - this.content = content; + this.fileList = fileList; } /** Constructor (typically used by client) with a byte[] parameter containing the datagram received. @@ -49,7 +49,7 @@ public class FileList extends Payload { } int size = getPayloadSize(datagram); try { - content = (new String(datagram, 8, size, "UTF-8")).split("\n"); + fileList = (new String(datagram, 8, size, "UTF-8")).split("\n"); } catch (UnsupportedEncodingException e) { throw new InternalError(); } @@ -64,18 +64,17 @@ public class FileList extends Payload { protected byte[] toDatagram() throws InternalError { // compute size int size = 8; - for (String s : content) { + for (String s : fileList) { size += s.length(); } byte[] datagram = new byte[size]; // java initialize all to zero - // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // bits 16-31 are reserved for future use - setPayloadSize(size, datagram); - // Write content + setPayloadSize(size - 8, datagram); + // Write fileList int bCount = 8; - for(String s : content) { + for(String s : fileList) { if (bCount != 8) { // not on first iteration try { datagram[bCount] = "\n".getBytes("UTF-8")[0]; // separator @@ -98,4 +97,11 @@ public class FileList extends Payload { } return datagram; } + + /** fileList getter. + * @return fileList + */ + public String[] getFileList() { + return fileList; + } } diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index d4a3fb2..32deb5f 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -76,11 +76,10 @@ public class FilePart extends Payload { // compute payload size int size = 28 + filename.length() + partialContent.length; byte[] datagram = new byte[size]; // java initialize all to zero - // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // bits 16-31 are reserved for future use - setPayloadSize(size, datagram); + setPayloadSize(size - 8, datagram); // write offset to datagram (Byte 8) BytesArrayTools.write(datagram, 8, offset); // write totalSize to datagram (Byte 16) @@ -90,20 +89,20 @@ public class FilePart extends Payload { // write filename to datagram try { byte[] bFilename = filename.getBytes("UTF-8"); + int i = filename.length() + 24; + for (byte b : bFilename) { + datagram[i] = b; + i += 1; + } + // write partialContent to datagram + for (byte b: partialContent) { + datagram[i] = b; + i += 1; + } + return datagram; } catch (UnsupportedEncodingException e) { throw new InternalError(); } - int i = filename.length() + 24; - for (byte b : bFilename) { - datagram[i] = b; - i += 1; - } - // write partialContent to datagram - for (byte b: partialContent) { - datagram[i] = b; - i += 1; - } - return datagram; } /** Write from bytes 8 to 15 of datagram into offset. @@ -170,4 +169,31 @@ public class FilePart extends Payload { } } + /** partialContent getter. + * @return partialcontent + */ + public byte[] getPartialContent() { + return partialContent; + } + + /** filename getter. + * @return String + */ + public String getFilename() { + return filename; + } + + /** offset getter. + * @return offset + */ + public long getOffset() { + return offset; + } + + /** totalSize getter. + * @return totalSize + */ + public long getTotalSize() { + return totalSize; + } } diff --git a/src/protocolP2P/LoadRequest.java b/src/protocolP2P/LoadRequest.java new file mode 100644 index 0000000..af3082f --- /dev/null +++ b/src/protocolP2P/LoadRequest.java @@ -0,0 +1,90 @@ +package protocolP2P; +import protocolP2P.Payload; +import protocolP2P.RequestResponseCode; +import exception.TransmissionError; +import exception.ProtocolError; +import exception.InternalError; +import exception.SizeError; +import java.io.UnsupportedEncodingException; + +/** Representation of payload for load request. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ +public class LoadRequest extends Payload { + private String filename; + + /** Constructor (typically used by the server) with a filename parameter. + * @param filename name of the file to download. Must not be empty. + * @throws InternalError + */ + public LoadRequest(String filename) throws InternalError { + super(RequestResponseCode.LOAD_REQUEST); + /* assert to help debugging */ + assert filename.length() != 0 : "Payload size of LoadRequest must not be empty"; + if (filename.length() == 0) { + throw new InternalError(); + } + this.filename = filename; + } + + /** Constructor (typically used by client) with a byte[] parameter containing the datagram received. + * @param datagram the full datagram received + * @throws SizeError + * @throws InternalError + * @throws ProtocolError + * @throws TransmissionError + */ + protected LoadRequest(byte[] datagram) throws TransmissionError, SizeError, ProtocolError, InternalError { + super(datagram); + /* assert to help debugging */ + assert requestResponseCode == RequestResponseCode.LOAD_REQUEST : "LoadRequest subclass is incompatible with this datagram, request/response code must be checked before using this constructor"; + /* InternalErrorException */ + if (requestResponseCode!= RequestResponseCode.LOAD_REQUEST) { + throw new InternalError(); + } + int size = getPayloadSize(datagram); + try { + filename = new String(datagram, 8, size, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new InternalError(); + } + } + + /** Returns a byte[] containing datagram with padding. + * This datagram is still incomplete and should not be send directly. + * ProtocolP2PDatagram will use this method to generate the complete datagram. + * @return datagram with padding + * @throws InternalError + */ + protected byte[] toDatagram() throws InternalError { + // compute size + int size = 8 + filename.length(); + byte[] datagram = new byte[size]; // java initialize all to zero + // set request/response code + datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; + // bits 16-31 are reserved for future use + setPayloadSize(size - 8, datagram); + // Write filename + int bCount = 8; + try { + byte[] sb = filename.getBytes("UTF-8"); + for(byte b : sb) { + datagram[bCount] = b; + bCount += 1; + } + } catch (UnsupportedEncodingException e) { + throw new InternalError(); + } + return datagram; + } + + /** filename getter. + * @return filename + */ + public String getFilename() { + return filename; + } +} diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index f09010c..c3638f6 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -2,6 +2,7 @@ package protocolP2P; import protocolP2P.RequestResponseCode; import protocolP2P.FilePart; import protocolP2P.FileList; +import protocolP2P.LoadRequest; import exception.ProtocolError; import exception.InternalError; import exception.TransmissionError; @@ -26,6 +27,7 @@ public class Payload { /* asserts to help debugging */ assert requestResponseCode != RequestResponseCode.LIST_RESPONSE || (this instanceof FileList) : "LIST_RESPONSE must use FilePart class"; assert requestResponseCode != RequestResponseCode.LOAD_RESPONSE || (this instanceof FilePart) : "LOAD_RESPONSE must use FileList class"; + assert requestResponseCode != RequestResponseCode.LOAD_REQUEST || (this instanceof LoadRequest) : "LOAD_REQUEST must use LoadRequest class"; this.requestResponseCode = requestResponseCode; checkRequestResponseCode(); // this can throw InternalError } @@ -46,6 +48,7 @@ public class Payload { } assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE || (this instanceof FileList) : "LIST_RESPONSE must use FilePart class"; assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE || (this instanceof FilePart) : "LOAD_RESPONSE must use FileList class"; + assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_REQUEST || (this instanceof LoadRequest) : "LOAD_REQUEST must use LoadRequest class"; requestResponseCode = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]); checkRequestResponseCode(); // this can throw InternalError } @@ -56,7 +59,8 @@ public class Payload { private void checkRequestResponseCode() throws InternalError { /* Incorrect use cases (use subclasses instead) */ if ((requestResponseCode == RequestResponseCode.LIST_RESPONSE && !(this instanceof FileList)) - || (requestResponseCode == RequestResponseCode.LOAD_RESPONSE && !(this instanceof FilePart))) { + || (requestResponseCode == RequestResponseCode.LOAD_RESPONSE && !(this instanceof FilePart)) + || (requestResponseCode == RequestResponseCode.LOAD_REQUEST && !(this instanceof LoadRequest))) { throw new InternalError(); } } @@ -70,7 +74,7 @@ public class Payload { protected byte[] toDatagram() throws InternalError { // InternalError is impossible in this method on Payload class but still on subclasses byte [] datagram = new byte[8]; // java initialize all to zero - // size is keep blank (ProtocolP2PDatagram will handle it) + // size is zero (and this is the default) // set request/response code datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; // bits 16-31 are reserved for future use @@ -102,4 +106,11 @@ public class Payload { protected static int getPayloadSize(byte[] datagram) throws SizeError { return BytesArrayTools.readInt(datagram, PAYLOAD_SIZE_POSITION); } + + /** RRCode getter. + * @return Request/Response code + */ + public RequestResponseCode getRequestResponseCode() { + return requestResponseCode; + } } diff --git a/src/protocolP2P/ProtocolP2PDatagram.java b/src/protocolP2P/ProtocolP2PDatagram.java index 730eac3..caa8874 100644 --- a/src/protocolP2P/ProtocolP2PDatagram.java +++ b/src/protocolP2P/ProtocolP2PDatagram.java @@ -1,11 +1,19 @@ package protocolP2P; +import exception.InternalError; import exception.ProtocolError; -import exception.VersionError; import exception.SizeError; -import exception.InternalError; import exception.TransmissionError; +import exception.VersionError; +import remoteException.EmptyDirectory; +import remoteException.InternalRemoteError; +import remoteException.NotFound; +import remoteException.ProtocolRemoteError; +import remoteException.VersionRemoteError; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; +import protocolP2P.LoadRequest; +import protocolP2P.FileList; +import protocolP2P.FilePart; import java.util.ArrayList; import java.lang.Byte; import java.net.DatagramPacket; @@ -25,6 +33,8 @@ public class ProtocolP2PDatagram { private final static int VERSION_POSITON = 0; private byte version; private Payload payload; + private InetAddress hostR; + private int portR; /** Constructor with payload parameter (typically used when sending datagram). * @param payload the payload associated with the datagram to send @@ -53,32 +63,86 @@ public class ProtocolP2PDatagram { /** Send datagram on socket (from server, as a response) * @param socket DatagramSocket used to send datagram. + * @param received datagram to respond (aka request) * @throws InternalError * @throws IOException */ - public void send(DatagramSocket socket) throws InternalError, IOException { + public void send(DatagramSocket socket, ProtocolP2PDatagram received) throws InternalError, IOException { + assert received.getPortR() != 0 && received.getHostR() != null : "This method should be used only as response to a request"; + if (received.getPortR() == 0 || received.getHostR() == null) { + throw new InternalError(); + } byte[] datagram = toDatagram(); // generate DatagramPacket - DatagramPacket datagramPacket = new DatagramPacket(datagram, datagram.length); + DatagramPacket datagramPacket = new DatagramPacket(datagram, datagram.length, received.getHostR(), received.getPortR()); socket.send(datagramPacket); } + protected void sendResponse(DatagramSocket socket, InetAddress host, int port) throws InternalError, IOException { + assert port != 0 && host != null : "This method should be used only as response to a request"; + if (port == 0 || host == null) { + throw new InternalError(); + } + byte[] datagram = toDatagram(); + DatagramPacket datagramPacket = new DatagramPacket(datagram, datagram.length, host, port); + socket.send(datagramPacket); + } + /** Receive datagram on socket * @param socket DatagramSocket used to receive datagram - * @return payload of the datagram * @throws TransmissionError * @throws ProtocolError * @throws VersionError * @throws InternalError * @throws SizeError + * @throws ProtocolRemoteError + * @throws VersionRemoteError + * @throws InternalRemoteError + * @throws EmptyDirectory + * @throws NotFound + * @throws IOException */ - public static Payload receive(DatagramSocket socket) throws TransmissionError, ProtocolError, VersionError, InternalError, SizeError { + public static ProtocolP2PDatagram receive(DatagramSocket socket) throws NotFound, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException { // reception byte[] datagram = new byte[4096]; DatagramPacket reception = new DatagramPacket(datagram, datagram.length); + socket.receive(reception); // contruction - ProtocolP2PDatagram p = new ProtocolP2PDatagram(datagram); - return p.getPayload(); + try { + ProtocolP2PDatagram p = new ProtocolP2PDatagram(datagram); + p.setHostR(reception.getAddress()); + p.setPortR(reception.getPort()); + Payload payload = p.getPayload(); + switch (payload.getRequestResponseCode()) { + case PROTOCOL_ERROR : + throw new ProtocolRemoteError(); + case VERSION_ERROR : + throw new VersionRemoteError(); + case INTERNAL_ERROR : + throw new InternalRemoteError(); + case EMPTY_DIRECTORY : + throw new EmptyDirectory(); + case NOT_FOUND : + throw new NotFound(); + default : + return p; + } + } catch (TransmissionError e) { + (new ProtocolP2PDatagram(new Payload(RequestResponseCode.INTERNAL_ERROR))).sendResponse(socket, reception.getAddress(), reception.getPort()); + throw e; + } catch (ProtocolError e) { + (new ProtocolP2PDatagram(new Payload(RequestResponseCode.PROTOCOL_ERROR))).sendResponse(socket, reception.getAddress(), reception.getPort()); + throw e; + } catch (VersionError e) { + (new ProtocolP2PDatagram(new Payload(RequestResponseCode.VERSION_ERROR))).sendResponse(socket, reception.getAddress(), reception.getPort()); + throw e; + } catch (InternalError e) { + (new ProtocolP2PDatagram(new Payload(RequestResponseCode.INTERNAL_ERROR))).sendResponse(socket, reception.getAddress(), reception.getPort()); + throw e; + } catch (SizeError e) { + (new ProtocolP2PDatagram(new Payload(RequestResponseCode.INTERNAL_ERROR))).sendResponse(socket, reception.getAddress(), reception.getPort()); + throw e; + } } /** Private constructor with datagram as byte[] parameter (typically used when receiving datagram). * @param datagram the full datagram received @@ -100,6 +164,9 @@ public class ProtocolP2PDatagram { case LOAD_RESPONSE: payload = (Payload) new FilePart(datagram); break; + case LOAD_REQUEST: + payload = (Payload) new LoadRequest(datagram); + break; default: payload = new Payload(datagram); // this can throw TransmissionError break; @@ -111,7 +178,7 @@ public class ProtocolP2PDatagram { * @return the full datagram to send * @throws InternalError */ - private byte[] toDatagram() throws InternalError { + protected byte[] toDatagram() throws InternalError { byte[] datagram = payload.toDatagram(); datagram[VERSION_POSITON] = version; return datagram; @@ -121,7 +188,7 @@ public class ProtocolP2PDatagram { /** Returns Payload associated with the datagram. * @return payload associated with the datagram */ - private Payload getPayload() { + public Payload getPayload() { return payload; } @@ -133,5 +200,33 @@ public class ProtocolP2PDatagram { throw new VersionError(); } } + + /** portR getter. + * @return portR + */ + protected int getPortR() { + return portR; + } + + /** portR setter. + * @param portR portR + */ + protected void setPortR(int portR) { + this.portR = portR; + } + + /** hostR getter. + * @return hostR + */ + protected InetAddress getHostR() { + return hostR; + } + + /** hostH setter. + * @param hostR hostR + */ + protected void setHostR(InetAddress hostR) { + this.hostR = hostR; + } } diff --git a/src/protocolP2P/RequestResponseCode.java b/src/protocolP2P/RequestResponseCode.java index 81f3cad..e030bc1 100644 --- a/src/protocolP2P/RequestResponseCode.java +++ b/src/protocolP2P/RequestResponseCode.java @@ -50,7 +50,7 @@ public enum RequestResponseCode { * @param code value of the element in datagram * @return enum element */ - public static RequestResponseCode fromCode(byte code) throws ProtocolError { + protected static RequestResponseCode fromCode(byte code) throws ProtocolError { RequestResponseCode r= BY_CODE.get(Byte.valueOf(code)); if (r == null) { throw new ProtocolError(); diff --git a/src/remoteException/EmptyDirectory.java b/src/remoteException/EmptyDirectory.java new file mode 100644 index 0000000..39146fd --- /dev/null +++ b/src/remoteException/EmptyDirectory.java @@ -0,0 +1,4 @@ +package remoteException; +public class EmptyDirectory extends Exception { + private static final long serialVersionUID = 11L; +} diff --git a/src/remoteException/InternalRemoteError.java b/src/remoteException/InternalRemoteError.java new file mode 100644 index 0000000..9ca91ea --- /dev/null +++ b/src/remoteException/InternalRemoteError.java @@ -0,0 +1,4 @@ +package remoteException; +public class InternalRemoteError extends Exception { + private static final long serialVersionUID = 11L; +} diff --git a/src/exception/NotFound.java b/src/remoteException/NotFound.java similarity index 79% rename from src/exception/NotFound.java rename to src/remoteException/NotFound.java index 7018d43..40c9507 100644 --- a/src/exception/NotFound.java +++ b/src/remoteException/NotFound.java @@ -1,4 +1,4 @@ -package exception; +package remoteException; public class NotFound extends Exception { private static final long serialVersionUID = 11L; } diff --git a/src/remoteException/ProtocolRemoteError.java b/src/remoteException/ProtocolRemoteError.java new file mode 100644 index 0000000..6199f8f --- /dev/null +++ b/src/remoteException/ProtocolRemoteError.java @@ -0,0 +1,4 @@ +package remoteException; +public class ProtocolRemoteError extends Exception { + private static final long serialVersionUID = 11L; +} diff --git a/src/remoteException/VersionRemoteError.java b/src/remoteException/VersionRemoteError.java new file mode 100644 index 0000000..4c372c9 --- /dev/null +++ b/src/remoteException/VersionRemoteError.java @@ -0,0 +1,4 @@ +package remoteException; +public class VersionRemoteError extends Exception { + private static final long serialVersionUID = 11L; +} diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index 7f09964..b345b5b 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -2,13 +2,26 @@ package serverP2P; import java.util.Vector; import java.io.File; import java.io.IOException; -import java.net.DatagramPacket; import java.net.DatagramSocket; -import exception.ProtocolError; -import exception.NotFound; -import exception.InternalError; +import java.net.SocketException; import java.nio.file.Paths; import java.nio.file.Files; +import protocolP2P.ProtocolP2PDatagram; +import protocolP2P.RequestResponseCode; +import protocolP2P.Payload; +import protocolP2P.LoadRequest; +import protocolP2P.FileList; +import protocolP2P.FilePart; +import exception.InternalError; +import exception.ProtocolError; +import exception.SizeError; +import exception.TransmissionError; +import exception.VersionError; +import remoteException.EmptyDirectory; +import remoteException.InternalRemoteError; +import remoteException.NotFound; +import remoteException.ProtocolRemoteError; +import remoteException.VersionRemoteError; /** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP. @@ -19,10 +32,10 @@ import java.nio.file.Files; */ public class ServerManagementUDP implements Runnable { - private Vector fileList; + private String[] fileList; private String baseDirectory; private int UDPPort; - private final String protocolID = "P2P-JAVA-PROJECT VERSION 1.0"; + private DatagramSocket socket; /** Constructor for UDP implementation, with baseDirectory and UDPPort parameters. * @param baseDirectory the root directory where files are stored @@ -31,148 +44,106 @@ public class ServerManagementUDP implements Runnable { 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(buffer, buffer.length); - // wait and receive datagram - socket.receive(dgram); - // extract data - String str = new String(dgram.getData(), 0, dgram.getLength()); - // process request - str = processRequest(str); - dgram.setData(str.getBytes()); - dgram.setLength(str.getBytes().length); - // send response - socket.send(dgram); - } - } catch (Exception e) { - // TODO: treat exceptions + socket = new DatagramSocket(UDPPort); + } catch (SocketException e) { + System.err.println("Error: cannot listen on port " + UDPPort); + System.exit(-1); } } - /** Process the request received. - * @param request the request received - * @return data to be send as response + /** Implementation of runnable. This methods allows to run the server. */ - String processRequest(String request) { - String res = protocolID + "\n"; - String formattedRequest[] = request.split("\n"); - try { + public void run() { + while(true) { try { - checkProtocolID(formattedRequest[0]); - switch (formattedRequest[1]) { - case "LIST": - System.out.println("List request"); - res += sendFileList(); + ProtocolP2PDatagram pd = ProtocolP2PDatagram.receive(socket); + Payload p = pd.getPayload(); + switch (p.getRequestResponseCode()) { + case LOAD_REQUEST: + System.out.println("Received LOAD_REQUEST"); + assert p instanceof LoadRequest : "payload must be an instance of LoadRequest"; + if (!(p instanceof LoadRequest)) { + sendInternalError(pd); + } else { + String filename = ((LoadRequest)p).getFilename(); + try { + byte[] load = Files.readAllBytes(Paths.get(baseDirectory + filename)); + try { + (new ProtocolP2PDatagram((Payload)(new FilePart(filename, load.length, 0, load)))).send(socket, pd); + } catch (Exception e2) { + System.err.println(e2); + } + } catch (IOException e) { + try { + (new ProtocolP2PDatagram(new Payload(RequestResponseCode.NOT_FOUND))).send(socket, pd); + } catch (Exception e2) { + System.err.println(e2); + } + } + } break; - case "DOWNLOAD": - System.out.println("Download request: " + formattedRequest[2]); - res += upload(formattedRequest[2]); + case LIST_REQUEST: + System.out.println("Received LIST_REQUEST"); + try { + if (fileList.length == 0) { + System.err.println("Sending EMPTY_DIRECTORY"); + (new ProtocolP2PDatagram(new Payload(RequestResponseCode.EMPTY_DIRECTORY))).send(socket, pd); + } else { + System.out.println("Sending LIST_RESPONSE"); + (new ProtocolP2PDatagram((Payload)(new FileList(fileList)))).send(socket, pd); + } + } catch (Exception e2) { + System.err.println(e2); + } break; default: - throw new ProtocolError(); + sendInternalError(pd); } - } catch (java.lang.ArrayIndexOutOfBoundsException e) { - throw new ProtocolError(); + } catch (NotFound e) { + } catch (EmptyDirectory e) { + } catch (InternalRemoteError e) { + } catch (VersionRemoteError e) { + } catch (ProtocolRemoteError e) { + } catch (IOException e) { + } catch (TransmissionError e) { + } catch (ProtocolError e) { + } catch (VersionError e) { + } catch (InternalError e) { + } catch (SizeError e) { } - } catch (ProtocolError e) { - // wrong version or wrong implementation - res += sendProtocolError(); - } catch (InternalError e) { - res += sendInternalError(); - } catch (NotFound e) { - res += sendNotFound(); } - return res; } /** Initialize local list of all files allowed to be shared. */ private void initFileList() { File folder = new File(baseDirectory); + Vector v = new Vector(); File[] files = folder.listFiles(); /* Add non-recursively files's names to fileList */ for (File f : files) { if (f.isFile()) { - fileList.add(f.getName()); + v.add(f.getName()); } } + fileList = new String[v.size()]; + v.toArray(fileList); } - /** 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.equals(msgProtocolID)) { - throw new 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 + + /** Send an internal error message. + * @param pd ProtocolP2PDatagram to respond */ - private String upload(String filename) throws NotFound, InternalError { - File file = new File(baseDirectory + filename); - System.out.println("Uploading `" + baseDirectory + filename + "`"); - if (!file.exists() || !file.isFile()) { - throw new NotFound(); - } - String res = "LOAD\n" + file.length() + "\n"; + private void sendInternalError(ProtocolP2PDatagram pd) { try { - res += new String(Files.readAllBytes(Paths.get(baseDirectory + filename))); - } catch (IOException e) { - throw new 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"); + (new ProtocolP2PDatagram(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(socket, pd); + } catch (Exception e) { + System.err.println(e); } - 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"; } } + diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 00be061..b22c715 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -8,7 +8,7 @@ public class ServerP2P { public ServerP2P() { directories = new Directories("P2P_JAVA_PROJECT_SERVER"); - port = 40000; + port = 40001; System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory()); directories.askOpenDataHomeDirectory(); } From 648cb12953b2519c069b19d6b30c02a72cc51db2 Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 25 Jan 2020 20:04:01 +0100 Subject: [PATCH 46/48] =?UTF-8?q?Test=20&=20debug=20Transfert=20de=20fichi?= =?UTF-8?q?er=20r=C3=A9alis=C3=A9=20!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/clientP2P/ClientManagementUDP.java | 1 + src/protocolP2P/FilePart.java | 2 +- src/protocolP2P/ProtocolP2PDatagram.java | 7 +++++++ src/tools/BytesArrayTools.java | 8 ++++---- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 3b8a811..445bbea 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -125,6 +125,7 @@ public class ClientManagementUDP implements Runnable { throw new ProtocolError(); } if (fp.getOffset() != 0 || fp.getPartialContent().length != fp.getTotalSize()) { + System.err.println("offset: " + fp.getOffset() + " ; content.length: " + fp.getPartialContent().length + " ; totalSize: " + fp.getTotalSize()); System.err.println("Error: cannot handle partial files (not implemented)"); throw new InternalError(); } diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 32deb5f..b2d539a 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -163,7 +163,7 @@ public class FilePart extends Payload { int start = 28 + getFilenameSize(datagram); // this can throw SizeError or ProtocolError int end = 8 + getPayloadSize(datagram); // this can throw SizeError try { - partialContent = Arrays.copyOfRange(datagram, start, end+1); + partialContent = Arrays.copyOfRange(datagram, start, end); } catch (ArrayIndexOutOfBoundsException e) { throw new ProtocolError(); } diff --git a/src/protocolP2P/ProtocolP2PDatagram.java b/src/protocolP2P/ProtocolP2PDatagram.java index caa8874..744c5e8 100644 --- a/src/protocolP2P/ProtocolP2PDatagram.java +++ b/src/protocolP2P/ProtocolP2PDatagram.java @@ -78,6 +78,13 @@ public class ProtocolP2PDatagram { socket.send(datagramPacket); } + /** Send a response. + * @param socket DatagramSocket used to send response + * @param host host to send response + * @param port port to send response + * @throws InternalError + * @throws IOException + */ protected void sendResponse(DatagramSocket socket, InetAddress host, int port) throws InternalError, IOException { assert port != 0 && host != null : "This method should be used only as response to a request"; if (port == 0 || host == null) { diff --git a/src/tools/BytesArrayTools.java b/src/tools/BytesArrayTools.java index 9547f64..3813349 100644 --- a/src/tools/BytesArrayTools.java +++ b/src/tools/BytesArrayTools.java @@ -23,8 +23,8 @@ public class BytesArrayTools { * @param value long to write */ public static void write(byte[] array, int start, long value) { - for(int i=0;i<4;i++) { - array[start + i] = (byte) ((value >> (8 * (4 - i))) & 0xFF); + for(int i=0;i<8;i++) { + array[start + i] = (byte) ((value >> (8 * (7 - i))) & 0xFF); } } @@ -36,7 +36,7 @@ public class BytesArrayTools { public static int readInt(byte[] array, int start) throws SizeError { int size = 0; for(int i=0;i<4;i++) { - size |= ((int)array[start + i]) << (8* i); + size |= ((int)array[start + i]) << (8* (3 -i)); } if (size < 0) { // Size in array is probably correct @@ -53,7 +53,7 @@ public class BytesArrayTools { public static long readLong(byte[] array, int start) throws SizeError { long size = 0; for(int i=0;i<8;i++) { - size |= ((int)array[start + i]) << (8* i); + size |= ((int)array[start + i]) << (8* (7 - i)); } if (size < 0) { // Size in array is probably correct From d33d9bf6249f5610a3146f771220b7859a7366cc Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 25 Jan 2020 21:25:36 +0100 Subject: [PATCH 47/48] Add empty file error code --- doc/protocol.md | 1 + src/clientP2P/ClientManagementUDP.java | 10 ++++++++-- src/protocolP2P/FileList.java | 2 ++ src/protocolP2P/FilePart.java | 2 +- src/protocolP2P/ProtocolP2PDatagram.java | 6 +++++- src/protocolP2P/RequestResponseCode.java | 3 ++- src/remoteException/EmptyFile.java | 4 ++++ src/serverP2P/ServerManagementUDP.java | 19 +++++++++++++++---- 8 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 src/remoteException/EmptyFile.java diff --git a/doc/protocol.md b/doc/protocol.md index 7a9fb8a..d5b9171 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -38,6 +38,7 @@ x bytes: [64-xx: PAYLOAD] - `INTERNAL ERROR` (0xC2) - `EMPTY DIRECTORY` (0xC3) - `NOT FOUND` (0xC4) + - `EMPTY FILE` (0xC5) ### List Payload size for list request is always zero. diff --git a/src/clientP2P/ClientManagementUDP.java b/src/clientP2P/ClientManagementUDP.java index 445bbea..20f8eb8 100644 --- a/src/clientP2P/ClientManagementUDP.java +++ b/src/clientP2P/ClientManagementUDP.java @@ -4,6 +4,7 @@ import exception.ProtocolError; import exception.SizeError; import exception.TransmissionError; import exception.VersionError; +import remoteException.EmptyFile; import remoteException.EmptyDirectory; import remoteException.InternalRemoteError; import remoteException.NotFound; @@ -92,7 +93,9 @@ public class ClientManagementUDP implements Runnable { } catch (VersionRemoteError e) { System.err.println("Error: Server cannot decode this version of the protocol"); } catch (NotFound e) { - System.err.println("Error: Server have not this file in directory"); + System.err.println("Error: Server has not this file in directory"); + } catch (EmptyFile e) { + System.err.println("Error: File is empty"); } } @@ -109,8 +112,9 @@ public class ClientManagementUDP implements Runnable { * @throws InternalRemoteError * @throws ProtocolRemoteError * @throws VersionRemoteError + * @throws EmptyFile */ - private void download(String filename) throws NotFound, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { + private void download(String filename) throws EmptyFile, NotFound, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { ProtocolP2PDatagram d = new ProtocolP2PDatagram((Payload) new LoadRequest(filename)); d.send(socket, host, UDPPort); try { @@ -168,6 +172,8 @@ public class ClientManagementUDP implements Runnable { } } catch (NotFound e) { throw new ProtocolError(); + } catch (EmptyFile e) { + throw new ProtocolError(); } } } diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index 9f141c9..1dc0887 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -66,7 +66,9 @@ public class FileList extends Payload { int size = 8; for (String s : fileList) { size += s.length(); + size += 1; } + size -=1; byte[] datagram = new byte[size]; // java initialize all to zero // set request/response code datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index b2d539a..f8b35b3 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -32,7 +32,7 @@ public class FilePart extends Payload { super(RequestResponseCode.LOAD_RESPONSE); /* asserts to help debugging */ assert totalSize >= 0 : "totalSize cannot be negative"; - assert partialContent.length != 0 : "partialContent.length cannot be zero"; + assert partialContent.length != 0 : "partialContent.length cannot be zero, see RRCode.EMPTY_FILE"; assert totalSize >= partialContent.length : "totalSize must be greater than partialContent.length"; assert offset >= 0 : "offset cannot be negative"; assert filename != null : "filename is required"; diff --git a/src/protocolP2P/ProtocolP2PDatagram.java b/src/protocolP2P/ProtocolP2PDatagram.java index 744c5e8..1c31073 100644 --- a/src/protocolP2P/ProtocolP2PDatagram.java +++ b/src/protocolP2P/ProtocolP2PDatagram.java @@ -9,6 +9,7 @@ import remoteException.InternalRemoteError; import remoteException.NotFound; import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; +import remoteException.EmptyFile; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import protocolP2P.LoadRequest; @@ -108,8 +109,9 @@ public class ProtocolP2PDatagram { * @throws EmptyDirectory * @throws NotFound * @throws IOException + * @throws EmptyFile */ - public static ProtocolP2PDatagram receive(DatagramSocket socket) throws NotFound, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException { + public static ProtocolP2PDatagram receive(DatagramSocket socket) throws EmptyFile, NotFound, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException { // reception byte[] datagram = new byte[4096]; DatagramPacket reception = new DatagramPacket(datagram, datagram.length); @@ -131,6 +133,8 @@ public class ProtocolP2PDatagram { throw new EmptyDirectory(); case NOT_FOUND : throw new NotFound(); + case EMPTY_FILE: + throw new EmptyFile(); default : return p; } diff --git a/src/protocolP2P/RequestResponseCode.java b/src/protocolP2P/RequestResponseCode.java index e030bc1..8b4e20f 100644 --- a/src/protocolP2P/RequestResponseCode.java +++ b/src/protocolP2P/RequestResponseCode.java @@ -20,7 +20,8 @@ public enum RequestResponseCode { PROTOCOL_ERROR(CodeType.ERROR, (byte)0xC1), INTERNAL_ERROR(CodeType.ERROR, (byte)0xC2), EMPTY_DIRECTORY(CodeType.ERROR, (byte)0xC3), - NOT_FOUND(CodeType.ERROR, (byte)0xC4); + NOT_FOUND(CodeType.ERROR, (byte)0xC4), + EMPTY_FILE(CodeType.ERROR, (byte)0xC5); public final CodeType codeType; public final byte codeValue; diff --git a/src/remoteException/EmptyFile.java b/src/remoteException/EmptyFile.java new file mode 100644 index 0000000..a05cdd7 --- /dev/null +++ b/src/remoteException/EmptyFile.java @@ -0,0 +1,4 @@ +package remoteException; +public class EmptyFile extends Exception { + private static final long serialVersionUID = 11L; +} diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index b345b5b..b7e1cbb 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -22,6 +22,8 @@ import remoteException.InternalRemoteError; import remoteException.NotFound; import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; +import remoteException.EmptyFile; +import java.util.Arrays; /** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP. @@ -70,10 +72,18 @@ public class ServerManagementUDP implements Runnable { String filename = ((LoadRequest)p).getFilename(); try { byte[] load = Files.readAllBytes(Paths.get(baseDirectory + filename)); - try { - (new ProtocolP2PDatagram((Payload)(new FilePart(filename, load.length, 0, load)))).send(socket, pd); - } catch (Exception e2) { - System.err.println(e2); + if (Arrays.binarySearch(fileList, filename) >= 0) { + try { + if (load.length == 0) { + (new ProtocolP2PDatagram(new Payload(RequestResponseCode.EMPTY_FILE))).send(socket, pd); + } else { + (new ProtocolP2PDatagram((Payload)(new FilePart(filename, load.length, 0, load)))).send(socket, pd); + } + } catch (Exception e2) { + System.err.println(e2); + } + } else { + throw new IOException(); // to send a NOT_FOUND in the catch block } } catch (IOException e) { try { @@ -112,6 +122,7 @@ public class ServerManagementUDP implements Runnable { } catch (VersionError e) { } catch (InternalError e) { } catch (SizeError e) { + } catch (EmptyFile e) { } } } From fc444bcd81edac2ba3b1e4bd416622bb43e68b39 Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 25 Jan 2020 22:42:08 +0100 Subject: [PATCH 48/48] Update documentation --- doc/classdiagram.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/classdiagram.xml b/doc/classdiagram.xml index c99baab..8c652a6 100644 --- a/doc/classdiagram.xml +++ b/doc/classdiagram.xml @@ -1 +1 @@ -7Vxbd9o4EP41nNM+JMc3DDwGSNrupi2n6XVf9ihYAbXGYmWRQH/9SrZ8k2RjEhuSXechxxaSkDQz33wzHtOzJ6vtGwLWy/fYg37PMrxtz572LGvoDtl/3rCLG5zRKG5YEOTFTWbWcIN+Q9FoiNYN8mBY6Egx9ilaFxvnOAjgnBbaACH4odjtDvvFb12DBVQabubAV1u/IY8uxbasQdb+FqLFMvlm0xX7W4Gks9hJuAQefsg12Zc9e0IwpvHVajuBPj+75FzicVcln6YLIzCgdQbssL+4pN7804P/5+fL3e2vt+j6zBT7uAf+Ruz4A1uQZVzhTeCJhdNdchrhA1r5IGB34zsc0BvxicnugY8WAbues+VAwhruIaGIHeSF+IDiNWudL5HvXYMd3vBFhxTMfyV34yUm6DebFvhiT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