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 90c81e9..0000000 Binary files a/src/exception/InternalError.class and /dev/null differ diff --git a/src/exception/NotFound.class b/src/exception/NotFound.class deleted file mode 100644 index 6e896f4..0000000 Binary files a/src/exception/NotFound.class and /dev/null differ diff --git a/src/exception/ProtocolError.class b/src/exception/ProtocolError.class deleted file mode 100644 index ab341e7..0000000 Binary files a/src/exception/ProtocolError.class and /dev/null differ diff --git a/src/exception/TransmissionError.class b/src/exception/TransmissionError.class deleted file mode 100644 index ec43b93..0000000 Binary files a/src/exception/TransmissionError.class and /dev/null differ 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;