FileList is done
This commit is contained in:
parent
2afb87303f
commit
eeb22e74cc
@ -1,10 +1,11 @@
|
|||||||
package protocolP2P;
|
package protocolP2P;
|
||||||
import java.util.ArrayList;
|
import java.util.Arrays;
|
||||||
import protocolP2P.Payload;
|
import protocolP2P.Payload;
|
||||||
import protocolP2P.RequestResponseCode;
|
import protocolP2P.RequestResponseCode;
|
||||||
import exception.ProtocolError;
|
import exception.ProtocolError;
|
||||||
import exception.InternalError;
|
import exception.InternalError;
|
||||||
import exception.SizeError;
|
import exception.SizeError;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
/** Representation of payload for list response.
|
/** Representation of payload for list response.
|
||||||
* @author Louis Royer
|
* @author Louis Royer
|
||||||
@ -13,18 +14,18 @@ import exception.SizeError;
|
|||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class FileList extends Payload {
|
public class FileList extends Payload {
|
||||||
private ArrayList<String> content;
|
private String[] content;
|
||||||
|
|
||||||
/** Constructor (typically used by the server) with an ArrayList parameter containing
|
/** Constructor (typically used by the server) with an ArrayList parameter containing
|
||||||
* filenames.
|
* filenames.
|
||||||
* @param content a list of files. Must not be empty.
|
* @param content a list of files. Must not be empty.
|
||||||
* @throws InternalError
|
* @throws InternalError
|
||||||
*/
|
*/
|
||||||
public FileList(ArrayList<String> content) throws InternalError {
|
public FileList(String[] content) throws InternalError {
|
||||||
super(RequestResponseCode.LIST_RESPONSE);
|
super(RequestResponseCode.LIST_RESPONSE);
|
||||||
/* assert to help debugging */
|
/* assert to help debugging */
|
||||||
assert !content.isEmpty() : "Payload size of FileList must not be empty, use EmptyDirectory from Payload instead";
|
assert content.length != 0 : "Payload size of FileList must not be empty, use EmptyDirectory from Payload instead";
|
||||||
if (content.isEmpty()) {
|
if (content.length == 0) {
|
||||||
throw new InternalError();
|
throw new InternalError();
|
||||||
}
|
}
|
||||||
this.content = content;
|
this.content = content;
|
||||||
@ -45,7 +46,11 @@ public class FileList extends Payload {
|
|||||||
throw new InternalError();
|
throw new InternalError();
|
||||||
}
|
}
|
||||||
int size = getPayloadSize(datagram);
|
int size = getPayloadSize(datagram);
|
||||||
//TODO
|
try {
|
||||||
|
content = (new String(datagram, 8, size, "UTF-8")).split("\n");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
throw new InternalError();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns a byte[] containing datagram with padding.
|
/** Returns a byte[] containing datagram with padding.
|
||||||
@ -54,15 +59,41 @@ public class FileList extends Payload {
|
|||||||
* @return datagram with padding
|
* @return datagram with padding
|
||||||
* @throws InternalError
|
* @throws InternalError
|
||||||
*/
|
*/
|
||||||
/*protected byte[] toDatagram() throws InternalError {
|
protected byte[] toDatagram() throws InternalError {
|
||||||
//TODO compute size
|
// compute size
|
||||||
int size = 8 + ;
|
int size = 8;
|
||||||
|
for (String s : content) {
|
||||||
|
size += s.length();
|
||||||
|
}
|
||||||
byte[] datagram = new byte[size]; // java initialize all to zero
|
byte[] datagram = new byte[size]; // java initialize all to zero
|
||||||
// size is keep blank (ProtocolP2PDatagram will handle it)
|
// size is keep blank (ProtocolP2PDatagram will handle it)
|
||||||
// set request/response code
|
// set request/response code
|
||||||
datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
||||||
// bits 16-31 are reserved for future use
|
// bits 16-31 are reserved for future use
|
||||||
datagram = setPayloadSize(size, datagram);
|
setPayloadSize(size, datagram);
|
||||||
//TODO Write content
|
// Write content
|
||||||
}*/
|
int bCount = 8;
|
||||||
|
for(String s : content) {
|
||||||
|
if (bCount != 8) { // not on first iteration
|
||||||
|
try {
|
||||||
|
datagram[bCount] = "\n".getBytes("UTF-8")[0]; // separator
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
throw new InternalError();
|
||||||
|
}
|
||||||
|
bCount += 1;
|
||||||
|
}
|
||||||
|
// Copy filename
|
||||||
|
try {
|
||||||
|
byte[] sb = s.getBytes("UTF-8");
|
||||||
|
for(byte b : sb) {
|
||||||
|
datagram[bCount] = b;
|
||||||
|
bCount += 1;
|
||||||
|
}
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
throw new InternalError();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return datagram;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user