From 855e4ed73e8454ad7100c87d930b37bc040c53a6 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 22 Jan 2020 13:02:03 +0100 Subject: [PATCH] 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;