43 lines
1.6 KiB
Java
43 lines
1.6 KiB
Java
package protocolP2P;
|
|
import java.util.ArrayList;
|
|
import protocolP2P.Payload;
|
|
import protocolP2P.RequestResponseCode;
|
|
import exception.ProtocolError;
|
|
import exception.InternalError;
|
|
|
|
public class FileList extends Payload {
|
|
private final static RequestResponseCode requestResponseCode = RequestResponseCode.LIST_RESPONSE;
|
|
private ArrayList<String> content;
|
|
|
|
public FileList(ArrayList<String> content) {
|
|
this.content = content;
|
|
}
|
|
|
|
public FileList(byte[] datagram) throws ProtocolError, InternalError {
|
|
//TODO
|
|
/* 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";
|
|
/* InternalErrorException */
|
|
if (RequestResponseCode.fromCode(datagram[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.LIST_RESPONSE) {
|
|
throw new InternalError();
|
|
}
|
|
int size = (datagram[PAYLOAD_SIZE_POSITON] << (8*3)) | (datagram[PAYLOAD_SIZE_POSITON+1] << (8*2)) | (datagram[PAYLOAD_SIZE_POSITON+2] << 8) | datagram[PAYLOAD_SIZE_POSITON+3];
|
|
|
|
}
|
|
|
|
/** To datagram with padding */
|
|
public byte[] toDatagram() {
|
|
byte[] datagram; // java initialize all to zero
|
|
// size is keep blank (ProtocolP2PDatagram will handle it)
|
|
// set request/response code
|
|
datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
|
// bits 16-31 are reserved for future use
|
|
//TODO size
|
|
int size = ;
|
|
for(i=0;i<4;i++) {
|
|
datagram[Payload.PAYLOAD_SIZE_POSITON + i] = (byte) (size >> (8 * (3 - i)) & 0xFF);
|
|
}
|
|
//TODO content
|
|
}
|
|
}
|