Merge pull request 'Fix #95' (#111) from fix96 into master
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/head This commit looks good Details

pull/120/head
Flavien Haas 4 years ago
commit cd7d83fb0d

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

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

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