Update project

pull/1/head
Louis Royer 5 years ago
parent 72ce99fd6f
commit f3d47433e1

@ -7,6 +7,6 @@ All messages begins with `P2P-JAVA-PROJECT VERSION 1.0\n` (this version of the p
## Server responses ## 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 `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 `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. - The server send a `INTERNAL ERROR\n` message if it encounters an internal error.

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

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

Loading…
Cancel
Save