Update protocol classes modelisation
This commit is contained in:
parent
6e2e5451df
commit
c3b8c2c5e0
3
src/exception/SizeError.java
Normal file
3
src/exception/SizeError.java
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
package exception;
|
||||||
|
/** Used on reception side when size as set in datagram is too big, and we cant store this in a int/long as usual. */
|
||||||
|
public class SizeError extends Exception {}
|
@ -31,10 +31,10 @@ public class FileList extends Payload {
|
|||||||
|
|
||||||
/** Constructor (typically used by client) with a byte[] parameter containing the datagram received.
|
/** Constructor (typically used by client) with a byte[] parameter containing the datagram received.
|
||||||
* @param datagram the full datagram received
|
* @param datagram the full datagram received
|
||||||
* @throws ProtocolError
|
* @throws SizeError
|
||||||
* @throws InternalError
|
* @throws InternalError
|
||||||
*/
|
*/
|
||||||
public FileList(byte[] datagram) throws ProtocolError, InternalError {
|
protected FileList(byte[] datagram) throws SizeError, InternalError {
|
||||||
//TODO
|
//TODO
|
||||||
/* assert to help debugging */
|
/* assert to help debugging */
|
||||||
assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) == RequestResponseCode.LIST_RESPONSE : "FileList subclass is incompatible with this datagram, request/response code must be checked before using this constructor";
|
assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) == RequestResponseCode.LIST_RESPONSE : "FileList subclass is incompatible with this datagram, request/response code must be checked before using this constructor";
|
||||||
|
@ -39,19 +39,19 @@ public class FilePart extends Payload {
|
|||||||
|
|
||||||
/** Constructor (typically used by client) with datagram received as parameter.
|
/** Constructor (typically used by client) with datagram received as parameter.
|
||||||
* @param datagram the full datagram received
|
* @param datagram the full datagram received
|
||||||
* @throws ProtocolError
|
* @throws SizeError
|
||||||
* @throws InternalError
|
* @throws InternalError
|
||||||
*/
|
*/
|
||||||
public FilePart(byte[] datagram) throws ProtocolError, InternalError {
|
protected FilePart(byte[] datagram) throws SizeError, ProtocolError, InternalError {
|
||||||
/* 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();
|
||||||
}
|
}
|
||||||
setOffset(datagram); // this can throw ProtocolError
|
setOffset(datagram); // this can throw SizeError
|
||||||
setTotalFileSize(datagram); // this can throw ProtocolError
|
setTotalFileSize(datagram); // this can throw SizeError
|
||||||
int size = getPayloadSize(datagram); // this can throw ProtocolError
|
int size = getPayloadSize(datagram); // this can throw SizeError
|
||||||
int partialContentStart = setFilename(datagram, size) + 1; // this can throw ProtocolError
|
int partialContentStart = setFilename(datagram, size) + 1; // this can throw ProtocolError
|
||||||
setPartialContent(datagram, partialContentStart, size);
|
setPartialContent(datagram, partialContentStart, size);
|
||||||
}
|
}
|
||||||
@ -61,7 +61,7 @@ public class FilePart extends Payload {
|
|||||||
* ProtocolP2PDatagram will use this method to generate the complete datagram.
|
* ProtocolP2PDatagram will use this method to generate the complete datagram.
|
||||||
* @return datagram with padding
|
* @return datagram with padding
|
||||||
*/
|
*/
|
||||||
public byte[] toDatagram() {
|
protected byte[] toDatagram() {
|
||||||
//TODO : calculate payload size
|
//TODO : calculate payload size
|
||||||
int size = ;
|
int size = ;
|
||||||
byte[] datagram = new byte[size]; // java initialize all to zero
|
byte[] datagram = new byte[size]; // java initialize all to zero
|
||||||
@ -76,16 +76,16 @@ public class FilePart extends Payload {
|
|||||||
//TODO : write partialContent to datagram
|
//TODO : write partialContent to datagram
|
||||||
}
|
}
|
||||||
|
|
||||||
private setOffset(byte[] datagram) throws ProtocolError {
|
private setOffset(byte[] datagram) throws SizeError {
|
||||||
//TODO: copy from 8 to 15 into long and write into offset.
|
//TODO: copy from 8 to 15 into long and write into offset.
|
||||||
// throw protocolerror if offset is < 0
|
// throw protocolerror if offset is < 0
|
||||||
}
|
}
|
||||||
private setTotalFileSize(byte[] datagram) throw ProtocolError {
|
private setTotalFileSize(byte[] datagram) throw SizeError {
|
||||||
// TODO: convert from byte 16 to 23 (included)
|
// TODO: convert from byte 16 to 23 (included)
|
||||||
// into long and write into totalFileSize
|
// into long and write into totalFileSize
|
||||||
// throw protocolerror if offset is < 0
|
// throw protocolerror if offset is < 0
|
||||||
}
|
}
|
||||||
private int setFilename(byte[] datagram, size) 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
|
||||||
// (in this case the wrong size has been
|
// (in this case the wrong size has been
|
||||||
@ -93,7 +93,7 @@ public class FilePart extends Payload {
|
|||||||
// return position of the \n
|
// return position of the \n
|
||||||
|
|
||||||
}
|
}
|
||||||
private setPartialContent(byte[] datagram, int start, size) {
|
private setPartialContent(byte[] datagram, int start, int payloadSize) {
|
||||||
// TODO: copy datagram from start to size into partialContent
|
// TODO: copy datagram from start to size into partialContent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ public class Payload {
|
|||||||
* @throws ProtocolError
|
* @throws ProtocolError
|
||||||
* @throws InternalError
|
* @throws InternalError
|
||||||
*/
|
*/
|
||||||
public Payload(byte[] datagram) throws ProtocolError, InternalError {
|
protected Payload(byte[] datagram) throws ProtocolError, InternalError {
|
||||||
/* asserts to help debugging */
|
/* asserts to help debugging */
|
||||||
assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class";
|
assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE : "LIST_RESPONSE must use FilePart class";
|
||||||
assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class";
|
assert RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LOAD_RESPONSE : "LOAD_RESPONSE must use FileList class";
|
||||||
@ -56,7 +56,7 @@ public class Payload {
|
|||||||
* ProtocolP2PDatagram will use this method to generate the complete datagram.
|
* ProtocolP2PDatagram will use this method to generate the complete datagram.
|
||||||
* @return datagram with padding
|
* @return datagram with padding
|
||||||
*/
|
*/
|
||||||
public byte[] toDatagram() {
|
protected byte[] toDatagram() {
|
||||||
byte [] datagram = new byte[8]; // java initialize all to zero
|
byte [] datagram = new byte[8]; // 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
|
||||||
@ -68,34 +68,35 @@ public class Payload {
|
|||||||
/** Set payload’s size in a datagram.
|
/** Set payload’s size in a datagram.
|
||||||
* @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 payload’s size set
|
|
||||||
* @throws InternalError
|
* @throws InternalError
|
||||||
*/
|
*/
|
||||||
protected static byte[] setPayloadSize(int size, byte[] datagram) throws InternalError {
|
protected static setPayloadSize(int size, byte[] datagram) throws InternalError {
|
||||||
/* assert to help debugging */
|
/* assert to help debugging */
|
||||||
assert size >= 0: "Payload size cannont be negative";
|
assert size >= 0: "Payload size cannot be negative";
|
||||||
if (size < 0) {
|
if (size < 0) {
|
||||||
|
// We don't throw SizeError
|
||||||
|
// because this is only for reception side
|
||||||
throw new InternalError();
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get payload’s size from a datagram.
|
/** Get payload’s 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
|
* @throws SizeError
|
||||||
*/
|
*/
|
||||||
protected static int getPayloadSize(byte[] datagram) throws ProtocolError {
|
protected static int getPayloadSize(byte[] datagram) throws SizeError {
|
||||||
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) {
|
if (size < 0) {
|
||||||
throw new ProtocolError();
|
// 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 size;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@ import protocolP2P.Payload;
|
|||||||
import protocolP2P.RequestResponseCode;
|
import protocolP2P.RequestResponseCode;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.lang.Byte;
|
import java.lang.Byte;
|
||||||
|
import java.net.DatagramPacket;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
|
|
||||||
/** Representation of datagram.
|
/** Representation of datagram.
|
||||||
* @author Louis Royer
|
* @author Louis Royer
|
||||||
@ -26,12 +28,37 @@ public class ProtocolP2PDatagram {
|
|||||||
this.payload = payload;
|
this.payload = payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Constructor with datagram as byte[] parameter (typically used when receiving datagram).
|
/** Send datagram on socket
|
||||||
|
* @param socket DatagramSocket used to send datagram
|
||||||
|
*/
|
||||||
|
public void send(DatagramSocket socket) {
|
||||||
|
//TODO
|
||||||
|
byte[] datagram = toDatagram();
|
||||||
|
// generate DatagramPacket
|
||||||
|
// send it
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Receive datagram on socket
|
||||||
|
* @param socket DatagramSocket used to receive datagram
|
||||||
|
* @return payload of the datagram
|
||||||
|
* @throws ProtocolError
|
||||||
|
* @throws VersionError
|
||||||
|
*/
|
||||||
|
public static Payload receive(DatagramSocket socket) throws ProtocolError, VersionError {
|
||||||
|
//TODO
|
||||||
|
// reception
|
||||||
|
//datagram =
|
||||||
|
// contruction
|
||||||
|
ProtocolP2PDatagram p = new ProtocolP2PDatagram(datagram);
|
||||||
|
return p.getPayload();
|
||||||
|
|
||||||
|
}
|
||||||
|
/** Private constructor with datagram as byte[] parameter (typically used when receiving datagram).
|
||||||
* @param datagram the full datagram received
|
* @param datagram the full datagram received
|
||||||
* @throws ProtocolError
|
* @throws ProtocolError
|
||||||
* @throws VersionError
|
* @throws VersionError
|
||||||
*/
|
*/
|
||||||
public ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError {
|
private ProtocolP2PDatagram(byte[] datagram) throws ProtocolError, VersionError, SizeError {
|
||||||
// unwrap version
|
// unwrap version
|
||||||
version = datagram[VERSION_POSITON];
|
version = datagram[VERSION_POSITON];
|
||||||
checkProtocolVersion(); // this can throw VersionError
|
checkProtocolVersion(); // this can throw VersionError
|
||||||
@ -53,7 +80,7 @@ public class ProtocolP2PDatagram {
|
|||||||
* This datagram is still complete and ready to be send.
|
* This datagram is still complete and ready to be send.
|
||||||
* @return the full datagram to send
|
* @return the full datagram to send
|
||||||
*/
|
*/
|
||||||
public byte[] toDatagram() {
|
private byte[] toDatagram() {
|
||||||
byte[] datagram = payload.toDatagram();
|
byte[] datagram = payload.toDatagram();
|
||||||
datagram[VERSION_POSITON] = version;
|
datagram[VERSION_POSITON] = version;
|
||||||
return datagram;
|
return datagram;
|
||||||
@ -62,7 +89,7 @@ public class ProtocolP2PDatagram {
|
|||||||
/** Returns Payload associated with the datagram.
|
/** Returns Payload associated with the datagram.
|
||||||
* @return payload associated with the datagram
|
* @return payload associated with the datagram
|
||||||
*/
|
*/
|
||||||
public Payload getPayload() {
|
private Payload getPayload() {
|
||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user