From f3d47433e17bb004db47b5db146f9af62d4bfa59 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 10:55:20 +0100 Subject: [PATCH] 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(); }