From 1b6db26e0b0a32fdfeedf92e718794581df972f6 Mon Sep 17 00:00:00 2001 From: flavien Date: Fri, 27 Mar 2020 12:07:52 +0100 Subject: [PATCH 1/5] added functionnality to select a file to download by a number --- src/clientP2P/ClientManagement.java | 52 +++++++++++++++++++---------- src/clientP2P/ClientP2P.java | 6 ++-- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/clientP2P/ClientManagement.java b/src/clientP2P/ClientManagement.java index cf1dad8..b139f55 100644 --- a/src/clientP2P/ClientManagement.java +++ b/src/clientP2P/ClientManagement.java @@ -1,11 +1,14 @@ package clientP2P; -import tools.HostItem; -import tools.Logger; -import tools.LogLevel; + +import java.util.Arrays; import java.util.Scanner; import java.util.List; -import localException.ProtocolError; -import tools.ServeErrors; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.net.UnknownHostException; import protocolP2P.RequestResponseCode; import protocolP2P.FileList; import protocolP2P.ProtocolP2PPacket; @@ -13,6 +16,7 @@ import protocolP2P.DiscoverRequest; import protocolP2P.DiscoverResponse; import protocolP2P.Payload; import protocolP2P.HashAlgorithm; +import localException.ProtocolError; import localException.InternalError; import localException.ProtocolError; import localException.SizeError; @@ -26,14 +30,10 @@ import remoteException.NotFound; import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; import remoteException.NotATracker; -import java.io.IOException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.net.UnknownHostException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Arrays; - +import tools.ServeErrors; +import tools.HostItem; +import tools.Logger; +import tools.LogLevel; /** Implementation of P2P-JAVA-PROJECT CLIENT * @author Louis Royer @@ -49,7 +49,7 @@ public abstract class ClientManagement extends ServeErrors implements Runnable { protected Logger logger; protected Scanner scanner; protected ClientDownload downLoader; - + /** Constructor with baseDirectory, tracker, partsSubdir, logger, and scanner parameters. * @param baseDirectory the root directory where files are stored * @param tracker Tracker hostItem @@ -74,7 +74,7 @@ public abstract class ClientManagement extends ServeErrors implements Runnable { /** Getter for tracker socket */ - protected abstract Object getTrackerSocket(); + protected abstract Object getTrackerSocket(); /** Initialize hostList from tracker @@ -220,14 +220,30 @@ public abstract class ClientManagement extends ServeErrors implements Runnable { */ public void run() { try { + int i = 1; 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(listItem); + System.out.println(i + " : " + listItem); + i++; } - System.out.println("Name of the file to download:"); + System.out.println("Type the number associated with the file to download:"); String f = scanner.nextLine(); - download(f); + if(f.equals("0")){ + System.out.println("on ferme tout"); + } + else{ + int j = Integer.parseInt(f); + if(j <= list.length){ + j = j-1; + download(list[j]); + } + else{ + System.out.println("Wrong number"); + } + } + //download(f); System.out.println("File " + f + " sucessfully downloaded"); writeLog("File " + f + " sucessfully downloaded", LogLevel.Info); } catch (EmptyDirectory e) { diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 5985479..fcbe496 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -4,7 +4,6 @@ import java.util.Scanner; import java.util.List; import clientP2P.ClientManagementUDP; import clientP2P.ClientManagementTCP; - import serverP2P.ServerManagementUDP; import serverP2P.ServerManagementTCP; import tools.Logger; @@ -14,7 +13,6 @@ import tools.HostItem; import tools.ServerPortRange; import tools.TrackerPortRange; - /** Client + Server implementation. * @author Louis Royer * @author Flavien Haas @@ -31,7 +29,7 @@ public class ClientP2P { private HostItem tracker; private HostItem server; private Scanner scanner; - + /** Initialize loggers if directories and logger are null, * else fail silently. */ @@ -49,7 +47,7 @@ public class ClientP2P { * @param hostnameServer hostname to bind * @param portServer port to bind * @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) { scanner = new Scanner(System.in); -- 2.30.2 From 3d515106fc09b5370bfd5a01619f48e9f43b4492 Mon Sep 17 00:00:00 2001 From: flavien Date: Sun, 29 Mar 2020 15:02:57 +0200 Subject: [PATCH 2/5] added cli info and journalization --- src/clientP2P/ClientManagement.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/clientP2P/ClientManagement.java b/src/clientP2P/ClientManagement.java index b139f55..a9e7564 100644 --- a/src/clientP2P/ClientManagement.java +++ b/src/clientP2P/ClientManagement.java @@ -236,16 +236,16 @@ public abstract class ClientManagement extends ServeErrors implements Runnable { else{ int j = Integer.parseInt(f); if(j <= list.length){ - j = j-1; + j--; download(list[j]); + System.out.println("File " + f + " sucessfully downloaded"); + writeLog("File " + f + " sucessfully downloaded", LogLevel.Info); } else{ - System.out.println("Wrong number"); + System.out.println("File " + f + " unsucessfully downloaded, wrong number"); + writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Info); } } - //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) { -- 2.30.2 From 7af42f000fd585f175a95ddbec2db0ed33b26f2b Mon Sep 17 00:00:00 2001 From: flavien Date: Fri, 27 Mar 2020 12:07:52 +0100 Subject: [PATCH 3/5] added functionnality to select a file to download by a number --- src/clientP2P/ClientManagement.java | 53 ++++++++++++++++++----------- src/clientP2P/ClientP2P.java | 6 ++-- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/clientP2P/ClientManagement.java b/src/clientP2P/ClientManagement.java index 97f07d2..2c00d5f 100644 --- a/src/clientP2P/ClientManagement.java +++ b/src/clientP2P/ClientManagement.java @@ -1,11 +1,14 @@ package clientP2P; -import tools.HostItem; -import tools.Logger; -import tools.LogLevel; + +import java.util.Arrays; import java.util.Scanner; import java.util.List; -import localException.ProtocolError; -import tools.ServeErrors; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.net.UnknownHostException; import protocolP2P.RequestResponseCode; import protocolP2P.FileList; import protocolP2P.ProtocolP2PPacket; @@ -13,6 +16,7 @@ import protocolP2P.DiscoverRequest; import protocolP2P.DiscoverResponse; import protocolP2P.Payload; import protocolP2P.HashAlgorithm; +import localException.ProtocolError; import localException.InternalError; import localException.ProtocolError; import localException.SizeError; @@ -26,15 +30,10 @@ import remoteException.NotFound; import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; import remoteException.NotATracker; -import remoteException.UnknownHost; -import java.io.IOException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.net.UnknownHostException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Arrays; - +import tools.ServeErrors; +import tools.HostItem; +import tools.Logger; +import tools.LogLevel; /** Implementation of P2P-JAVA-PROJECT CLIENT * @author Louis Royer @@ -51,7 +50,7 @@ public abstract class ClientManagement extends ServeErrors implements Runnable { protected Logger logger; protected Scanner scanner; protected ClientDownload downLoader; - + /** Constructor with baseDirectory, tracker, partsSubdir, logger, and scanner parameters. * @param baseDirectory the root directory where files are stored * @param tracker Tracker hostItem @@ -78,7 +77,7 @@ public abstract class ClientManagement extends ServeErrors implements Runnable { /** Getter for tracker socket */ - protected abstract Object getTrackerSocket(); + protected abstract Object getTrackerSocket(); /** Initialize hostList from tracker @@ -230,14 +229,30 @@ public abstract class ClientManagement extends ServeErrors implements Runnable { */ public void run() { try { + int i = 1; 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(listItem); + System.out.println(i + " : " + listItem); + i++; } - System.out.println("Name of the file to download:"); + System.out.println("Type the number associated with the file to download:"); String f = scanner.nextLine(); - download(f); + if(f.equals("0")){ + System.out.println("on ferme tout"); + } + else{ + int j = Integer.parseInt(f); + if(j <= list.length){ + j = j-1; + download(list[j]); + } + else{ + System.out.println("Wrong number"); + } + } + //download(f); System.out.println("File " + f + " sucessfully downloaded"); writeLog("File " + f + " sucessfully downloaded", LogLevel.Info); } catch (EmptyDirectory e) { diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 15c8acc..ec63ef3 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -4,7 +4,6 @@ import java.util.Scanner; import java.util.List; import clientP2P.ClientManagementUDP; import clientP2P.ClientManagementTCP; - import serverP2P.ServerManagementUDP; import serverP2P.ServerManagementTCP; import tools.Logger; @@ -14,7 +13,6 @@ import tools.HostItem; import tools.ServerPortRange; import tools.TrackerPortRange; - /** Client + Server implementation. * @author Louis Royer * @author Flavien Haas @@ -31,7 +29,7 @@ public class ClientP2P { private HostItem tracker; private HostItem server; private Scanner scanner; - + /** Initialize loggers if directories and logger are null, * else fail silently. */ @@ -49,7 +47,7 @@ public class ClientP2P { * @param hostnameServer hostname to bind * @param portServer port to bind * @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) { scanner = new Scanner(System.in); -- 2.30.2 From ae34ca45f7129d71bd4e33b88cde791b488c0bff Mon Sep 17 00:00:00 2001 From: flavien Date: Sun, 29 Mar 2020 15:02:57 +0200 Subject: [PATCH 4/5] added cli info and journalization --- src/clientP2P/ClientManagement.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/clientP2P/ClientManagement.java b/src/clientP2P/ClientManagement.java index 2c00d5f..4e70686 100644 --- a/src/clientP2P/ClientManagement.java +++ b/src/clientP2P/ClientManagement.java @@ -245,16 +245,16 @@ public abstract class ClientManagement extends ServeErrors implements Runnable { else{ int j = Integer.parseInt(f); if(j <= list.length){ - j = j-1; + j--; download(list[j]); + System.out.println("File " + f + " sucessfully downloaded"); + writeLog("File " + f + " sucessfully downloaded", LogLevel.Info); } else{ - System.out.println("Wrong number"); + System.out.println("File " + f + " unsucessfully downloaded, wrong number"); + writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Info); } } - //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) { -- 2.30.2 From 2f924af9df462c057339ce5ca28caab090560af7 Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 30 Mar 2020 14:49:05 +0200 Subject: [PATCH 5/5] option 0 now can close the program --- src/clientP2P/ClientManagement.java | 121 ++++++++++++++-------------- 1 file changed, 62 insertions(+), 59 deletions(-) diff --git a/src/clientP2P/ClientManagement.java b/src/clientP2P/ClientManagement.java index 4e70686..9cec7bd 100644 --- a/src/clientP2P/ClientManagement.java +++ b/src/clientP2P/ClientManagement.java @@ -3,19 +3,12 @@ package clientP2P; import java.util.Arrays; import java.util.Scanner; import java.util.List; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.net.UnknownHostException; -import protocolP2P.RequestResponseCode; -import protocolP2P.FileList; -import protocolP2P.ProtocolP2PPacket; -import protocolP2P.DiscoverRequest; -import protocolP2P.DiscoverResponse; -import protocolP2P.Payload; -import protocolP2P.HashAlgorithm; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import localException.ProtocolError; import localException.InternalError; import localException.ProtocolError; @@ -23,6 +16,13 @@ import localException.SizeError; import localException.TransmissionError; import localException.VersionError; import localException.SocketClosed; +import protocolP2P.RequestResponseCode; +import protocolP2P.FileList; +import protocolP2P.ProtocolP2PPacket; +import protocolP2P.DiscoverRequest; +import protocolP2P.DiscoverResponse; +import protocolP2P.Payload; +import protocolP2P.HashAlgorithm; import remoteException.EmptyFile; import remoteException.EmptyDirectory; import remoteException.InternalRemoteError; @@ -30,6 +30,7 @@ import remoteException.NotFound; import remoteException.ProtocolRemoteError; import remoteException.VersionRemoteError; import remoteException.NotATracker; +import remoteException.UnknownHost; import tools.ServeErrors; import tools.HostItem; import tools.Logger; @@ -228,60 +229,62 @@ public abstract class ClientManagement extends ServeErrors implements Runnable { /** Implementation of Runnable */ public void run() { - try { - int i = 1; - 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")){ - System.out.println("on ferme tout"); - } - 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); + boolean isrunning = true; + while (isrunning){ + try { + int i = 1; + 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{ - System.out.println("File " + f + " unsucessfully downloaded, wrong number"); - writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Info); + 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 server’s response", LogLevel.Error); + } catch (VersionError e) { + writeLog("Server’s 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 client’s 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); } - } 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 server’s response", LogLevel.Error); - } catch (VersionError e) { - writeLog("Server’s 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 client’s 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); } } - } -- 2.30.2