Add empty file error code

pull/1/head
Louis Royer 4 years ago
parent 648cb12953
commit d33d9bf624

@ -38,6 +38,7 @@ x bytes: [64-xx: PAYLOAD]
- `INTERNAL ERROR` (0xC2) - `INTERNAL ERROR` (0xC2)
- `EMPTY DIRECTORY` (0xC3) - `EMPTY DIRECTORY` (0xC3)
- `NOT FOUND` (0xC4) - `NOT FOUND` (0xC4)
- `EMPTY FILE` (0xC5)
### List ### List
Payload size for list request is always zero. Payload size for list request is always zero.

@ -4,6 +4,7 @@ import exception.ProtocolError;
import exception.SizeError; import exception.SizeError;
import exception.TransmissionError; import exception.TransmissionError;
import exception.VersionError; import exception.VersionError;
import remoteException.EmptyFile;
import remoteException.EmptyDirectory; import remoteException.EmptyDirectory;
import remoteException.InternalRemoteError; import remoteException.InternalRemoteError;
import remoteException.NotFound; import remoteException.NotFound;
@ -92,7 +93,9 @@ public class ClientManagementUDP implements Runnable {
} catch (VersionRemoteError e) { } catch (VersionRemoteError e) {
System.err.println("Error: Server cannot decode this version of the protocol"); System.err.println("Error: Server cannot decode this version of the protocol");
} catch (NotFound e) { } catch (NotFound e) {
System.err.println("Error: Server have not this file in directory"); System.err.println("Error: Server has not this file in directory");
} catch (EmptyFile e) {
System.err.println("Error: File is empty");
} }
} }
@ -109,8 +112,9 @@ public class ClientManagementUDP implements Runnable {
* @throws InternalRemoteError * @throws InternalRemoteError
* @throws ProtocolRemoteError * @throws ProtocolRemoteError
* @throws VersionRemoteError * @throws VersionRemoteError
* @throws EmptyFile
*/ */
private void download(String filename) throws NotFound, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { private void download(String filename) throws EmptyFile, NotFound, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError {
ProtocolP2PDatagram d = new ProtocolP2PDatagram((Payload) new LoadRequest(filename)); ProtocolP2PDatagram d = new ProtocolP2PDatagram((Payload) new LoadRequest(filename));
d.send(socket, host, UDPPort); d.send(socket, host, UDPPort);
try { try {
@ -168,6 +172,8 @@ public class ClientManagementUDP implements Runnable {
} }
} catch (NotFound e) { } catch (NotFound e) {
throw new ProtocolError(); throw new ProtocolError();
} catch (EmptyFile e) {
throw new ProtocolError();
} }
} }
} }

