|
|
|
@ -31,6 +31,8 @@ import clientP2P.ClientDownloadTCP;
|
|
|
|
|
import tools.HostItem;
|
|
|
|
|
import tools.Logger;
|
|
|
|
|
import tools.LogLevel;
|
|
|
|
|
import protocolP2P.DiscoverRequest;
|
|
|
|
|
import protocolP2P.DiscoverResponse;
|
|
|
|
|
|
|
|
|
|
/** Implementation of P2P-JAVA-PROJECT CLIENT
|
|
|
|
|
* @author Louis Royer
|
|
|
|
@ -42,26 +44,36 @@ public class ClientManagementTCP implements Runnable {
|
|
|
|
|
private String baseDirectory;
|
|
|
|
|
private String partsSubdir;
|
|
|
|
|
private List<HostItem> hostList;
|
|
|
|
|
private HostItem tracker;
|
|
|
|
|
private Logger logger;
|
|
|
|
|
private Scanner scanner;
|
|
|
|
|
|
|
|
|
|
/** Constructor for TCP implementation, with baseDirectory and TCPPort parameters.
|
|
|
|
|
/** Constructor for TCP implementation, with baseDirectory, tracker, partsSubdir, logger, and scanner parameters.
|
|
|
|
|
* @param baseDirectory the root directory where files are stored
|
|
|
|
|
* @param host hostname of the server
|
|
|
|
|
* @param TCPPort the server will listen on this port
|
|
|
|
|
* @param tracker Tracker hostItem
|
|
|
|
|
* @param partsSubdir subdirectory to store file parts
|
|
|
|
|
* @param logger Loggger
|
|
|
|
|
* @param scanner Scanner used to read input
|
|
|
|
|
*/
|
|
|
|
|
public ClientManagementTCP(String baseDirectory, List<HostItem> hostList, String partsSubdir, Logger logger) {
|
|
|
|
|
public ClientManagementTCP(String baseDirectory, HostItem tracker, String partsSubdir, Logger logger, Scanner scanner) {
|
|
|
|
|
this.scanner = scanner;
|
|
|
|
|
this.baseDirectory = baseDirectory;
|
|
|
|
|
this.hostList = hostList;
|
|
|
|
|
this.tracker = tracker;
|
|
|
|
|
this.partsSubdir = partsSubdir;
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
try {
|
|
|
|
|
initHostList();
|
|
|
|
|
} catch (InternalError e) {
|
|
|
|
|
System.exit(-1);
|
|
|
|
|
} catch (ProtocolError e) {
|
|
|
|
|
System.exit(-2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Implementation of Runnable
|
|
|
|
|
*/
|
|
|
|
|
public void run() {
|
|
|
|
|
try {
|
|
|
|
|
System.out.println("Enter all servers: type \"stop\" when finished");
|
|
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
|
String[] list = listDirectory();
|
|
|
|
|
System.out.println("Files present on the server:");
|
|
|
|
|
for(String listItem: list) {
|
|
|
|
@ -69,49 +81,35 @@ public class ClientManagementTCP implements Runnable {
|
|
|
|
|
}
|
|
|
|
|
System.out.println("Name of the file to download:");
|
|
|
|
|
String f = scanner.nextLine();
|
|
|
|
|
scanner.close();
|
|
|
|
|
download(f);
|
|
|
|
|
System.out.println("File " + f + " sucessfully downloaded");
|
|
|
|
|
logger.writeTCP("File " + f + " sucessfully downloaded", LogLevel.Info);
|
|
|
|
|
} catch (EmptyDirectory e) {
|
|
|
|
|
System.err.println("Error: Server has no file in directory");
|
|
|
|
|
logger.writeTCP("Error: Server has no file in directory", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Server has no file in directory", LogLevel.Error);
|
|
|
|
|
} catch (InternalError e) {
|
|
|
|
|
System.err.println("Error: Client internal error");
|
|
|
|
|
logger.writeTCP("Error: Client internal error", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Client internal error", LogLevel.Error);
|
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
|
System.err.println("Error: Server host is unknown");
|
|
|
|
|
logger.writeTCP("Error: Server host is unknown", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Server host is unknown", LogLevel.Error);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.err.println("Error: Request cannot be send or response cannot be received");
|
|
|
|
|
logger.writeTCP("Error: Request cannot be send or response cannot be received", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Request cannot be send or response cannot be received", LogLevel.Error);
|
|
|
|
|
} catch (TransmissionError e) {
|
|
|
|
|
System.err.println("Error: Message received is too big");
|
|
|
|
|
logger.writeTCP("Error: Message received is too big", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Message received is too big", LogLevel.Error);
|
|
|
|
|
} catch (ProtocolError e) {
|
|
|
|
|
System.err.println("Error: Cannot decode server’s response");
|
|
|
|
|
logger.writeTCP("Error: Cannot decode server’s response", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Cannot decode server’s response", LogLevel.Error);
|
|
|
|
|
} catch (VersionError e) {
|
|
|
|
|
System.err.println("Error: Server’s response use bad version of the protocol");
|
|
|
|
|
logger.writeTCP("Error: Server’s response use bad version of the protocol", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Server’s response use bad version of the protocol", LogLevel.Error);
|
|
|
|
|
} catch (SizeError e) {
|
|
|
|
|
System.err.println("Error: Cannot handle this packets because of internal representation limitations of numbers on the client");
|
|
|
|
|
logger.writeTCP("Error: Cannot handle this packets because of internal representation limitations of numbers on the client", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Cannot handle this packets because of internal representation limitations of numbers on the client", LogLevel.Error);
|
|
|
|
|
} catch (InternalRemoteError e) {
|
|
|
|
|
System.err.println("Error: Server internal error");
|
|
|
|
|
logger.writeTCP("Error: Server internal error", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Server internal error", LogLevel.Error);
|
|
|
|
|
} catch (ProtocolRemoteError e) {
|
|
|
|
|
System.err.println("Error: Server cannot decode client’s request");
|
|
|
|
|
logger.writeTCP("Error: Server cannot decode client’s request", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Server cannot decode client’s request", LogLevel.Error);
|
|
|
|
|
} catch (VersionRemoteError e) {
|
|
|
|
|
System.err.println("Error: Server cannot decode this version of the protocol");
|
|
|
|
|
logger.writeTCP("Error: Server cannot decode this version of the protocol", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Server cannot decode this version of the protocol", LogLevel.Error);
|
|
|
|
|
} catch (NotFound e) {
|
|
|
|
|
System.err.println("Error: Server has not this file in directory");
|
|
|
|
|
logger.writeTCP("Error: Server has not this file in directory", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Server has not this file in directory", LogLevel.Error);
|
|
|
|
|
} catch (EmptyFile e) {
|
|
|
|
|
System.err.println("Error: File is empty");
|
|
|
|
|
logger.writeTCP("Error: File is empty", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("File is empty", LogLevel.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -139,21 +137,18 @@ public class ClientManagementTCP implements Runnable {
|
|
|
|
|
if (downLoader.getSuccess()) {
|
|
|
|
|
byte[] hash512 = downLoader.getHashSum512();
|
|
|
|
|
if (!Arrays.equals(hash512, computeHashsum(filename, HashAlgorithm.SHA512))) {
|
|
|
|
|
System.err.println("Error: Hashsum does not match");
|
|
|
|
|
logger.writeTCP("Error: Hashsum does not match", LogLevel.Error);
|
|
|
|
|
System.err.println("Computed checksum:");
|
|
|
|
|
logger.writeTCP("Hashsum does not match", LogLevel.Error);
|
|
|
|
|
String line = "Computed checksum:\n";
|
|
|
|
|
byte[] c = computeHashsum(filename, HashAlgorithm.SHA512);
|
|
|
|
|
for (byte b: c) {
|
|
|
|
|
System.err.print(String.format("%02X", b));
|
|
|
|
|
logger.writeTCP("Computed checksum:" + String.format("%02X", b), LogLevel.Info);
|
|
|
|
|
line += String.format("%02X", b);
|
|
|
|
|
}
|
|
|
|
|
System.err.println("");
|
|
|
|
|
System.err.println("Received checksum:");
|
|
|
|
|
line += "\nReceived checksum:\n";
|
|
|
|
|
for (byte b: hash512) {
|
|
|
|
|
System.err.print(String.format("%02X", b));
|
|
|
|
|
logger.writeTCP("Received checksum:" + String.format("%02X", b), LogLevel.Info);
|
|
|
|
|
line += String.format("%02X", b);
|
|
|
|
|
}
|
|
|
|
|
System.err.println("");
|
|
|
|
|
line += "\n";
|
|
|
|
|
logger.writeTCP(line, LogLevel.Info);
|
|
|
|
|
throw new InternalError();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
@ -196,7 +191,6 @@ public class ClientManagementTCP implements Runnable {
|
|
|
|
|
logger.writeTCP(e, LogLevel.Error);
|
|
|
|
|
throw new ProtocolError();
|
|
|
|
|
} catch (SocketClosed e){
|
|
|
|
|
System.err.println("listDirectory : SocketClosed");
|
|
|
|
|
logger.writeTCP("listDirectory : SocketClosed", LogLevel.Error);
|
|
|
|
|
throw new ProtocolError();
|
|
|
|
|
} catch (NotATracker e) {
|
|
|
|
@ -214,12 +208,37 @@ public class ClientManagementTCP implements Runnable {
|
|
|
|
|
MessageDigest md = MessageDigest.getInstance(HashAlgorithm.SHA512.getName());
|
|
|
|
|
return md.digest(Files.readAllBytes(Paths.get(baseDirectory + filename)));
|
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
|
System.out.println("Error: " + h.getName() + " not supported");
|
|
|
|
|
logger.writeTCP("Error: " + h.getName() + " not supported", LogLevel.Error);
|
|
|
|
|
logger.writeTCP(h.getName() + " not supported", LogLevel.Error);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.out.println("Error: cannot read " + filename);
|
|
|
|
|
logger.writeTCP("Error: cannot read " + filename, LogLevel.Error);
|
|
|
|
|
logger.writeTCP("cannot read " + filename, LogLevel.Error);
|
|
|
|
|
}
|
|
|
|
|
return new byte[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Initialize hostList from tracker
|
|
|
|
|
* @throws ProtocolError
|
|
|
|
|
* @throws InternalError
|
|
|
|
|
*/
|
|
|
|
|
private void initHostList() throws ProtocolError, InternalError {
|
|
|
|
|
ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new DiscoverRequest(null));
|
|
|
|
|
try {
|
|
|
|
|
d.sendRequest((Object)tracker.getTCPSocket());
|
|
|
|
|
Payload p = d.receiveResponse().getPayload();
|
|
|
|
|
assert p instanceof DiscoverResponse : "This payload must be instance of Filelist";
|
|
|
|
|
if (!(p instanceof DiscoverResponse)) {
|
|
|
|
|
throw new InternalError();
|
|
|
|
|
} else {
|
|
|
|
|
hostList = ((DiscoverResponse)p).getHostList();
|
|
|
|
|
}
|
|
|
|
|
} catch (SocketClosed e){
|
|
|
|
|
logger.writeTCP("listDirectory : SocketClosed", LogLevel.Error);
|
|
|
|
|
throw new ProtocolError();
|
|
|
|
|
} catch (NotATracker e) {
|
|
|
|
|
logger.writeTCP(e, LogLevel.Error);
|
|
|
|
|
throw new ProtocolError();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.writeTCP(e, LogLevel.Error);
|
|
|
|
|
throw new ProtocolError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|