Projet_JAVA_P2P_STRI2A/src/protocolP2P/DiscoverRequest.java
Louis Royer c81f5d2e5a
All checks were successful
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/pr-etape5 This commit looks good
Fix #41
2020-03-27 18:41:27 +01:00

72 lines
2.2 KiB
Java

package protocolP2P;
import protocolP2P.Payload;
import tools.BytesArrayTools;
import localException.InternalError;
import localException.SizeError;
import localException.ProtocolError;
import localException.TransmissionError;
/** Representation of payload for discover request.
* @author Louis Royer
* @author Flavien Haas
* @author JS Auge
* @version 1.0
*/
public class DiscoverRequest extends Payload {
private String filename;
/** Constructor with filename (typically used by client). If filename is null, it is initialized with "".
* @param filename Name of the file you want a server list of.
* @throws InternalError
*/
public DiscoverRequest(String filename) throws InternalError {
super(RequestResponseCode.DISCOVER_REQUEST);
if (filename == null) {
this.filename = "";
} else {
this.filename = filename;
}
}
/** Constructor (typically used by server) with a byte[] parameter containing the Packet received.
* @param packet the full Packet received
* @throws SizeError
* @throws InternalError
* @throws ProtocolError
* @throws TransmissionError
*/
protected DiscoverRequest(byte[] packet) throws SizeError, ProtocolError, InternalError, TransmissionError {
super(packet);
int size = getPayloadSize(packet);
filename = BytesArrayTools.readString(packet, Payload.PAYLOAD_START_POSITION, size);
}
/** Returns a byte[] containing Packet with padding.
* This Packet is still incomplete and should not be send directly.
* ProtocolP2PPacket will use this method to generate the complete Packet.
* @return Packet with padding
* @throws InternalError
*/
protected byte[] toPacket() throws InternalError {
// compute total size
int size = PAYLOAD_START_POSITION + filename.length();
byte[] packet = new byte[size]; // java initialize all to zero
// set request/response code
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
// set Payload size
setPayloadSize(size - PAYLOAD_START_POSITION, packet);
// write filename to Packet
BytesArrayTools.write(packet, filename, PAYLOAD_START_POSITION);
return packet;
}
/** Filename getter.
* @return filename
*/
public String getFilename() {
return filename;
}
}