From 5eb7e0deabd04839f6486338beb741038e77d9f1 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 23 Jan 2020 12:26:44 +0100 Subject: [PATCH] Refactoring + implementation of some methods --- doc/protocol.md | 18 +++++----- src/protocolP2P/FileList.java | 3 +- src/protocolP2P/FilePart.java | 20 +++++++---- src/protocolP2P/Payload.java | 17 +++------ src/tools/ByteArrayTools.java | 66 +++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 29 deletions(-) create mode 100644 src/tools/ByteArrayTools.java diff --git a/doc/protocol.md b/doc/protocol.md index 227e3b8..0ea88aa 100644 --- a/doc/protocol.md +++ b/doc/protocol.md @@ -15,11 +15,11 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p ```Datagram format -[0-7: VERSION(0x11, first quartet is major version, second is minor)] -[8-15: REQUEST/RESPONSE CODE] -[16-31: RESERVED FOR FUTURE USE] -[32-63: PAYLOAD SIZE IN BYTES] -[64-xx: PAYLOAD] +1 byte: [0-7: VERSION(0x11, first quartet is major version, second is minor)] +1 byte: [8-15: REQUEST/RESPONSE CODE] +2 bytes: [16-31: RESERVED FOR FUTURE USE] +4 bytes: [32-63: PAYLOAD SIZE IN BYTES] +x bytes: [64-xx: PAYLOAD] ``` @@ -56,10 +56,10 @@ Payload size for Not found is zero. Payload contains ``` -[64-127: OFFSET OF FILE CONTENT IN BYTES] -[128-191: TOTAL FILESIZE] -[\n] -[FILE CONTENT] +8 bytes: [64-127: OFFSET OF FILE CONTENT IN BYTES] +8 bytes: [128-191: TOTAL FILESIZE] +y bytes: [\n] +z bytes: [FILE CONTENT] ``` #### Load request diff --git a/src/protocolP2P/FileList.java b/src/protocolP2P/FileList.java index f5b6c70..4372e9f 100644 --- a/src/protocolP2P/FileList.java +++ b/src/protocolP2P/FileList.java @@ -4,6 +4,7 @@ import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import exception.ProtocolError; import exception.InternalError; +import exception.SizeError; /** Representation of payload for list response. * @author Louis Royer @@ -52,7 +53,7 @@ public class FileList extends Payload { */ protected byte[] toDatagram() { //TODO compute size - int size = ; + int size = 8 + ; byte[] datagram = new byte[size]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 6a86560..c63801c 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -3,6 +3,8 @@ import protocolP2P.Payload; import protocolP2P.RequestResponseCode; import exception.ProtocolError; import exception.InternalError; +import exception.SizeError; +import tools.BytesArrayTools; /** Representation of payload for load response. * @author Louis Royer @@ -63,7 +65,7 @@ public class FilePart extends Payload { */ protected byte[] toDatagram() { //TODO : calculate payload size - int size = ; + int size = 24 + filename.length + 1; byte[] datagram = new byte[size]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code @@ -76,15 +78,21 @@ public class FilePart extends Payload { //TODO : write partialContent to datagram } + /** Write from bytes 8 to 15 of datagram into offset. + * @param datagram received datagram + * @throws SizeError + */ private setOffset(byte[] datagram) throws SizeError { - //TODO: copy from 8 to 15 into long and write into offset. - // throw protocolerror if offset is < 0 + offset = BytesArrayTools.readLong(datagram, 8); } + /** Write from bytes 16 to 23 of datagram into totalSize. + * @param datagram received datagram + * @throws SizeError + */ 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 + totalSize = BytesArrayTools.readLong(datagram, 16); } + 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 diff --git a/src/protocolP2P/Payload.java b/src/protocolP2P/Payload.java index c38acf1..44a1458 100644 --- a/src/protocolP2P/Payload.java +++ b/src/protocolP2P/Payload.java @@ -4,6 +4,8 @@ import protocolP2P.FilePart; import protocolP2P.FileList; import exception.ProtocolError; import exception.InternalError; +import exception.SizeError; +import tools.BytesArrayTools; /** Representation of payload. If payload has a size, use subclasses instead. * @author Louis Royer * @author Flavien Haas @@ -78,9 +80,7 @@ public class Payload { // 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); - } + BytesArrayTools.write(datagram, PAYLOAD_SIZE_POSITON, size); } /** Get payload’s size from a datagram. @@ -89,15 +89,6 @@ public class Payload { * @throws SizeError */ 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) { - // Size in datagram is probably correct - // but we cannot store it into a int (or this will be negative) - throw new SizeError(); - } - return size; + return BytesArrayTools.readInt(datagram, PAYLOAD_SIZE_POSITON]; } } diff --git a/src/tools/ByteArrayTools.java b/src/tools/ByteArrayTools.java new file mode 100644 index 0000000..176d562 --- /dev/null +++ b/src/tools/ByteArrayTools.java @@ -0,0 +1,66 @@ +package tools; +import exception.SizeError; +/** Helper to manipulate byte[]. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ +public class BytesArrayTools { + /** Write int in a bytearray + * @param array the array to write + * @param start where to begin writting + * @param value int to write + */ + public static void write(byte[] array, int start, int value) { + for(int i=0;i<4;i++) { + array[start + i] = (byte) ((size >> (8 * (3 - i))) & 0xFF); + } + } + /** Write long in a bytearray + * @param array the array to write + * @param start where to begin writting + * @param value long to write + */ + public static void write(byte[] array, int start, long value) { + for(int i=0;i<4;i++) { + array[start + i] = (byte) ((size >> (8 * (4 - i))) & 0xFF); + } + } + + /** Read int from a bytearray + * @param array the array to read + * @param start where to begin reading + * @return value read as int + */ + public static int readInt(byte[] array, int start) throws SizeError { + int size = 0; + for(int i=0;i<4;i++) { + size |= ((int)array[start + i]) << (8* i); + } + if (size < 0) { + // Size in array is probably correct + // but we cannot store it into a int (or this will be negative) + throw new SizeError(); + } + return size; + } + /** Read long from a bytearray + * @param array the array to read + * @param start where to begin reading + * @return value read as long + */ + public static long readLong(byte[] array, int start) throws SizeError { + long size = 0; + for(int i=0;i<8;i++) { + size |= ((int)array[start + i]) << (8* i); + } + if (size < 0) { + // Size in array is probably correct + // but we cannot store it into a int (or this will be negative) + throw new SizeError(); + } + return size; + } + +}