|
|
package clientP2P;
|
|
|
import clientP2P.ClientInterface;
|
|
|
import clientP2P.ClientManagement;
|
|
|
import tools.SearchFile;
|
|
|
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;
|
|
|
private String[] list;
|
|
|
|
|
|
/** 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 = initHostList();
|
|
|
while (isRunning) {
|
|
|
try {
|
|
|
int i = 1;
|
|
|
SearchFile searchEngine = new SearchFile();
|
|
|
int optionSearch = 0;
|
|
|
String searchInput = "";
|
|
|
if (!initFileList()) {
|
|
|
break;
|
|
|
}
|
|
|
String[] resultArray = {};
|
|
|
boolean searchChoice = false;
|
|
|
while (!searchChoice) {
|
|
|
System.out.println("Do you want to show a list of downloadable files or search by name or extention");
|
|
|
System.out.println("1 : show a list of downloadable files");
|
|
|
System.out.println("2 : search by name");
|
|
|
System.out.println("3 : search by extention");
|
|
|
try {
|
|
|
optionSearch = Integer.parseInt(scanner.nextLine());
|
|
|
} catch (NumberFormatException e) {
|
|
|
System.out.println("Wrong input, try again");
|
|
|
continue;
|
|
|
}
|
|
|
switch(optionSearch) {
|
|
|
case 1:
|
|
|
System.out.println("Files present on server(s):");
|
|
|
System.out.println("R: Restart the program");
|
|
|
System.out.println("0: Exit the program");
|
|
|
resultArray = list;
|
|
|
for(String listItem: list) {
|
|
|
System.out.println(i + " : " + listItem);
|
|
|
i++;
|
|
|
}
|
|
|
searchChoice = true;
|
|
|
break;
|
|
|
|
|
|
case 2 :
|
|
|
System.out.println("please type the term to search");
|
|
|
searchInput = scanner.nextLine();
|
|
|
resultArray = searchEngine.searchByName(list, searchInput);
|
|
|
if (resultArray.length == 0) {
|
|
|
System.out.println("Nothing found for your request.");
|
|
|
System.out.println("R: Restart the program");
|
|
|
System.out.println("0: Exit the program");
|
|
|
} else {
|
|
|
System.out.println("Result of files present on server(s):");
|
|
|
System.out.println("R: Restart the program");
|
|
|
System.out.println("0: Exit the program");
|
|
|
for(String listItem: resultArray) {
|
|
|
System.out.println(i + " : " + listItem);
|
|
|
i++;
|
|
|
}
|
|
|
}
|
|
|
searchChoice = true;
|
|
|
break;
|
|
|
|
|
|
case 3 :
|
|
|
System.out.println("Please type the term to search");
|
|
|
searchInput = scanner.nextLine();
|
|
|
resultArray = searchEngine.searchByExtention(list, searchInput);
|
|
|
if (resultArray.length == 0) {
|
|
|
System.out.println("Nothing found for your request.");
|
|
|
System.out.println("R: Restart the program");
|
|
|
System.out.println("0: Exit the program");
|
|
|
} else {
|
|
|
System.out.println("Result of files present on server(s):");
|
|
|
System.out.println("R: Restart the program");
|
|
|
System.out.println("0: Exit the program");
|
|
|
for(String listItem: resultArray) {
|
|
|
System.out.println(i + " : " + listItem);
|
|
|
i++;
|
|
|
}
|
|
|
}
|
|
|
searchChoice = true;
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
System.out.println("Wrong input, try again");
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
System.out.println("Type the number associated with the file to download / or exit the program:");
|
|
|
String f = scanner.nextLine();
|
|
|
if (f.equals("0")) {
|
|
|
isRunning = false;
|
|
|
} else if (f.equals("R") || f.equals("r")) {
|
|
|
writeLog("Restarting.", LogLevel.Info);
|
|
|
} else {
|
|
|
try {
|
|
|
int j = Integer.parseInt(f);
|
|
|
if (j <= resultArray.length) {
|
|
|
j--;
|
|
|
clientManagement.download(resultArray[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 (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;
|
|
|
}
|
|
|
|
|
|
/** Initialize file list
|
|
|
* @return true if successfully initialized
|
|
|
*/
|
|
|
private boolean initFileList() {
|
|
|
boolean contacted = false;
|
|
|
boolean firstLoop = true;
|
|
|
boolean stop = false;
|
|
|
while (!contacted && !stop) {
|
|
|
try {
|
|
|
if (!firstLoop) {
|
|
|
String tryAgain = scanner.nextLine();
|
|
|
if (tryAgain.equals("n") || tryAgain.equals("N")) {
|
|
|
stop = true;
|
|
|
}
|
|
|
}
|
|
|
firstLoop = false;
|
|
|
list = clientManagement.listDirectory();
|
|
|
contacted = true;
|
|
|
} catch(EmptyDirectory e) {
|
|
|
writeLog("Tracker has no file registered. Try again [Y/n]?", LogLevel.Error);
|
|
|
} catch(InternalError e) {
|
|
|
writeLog("Internal error while fetching tracked files. Try again [Y/n]?", LogLevel.Error);
|
|
|
} catch(Exception e) {
|
|
|
writeLog("Cannot contact tracker. Try again [Y/n]?", LogLevel.Error);
|
|
|
}
|
|
|
}
|
|
|
return !stop;
|
|
|
}
|
|
|
}
|