Add algorithm but did not implement it

pull/1/head
Louis Royer 5 years ago
parent 0470a67f0f
commit 6e2e5451df

@ -57,7 +57,7 @@ Payload contains
``` ```
[64-127: OFFSET OF FILE CONTENT IN BYTES] [64-127: OFFSET OF FILE CONTENT IN BYTES]
[128-159: TOTAL FILESIZE] [128-191: TOTAL FILESIZE]
[<FILENAME>\n] [<FILENAME>\n]
[FILE CONTENT] [FILE CONTENT]
``` ```

@ -51,14 +51,14 @@ public class FileList extends Payload {
* @return datagram with padding * @return datagram with padding
*/ */
protected byte[] toDatagram() { protected byte[] toDatagram() {
byte[] datagram; // java initialize all to zero //TODO compute size
int size = ;
byte[] datagram = new byte[size]; // java initialize all to zero
// size is keep blank (ProtocolP2PDatagram will handle it) // size is keep blank (ProtocolP2PDatagram will handle it)
// set request/response code // set request/response code
datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
// bits 16-31 are reserved for future use // bits 16-31 are reserved for future use
//TODO size
int size = ;
datagram = setPayloadSize(size, datagram); datagram = setPayloadSize(size, datagram);
//TODO content //TODO Write content
} }
} }

@ -13,8 +13,8 @@ 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;
private String filename; private String filename;
private int totalSize; private long totalSize;
private int offset; private long offset;
private byte[] partialContent; private byte[] partialContent;
/** Constructor (typically used by server) with informations about file part to send as parameters. /** Constructor (typically used by server) with informations about file part to send as parameters.
@ -22,8 +22,15 @@ public class FilePart extends Payload {
* @param totalSize total size of the file to send * @param totalSize total size of the file to send
* @param offset where in the file begins the part we are sending * @param offset where in the file begins the part we are sending
* @param partialContent content of the file we send * @param partialContent content of the file we send
* @throws InternalError
*/ */
public FilePart(String filename, int totalSize, int offset, byte[] partialContent) { public FilePart(String filename, long totalSize, long offset, byte[] partialContent) throws InternalError {
/* asserts to help debugging */
assert totalSize >= 0 : "totalSize cannot be negative";
assert offset >= 0 : "offset cannot be negative";
if (totalSize < 0 || offset < 0) {
throw new InternalError();
}
this.filename = filename; this.filename = filename;
this.totalSize = totalSize; this.totalSize = totalSize;
this.offset = offset; this.offset = offset;
@ -36,14 +43,17 @@ public class FilePart extends Payload {
* @throws InternalError * @throws InternalError
*/ */
public FilePart(byte[] datagram) throws ProtocolError, InternalError { public FilePart(byte[] datagram) throws ProtocolError, InternalError {
//TODO
/* assert to help debugging */ /* 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 */ /* InternalErrorException */
if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE) { if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE) {
throw new InternalError(); throw new InternalError();
} }
int size = getPayloadSize(datagram); setOffset(datagram); // this can throw ProtocolError
setTotalFileSize(datagram); // this can throw ProtocolError
int size = getPayloadSize(datagram); // this can throw ProtocolError
int partialContentStart = setFilename(datagram, size) + 1; // this can throw ProtocolError
setPartialContent(datagram, partialContentStart, size);
} }
/** Returns a byte[] containing datagram with padding. /** Returns a byte[] containing datagram with padding.
@ -52,15 +62,39 @@ public class FilePart extends Payload {
* @return datagram with padding * @return datagram with padding
*/ */
public byte[] toDatagram() { public byte[] toDatagram() {
byte[] datagram; // java initialize all to zero //TODO : calculate payload size
int size = ;
byte[] datagram = new byte[size]; // java initialize all to zero
// size is keep blank (ProtocolP2PDatagram will handle it) // size is keep blank (ProtocolP2PDatagram will handle it)
// set request/response code // set request/response code
datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
// bits 16-31 are reserved for future use // bits 16-31 are reserved for future use
//TODO size
int size = ;
datagram = setPayloadSize(size, datagram); datagram = setPayloadSize(size, datagram);
//TODO content //TODO : write offset to datagram
//TODO : write totalFileSize to datagram
//TODO : write filename\n to datagram
//TODO : write partialContent to datagram
}
private setOffset(byte[] datagram) throws ProtocolError {
//TODO: copy from 8 to 15 into long and write into offset.
// throw protocolerror if offset is < 0
}
private setTotalFileSize(byte[] datagram) throw ProtocolError {
// 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 {
// 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
// set by the emitter, it is a protocolerror)
// return position of the \n
}
private setPartialContent(byte[] datagram, int start, size) {
// TODO: copy datagram from start to size into partialContent
} }
} }

@ -69,10 +69,16 @@ public class Payload {
* @param size integer representing payload size * @param size integer representing payload size
* @param datagram datagram to be completed * @param datagram datagram to be completed
* @return datagram with payloads size set * @return datagram with payloads size set
* @throws InternalError
*/ */
protected static byte[] setPayloadSize(int size, byte[] datagram) { protected static byte[] setPayloadSize(int size, byte[] datagram) throws InternalError {
/* assert to help debugging */
assert size >= 0: "Payload size cannont be negative";
if (size < 0) {
throw new InternalError();
}
for(int i=0;i<4;i++) { for(int i=0;i<4;i++) {
datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) (size >> (8 * (3 - i)) & 0xFF); datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) ((size >> (8 * (3 - i))) & 0xFF);
} }
} }
return datagram; return datagram;
@ -81,12 +87,16 @@ public class Payload {
/** Get payloads size from a datagram. /** Get payloads size from a datagram.
* @param datagram the full datagram received * @param datagram the full datagram received
* @return integer representing payload size * @return integer representing payload size
* @throws ProtocolError
*/ */
protected static int getPayloadSize(byte[] datagram) { protected static int getPayloadSize(byte[] datagram) throws ProtocolError {
int size = 0; int size = 0;
for(int i=0;i<4;i++) { for(int i=0;i<4;i++) {
size |= ((int)datagram[PAYLOAD_SIZE_POSITON + i]) << (8* i); size |= ((int)datagram[PAYLOAD_SIZE_POSITON + i]) << (8* i);
} }
if (size < 0) {
throw new ProtocolError();
}
return size; return size;
} }
} }

Loading…
Cancel
Save