package clientP2P; import clientP2P.ClientInterface; import clientP2P.ClientManagement; import tools.LogLevel; import tools.Logger; import java.util.Scanner; import localException.ProtocolError; import localException.InternalError; import localException.ProtocolError; import localException.SizeError; import localException.TransmissionError; import localException.VersionError; import localException.SocketClosed; import remoteException.EmptyFile; import remoteException.EmptyDirectory; import remoteException.InternalRemoteError; import remoteException.NotFound; import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; import remoteException.NotATracker; import remoteException.UnknownHost; import java.io.IOException; import java.net.UnknownHostException; import java.net.SocketException; /** Implementation of P2P-JAVA-PROJECT CLIENT interface for CLI * @author Louis Royer * @author Flavien Haas * @author JS Auge * @version 1.0 */ public class ClientInterfaceCLI extends ClientInterface { private Scanner scanner; /** Constructor with clientManagement, logger and scanner. * @param clientManagement ClientManagement used * @param logger Logger used * @param scanner Scanner used to read input */ public ClientInterfaceCLI(ClientManagement clientManagement, Logger logger, Scanner scanner) { super(clientManagement, logger); this.scanner = scanner; } /** Implementation of Runnable */ public void run() { boolean isRunning = true; while (isRunning) { try { int i = 1; String[] list = clientManagement.listDirectory(); System.out.println("Files present on the server:"); System.out.println("R: Refresh file list"); System.out.println("0: Exit the program"); for(String listItem: list) { System.out.println(i + ": " + listItem); i++; } System.out.println("Type the number associated with the file to download:"); String f = scanner.nextLine(); if (f.equals("0")) { isRunning = false; } else if (f.equals("R") || f.equals("r")) { writeLog("File list refresh.", LogLevel.Info); } else { try { int j = Integer.parseInt(f); if (j <= list.length) { j--; clientManagement.download(list[j]); writeLog("File " + f + " sucessfully downloaded", LogLevel.Info); } else { writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Error); } } catch (NumberFormatException e) { writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Error); } } } catch (EmptyDirectory e) { writeLog("Server has no file in directory", LogLevel.Error); } catch (InternalError e) { writeLog("Client internal error", LogLevel.Error); } catch (UnknownHostException e) { writeLog("Server host is unknown", LogLevel.Error); } catch (IOException e) { writeLog("Request cannot be send or response cannot be received", LogLevel.Error); } catch (TransmissionError e) { writeLog("Message received is too big", LogLevel.Error); } catch (ProtocolError e) { writeLog("Cannot decode server’s response", LogLevel.Error); } catch (VersionError e) { writeLog("Server’s response use bad version of the protocol", LogLevel.Error); } catch (SizeError e) { writeLog("Cannot handle this packets because of internal representation limitations of numbers on the client", LogLevel.Error); } catch (InternalRemoteError e) { writeLog("Server internal error", LogLevel.Error); } catch (ProtocolRemoteError e) { writeLog("Server cannot decode client’s request", LogLevel.Error); } catch (VersionRemoteError e) { writeLog("Server cannot decode this version of the protocol", LogLevel.Error); } catch (NotFound e) { writeLog("Server has not this file in directory", LogLevel.Error); } catch (EmptyFile e) { writeLog("File is empty", LogLevel.Error); } } writeLog("Exiting client", LogLevel.Info); } /** Initialization of hostList with retry in failure * @return true if successfully initialized */ private boolean initHostList() { boolean contacted = false; boolean firstLoop = true; boolean stop = false; while (!contacted && !stop) { try { if (!firstLoop) { writeLog("Cannot contact tracker. Try again [Y/n] ?", LogLevel.Error); String tryAgain = scanner.nextLine(); if (tryAgain.equals("n") || tryAgain.equals("N")) { stop = true; } } firstLoop = false; clientManagement.initHostList(); contacted = true; } catch (SocketException e) { } catch (UnknownHostException e) { } catch (IOException e) { } catch (Exception e) { return false; } } return !stop; } }