From d33d9bf6249f5610a3146f771220b7859a7366cc Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 25 Jan 2020 21:25:36 +0100 Subject: [PATCH] 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) { } } }