You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Projet_JAVA_P2P_STRI2A/src/clientP2P/ClientManagementUDP.java

174 lines
6.1 KiB
Java

package clientP2P;
import exception.InternalError;
import exception.ProtocolError;
import exception.SizeError;
import exception.TransmissionError;
import exception.VersionError;
import remoteException.EmptyDirectory;
import remoteException.InternalRemoteError;
import remoteException.NotFound;
import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.net.InetAddress;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.io.IOException;
import java.nio.file.Files;
import java.io.File;
import protocolP2P.ProtocolP2PDatagram;
import protocolP2P.Payload;
import protocolP2P.RequestResponseCode;
import protocolP2P.FileList;
import protocolP2P.FilePart;
import protocolP2P.LoadRequest;
/** Implementation of P2P-JAVA-PROJECT CLIENT
* @author Louis Royer
* @author Flavien Haas
* @author JS Auge
* @version 1.0
*/
public class ClientManagementUDP implements Runnable {
private String baseDirectory;
private int UDPPort;
private String host;
private DatagramSocket socket;
/** Constructor for UDP implementation, with baseDirectory and UDPPort parameters.
* @param baseDirectory the root directory where files are stored
* @param host hostname of the server
* @param UDPPort the server will listen on this port
*/
public ClientManagementUDP(String baseDirectory, String host, int UDPPort) {
this.baseDirectory = baseDirectory;
this.host = host;
this.UDPPort = UDPPort;
try {
socket = new DatagramSocket();
} catch (SocketException e) {
System.err.println("Error: No socket available.");
System.exit(-1);
}
}
/** Implementation of Runnable
*/
public void run() {
try {
String[] list = listDirectory();
System.out.println("Files present on the server:");
for(String listItem: list) {
System.out.println(listItem);
}
System.out.println("Name of the file to download:");
Scanner scanner = new Scanner(System.in);
String f = scanner.nextLine();
download(f);
System.out.println("File sucessfully downloaded");
} catch (EmptyDirectory e) {
System.err.println("Error: Server has no file in directory");
} catch (InternalError e) {
System.err.println("Error: Client internal error");
} catch (UnknownHostException e) {
System.err.println("Error: Server host is unknown");
} catch (IOException e) {
System.err.println("Error: Request cannot be send or response cannot be received");
} catch (TransmissionError e) {
System.err.println("Error: Message received is too big");
} catch (ProtocolError e) {
System.err.println("Error: Cannot decode servers response");
} catch (VersionError e) {
System.err.println("Error: Servers response use bad version of the protocol");
} catch (SizeError e) {
System.err.println("Error: Cannot handle this packets because of internal representation limitations of numbers on the client");
} catch (InternalRemoteError e) {
System.err.println("Error: Server internal error");
} catch (ProtocolRemoteError e) {
System.err.println("Error: Server cannot decode clients request");
} catch (VersionRemoteError e) {
System.err.println("Error: Server cannot decode this version of the protocol");
} catch (NotFound e) {
System.err.println("Error: Server have not this file in directory");
}
}
/** Try to download a file
* @param filename name of the file to download
* @throws NotFound
* @throws InternalError
* @throws UnknownHostException
* @throws IOException
* @throws TransmissionError
* @throws ProtocolError
* @throws VersionError
* @throws SizeError
* @throws InternalRemoteError
* @throws ProtocolRemoteError
* @throws VersionRemoteError
*/
private void download(String filename) throws NotFound, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError {
ProtocolP2PDatagram d = new ProtocolP2PDatagram((Payload) new LoadRequest(filename));
d.send(socket, host, UDPPort);
try {
Payload p = ProtocolP2PDatagram.receive(socket).getPayload();
assert p instanceof FilePart : "This payload must be instance of FilePart";
if (!(p instanceof FilePart)) {
5 years ago
throw new InternalError();
} else {
FilePart fp = (FilePart)p;
if (!fp.getFilename().equals(filename)) {
System.err.println("Error: wrong file received");
throw new ProtocolError();
}
if (fp.getOffset() != 0 || fp.getPartialContent().length != fp.getTotalSize()) {
System.err.println("offset: " + fp.getOffset() + " ; content.length: " + fp.getPartialContent().length + " ; totalSize: " + fp.getTotalSize());
System.err.println("Error: cannot handle partial files (not implemented)");
throw new InternalError();
}
try {
Files.write(new File(baseDirectory + filename).toPath(), fp.getPartialContent());
} catch (IOException e) {
System.err.println("Error: cannot write file (" + baseDirectory + filename + ")");
}
}
} catch (EmptyDirectory e) {
throw new ProtocolError();
}
}
/** list servers directory content
* @return list of files
* @throws InternalError
* @throws UnknowHostException
* @throws IOException
* @throws TransmissionError
* @throws ProtocolError
* @throws VersionError
* @throws SizeError
* @throws EmptyDirectory
* @throws InternalRemoteError
* @throws ProtocolRemoteError
* @throws VersionRemoteError
*/
private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError {
ProtocolP2PDatagram d = new ProtocolP2PDatagram(new Payload(RequestResponseCode.LIST_REQUEST));
d.send(socket, host, UDPPort);
try {
Payload p = ProtocolP2PDatagram.receive(socket).getPayload();
assert p instanceof FileList : "This payload must be instance of Filelist";
if (!(p instanceof FileList)) {
throw new InternalError();
} else {
return ((FileList)p).getFileList();
}
} catch (NotFound e) {
throw new ProtocolError();
}
}
}