Add client implementation + update doc
parent
acaa363153
commit
192f0a2c94
@ -1 +1 @@
|
||||
<mxfile host="www.draw.io" modified="2020-01-12T21:18:15.190Z" agent="Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" etag="AragDVgr5JVvbnQoNFHI" version="12.5.1" type="device"><diagram id="MxademQTiYvtQCVb_gz8" name="Page-1">7Vxbc9o4FP41mWkfksE3Lo+BkLa7JMskve7LjmIL0EZYrCwC9NevZMs3WRgTbNruug8d61gSkr5z+XQs5cIaLbfvKFgt7ogH8YXZ8bYX1s2FafZMi/8vBLtIYBvdSDCnyItERip4RN+hFHakdI08GOQqMkIwQ6u80CW+D12WkwFKySZfbUZw/ldXYA4LgkcX4KL0C/LYIpL2zV4qfw/RfBH/stEdRG+WIK4sZxIsgEc2GZE1vrBGlBAWPS23I4jF2sXrErW73fM2GRiFPqvS4I/A+wYuf1te3883YLT+AL70Hy/jeQRsF88YenwBZJFQtiBz4gM8TqVDSta+B0W3HV5K60wIWXGhwYV/Q8Z2Ek2wZoSLFmyJ5Vs+Yrr7Ktpf2b1E8E12GBZutrnSLluaQoqWkEGaCL1rATUv+sSHkeQWYSzfF1dKLl5A1tSV094RPB8zz33Y4N8/jndPz+/R5FJiyQCdQ1ZSz5BaLtYu8wsSiHeQ8OHSHa9AIQYMveSVC0gdnSf1Uhj5g0RSj2rZsF8AXstfGmEkJq9iHWzQEoNwyWbEZzHsAhKA0dznzy5vJ1Z6+AIpQ9wwruULJsAeuguEvQnYkbVYn4AB9zkuDReEou+8WxDjzl9TJrXC7OZqPIqWEi8KA15nGuNlKKI7sM1VnICASYFLMAarAD0l01hy6JA/JIyR5SF1EDOE21L85Nv+QBq19Go9WdykLsLoStki4x5iWe2IG0YB8gLYGIVAB4yS58SZiTWacVMZEUxoakAx+hjOmAb7JfK80BUEK+Aif/4xNPxLI5VMwoY3Vip5kMtghR6EAQYilAQkGDxBPCUBYoiI/mlUd7giyGfhWjnDC+cmlFA2Ij6fB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agxo8thbxosNOuduqSNwSP+GlrK/TeoWKurHf5k7+SGTXwNi0w6tw5ON/QtjqB7fq8cym3L0u/TtsD+81CrlZEfIaDu9ph/eDDtZXOQmvZPQOfsot0+nsl9wSfnZq1pDPCuwyFaQG7k0qWuopLUe5HHFcff4QjeC1yULtEsbH/M6sI/FJgYvMOYHk1MCBkwL5yxpXhw4LnlEfNVc1SrbOZ7iZUTbKTCRQNr8t/6+F/ztKZtjun/GMl97Y2xsap0T7QS3svandnSYf95Of8Tp00a66Z8563GOga+DkVtfMW73VUz7zVP2QqHZUSNTX9xlRr0/d/zh7NCrTx1PD9avoo3pEuGuX08dCfesc9LG4rXxY+37otAs8YgFW4nFFyQviSiIS/4jCKAU9AwKrrCbE0WSieP0nGaZrCdF9/ZplHYOp27HV8OlWv6H8hej4+QxVc4/nZzJU5fCIY/fKDVWt3z2HoRbv8SmfplrWX4tL6XUUdM95k08PfXtd6xTWH1nOT0v7Nfd2GqT98fNRCZvGaP+r7nocBXIDG4ReT9kgDBTDr3yxQ+nI6dhXncw/ZWSv3i7wYvoXYqLq6Z/Zscb/Ag==</diagram></mxfile>
|
||||
<mxfile host="www.draw.io" modified="2020-01-12T22:28:59.744Z" agent="Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" etag="NNaHYQiNJ1kfl6GISq3f" version="12.5.1" type="device"><diagram id="MxademQTiYvtQCVb_gz8" name="Page-1">7Vxbd6M2EP41Pmf7kBzuth/XdvbSJlufzV770qMYxdYGI1fIib2/vhKImyQIjsHJtuQlMAghaWa++TQMHtjT9e4tAZvVFfZhMLAMfzewZwPLMh1nyP5xyT6RjGwrESwJ8kWjXHCNfkIhNIR0i3wYlRpSjAOKNmXhAochXNCSDBCCH8rNbnFQfuoGLKEiuF6AQJV+RT5diVlYw1z+DqLlKn2y6Y2TK2uQNhYziVbAxw8FkX0xsKcEY5ocrXdTGPDFS9clue9NxdVsYASGtMkNexwsL6i/+PgQ/PHpYn9z9w5dn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hsX3J8bj9XcPzcT1plvN1T4uLCkJrw+bsMwDusK01yBDT/cEHyPmIXwV5KIwOTl2C3gy1E0g5RvXEq84EYQuVZI3EjvVcXQYelySS0UlehTXb9QouB0vqD5tvUlQblU1uaKz/wroVxu750CykfPE+8zCDYtU4Jg68VjcONvqp8lHTF2ZLurT0fI7R3X7t7uNL+pIBVr9PmIVkJZVqacWsMpf1VBr/r+0/lj8hGJ57zYhITmG+oOExIH7QU6T0g86bvbg5TcQepiOJRSF2PJ8Rt/ZCt15BrOuVH4k0bWcSLDNlV7O/V7Fq9kZozqjF84s7F+jd2lpe4uPxEQRmsURQyUewLRJoHw5E9qNJ9Sn5ZAWCp37AnEAWUz7osmECk/fQYCUUsfWqcBv9aLiaEndWRLHXUdzy3FLJIf0OiBviWgV76dbIjzptkV0Nv6mmn5JyrUb+Ea1lP3xdRPjyKJPx4dRdzObKcvp+5QvU2LWjojCdqC6kH2DVpfU92R3tUPp05bVG3/1+sT7MZb8I7KWNlp/nvLCXvLf7XavvgX</diagram></mxfile>
|
@ -1,10 +1,39 @@
|
||||
package client;
|
||||
|
||||
public class Client {
|
||||
private String host;
|
||||
private int port;
|
||||
private String directory;
|
||||
public Client() {
|
||||
|
||||
host = "localhost";
|
||||
port = 40000;
|
||||
String d;
|
||||
/* Follow XDG Base Directory Specification
|
||||
* https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||
*/
|
||||
if (System.getProperty("os.name").equals("Linux")) {
|
||||
d = System.getenv().get("XDG_DATA_HOME");
|
||||
if (d == null || f.equals("")) {
|
||||
d = System.getenv().get("HOME");
|
||||
if (d != null && (!f.equals(""))) {
|
||||
d += "/.local/share";
|
||||
} else {
|
||||
d += "."
|
||||
}
|
||||
}
|
||||
} else {
|
||||
d = ".";
|
||||
}
|
||||
d += "P2P_JAVA_PROJECT_CLIENT/";
|
||||
// create directory if not already exists
|
||||
new File(d).mkdirs();
|
||||
}
|
||||
}
|
||||
public static void main(String [] args) {
|
||||
Client c = new Client("client");
|
||||
ClientManagementUDP cm = new ClientManagementUDP(c.directory, c.host, c.port);
|
||||
Thread t = new Thread(cm);
|
||||
t.setName("client P2P-JAVA-PROJECT");
|
||||
t.start();
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
package client;
|
||||
import exception.NotFound;
|
||||
|
||||
public class ClientManagment {
|
||||
private String baseDirectory;
|
||||
public ClientManagment(String baseDirectory) {
|
||||
this.baseDirectory = baseDirectory;
|
||||
}
|
||||
|
||||
public void download(String filename) throws NotFound {
|
||||
}
|
||||
|
||||
public String listDirectory() {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,140 @@
|
||||
package client;
|
||||
import exception.NotFound;
|
||||
import exception.ProtocolError;
|
||||
import exception.InternalError;
|
||||
import exception.TransmissionError;
|
||||
import java.utils.Scanner;
|
||||
import java.net.InetAddress;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class ClientManagmentUDP implements Runnable {
|
||||
private String baseDirectory;
|
||||
private int UDPPort;
|
||||
private String host;
|
||||
private final String protocolID = "P2P-JAVA-PROJECT VERSION 1.0";
|
||||
|
||||
|
||||
/** Constructor for UDP implementation, with baseDirectory and UDPPort parameters.
|
||||
* @param baseDirectory the root directory where files are stored
|
||||
* @param UDPPort the server will listen on this port
|
||||
*/
|
||||
public ClientManagmentUDP(String baseDirectory, String host, int UDPPort) {
|
||||
this.baseDirectory = baseDirectory;
|
||||
this.host = host;
|
||||
this.UDPPort = UDPPort;
|
||||
}
|
||||
|
||||
/** Implementation of Runnable
|
||||
*/
|
||||
public void run() {
|
||||
System.out.println("Files present on the server:");
|
||||
try {
|
||||
String msg = sendMsg(sendFileList());
|
||||
System.out.println(listDirectory(msg));
|
||||
System.out.println("Name of the file to download:");
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
f = scanner.nextLine();
|
||||
msg = sendMsg(sendDownloadRequest(f));
|
||||
download(msg, f);
|
||||
} catch (ProtocolError e) {
|
||||
System.out.println("Protocol error");
|
||||
} catch (NotFound e) {
|
||||
System.out.println("File not found");
|
||||
} catch (InternalError e) {
|
||||
System.out.println("Server internal error");
|
||||
} catch (IOException e) {
|
||||
System.out.println("Cannot write to file");
|
||||
}
|
||||
}
|
||||
|
||||
/** Prepare request to download file
|
||||
* @param filename name of the file to be downloaded
|
||||
* @return request to be send
|
||||
*/
|
||||
private String sendDownloadRequest(String filename) {
|
||||
return protocolID + "\nDOWNLOAD\n" + filename + "\n";
|
||||
}
|
||||
|
||||
/** Download file.
|
||||
* @param response Servers's response
|
||||
* @throws NotFound
|
||||
* @throws ProtocolError
|
||||
* @throws InternalError
|
||||
*/
|
||||
private void download(String response, String filename) throws NotFound, ProtocolError, InternalError, IOException {
|
||||
try {
|
||||
String r[] = r.split('\n', 3);
|
||||
checkProtocolID(r[0]);
|
||||
String r2[] = r[1].split(' ');
|
||||
if (r2[0] != "LOAD") {
|
||||
throw ProtocolError;
|
||||
}
|
||||
int size = Integer.parseInt(r2[1]);
|
||||
if (r[2].length() != size) {
|
||||
throws TransmissionError;
|
||||
}
|
||||
FileWriter fileWriter = new FileWriter(baseDirectory + f);
|
||||
fileWriter.write(r[2]);
|
||||
fileWriter.close();
|
||||
|
||||
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
|
||||
throw ProtocolError;
|
||||
} catch (ParseException e) {
|
||||
throw ProtocolError;
|
||||
}
|
||||
}
|
||||
|
||||
/** Prepare request to list files on server
|
||||
* @return request to be send
|
||||
*/
|
||||
private String sendListDirectoryRequest() {
|
||||
return protocolID + "\nLIST\n";
|
||||
}
|
||||
|
||||
/** Parse list of directory response from server
|
||||
* @param response server's response
|
||||
* @return list of files, separated by CRLF
|
||||
* @throws ProtocolError
|
||||
*/
|
||||
private String listDirectory(String response) throws ProtocolError {
|
||||
try {
|
||||
String r[] = r.split('\n');
|
||||
checkProtocolID(r[0]);
|
||||
return r.split(protocolID + "\nLOAD \n")[1];
|
||||
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
|
||||
throw ProtocolError;
|
||||
}
|
||||
}
|
||||
|
||||
/** Check client's protocol identifier matches message's protocol identifier.
|
||||
* Throws a ProtocolError if mismatched.
|
||||
* @param msgProtocolID part of the request containing protocol identifier
|
||||
* @throws ProtocolError
|
||||
*/
|
||||
private void checkProtocolID(String msgProtocolID) throws ProtocolError {
|
||||
if (protocolID != msgProtocolID) {
|
||||
throw ProtocolError;
|
||||
}
|
||||
}
|
||||
|
||||
/** Send message to server
|
||||
* @param msg message to be send
|
||||
* @return server's response
|
||||
*/
|
||||
private String sendMsg(String msg) {
|
||||
InetAddress dst = InetAddress.getByName(host);
|
||||
byte [] bytesString = msg.getBytes();
|
||||
DatagramPacket emission = new DatagramPacket(bytesString, bytesString.length, dst, UDPPort);
|
||||
socket.send(emission);
|
||||
socket.receive(reception);
|
||||
return new String(reception.getData(), 0, reception.getLength());
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
package exception;
|
||||
public class TransmissionError extends Exceptions {}
|
Loading…
Reference in New Issue