Update RequestResponse codes
This commit is contained in:
parent
026008473a
commit
855e4ed73e
@ -31,26 +31,26 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p
|
|||||||
- RESPONSES (msb is 1):
|
- RESPONSES (msb is 1):
|
||||||
- `LIST` (0x80)
|
- `LIST` (0x80)
|
||||||
- `LOAD` (0x81)
|
- `LOAD` (0x81)
|
||||||
- `NOT FOUND` (0x82)
|
- `VERSION ERROR` (0xC0)
|
||||||
- `PROTOCOL ERROR` (0x83)
|
- `PROTOCOL ERROR` (0xC1)
|
||||||
- `INTERNAL ERROR` (0x84)
|
- `INTERNAL ERROR` (0xC2)
|
||||||
|
- `EMPTY DIRECTORY` (0xC3)
|
||||||
|
- `NOT FOUND` (0xC4)
|
||||||
|
|
||||||
### List
|
### List
|
||||||
Payload size for list request is always zero.
|
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
|
### Load
|
||||||
#### Not found
|
#### Not found
|
||||||
Response when the file requested is not found on the server.
|
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.
|
|
||||||
Payload size for Protocol error is zero
|
|
||||||
|
|
||||||
#### Internal error
|
|
||||||
Response in internal failure case.
|
|
||||||
Payload size for Internal error is zero.
|
|
||||||
|
|
||||||
#### Load response
|
#### Load response
|
||||||
Payload contains
|
Payload contains
|
||||||
@ -64,3 +64,15 @@ Payload contains
|
|||||||
|
|
||||||
#### Load request
|
#### Load request
|
||||||
Payload contains only the name of the file to load.
|
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.
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2
src/exception/VersionError.java
Normal file
2
src/exception/VersionError.java
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
package exception;
|
||||||
|
public class VersionError extends Exceptions {}
|
@ -1,5 +1,6 @@
|
|||||||
package protocolP2P;
|
package protocolP2P;
|
||||||
public enum CodeType {
|
public enum CodeType {
|
||||||
REQUEST,
|
REQUEST,
|
||||||
RESPONSE
|
RESPONSE,
|
||||||
|
ERROR
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ import java.util.ArrayList;
|
|||||||
import protocolP2P.Payload;
|
import protocolP2P.Payload;
|
||||||
import protocolP2P.RequestResponseCode;
|
import protocolP2P.RequestResponseCode;
|
||||||
import exception.ProtocolError;
|
import exception.ProtocolError;
|
||||||
|
import exception.InternalError;
|
||||||
|
|
||||||
public class FileList extends Payload {
|
public class FileList extends Payload {
|
||||||
private final static RequestResponseCode requestResponseCode = RequestResponseCode.LIST_RESPONSE;
|
private final static RequestResponseCode requestResponseCode = RequestResponseCode.LIST_RESPONSE;
|
||||||
@ -12,9 +13,14 @@ public class FileList extends Payload {
|
|||||||
this.content = content;
|
this.content = content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileList(byte[] datagram) throws ProtocolError {
|
public FileList(byte[] datagram) throws ProtocolError, InternalError {
|
||||||
//TODO
|
//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";
|
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];
|
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];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package protocolP2P;
|
|||||||
import protocolP2P.Payload;
|
import protocolP2P.Payload;
|
||||||
import protocolP2P.RequestResponseCode;
|
import protocolP2P.RequestResponseCode;
|
||||||
import exception.ProtocolError;
|
import exception.ProtocolError;
|
||||||
|
import exception.InternalError;
|
||||||
|
|
||||||
public class FilePart extends Payload {
|
public class FilePart extends Payload {
|
||||||
private static final RequestResponseCode requestResponseCode = RequestResponseCode.LOAD_RESPONSE;
|
private static final RequestResponseCode requestResponseCode = RequestResponseCode.LOAD_RESPONSE;
|
||||||
@ -17,9 +18,14 @@ public class FilePart extends Payload {
|
|||||||
this.partialContent = partialContent;
|
this.partialContent = partialContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FilePart(byte[] datagram) throws ProtocolError {
|
public FilePart(byte[] datagram) throws ProtocolError, InternalError {
|
||||||
//TODO
|
//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";
|
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];
|
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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ import protocolP2P.RequestResponseCode;
|
|||||||
import protocolP2P.FilePart;
|
import protocolP2P.FilePart;
|
||||||
import protocolP2P.FileList;
|
import protocolP2P.FileList;
|
||||||
import exception.ProtocolError;
|
import exception.ProtocolError;
|
||||||
|
import exception.InternalError;
|
||||||
|
|
||||||
public class Payload {
|
public class Payload {
|
||||||
private RequestResponseCode requestResponseCode;
|
private RequestResponseCode requestResponseCode;
|
||||||
@ -10,16 +11,27 @@ public class Payload {
|
|||||||
protected final static int PAYLOAD_START_POSITION = 8;
|
protected final static int PAYLOAD_START_POSITION = 8;
|
||||||
|
|
||||||
/** To create request/response with a payload size of zero */
|
/** 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.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class";
|
||||||
assert requestResponseCode != requestResponseCode.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class";
|
assert requestResponseCode != requestResponseCode.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class";
|
||||||
this.requestResponseCode = requestResponseCode;
|
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.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.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class";
|
||||||
requestResponseCode = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]);
|
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 */
|
/** Payload with padding */
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package protocolP2P;
|
package protocolP2P;
|
||||||
import exception.ProtocolError;
|
import exception.ProtocolError;
|
||||||
|
import exception.VersionError;
|
||||||
import protocolP2P.Payload;
|
import protocolP2P.Payload;
|
||||||
import protocolP2P.RequestResponseCode;
|
import protocolP2P.RequestResponseCode;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -16,10 +17,10 @@ public class ProtocolP2PDatagram {
|
|||||||
this.payload = payload;
|
this.payload = payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProtocolP2PDatagram(byte[] datagram) throws ProtocolError {
|
public ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError {
|
||||||
// unwrap version
|
// unwrap version
|
||||||
version = datagram[VERSION_POSITON];
|
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
|
RequestResponseCode r = RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]); // this can throw ProtocolError
|
||||||
switch (r) {
|
switch (r) {
|
||||||
case RequestResponseCode.LIST_RESPONSE:
|
case RequestResponseCode.LIST_RESPONSE:
|
||||||
@ -44,9 +45,9 @@ public class ProtocolP2PDatagram {
|
|||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkProtocolVersion() throws ProtocolError {
|
private void checkProtocolVersion() throws VersionError {
|
||||||
if (PROTOCOL_VERSION != version) {
|
if (PROTOCOL_VERSION != version) {
|
||||||
throw new ProtocolError();
|
throw new VersionError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,11 @@ public enum RequestResponseCode {
|
|||||||
LOAD_REQUEST(CodeType.REQUEST, (byte)0x01),
|
LOAD_REQUEST(CodeType.REQUEST, (byte)0x01),
|
||||||
LIST_RESPONSE(CodeType.RESPONSE, (byte)0x80),
|
LIST_RESPONSE(CodeType.RESPONSE, (byte)0x80),
|
||||||
LOAD_RESPONSE(CodeType.RESPONSE, (byte)0x81),
|
LOAD_RESPONSE(CodeType.RESPONSE, (byte)0x81),
|
||||||
NOT_FOUND(CodeType.RESPONSE, (byte)0x82),
|
VERSION_ERROR(CodeType.ERROR, (byte)0xC0),
|
||||||
PROTOCOL_ERROR(CodeType.RESPONSE, (byte)0x83),
|
PROTOCOL_ERROR(CodeType.ERROR, (byte)0xC1),
|
||||||
INTERNAL_ERROR(CodeType.RESPONSE, (byte)0x84);
|
INTERNAL_ERROR(CodeType.ERROR, (byte)0xC2),
|
||||||
|
EMPTY_DIRECTORY(CodeType.ERROR, (byte)0xC3),
|
||||||
|
NOT_FOUND(CodeType.ERROR, (byte)0xC4);
|
||||||
|
|
||||||
public final CodeType codeType;
|
public final CodeType codeType;
|
||||||
public final byte codeValue;
|
public final byte codeValue;
|
||||||
|
Loading…
Reference in New Issue
Block a user