From c3b8c2c5e0bb7d123f95b0eda116b27526dc37fc Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 23 Jan 2020 11:37:07 +0100 Subject: [PATCH] Update protocol classes modelisation --- src/exception/SizeError.java | 3 ++ src/protocolP2P/FileList.java | 4 +-- src/protocolP2P/FilePart.java | 20 +++++++------- src/protocolP2P/Payload.java | 21 +++++++------- src/protocolP2P/ProtocolP2PDatagram.java | 35 +++++++++++++++++++++--- 5 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 src/exception/SizeError.java diff --git a/src/exception/SizeError.java b/src/exception/SizeError.java new file mode 100644 index 0000000..0af8f87 --- /dev/null +++ b/src/exception/SizeError.java @@ -0,0 +1,3 @@ +package exception; +/** Used on reception side when size as set in datagram is too big, and we cant store this in a int/long as usual. */ +public class SizeError extends Exception {} diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index 35b6088..f5b6c70 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -31,10 +31,10 @@ public class FileList extends Payload { /** Constructor (typically used by client) with a byte[] parameter containing the datagram received. * @param datagram the full datagram received - * @throws ProtocolError + * @throws SizeError * @throws InternalError */ - public FileList(byte[] datagram) throws ProtocolError, InternalError { + protected FileList(byte[] datagram) throws SizeError, 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"; diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 230da36..6a86560 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -39,19 +39,19 @@ public class FilePart extends Payload { /** Constructor (typically used by client) with datagram received as parameter. * @param datagram the full datagram received - * @throws ProtocolError + * @throws SizeError * @throws InternalError */ - public FilePart(byte[] datagram) throws ProtocolError, InternalError { + protected FilePart(byte[] datagram) throws SizeError, ProtocolError, InternalError { /* 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(); } - setOffset(datagram); // this can throw ProtocolError - setTotalFileSize(datagram); // this can throw ProtocolError - int size = getPayloadSize(datagram); // this can throw ProtocolError + setOffset(datagram); // this can throw SizeError + setTotalFileSize(datagram); // this can throw SizeError + int size = getPayloadSize(datagram); // this can throw SizeError int partialContentStart = setFilename(datagram, size) + 1; // this can throw ProtocolError setPartialContent(datagram, partialContentStart, size); } @@ -61,7 +61,7 @@ public class FilePart extends Payload { * ProtocolP2PDatagram will use this method to generate the complete datagram. * @return datagram with padding */ - public byte[] toDatagram() { + protected byte[] toDatagram() { //TODO : calculate payload size int size = ; byte[] datagram = new byte[size]; // java initialize all to zero @@ -76,16 +76,16 @@ public class FilePart extends Payload { //TODO : write partialContent to datagram } - private setOffset(byte[] datagram) throws ProtocolError { + private setOffset(byte[] datagram) throws SizeError { //TODO: copy from 8 to 15 into long and write into offset. // throw protocolerror if offset is < 0 } - private setTotalFileSize(byte[] datagram) throw ProtocolError { + private setTotalFileSize(byte[] datagram) throw SizeError { // TODO: convert from byte 16 to 23 (included) // into long and write into totalFileSize // throw protocolerror if offset is < 0 } - private int setFilename(byte[] datagram, size) throws ProtocolError { + private int setFilename(byte[] datagram, int payloadSize) throws ProtocolError { // TODO: copy datagram from byte 24 to the first \n (excluded) // into filename unless we excess size // (in this case the wrong size has been @@ -93,7 +93,7 @@ public class FilePart extends Payload { // return position of the \n } - private setPartialContent(byte[] datagram, int start, size) { + private setPartialContent(byte[] datagram, int start, int payloadSize) { // TODO: copy datagram from start to size into partialContent } diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index 6a8fd51..c38acf1 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -33,7 +33,7 @@ public class Payload { * @throws ProtocolError * @throws InternalError */ - public Payload(byte[] datagram) throws ProtocolError, InternalError { + protected 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"; @@ -56,7 +56,7 @@ public class Payload { * ProtocolP2PDatagram will use this method to generate the complete datagram. * @return datagram with padding */ - public byte[] toDatagram() { + protected byte[] toDatagram() { byte [] datagram = new byte[8]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code @@ -68,34 +68,35 @@ public class Payload { /** Set payload’s size in a datagram. * @param size integer representing payload size * @param datagram datagram to be completed - * @return datagram with payload’s size set * @throws InternalError */ - protected static byte[] setPayloadSize(int size, byte[] datagram) throws InternalError { + protected static setPayloadSize(int size, byte[] datagram) throws InternalError { /* assert to help debugging */ - assert size >= 0: "Payload size cannont be negative"; + assert size >= 0: "Payload size cannot be negative"; if (size < 0) { + // We don't throw SizeError + // because this is only for reception side throw new InternalError(); } for(int i=0;i<4;i++) { datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) ((size >> (8 * (3 - i))) & 0xFF); - } } - return datagram; } /** Get payload’s size from a datagram. * @param datagram the full datagram received * @return integer representing payload size - * @throws ProtocolError + * @throws SizeError */ - protected static int getPayloadSize(byte[] datagram) throws ProtocolError { + protected static int getPayloadSize(byte[] datagram) throws SizeError { int size = 0; for(int i=0;i<4;i++) { size |= ((int)datagram[PAYLOAD_SIZE_POSITON + i]) << (8* i); } if (size < 0) { - throw new ProtocolError(); + // Size in datagram is probably correct + // but we cannot store it into a int (or this will be negative) + throw new SizeError(); } return size; } diff --git a/src/protocolP2P/ProtocolP2PDatagram.java b/src/protocolP2P/ProtocolP2PDatagram.java index 31ae53f..1846258 100644 --- a/src/protocolP2P/ProtocolP2PDatagram.java +++ b/src/protocolP2P/ProtocolP2PDatagram.java @@ -5,6 +5,8 @@ import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import java.util.ArrayList; import java.lang.Byte; +import java.net.DatagramPacket; +import java.net.DatagramSocket; /** Representation of datagram. * @author Louis Royer @@ -26,12 +28,37 @@ public class ProtocolP2PDatagram { this.payload = payload; } - /** Constructor with datagram as byte[] parameter (typically used when receiving datagram). + /** Send datagram on socket + * @param socket DatagramSocket used to send datagram + */ + public void send(DatagramSocket socket) { + //TODO + byte[] datagram = toDatagram(); + // generate DatagramPacket + // send it + } + + /** Receive datagram on socket + * @param socket DatagramSocket used to receive datagram + * @return payload of the datagram + * @throws ProtocolError + * @throws VersionError + */ + public static Payload receive(DatagramSocket socket) throws ProtocolError, VersionError { + //TODO + // reception + //datagram = + // contruction + ProtocolP2PDatagram p = new ProtocolP2PDatagram(datagram); + return p.getPayload(); + + } + /** Private constructor with datagram as byte[] parameter (typically used when receiving datagram). * @param datagram the full datagram received * @throws ProtocolError * @throws VersionError */ - public ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError { + private ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError, SizeError { // unwrap version version = datagram[VERSION_POSITON]; checkProtocolVersion(); // this can throw VersionError @@ -53,7 +80,7 @@ public class ProtocolP2PDatagram { * This datagram is still complete and ready to be send. * @return the full datagram to send */ - public byte[] toDatagram() { + private byte[] toDatagram() { byte[] datagram = payload.toDatagram(); datagram[VERSION_POSITON] = version; return datagram; @@ -62,7 +89,7 @@ public class ProtocolP2PDatagram { /** Returns Payload associated with the datagram. * @return payload associated with the datagram */ - public Payload getPayload() { + private Payload getPayload() { return payload; }