new tools to search files
All checks were successful
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/pr-master This commit looks good

This commit is contained in:
Flavien Haas 2020-04-03 16:37:09 +02:00 committed by Louis Royer
parent 7a263fc9d8
commit 1d5a4b2028
3 changed files with 141 additions and 11 deletions

View File

@ -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;
System.out.println("0: Exit the program"); while (!searchChoice) {
for(String listItem: list) { System.out.println("Do you want to show a list of downloadable files or search by name or extention");
System.out.println(i + ": " + listItem); System.out.println("1 : show a list of downloadable files");
i++; 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");
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:");
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);

View File

@ -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

60
src/tools/SearchFile.java Normal file
View File

@ -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;
}
}
}