Add empty file error code
This commit is contained in:
parent
648cb12953
commit
d33d9bf624
@ -38,6 +38,7 @@ x bytes: [64-xx: PAYLOAD]
|
||||
- `INTERNAL ERROR` (0xC2)
|
||||
- `EMPTY DIRECTORY` (0xC3)
|
||||
- `NOT FOUND` (0xC4)
|
||||
- `EMPTY FILE` (0xC5)
|
||||
|
||||
### List
|
||||
Payload size for list request is always zero.
|
||||
|
@ -4,6 +4,7 @@ import exception.ProtocolError;
|
||||
import exception.SizeError;
|
||||
import exception.TransmissionError;
|
||||
import exception.VersionError;
|
||||
import remoteException.EmptyFile;
|
||||
import remoteException.EmptyDirectory;
|
||||
import remoteException.InternalRemoteError;
|
||||
import remoteException.NotFound;
|
||||
@ -92,7 +93,9 @@ public class ClientManagementUDP implements Runnable {
|
||||
} catch (VersionRemoteError e) {
|
||||
System.err.println("Error: Server cannot decode this version of the protocol");
|
||||
} 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 ProtocolRemoteError
|
||||
* @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));
|
||||
d.send(socket, host, UDPPort);
|
||||
try {
|
||||
@ -168,6 +172,8 @@ public class ClientManagementUDP implements Runnable {
|
||||
}
|
||||
} catch (NotFound e) {
|
||||
throw new ProtocolError();
|
||||
} catch (EmptyFile e) {
|
||||
throw new ProtocolError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -66,7 +66,9 @@ public class FileList extends Payload {
|
||||
int size = 8;
|
||||
for (String s : fileList) {
|
||||
size += s.length();
|
||||
size += 1;
|
||||
}
|
||||
size -=1;
|
||||
byte[] datagram = new byte[size]; // java initialize all to zero
|
||||
// set request/response code
|
||||
datagram[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
||||
|
@ -32,7 +32,7 @@ public class FilePart extends Payload {
|
||||
super(RequestResponseCode.LOAD_RESPONSE);
|
||||
/* asserts to help debugging */
|
||||
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 offset >= 0 : "offset cannot be negative";
|
||||
assert filename != null : "filename is required";
|
||||
|
@ -9,6 +9,7 @@ import remoteException.InternalRemoteError;
|
||||
import remoteException.NotFound;
|
||||
import remoteException.ProtocolRemoteError;
|
||||
import remoteException.VersionRemoteError;
|
||||
import remoteException.EmptyFile;
|
||||
import protocolP2P.Payload;
|
||||
import protocolP2P.RequestResponseCode;
|
||||
import protocolP2P.LoadRequest;
|
||||
@ -108,8 +109,9 @@ public class ProtocolP2PDatagram {
|
||||
* @throws EmptyDirectory
|
||||
* @throws NotFound
|
||||
* @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
|
||||
byte[] datagram = new byte[4096];
|
||||
DatagramPacket reception = new DatagramPacket(datagram, datagram.length);
|
||||
@ -131,6 +133,8 @@ public class ProtocolP2PDatagram {
|
||||
throw new EmptyDirectory();
|
||||
case NOT_FOUND :
|
||||
throw new NotFound();
|
||||
case EMPTY_FILE:
|
||||
throw new EmptyFile();
|
||||
default :
|
||||
return p;
|
||||
}
|
||||
|
@ -20,7 +20,8 @@ public enum RequestResponseCode {
|
||||
PROTOCOL_ERROR(CodeType.ERROR, (byte)0xC1),
|
||||
INTERNAL_ERROR(CodeType.ERROR, (byte)0xC2),
|
||||
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 byte codeValue;
|
||||
|
4
src/remoteException/EmptyFile.java
Normal file
4
src/remoteException/EmptyFile.java
Normal file
@ -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.ProtocolRemoteError;
|
||||
import remoteException.VersionRemoteError;
|
||||
import remoteException.EmptyFile;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
/** 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();
|
||||
try {
|
||||
byte[] load = Files.readAllBytes(Paths.get(baseDirectory + filename));
|
||||
try {
|
||||
(new ProtocolP2PDatagram((Payload)(new FilePart(filename, load.length, 0, load)))).send(socket, pd);
|
||||
} catch (Exception e2) {
|
||||
System.err.println(e2);
|
||||
if (Arrays.binarySearch(fileList, filename) >= 0) {
|
||||
try {
|
||||
if (load.length == 0) {
|
||||
(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) {
|
||||
try {
|
||||
@ -112,6 +122,7 @@ public class ServerManagementUDP implements Runnable {
|
||||
} catch (VersionError e) {
|
||||
} catch (InternalError e) {
|
||||
} catch (SizeError e) {
|
||||
} catch (EmptyFile e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user