Refactoring + implementation of some methods

pull/1/head
Louis Royer 5 years ago
parent c3b8c2c5e0
commit 5eb7e0deab

@ -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]
[<FILENAME>\n]
[FILE CONTENT]
8 bytes: [64-127: OFFSET OF FILE CONTENT IN BYTES]
8 bytes: [128-191: TOTAL FILESIZE]
y bytes: [<FILENAME>\n]
z bytes: [FILE CONTENT]
```
#### Load request

@ -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

@ -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

@ -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 payloads 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];
}
}

@ -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;
}
}
Loading…
Cancel
Save