From 4fec7446163e3ced894c13340130fa5ac0f069f7 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 23 Jan 2020 17:38:26 +0100 Subject: [PATCH] End of protocol implementation --- src/protocolP2P/FilePart.java | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/protocolP2P/FilePart.java b/src/protocolP2P/FilePart.java index 1dd89ce..d4a3fb2 100644 --- a/src/protocolP2P/FilePart.java +++ b/src/protocolP2P/FilePart.java @@ -73,8 +73,8 @@ public class FilePart extends Payload { * @throws InternalError */ protected byte[] toDatagram() throws InternalError { - //TODO : calculate payload size - int size = 24 + filename.length() + 1; + // compute payload size + int size = 28 + filename.length() + partialContent.length; byte[] datagram = new byte[size]; // java initialize all to zero // size is keep blank (ProtocolP2PDatagram will handle it) // set request/response code @@ -87,14 +87,22 @@ public class FilePart extends Payload { BytesArrayTools.write(datagram, 16, totalSize); // write filename’s size to datagram BytesArrayTools.write(datagram, 24, filename.length()); - //TODO : write filename to datagram + // write filename to datagram try { byte[] bFilename = filename.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new InternalError(); } - //TODO : write partialContent to datagram - + int i = filename.length() + 24; + for (byte b : bFilename) { + datagram[i] = b; + i += 1; + } + // write partialContent to datagram + for (byte b: partialContent) { + datagram[i] = b; + i += 1; + } return datagram; } @@ -113,7 +121,7 @@ public class FilePart extends Payload { totalSize = BytesArrayTools.readLong(datagram, 16); } - /** Read filename’s size from bytes 24 to 28 of datagram. + /** Read filename’s size from bytes 24 to 27 of datagram. * @param datagram received datagram * @throws ProtocolError * @throws SizeError @@ -132,7 +140,7 @@ public class FilePart extends Payload { return size; } - /** Write filename from byte 24 to byte (24 + (filenameSize - 1)) of datagram. + /** Write filename from byte 28 to byte (28 + (filenameSize - 1)) of datagram. * @param datagram received datagram * @throws ProtocolError * @throws SizeError @@ -141,19 +149,19 @@ public class FilePart extends Payload { private void setFilename(byte[] datagram) throws ProtocolError, SizeError, InternalError { int filenameSize = getFilenameSize(datagram); // this can throw ProtocolError or SizeError try { - filename = new String(datagram, 24, filenameSize, "UTF-8"); + filename = new String(datagram, 28, filenameSize, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new InternalError(); } } - /** Write partialContent from byte (24 + filenameSize) to byte (8 + payloadSize) of datagram. + /** Write partialContent from byte (28 + filenameSize) to byte (8 + payloadSize) of datagram. * @param datagram received datagram * @throws SizeError * @throws ProtocolError */ private void setPartialContent(byte[] datagram) throws ProtocolError, SizeError { - int start = 24 + getFilenameSize(datagram); // this can throw SizeError or ProtocolError + int start = 28 + getFilenameSize(datagram); // this can throw SizeError or ProtocolError int end = 8 + getPayloadSize(datagram); // this can throw SizeError try { partialContent = Arrays.copyOfRange(datagram, start, end+1);