@ -4,13 +4,21 @@ import protocolP2P.FilePart;
import protocolP2P.FileList ;
import exception.ProtocolError ;
import exception.InternalError ;
/ * * Representation of payload . If payload has a size , use subclasses instead .
* @author Louis Royer
* @author Flavien Haas
* @author JS Auge
* @version 1.0
* /
public class Payload {
private RequestResponseCode requestResponseCode ;
protected final static int PAYLOAD_SIZE_POSITON = 4 ;
protected final static int PAYLOAD_START_POSITION = 8 ;
/** To create request/response with a payload size of zero */
/ * * Consructor used to create Payload with a payload size of zero using a RRCode .
* @param requestResponseCode Request / Response code associated with the payload
* @throws InternalError
* /
public Payload ( RequestResponseCode requestResponseCode ) throws InternalError {
/* asserts to help debugging */
assert requestResponseCode ! = requestResponseCode . LIST_RESPONSE : "LIST_RESPONSE must use FilePart class" ;
@ -19,6 +27,12 @@ public class Payload {
checkRequestResponseCode ( ) ; // this can throw InternalError
}
/ * * Constructor used to create a Payload ( when no more specific subclasses exists ) using datagram as parameter .
* If payload size is not empty , using subclass is required .
* @param datagram the full datagram received
* @throws ProtocolError
* @throws InternalError
* /
public Payload ( byte [ ] datagram ) throws ProtocolError , InternalError {
/* asserts to help debugging */
assert RequestResponseCode . fromCode ( datagram [ RequestResponseCode . RRCODE_POSITION ] ) ! = RequestResponseCode . LIST_RESPONSE : "LIST_RESPONSE must use FilePart class" ;
@ -27,6 +41,9 @@ public class Payload {
checkRequestResponseCode ( ) ; // this can throw InternalError
}
/ * * Used to check RRCode used is compatible with this class use , or if a more specific subclass is required .
* @throws InternalError
* /
private void checkRequestResponseCode ( ) throws InternalError {
/* Incorrect use cases (use subclasses instead) */
if ( requestResponseCode = = RequestResponseCode . LIST_RESPONSE | | requestResponseCode = = RequestResponseCode . LOAD_RESPONSE ) {
@ -34,7 +51,11 @@ public class Payload {
}
}
/** Payload with padding */
/ * * Returns a byte [ ] containing datagram with padding .
* This datagram is still incomplete and should not be send directly .
* ProtocolP2PDatagram will use this method to generate the complete datagram .
* @return datagram with padding
* /
public byte [ ] toDatagram ( ) {
byte [ ] datagram = new byte [ 8 ] ; // java initialize all to zero
// size is keep blank (ProtocolP2PDatagram will handle it)
@ -44,6 +65,11 @@ public class Payload {
// payload size is 0 (this is what java have initialized datagram)
}
/ * * Set payload ’ s size in a datagram .
* @param size integer representing payload size
* @param datagram datagram to be completed
* @return datagram with payload ’ s size set
* /
protected static byte [ ] setPayloadSize ( int size , byte [ ] datagram ) {
for ( int i = 0 ; i < 4 ; i + + ) {
datagram [ Payload . PAYLOAD_SIZE_POSITON + i ] = ( byte ) ( size > > ( 8 * ( 3 - i ) ) & 0xFF ) ;
@ -51,7 +77,11 @@ public class Payload {
}
return datagram ;
}
/ * * Get payload ’ s size from a datagram .
* @param datagram the full datagram received
* @return integer representing payload size
* /
protected static int getPayloadSize ( byte [ ] datagram ) {
int size = 0 ;
for ( int i = 0 ; i < 4 ; i + + ) {