|
|
|
@ -6,6 +6,7 @@ import localException.ProtocolError;
|
|
|
|
|
import localException.InternalError;
|
|
|
|
|
import localException.SizeError;
|
|
|
|
|
import tools.BytesArrayTools;
|
|
|
|
|
import tools.HostItem;
|
|
|
|
|
|
|
|
|
|
/** Representation of payload for load request.
|
|
|
|
|
* @author Louis Royer
|
|
|
|
@ -17,18 +18,20 @@ public class LoadRequest extends Payload {
|
|
|
|
|
private String filename;
|
|
|
|
|
private long maxSizePartialContent;
|
|
|
|
|
private long offset;
|
|
|
|
|
private HostItem hostItem;
|
|
|
|
|
static final private int OFFSET_POSITION = PAYLOAD_START_POSITION;
|
|
|
|
|
static final private int MAX_SIZE_PARTIAL_CONTENT_POSITION = OFFSET_POSITION + 8;
|
|
|
|
|
static final private int FILENAME_SIZE_POSITION = MAX_SIZE_PARTIAL_CONTENT_POSITION + 8;
|
|
|
|
|
static final private int FILENAME_POSITION = FILENAME_SIZE_POSITION + 4;
|
|
|
|
|
|
|
|
|
|
/** Constructor (typically used by the server) with a filename parameter.
|
|
|
|
|
/** Constructor (typically used by the client) with a filename parameter.
|
|
|
|
|
* @param filename name of the file to download. Must not be empty.
|
|
|
|
|
* @param offset offset of the bloc
|
|
|
|
|
* @param maxSizePartialContent partial content in response should not excess this size, but this can be less (by example if endoffile is reached or server doesn't have the full block requested)
|
|
|
|
|
* @param hostItem hostItem used by the client to register on the tracker
|
|
|
|
|
* @throws InternalError
|
|
|
|
|
*/
|
|
|
|
|
public LoadRequest(String filename, long offset, long maxSizePartialContent) throws InternalError {
|
|
|
|
|
public LoadRequest(String filename, long offset, long maxSizePartialContent, HostItem hostItem) throws InternalError {
|
|
|
|
|
super(RequestResponseCode.LOAD_REQUEST);
|
|
|
|
|
/* assert to help debugging */
|
|
|
|
|
assert filename.length() != 0 : "Payload size of LoadRequest must not be empty";
|
|
|
|
@ -38,9 +41,10 @@ public class LoadRequest extends Payload {
|
|
|
|
|
this.filename = filename;
|
|
|
|
|
this.maxSizePartialContent = maxSizePartialContent;
|
|
|
|
|
this.offset = offset;
|
|
|
|
|
this.hostItem = hostItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Constructor (typically used by client) with a byte[] parameter containing the Packet received.
|
|
|
|
|
/** Constructor (typically used by server) with a byte[] parameter containing the Packet received.
|
|
|
|
|
* @param packet the full Packet received
|
|
|
|
|
* @throws SizeError
|
|
|
|
|
* @throws InternalError
|
|
|
|
@ -64,6 +68,12 @@ public class LoadRequest extends Payload {
|
|
|
|
|
/* Read filename */
|
|
|
|
|
int size = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION);
|
|
|
|
|
filename = BytesArrayTools.readString(packet, FILENAME_POSITION, size);
|
|
|
|
|
|
|
|
|
|
/* Read hostItem */
|
|
|
|
|
int portPosition = FILENAME_POSITION + size;
|
|
|
|
|
int hostnameStartPosition = portPosition + 2;
|
|
|
|
|
int hostnameSize = getPayloadSize(packet) - hostnameStartPosition;
|
|
|
|
|
hostItem = new HostItem(BytesArrayTools.readString(packet, hostnameStartPosition, hostnameSize), BytesArrayTools.readInt16Bits(packet, portPosition));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns a byte[] containing Packet with padding.
|
|
|
|
@ -75,8 +85,9 @@ public class LoadRequest extends Payload {
|
|
|
|
|
protected byte[] toPacket() throws InternalError {
|
|
|
|
|
// compute size
|
|
|
|
|
int filenameSize = filename.length();
|
|
|
|
|
int size = FILENAME_POSITION + filenameSize;
|
|
|
|
|
byte[] packet = new byte[size + 1]; // java initialize all to zero
|
|
|
|
|
String hostname = hostItem.getHostname();
|
|
|
|
|
int size = 1 + FILENAME_POSITION + filenameSize + 2 + hostname.length();
|
|
|
|
|
byte[] packet = new byte[size]; // java initialize all to zero
|
|
|
|
|
// set request/response code
|
|
|
|
|
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
|
|
|
|
// set Payload size
|
|
|
|
@ -89,7 +100,15 @@ public class LoadRequest extends Payload {
|
|
|
|
|
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filenameSize);
|
|
|
|
|
// Write filename
|
|
|
|
|
BytesArrayTools.write(packet, filename, FILENAME_POSITION);
|
|
|
|
|
return packet;
|
|
|
|
|
// Write hostitem
|
|
|
|
|
int portPosition = FILENAME_POSITION + filenameSize;
|
|
|
|
|
try {
|
|
|
|
|
BytesArrayTools.write16Bits(packet, portPosition, hostItem.getPort());
|
|
|
|
|
BytesArrayTools.write(packet, hostname, portPosition + 2);
|
|
|
|
|
return packet;
|
|
|
|
|
} catch (SizeError e) {
|
|
|
|
|
throw new InternalError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** filename getter.
|
|
|
|
@ -112,4 +131,11 @@ public class LoadRequest extends Payload {
|
|
|
|
|
public long getMaxSizePartialContent() {
|
|
|
|
|
return maxSizePartialContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** hostItem getter.
|
|
|
|
|
* @return hostItem
|
|
|
|
|
*/
|
|
|
|
|
public HostItem getHostItem() {
|
|
|
|
|
return hostItem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|