From 1d5a4b2028868d0818a4e5fbef5d40933bd2b09d Mon Sep 17 00:00:00 2001 From: flavien Date: Fri, 3 Apr 2020 16:37:09 +0200 Subject: [PATCH] new tools to search files --- src/clientP2P/ClientInterfaceCLI.java | 89 ++++++++++++++++++++++++--- src/clientP2P/ClientManagement.java | 3 +- src/tools/SearchFile.java | 60 ++++++++++++++++++ 3 files changed, 141 insertions(+), 11 deletions(-) create mode 100644 src/tools/SearchFile.java diff --git a/src/clientP2P/ClientInterfaceCLI.java b/src/clientP2P/ClientInterfaceCLI.java index 4361a10..b961e64 100644 --- a/src/clientP2P/ClientInterfaceCLI.java +++ b/src/clientP2P/ClientInterfaceCLI.java @@ -1,6 +1,7 @@ package clientP2P; import clientP2P.ClientInterface; import clientP2P.ClientManagement; +import tools.SearchFile; import tools.LogLevel; import tools.Logger; import java.util.Scanner; @@ -49,26 +50,94 @@ public class ClientInterfaceCLI extends ClientInterface { while (isRunning) { try { int i = 1; + SearchFile searchEngine = new SearchFile(); + int optionSearch = 0; + String searchInput = ""; 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++; + 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"); + 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(); if (f.equals("0")) { isRunning = false; } else if (f.equals("R") || f.equals("r")) { - writeLog("File list refresh.", LogLevel.Info); + writeLog("Restarting.", LogLevel.Info); } else { try { int j = Integer.parseInt(f); - if (j <= list.length) { + if (j <= resultArray.length) { j--; - clientManagement.download(list[j]); + clientManagement.download(resultArray[j]); writeLog("File " + f + " sucessfully downloaded", LogLevel.Info); } else { writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Error); diff --git a/src/clientP2P/ClientManagement.java b/src/clientP2P/ClientManagement.java index fdc7311..d47f7a3 100644 --- a/src/clientP2P/ClientManagement.java +++ b/src/clientP2P/ClientManagement.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.net.UnknownHostException; +import java.net.SocketException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import localException.ProtocolError; @@ -37,7 +38,7 @@ import tools.ServeErrors; import tools.HostItem; import tools.Logger; import tools.LogLevel; -import java.net.SocketException; + /** Implementation of P2P-JAVA-PROJECT CLIENT * @author Louis Royer diff --git a/src/tools/SearchFile.java b/src/tools/SearchFile.java new file mode 100644 index 0000000..135f239 --- /dev/null +++ b/src/tools/SearchFile.java @@ -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 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 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; + } + } +} -- 2.30.2