package protocolP2P; import protocolP2P.Payload; import tools.HostItem; import java.util.ArrayList; import java.util.List; import localException.InternalError; import localException.SizeError; import localException.ProtocolError; import localException.TransmissionError; import tools.BytesArrayTools; /** Representation of payload for discover response. * @author Louis Royer * @author Flavien Haas * @author JS Auge * @version 1.0 */ public class DiscoverResponse extends Payload { private List hostList; private String filename; private static final int FILENAME_SIZE_POSITION = PAYLOAD_START_POSITION; private static final int FILENAME_POSITION = FILENAME_SIZE_POSITION + 4; /** Constructor with filename (typically used by tracker). If filename is null, it is initialized with "". * @param filename Name of the file related to the server list. * @param hostList List of servers * @throws InternalError */ public DiscoverResponse(String filename, List hostList) throws InternalError { super(RequestResponseCode.DISCOVER_RESPONSE); this.filename = filename; this.hostList = hostList; } /** 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 DiscoverResponse(byte[] packet) throws SizeError, ProtocolError, InternalError, TransmissionError { super(packet); int size = getPayloadSize(packet); /* Read filename size */ int filenameSize = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION); /* Read filename */ filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize); // TODO hostList = new ArrayList<>(); int i = FILENAME_POSITION + filenameSize; while(i getHostList() { return hostList; } /** Filename getter. * @return filename */ public String getFilename() { return filename; } }