Projet_JAVA_P2P_STRI2A/src/clientP2P/ClientManagementUDP.java

164 lines
4.9 KiB
Java
Raw Normal View History

2020-01-14 11:10:11 +01:00
package clientP2P;
2020-01-12 23:29:49 +01:00
import exception.NotFound;
import exception.ProtocolError;
import exception.InternalError;
import exception.TransmissionError;
2020-01-14 11:10:11 +01:00
import java.util.Scanner;
2020-01-12 23:29:49 +01:00
import java.net.InetAddress;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.io.File;
import java.io.IOException;
2020-01-14 11:10:11 +01:00
import java.io.FileWriter;
2020-01-12 23:29:49 +01:00
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
* @author Louis Royer
* @author Flavien Haas
* @author JS Auge
* @version 1.0
*/
2020-01-14 11:10:11 +01:00
public class ClientManagementUDP implements Runnable {
2020-01-12 23:29:49 +01:00
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
*/
2020-01-14 11:10:11 +01:00
public ClientManagementUDP(String baseDirectory, String host, int UDPPort) {
2020-01-12 23:29:49 +01:00
this.baseDirectory = baseDirectory;
this.host = host;
this.UDPPort = UDPPort;
}
/** Implementation of Runnable
*/
public void run() {
System.out.println("Files present on the server:");
try {
2020-01-14 11:10:11 +01:00
String msg = sendMsg(sendListDirectoryRequest());
2020-01-12 23:29:49 +01:00
System.out.println(listDirectory(msg));
System.out.println("Name of the file to download:");
Scanner scanner = new Scanner(System.in);
2020-01-14 11:10:11 +01:00
String f = scanner.nextLine();
2020-01-12 23:29:49 +01:00
msg = sendMsg(sendDownloadRequest(f));
download(msg, f);
2020-01-14 11:10:11 +01:00
} catch (TransmissionError e) {
2020-01-14 12:33:21 +01:00
System.out.println("Transmission error");
2020-01-12 23:29:49 +01:00
} 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
*/
2020-01-14 11:10:11 +01:00
private void download(String response, String filename) throws TransmissionError, NotFound, ProtocolError, InternalError, IOException {
2020-01-12 23:29:49 +01:00
try {
2020-01-21 10:55:20 +01:00
String r[] = response.split("\n");
2020-01-12 23:29:49 +01:00
checkProtocolID(r[0]);
2020-01-21 10:55:20 +01:00
switch (r[1]) {
2020-01-21 10:37:21 +01:00
case "LOAD":
2020-01-21 10:55:20 +01:00
int size = Integer.parseInt(r[2]);
2020-01-21 10:57:27 +01:00
if (r[3].length() != size - 1) {
2020-01-21 10:37:21 +01:00
throw new TransmissionError();
}
FileWriter fileWriter = new FileWriter(baseDirectory + filename);
2020-01-21 10:55:20 +01:00
fileWriter.write(r[3]);
2020-01-21 10:37:21 +01:00
fileWriter.close();
break;
case "NOT FOUND":
throw new NotFound();
2020-01-21 10:55:20 +01:00
case "INTERNAL ERROR":
throw new InternalError();
2020-01-21 10:37:21 +01:00
default:
2020-01-21 10:55:20 +01:00
System.out.println("Error: Unknow format `" + r[1] + "`");
2020-01-14 11:10:11 +01:00
throw new ProtocolError();
2020-01-12 23:29:49 +01:00
}
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
2020-01-21 10:55:20 +01:00
System.out.println("Error: IndexOutOfBonds");
2020-01-14 11:10:11 +01:00
throw new ProtocolError();
} catch (NumberFormatException e) {
2020-01-21 10:55:20 +01:00
System.out.println("Error: NumberFormat");
2020-01-14 11:10:11 +01:00
throw new ProtocolError();
2020-01-12 23:29:49 +01:00
}
}
/** 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 {
2020-01-14 11:10:11 +01:00
String r[] = response.split("\n");
2020-01-12 23:29:49 +01:00
checkProtocolID(r[0]);
2020-01-21 10:37:21 +01:00
return response.split(protocolID + "\nLIST\n")[1].split("\n\n")[0];
2020-01-12 23:29:49 +01:00
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
2020-01-14 11:10:11 +01:00
throw new ProtocolError();
2020-01-12 23:29:49 +01:00
}
}
2020-01-14 11:10:11 +01:00
2020-01-12 23:29:49 +01:00
/** 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 {
2020-01-14 11:55:02 +01:00
if (!protocolID.equals(msgProtocolID)) {
2020-01-14 11:10:11 +01:00
throw new ProtocolError();
2020-01-12 23:29:49 +01:00
}
}
/** Send message to server
* @param msg message to be send
* @return server's response
*/
2020-01-14 11:10:11 +01:00
private String sendMsg(String msg) throws TransmissionError {
//TODO changer le gros try catch
try{
InetAddress dst = InetAddress.getByName(host);
byte [] buffer = msg.getBytes();
2020-01-16 13:04:22 +01:00
byte [] buffer2 = new byte[1500];
2020-01-14 11:10:11 +01:00
DatagramSocket socket = new DatagramSocket();
2020-01-16 13:04:22 +01:00
DatagramPacket reception = new DatagramPacket(buffer2, 1500);
2020-01-14 11:10:11 +01:00
DatagramPacket emission = new DatagramPacket(buffer, buffer.length, dst, UDPPort);
socket.send(emission);
socket.receive(reception);
2020-01-14 12:33:21 +01:00
return new String(reception.getData(), 0, reception.getLength());
2020-01-14 11:10:11 +01:00
} catch (Exception e){
2020-01-16 13:04:22 +01:00
System.out.println(e);
2020-01-14 11:10:11 +01:00
throw new TransmissionError();
}
}
2020-01-12 23:29:49 +01:00
}