Update project

This commit is contained in:
Louis Royer 2020-01-21 10:55:20 +01:00
parent 72ce99fd6f
commit f3d47433e1
3 changed files with 15 additions and 10 deletions

View File

@ -7,6 +7,6 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p
## Server responses
- The response to `LIST` request is in the format `LIST\n<FILENAME 1>\n<FILENAME 2>\n[…]<LAST FILENAME>\n\n`
- The response to `DOWNLOAD` request is `LOAD <FILESIZE (BYTES)>\n<CONTENT OF FILE>` or `NOT FOUND\n` if the file doesn't exists.
- The response to `DOWNLOAD` request is `LOAD\n<FILESIZE (BYTES)>\n<CONTENT OF FILE>` or `NOT FOUND\n` if the file doesn't exists.
- The server send a `PROTOCOL ERROR\n` message if it doesn't understands what the client sent.
- The server send a `INTERNAL ERROR\n` message if it encounters an internal error.

View File

@ -76,27 +76,31 @@ public class ClientManagementUDP implements Runnable {
*/
private void download(String response, String filename) throws TransmissionError, NotFound, ProtocolError, InternalError, IOException {
try {
String r[] = response.split("\n", 3);
String r[] = response.split("\n");
checkProtocolID(r[0]);
String r2[] = r[1].split("\n");
switch (r2[0]) {
switch (r[1]) {
case "LOAD":
int size = Integer.parseInt(r2[1]);
if (r[2].length() != size) {
int size = Integer.parseInt(r[2]);
if (r[3].length() != size + 1) {
throw new TransmissionError();
}
FileWriter fileWriter = new FileWriter(baseDirectory + filename);
fileWriter.write(r[2]);
fileWriter.write(r[3]);
fileWriter.close();
break;
case "NOT FOUND":
throw new NotFound();
case "INTERNAL ERROR":
throw new InternalError();
default:
System.out.println("Error: Unknow format `" + r[1] + "`");
throw new ProtocolError();
}
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
System.out.println("Error: IndexOutOfBonds");
throw new ProtocolError();
} catch (NumberFormatException e) {
System.out.println("Error: NumberFormat");
throw new ProtocolError();
}
}

View File

@ -129,13 +129,14 @@ public class ServerManagementUDP implements Runnable {
* @throws InternalError
*/
private String upload(String filename) throws NotFound, InternalError {
File file = new File(filename);
File file = new File(baseDirectory + filename);
System.out.println("Uploading `" + baseDirectory + filename + "`");
if (!file.exists() || !file.isFile()) {
throw new NotFound();
}
String res = "LOAD " + file.length() + "\n";
String res = "LOAD\n" + file.length() + "\n";
try {
res += new String(Files.readAllBytes(Paths.get(filename)));
res += new String(Files.readAllBytes(Paths.get(baseDirectory + filename)));
} catch (IOException e) {
throw new InternalError();
}