Added functionnality to select a file to download by a number (#88)
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/pr-master This commit looks good Details
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/head This commit looks good Details

option 0 now can close the program

Merge branch 'listefichiers' of ssh://git.flavien.ovh:10722/flavien/Projet_JAVA_P2P_STRI2A into listefichiers

added cli info and journalization

added functionnality to select a file to download by a number

added cli info and journalization

added functionnality to select a file to download by a number

Co-authored-by: flavien <flavien.haas@outlook.fr>
Co-authored-by: js_auge <auge1@hotmail.fr>
pull/84/head
Flavien Haas 5 years ago committed by Louis Royer
parent f77ea0ae2b
commit 2cc2868450

@ -1,11 +1,21 @@
package clientP2P; package clientP2P;
import tools.HostItem;
import tools.Logger; import java.util.Arrays;
import tools.LogLevel;
import java.util.Scanner; import java.util.Scanner;
import java.util.List; import java.util.List;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import localException.ProtocolError; import localException.ProtocolError;
import tools.ServeErrors; import localException.InternalError;
import localException.ProtocolError;
import localException.SizeError;
import localException.TransmissionError;
import localException.VersionError;
import localException.SocketClosed;
import protocolP2P.RequestResponseCode; import protocolP2P.RequestResponseCode;
import protocolP2P.FileList; import protocolP2P.FileList;
import protocolP2P.ProtocolP2PPacket; import protocolP2P.ProtocolP2PPacket;
@ -13,12 +23,6 @@ import protocolP2P.DiscoverRequest;
import protocolP2P.DiscoverResponse; import protocolP2P.DiscoverResponse;
import protocolP2P.Payload; import protocolP2P.Payload;
import protocolP2P.HashAlgorithm; import protocolP2P.HashAlgorithm;
import localException.InternalError;
import localException.ProtocolError;
import localException.SizeError;
import localException.TransmissionError;
import localException.VersionError;
import localException.SocketClosed;
import remoteException.EmptyFile; import remoteException.EmptyFile;
import remoteException.EmptyDirectory; import remoteException.EmptyDirectory;
import remoteException.InternalRemoteError; import remoteException.InternalRemoteError;
@ -27,14 +31,10 @@ import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError; import remoteException.VersionRemoteError;
import remoteException.NotATracker; import remoteException.NotATracker;
import remoteException.UnknownHost; import remoteException.UnknownHost;
import java.io.IOException; import tools.ServeErrors;
import java.security.MessageDigest; import tools.HostItem;
import java.security.NoSuchAlgorithmException; import tools.Logger;
import java.net.UnknownHostException; import tools.LogLevel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
/** Implementation of P2P-JAVA-PROJECT CLIENT /** Implementation of P2P-JAVA-PROJECT CLIENT
* @author Louis Royer * @author Louis Royer
@ -51,7 +51,7 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
protected Logger logger; protected Logger logger;
protected Scanner scanner; protected Scanner scanner;
protected ClientDownload downLoader; protected ClientDownload downLoader;
/** Constructor with baseDirectory, tracker, partsSubdir, logger, and scanner parameters. /** Constructor with baseDirectory, tracker, partsSubdir, logger, and scanner parameters.
* @param baseDirectory the root directory where files are stored * @param baseDirectory the root directory where files are stored
* @param tracker Tracker hostItem * @param tracker Tracker hostItem
@ -78,7 +78,7 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
/** Getter for tracker socket /** Getter for tracker socket
*/ */
protected abstract Object getTrackerSocket(); protected abstract Object getTrackerSocket();
/** Initialize hostList from tracker /** Initialize hostList from tracker
@ -229,44 +229,62 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
/** Implementation of Runnable /** Implementation of Runnable
*/ */
public void run() { public void run() {
try { boolean isrunning = true;
String[] list = listDirectory(); while (isrunning){
System.out.println("Files present on the server:"); try {
for(String listItem: list) { int i = 1;
System.out.println(listItem); String[] list = listDirectory();
System.out.println("Files present on the server:");
System.out.println("0 : Exit the program");
for(String listItem: list) {
System.out.println(i + " : " + listItem);
i++;
}
System.out.println("Type the number associated with the file to download:");
String f = scanner.nextLine();
if(f.equals("0")){
isrunning = false;
}
else{
int j = Integer.parseInt(f);
if(j <= list.length){
j--;
download(list[j]);
System.out.println("File " + f + " sucessfully downloaded");
writeLog("File " + f + " sucessfully downloaded", LogLevel.Info);
}
else{
System.out.println("File " + f + " unsucessfully downloaded, wrong number");
writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Info);
}
}
} catch (EmptyDirectory e) {
writeLog("Server has no file in directory", 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 servers response", LogLevel.Error);
} catch (VersionError e) {
writeLog("Servers 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 clients 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);
} }
System.out.println("Name of the file to download:");
String f = scanner.nextLine();
download(f);
System.out.println("File " + f + " sucessfully downloaded");
writeLog("File " + f + " sucessfully downloaded", LogLevel.Info);
} catch (EmptyDirectory e) {
writeLog("Server has no file in directory", 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 servers response", LogLevel.Error);
} catch (VersionError e) {
writeLog("Servers 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 clients 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);
} }
} }
} }

@ -4,7 +4,6 @@ import java.util.Scanner;
import java.util.List; import java.util.List;
import clientP2P.ClientManagementUDP; import clientP2P.ClientManagementUDP;
import clientP2P.ClientManagementTCP; import clientP2P.ClientManagementTCP;
import serverP2P.ServerManagementUDP; import serverP2P.ServerManagementUDP;
import serverP2P.ServerManagementTCP; import serverP2P.ServerManagementTCP;
import tools.Logger; import tools.Logger;
@ -14,7 +13,6 @@ import tools.HostItem;
import tools.ServerPortRange; import tools.ServerPortRange;
import tools.TrackerPortRange; import tools.TrackerPortRange;
/** Client + Server implementation. /** Client + Server implementation.
* @author Louis Royer * @author Louis Royer
* @author Flavien Haas * @author Flavien Haas
@ -31,7 +29,7 @@ public class ClientP2P {
private HostItem tracker; private HostItem tracker;
private HostItem server; private HostItem server;
private Scanner scanner; private Scanner scanner;
/** Initialize loggers if directories and logger are null, /** Initialize loggers if directories and logger are null,
* else fail silently. * else fail silently.
*/ */
@ -49,7 +47,7 @@ public class ClientP2P {
* @param hostnameServer hostname to bind * @param hostnameServer hostname to bind
* @param portServer port to bind * @param portServer port to bind
* @param hostnameTracker hostname of tracker * @param hostnameTracker hostname of tracker
* @param portTracker port of tracker * @param portTracker port of tracker
*/ */
public ClientP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker) { public ClientP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker) {
scanner = new Scanner(System.in); scanner = new Scanner(System.in);

Loading…
Cancel
Save