@ -66,7 +66,9 @@ public class FileList extends Payload {
int size = 8; int size = 8;
for (String s : fileList) { for (String s : fileList) {
size += s.length(); size += s.length();
size += 1;
} }
size -=1;
byte[] datagram = new byte[size]; // java initialize all to zero byte[] datagram = new byte[size]; // java initialize all to zero
// set request/response code // set request/response code
datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue; datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;

@ -32,7 +32,7 @@ public class FilePart extends Payload {
super(RequestResponseCode.LOAD_RESPONSE); super(RequestResponseCode.LOAD_RESPONSE);
/* asserts to help debugging */ /* asserts to help debugging */
assert totalSize >= 0 : "totalSize cannot be negative"; assert totalSize >= 0 : "totalSize cannot be negative";
assert partialContent.length != 0 : "partialContent.length cannot be zero"; assert partialContent.length != 0 : "partialContent.length cannot be zero, see RRCode.EMPTY_FILE";
assert totalSize >= partialContent.length : "totalSize must be greater than partialContent.length"; assert totalSize >= partialContent.length : "totalSize must be greater than partialContent.length";
assert offset >= 0 : "offset cannot be negative"; assert offset >= 0 : "offset cannot be negative";
assert filename != null : "filename is required"; assert filename != null : "filename is required";

@ -9,6 +9,7 @@ import remoteException.InternalRemoteError;
import remoteException.NotFound; import remoteException.NotFound;
import remoteException.ProtocolRemoteError; import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError; import remoteException.VersionRemoteError;
import remoteException.EmptyFile;
import protocolP2P.Payload; import protocolP2P.Payload;
import protocolP2P.RequestResponseCode; import protocolP2P.RequestResponseCode;
import protocolP2P.LoadRequest; import protocolP2P.LoadRequest;
@ -108,8 +109,9 @@ public class ProtocolP2PDatagram {
* @throws EmptyDirectory * @throws EmptyDirectory
* @throws NotFound * @throws NotFound
* @throws IOException * @throws IOException
* @throws EmptyFile
*/ */
public static ProtocolP2PDatagram receive(DatagramSocket socket) throws NotFound, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException { public static ProtocolP2PDatagram receive(DatagramSocket socket) throws EmptyFile, NotFound, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException {
// reception // reception
byte[] datagram = new byte[4096]; byte[] datagram = new byte[4096];
DatagramPacket reception = new DatagramPacket(datagram, datagram.length); DatagramPacket reception = new DatagramPacket(datagram, datagram.length);
@ -131,6 +133,8 @@ public class ProtocolP2PDatagram {
throw new EmptyDirectory(); throw new EmptyDirectory();
case NOT_FOUND : case NOT_FOUND :
throw new NotFound(); throw new NotFound();
case EMPTY_FILE:
throw new EmptyFile();
default : default :
return p; return p;
} }

@ -20,7 +20,8 @@ public enum RequestResponseCode {
PROTOCOL_ERROR(CodeType.ERROR, (byte)0xC1), PROTOCOL_ERROR(CodeType.ERROR, (byte)0xC1),
INTERNAL_ERROR(CodeType.ERROR, (byte)0xC2), INTERNAL_ERROR(CodeType.ERROR, (byte)0xC2),
EMPTY_DIRECTORY(CodeType.ERROR, (byte)0xC3), EMPTY_DIRECTORY(CodeType.ERROR, (byte)0xC3),
NOT_FOUND(CodeType.ERROR, (byte)0xC4); NOT_FOUND(CodeType.ERROR, (byte)0xC4),
EMPTY_FILE(CodeType.ERROR, (byte)0xC5);
public final CodeType codeType; public final CodeType codeType;
public final byte codeValue; public final byte codeValue;

@ -0,0 +1,4 @@
package remoteException;
public class EmptyFile extends Exception {
private static final long serialVersionUID = 11L;
}

@ -22,6 +22,8 @@ import remoteException.InternalRemoteError;
import remoteException.NotFound; import remoteException.NotFound;
import remoteException.ProtocolRemoteError; import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError; import remoteException.VersionRemoteError;
import remoteException.EmptyFile;
import java.util.Arrays;
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP. /** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
@ -70,10 +72,18 @@ public class ServerManagementUDP implements Runnable {
String filename = ((LoadRequest)p).getFilename(); String filename = ((LoadRequest)p).getFilename();
try { try {
byte[] load = Files.readAllBytes(Paths.get(baseDirectory + filename)); byte[] load = Files.readAllBytes(Paths.get(baseDirectory + filename));
try { if (Arrays.binarySearch(fileList, filename) >= 0) {
(new ProtocolP2PDatagram((Payload)(new FilePart(filename, load.length, 0, load)))).send(socket, pd); try {
} catch (Exception e2) { if (load.length == 0) {
System.err.println(e2); (new ProtocolP2PDatagram(new Payload(RequestResponseCode.EMPTY_FILE))).send(socket, pd);
} else {
(new ProtocolP2PDatagram((Payload)(new FilePart(filename, load.length, 0, load)))).send(socket, pd);
}
} catch (Exception e2) {
System.err.println(e2);
}
} else {
throw new IOException(); // to send a NOT_FOUND in the catch block
} }
} catch (IOException e) { } catch (IOException e) {
try { try {
@ -112,6 +122,7 @@ public class ServerManagementUDP implements Runnable {
} catch (VersionError e) { } catch (VersionError e) {
} catch (InternalError e) { } catch (InternalError e) {
} catch (SizeError e) { } catch (SizeError e) {
} catch (EmptyFile e) {
} }
} }
} }

Loading…
Cancel
Save