Refactoring + implementation of some methods
This commit is contained in:
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
|
```Datagram format
|
||||||
|
|
||||||
[0-7: VERSION(0x11, first quartet is major version, second is minor)]
|
1 byte: [0-7: VERSION(0x11, first quartet is major version, second is minor)]
|
||||||
[8-15: REQUEST/RESPONSE CODE]
|
1 byte: [8-15: REQUEST/RESPONSE CODE]
|
||||||
[16-31: RESERVED FOR FUTURE USE]
|
2 bytes: [16-31: RESERVED FOR FUTURE USE]
|
||||||
[32-63: PAYLOAD SIZE IN BYTES]
|
4 bytes: [32-63: PAYLOAD SIZE IN BYTES]
|
||||||
[64-xx: PAYLOAD]
|
x bytes: [64-xx: PAYLOAD]
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -56,10 +56,10 @@ Payload size for Not found is zero.
|
|||||||
Payload contains
|
Payload contains
|
||||||
|
|
||||||
```
|
```
|
||||||
[64-127: OFFSET OF FILE CONTENT IN BYTES]
|
8 bytes: [64-127: OFFSET OF FILE CONTENT IN BYTES]
|
||||||
[128-191: TOTAL FILESIZE]
|
8 bytes: [128-191: TOTAL FILESIZE]
|
||||||
[<FILENAME>\n]
|
y bytes: [<FILENAME>\n]
|
||||||
[FILE CONTENT]
|
z bytes: [FILE CONTENT]
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Load request
|
#### Load request
|
||||||
|
@ -4,6 +4,7 @@ import protocolP2P.Payload;
|
|||||||
import protocolP2P.RequestResponseCode;
|
import protocolP2P.RequestResponseCode;
|
||||||
import exception.ProtocolError;
|
import exception.ProtocolError;
|
||||||
import exception.InternalError;
|
import exception.InternalError;
|
||||||
|
import exception.SizeError;
|
||||||
|
|
||||||
/** Representation of payload for list response.
|
/** Representation of payload for list response.
|
||||||
* @author Louis Royer
|
* @author Louis Royer
|
||||||
@ -52,7 +53,7 @@ public class FileList extends Payload {
|
|||||||
*/
|
*/
|
||||||
protected byte[] toDatagram() {
|
protected byte[] toDatagram() {
|
||||||
//TODO compute size
|
//TODO compute size
|
||||||
int size = ;
|
int size = 8 + ;
|
||||||
byte[] datagram = new byte[size]; // java initialize all to zero
|
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
|
||||||
|
@ -3,6 +3,8 @@ import protocolP2P.Payload;
|
|||||||
import protocolP2P.RequestResponseCode;
|
import protocolP2P.RequestResponseCode;
|
||||||
import exception.ProtocolError;
|
import exception.ProtocolError;
|
||||||
import exception.InternalError;
|
import exception.InternalError;
|
||||||
|
import exception.SizeError;
|
||||||
|
import tools.BytesArrayTools;
|
||||||
|
|
||||||
/** Representation of payload for load response.
|
/** Representation of payload for load response.
|
||||||
* @author Louis Royer
|
* @author Louis Royer
|
||||||
@ -63,7 +65,7 @@ public class FilePart extends Payload {
|
|||||||
*/
|
*/
|
||||||
protected byte[] toDatagram() {
|
protected byte[] toDatagram() {
|
||||||
//TODO : calculate payload size
|
//TODO : calculate payload size
|
||||||
int size = ;
|
int size = 24 + filename.length + 1;
|
||||||
byte[] datagram = new byte[size]; // java initialize all to zero
|
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
|
||||||
@ -76,15 +78,21 @@ public class FilePart extends Payload {
|
|||||||
//TODO : write partialContent to datagram
|
//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 {
|
private setOffset(byte[] datagram) throws SizeError {
|
||||||
//TODO: copy from 8 to 15 into long and write into offset.
|
offset = BytesArrayTools.readLong(datagram, 8);
|
||||||
// throw protocolerror if offset is < 0
|
|
||||||
}
|
}
|
||||||
|
/** Write from bytes 16 to 23 of datagram into totalSize.
|
||||||
|
* @param datagram received datagram
|
||||||
|
* @throws SizeError
|
||||||
|
*/
|
||||||
private setTotalFileSize(byte[] datagram) throw SizeError {
|
private setTotalFileSize(byte[] datagram) throw SizeError {
|
||||||
// TODO: convert from byte 16 to 23 (included)
|
totalSize = BytesArrayTools.readLong(datagram, 16);
|
||||||
// into long and write into totalFileSize
|
|
||||||
// throw protocolerror if offset is < 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int setFilename(byte[] datagram, int payloadSize) throws ProtocolError {
|
private int setFilename(byte[] datagram, int payloadSize) throws ProtocolError {
|
||||||
// TODO: copy datagram from byte 24 to the first \n (excluded)
|
// TODO: copy datagram from byte 24 to the first \n (excluded)
|
||||||
// into filename unless we excess size
|
// into filename unless we excess size
|
||||||
|
@ -4,6 +4,8 @@ import protocolP2P.FilePart;
|
|||||||
import protocolP2P.FileList;
|
import protocolP2P.FileList;
|
||||||
import exception.ProtocolError;
|
import exception.ProtocolError;
|
||||||
import exception.InternalError;
|
import exception.InternalError;
|
||||||
|
import exception.SizeError;
|
||||||
|
import tools.BytesArrayTools;
|
||||||
/** Representation of payload. If payload has a size, use subclasses instead.
|
/** Representation of payload. If payload has a size, use subclasses instead.
|
||||||
* @author Louis Royer
|
* @author Louis Royer
|
||||||
* @author Flavien Haas
|
* @author Flavien Haas
|
||||||
@ -78,9 +80,7 @@ public class Payload {
|
|||||||
// because this is only for reception side
|
// because this is only for reception side
|
||||||
throw new InternalError();
|
throw new InternalError();
|
||||||
}
|
}
|
||||||
for(int i=0;i<4;i++) {
|
BytesArrayTools.write(datagram, PAYLOAD_SIZE_POSITON, size);
|
||||||
datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) ((size >> (8 * (3 - i))) & 0xFF);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get payload’s size from a datagram.
|
/** Get payload’s size from a datagram.
|
||||||
@ -89,15 +89,6 @@ public class Payload {
|
|||||||
* @throws SizeError
|
* @throws SizeError
|
||||||
*/
|
*/
|
||||||
protected static int getPayloadSize(byte[] datagram) throws SizeError {
|
protected static int getPayloadSize(byte[] datagram) throws SizeError {
|
||||||
int size = 0;
|
return BytesArrayTools.readInt(datagram, PAYLOAD_SIZE_POSITON];
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
66
src/tools/ByteArrayTools.java
Normal file
66
src/tools/ByteArrayTools.java
Normal file
@ -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…
Reference in New Issue
Block a user