141 lines
4.1 KiB
Java
141 lines
4.1 KiB
Java
|
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());
|
||
|
}
|