Fix #95 #111

Merged
flavien merged 1 commits from fix96 into master 5 years ago

@ -1,6 +1,7 @@
package clientP2P; package clientP2P;
import clientP2P.ClientInterface; import clientP2P.ClientInterface;
import clientP2P.ClientManagement; import clientP2P.ClientManagement;
import tools.SearchFile;
import tools.LogLevel; import tools.LogLevel;
import tools.Logger; import tools.Logger;
import java.util.Scanner; import java.util.Scanner;
@ -49,26 +50,94 @@ public class ClientInterfaceCLI extends ClientInterface {
while (isRunning) { while (isRunning) {
try { try {
int i = 1; int i = 1;
SearchFile searchEngine = new SearchFile();
int optionSearch = 0;
String searchInput = "";
String[] list = clientManagement.listDirectory(); String[] list = clientManagement.listDirectory();
System.out.println("Files present on the server:"); String[] resultArray = {};
System.out.println("R: Refresh file list"); 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");
break;
}
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"); System.out.println("0: Exit the program");
resultArray = list;
for(String listItem: list) { for(String listItem: list) {
System.out.println(i + ": " + listItem); System.out.println(i + " : " + listItem);
i++; i++;
} }
System.out.println("Type the number associated with the file to download:"); 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(); String f = scanner.nextLine();
if (f.equals("0")) { if (f.equals("0")) {
isRunning = false; isRunning = false;
} else if (f.equals("R") || f.equals("r")) { } else if (f.equals("R") || f.equals("r")) {
writeLog("File list refresh.", LogLevel.Info); writeLog("Restarting.", LogLevel.Info);
} else { } else {
try { try {
int j = Integer.parseInt(f); int j = Integer.parseInt(f);
if (j <= list.length) { if (j <= resultArray.length) {
j--; j--;
clientManagement.download(list[j]); clientManagement.download(resultArray[j]);
writeLog("File " + f + " sucessfully downloaded", LogLevel.Info); writeLog("File " + f + " sucessfully downloaded", LogLevel.Info);
} else { } else {
writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Error); writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Error);

@ -7,6 +7,7 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.net.SocketException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import localException.ProtocolError; import localException.ProtocolError;
@ -37,7 +38,7 @@ import tools.ServeErrors;
import tools.HostItem; import tools.HostItem;
import tools.Logger; import tools.Logger;
import tools.LogLevel; import tools.LogLevel;
import java.net.SocketException;
/** Implementation of P2P-JAVA-PROJECT CLIENT /** Implementation of P2P-JAVA-PROJECT CLIENT
* @author Louis Royer * @author Louis Royer

@ -0,0 +1,60 @@
package tools;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** Helper to search for files.
* @author Louis Royer
* @author Flavien Haas
* @author JS Auge
* @version 1.0
*/
public class SearchFile {
/** search correspondance of a String in a String array.
* @param filesArray is the array containing all the names of files.
* @param searchInput contain the search request.
*/
public String[] searchByName(String[] filesArray, String searchInput) {
List<String> resultList = new ArrayList<>(Arrays.asList(filesArray));
for(String listItem: filesArray) {
if(!listItem.contains(searchInput)){
resultList.remove(listItem);
}
}
String[] resultArray = new String[resultList.size()];
if(resultList.isEmpty()) {
return resultArray;
}
else {
resultArray = resultList.toArray(resultArray);
return resultArray;
}
}
/** search correspondance of a String in a the end of String in an array.
* @param filesArray is the array containing all the names of files.
* @param searchInput contain the search request.
*/
public String[] searchByExtention(String[] filesArray, String searchInput) {
List<String> resultList = new ArrayList<>(Arrays.asList(filesArray));
int i = 0;
for(String listItem: filesArray) {
if(!listItem.endsWith(searchInput)){
resultList.remove(listItem);
}
}
String[] resultArray = new String[resultList.size()];
if(resultList.isEmpty()) {
return resultArray;
}
else {
resultArray = resultList.toArray(resultArray);
return resultArray;
}
}
}
Loading…
Cancel
Save