From 3edb0bf9e5a3ee9fe1593cdf9d1c76bd863d49f3 Mon Sep 17 00:00:00 2001 From: flavien Date: Sat, 21 Mar 2020 18:13:45 +0100 Subject: [PATCH 01/14] aide tracker --- README.md | 8 +++++- src/clientP2P/ClientP2P.java | 2 +- src/tools/TestPort.java | 27 ++++++++++++++++++ src/tracker/Tracker.java | 40 ++++++++++++++++++++------- src/tracker/TrackerManagementTCP.java | 29 +++++++++---------- 5 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 src/tools/TestPort.java diff --git a/README.md b/README.md index 3a7cac2..822bd25 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,12 @@ Lien vers le [document original](https://stri-online.net/FTLV/mod/resource/view. **But** : le but de ce projet est de créer une application répartie en Java de téléchargement de fichier en mode P2P (peer to peer ou poste à poste). Les étapes suivantes sont conseillées. +# Usage + +tracker : java -ea tracker.Tracker, to specify port (default localhost:6969, range 6000 -> 6999): java -ea trackerP2P.trackerP2P -- +serveur : java -ea serveurP2P.ServeurP2P, to specify port and tracker (default for server localhost:7070 tracker localhost:6969, range 7000 -> 7999): java -ea serveurP2P.ServeurP2P -- +client/serveur : java -ea clientP2P.ClientP2P -- + ## Étape 1 : Téléchargement à la FTP La première étape doit permettre de télécharger un fichier en intégralité d'une machine vers une autre machine de façon similaire aux applications suivant le protocole FTP. @@ -47,4 +53,4 @@ Options : - Permettre la recherche de fichiers à partir de leur nom ou de toute autre caractéristique. À l'issu de la recherche on devra pouvoir connaître un ensemble d'application possédant le fichier et commencer le téléchargement. - Gérer le protocole d'une application de téléchargement P2P existante (bittorrent, emule ou autre). -Note : toute fonctionnalité supplémentaire ne sera prise en compte dans la notation que si toutes les étapes ont été correctement traitées. \ No newline at end of file +Note : toute fonctionnalité supplémentaire ne sera prise en compte dans la notation que si toutes les étapes ont été correctement traitées. diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 28d1b67..6c2814c 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -73,7 +73,7 @@ public class ClientP2P { */ public static void main(String [] args) { if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){ - System.out.println("usage : java -ea clientP2P.ClientP2P -- "); + System.out.println("usage : java -ea clientP2P.ClientP2P -- "); } else{ ClientP2P c; diff --git a/src/tools/TestPort.java b/src/tools/TestPort.java new file mode 100644 index 0000000..9f5e351 --- /dev/null +++ b/src/tools/TestPort.java @@ -0,0 +1,27 @@ +package tools; + +/** Test ports. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ +public class TestPort { + + public boolean testPortServer(int port) { + if((port > 7000) && (port < 7999)){ + return true; + } + return false; + } + + public boolean testPortTracker(int port) { + if((port > 6000) && (port < 6999)){ + return true; + } + return false; + } + + + +} diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 8607610..b274542 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -1,9 +1,12 @@ package tracker; + +import java.util.Scanner; import tracker.TrackerManagementTCP; import tracker.TrackerManagementUDP; import tools.Directories; import tools.Logger; -import java.util.Scanner; +import tools.LogLevel; +import tools.TestPort; /** Tracker implementation * First argument of main method is port listened by the tracker, and is mandatory. @@ -16,6 +19,7 @@ public class Tracker { private int port; private Directories directories; private Logger logger; + private static final int defaultPort = 6969; /** Constructor with portStr containing a port number. * @param portStr String containing port number of listening. @@ -36,15 +40,31 @@ public class Tracker { * @param args parameters */ public static void main(String [] args) { - Tracker t = new Tracker(args[1]); - TrackerManagementUDP tmudp = new TrackerManagementUDP(t.port, t.logger); - TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.port, t.logger); - Thread tudp = new Thread(tmudp); - tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(tmtcp); - ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); - ttcp.start(); + if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){ + System.out.println("usage : java -ea trackerP2P.trackerP2P, to specify port (default 6969, range 6000 -> 6999): java -ea trackerP2P.trackerP2P -- "); + } + else{ + Tracker t; + TestPort testPortTracker = new TestPort(); + if(testPortTracker.testPortTracker(Integer.parseInt(args[1]))){ + t = new Tracker(args[1]); + } + else { + System.out.println("Wrong port (6000 -> 6999), using default port 6969"); + t = new Tracker(String.valueOf(defaultPort)); + //t.logger.writeUDP("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); + //t.logger.writeTCP("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); + } + + TrackerManagementUDP tmudp = new TrackerManagementUDP(t.port, t.logger); + TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.port, t.logger); + Thread tudp = new Thread(tmudp); + tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(tmtcp); + ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); + ttcp.start(); + } } } diff --git a/src/tracker/TrackerManagementTCP.java b/src/tracker/TrackerManagementTCP.java index d124297..064d594 100644 --- a/src/tracker/TrackerManagementTCP.java +++ b/src/tracker/TrackerManagementTCP.java @@ -1,32 +1,32 @@ package tracker; -import tools.Logger; -import tools.LogLevel; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; +import java.net.UnknownHostException; +import java.net.InetAddress; import protocolP2P.ProtocolP2PPacketTCP; import protocolP2P.ProtocolP2PPacket; import protocolP2P.RequestResponseCode; import protocolP2P.Payload; import protocolP2P.Register; import protocolP2P.Unregister; -import tools.HostItem; -import java.util.ArrayList; -import java.util.List; -import java.io.IOException; -import exception.LocalException; -import java.util.Map; -import java.util.HashMap; import protocolP2P.DiscoverRequest; import protocolP2P.DiscoverResponse; import protocolP2P.FileList; -import localException.InternalError; +import exception.LocalException; import remoteException.EmptyDirectory; -import java.net.UnknownHostException; -import java.net.InetAddress; +import localException.InternalError; import localException.SocketClosed; import tracker.TrackerManagement; - +import tools.HostItem; +import tools.Logger; +import tools.LogLevel; /** Tracker management implementation with tcp * @author Louis Royer @@ -114,6 +114,7 @@ public class TrackerManagementTCP extends TrackerManagement { } } + /** Implementation of writeLog * @param text Text to log * @param logLevel level of logging @@ -144,7 +145,7 @@ public class TrackerManagementTCP extends TrackerManagement { protected Object getHostItemSocket(HostItem hostItem) { return (Object)hostItem.getTCPSocket(); } - + /** Close HostItem socket * @param hostItem HostItem */ From 60e0e3a7dab18c4ef471f12625cdf5d75eff511e Mon Sep 17 00:00:00 2001 From: flavien Date: Sat, 21 Mar 2020 19:45:07 +0100 Subject: [PATCH 02/14] modified usage help --- README.md | 6 +++--- src/clientP2P/ClientP2P.java | 2 +- src/serverP2P/ServerP2P.java | 2 +- src/tracker/Tracker.java | 5 ++--- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 822bd25..866870f 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ Les étapes suivantes sont conseillées. # Usage -tracker : java -ea tracker.Tracker, to specify port (default localhost:6969, range 6000 -> 6999): java -ea trackerP2P.trackerP2P -- -serveur : java -ea serveurP2P.ServeurP2P, to specify port and tracker (default for server localhost:7070 tracker localhost:6969, range 7000 -> 7999): java -ea serveurP2P.ServeurP2P -- -client/serveur : java -ea clientP2P.ClientP2P -- +tracker : java tracker.Tracker or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999) +server : java serveurP2P.ServeurP2P or java serveurP2P.ServeurP2P -- (default for server localhost:7070 and tracker localhost:6969, range 7000 -> 7999) +client/serveur : java clientP2P.ClientP2P or java clientP2P.ClientP2P -- (default for integratedServer localhost:7070 and tracker localhost:6969, range 7000 -> 7999) ## Étape 1 : Téléchargement à la FTP diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 6c2814c..e0bd9e3 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -73,7 +73,7 @@ public class ClientP2P { */ public static void main(String [] args) { if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){ - System.out.println("usage : java -ea clientP2P.ClientP2P -- "); + System.out.println("usage : java clientP2P.ClientP2P or java clientP2P.ClientP2P -- (default for integratedServer localhost:7070 and tracker localhost:6969, range 7000 -> 7999)"); } else{ ClientP2P c; diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index df0a3f1..7d607b9 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -42,7 +42,7 @@ public class ServerP2P { */ public static void main(String [] args) { if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){ - System.out.println("usage : java -ea serveurP2P.ServeurP2P -- "); + System.out.println("usage : java serveurP2P.ServeurP2P or java serveurP2P.ServeurP2P -- (default for server localhost:7070 and tracker localhost:6969, range 7000 -> 7999)"); } else{ ServerP2P s = new ServerP2P(args[1], args[2], args[3]); diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index b274542..2ad06d4 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -41,7 +41,7 @@ public class Tracker { */ public static void main(String [] args) { if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){ - System.out.println("usage : java -ea trackerP2P.trackerP2P, to specify port (default 6969, range 6000 -> 6999): java -ea trackerP2P.trackerP2P -- "); + System.out.println("usage : java tracker.Tracker or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999)"); } else{ Tracker t; @@ -52,8 +52,7 @@ public class Tracker { else { System.out.println("Wrong port (6000 -> 6999), using default port 6969"); t = new Tracker(String.valueOf(defaultPort)); - //t.logger.writeUDP("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); - //t.logger.writeTCP("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); + t.logger.write("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); } TrackerManagementUDP tmudp = new TrackerManagementUDP(t.port, t.logger); From f95f4245a58214dbbf4f30f701d3c962577bdf1d Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 23 Mar 2020 16:36:35 +0100 Subject: [PATCH 03/14] simplified cli usage of tracker --- src/tracker/Tracker.java | 63 ++++++++++++++++----------- src/tracker/TrackerManagementTCP.java | 4 +- src/tracker/TrackerManagementUDP.java | 4 +- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 2ad06d4..60ce316 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -19,7 +19,6 @@ public class Tracker { private int port; private Directories directories; private Logger logger; - private static final int defaultPort = 6969; /** Constructor with portStr containing a port number. * @param portStr String containing port number of listening. @@ -36,34 +35,48 @@ public class Tracker { /** Main program entry point * first parameter is port number and is mandatory - * to test, run with: java -ea serverP2P.ServerP2P -- + * to test, run with: java -ea serverP2P.ServerP2P * @param args parameters */ public static void main(String [] args) { - if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){ - System.out.println("usage : java tracker.Tracker or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999)"); + final String defaultPort = "6969"; + final String defaultHostname = "localhost"; + Scanner scanner = new Scanner(System.in); + Tracker t; + + System.out.println("Tracker Server, enter hostname to bind : "); + String hostname = scanner.nextLine(); + if(hostname.equals("")){ + hostname = defaultHostname; + System.out.println("using default hostname : " + hostname); + } else{ + System.out.println("using hostname : " + hostname); + } + System.out.println("enter port : "); + String port = scanner.nextLine(); + if(port.equals("")){ + port = defaultPort; + System.out.println("using default port : " + port); + } else { + System.out.println("using port : " + hostname); + } + TestPort testPortTracker = new TestPort(); + if(testPortTracker.testPortTracker(Integer.parseInt(port))){ + t = new Tracker(port); + } + else { + System.out.println("Wrong port (6000 -> 6999), using default port 6969"); + t = new Tracker(defaultPort); + t.logger.write("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); } - else{ - Tracker t; - TestPort testPortTracker = new TestPort(); - if(testPortTracker.testPortTracker(Integer.parseInt(args[1]))){ - t = new Tracker(args[1]); - } - else { - System.out.println("Wrong port (6000 -> 6999), using default port 6969"); - t = new Tracker(String.valueOf(defaultPort)); - t.logger.write("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); - } - TrackerManagementUDP tmudp = new TrackerManagementUDP(t.port, t.logger); - TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.port, t.logger); - Thread tudp = new Thread(tmudp); - tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(tmtcp); - ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); - ttcp.start(); + TrackerManagementUDP tmudp = new TrackerManagementUDP(hostname, t.port, t.logger); + TrackerManagementTCP tmtcp = new TrackerManagementTCP(hostname, t.port, t.logger); + Thread tudp = new Thread(tmudp); + tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(tmtcp); + ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); + ttcp.start(); } - } - } diff --git a/src/tracker/TrackerManagementTCP.java b/src/tracker/TrackerManagementTCP.java index 064d594..afba7bd 100644 --- a/src/tracker/TrackerManagementTCP.java +++ b/src/tracker/TrackerManagementTCP.java @@ -41,8 +41,8 @@ public class TrackerManagementTCP extends TrackerManagement { * @param port Port used to listen. * @param logger Logger object */ - public TrackerManagementTCP(int port, Logger logger) { - super(new HostItem("localhost", port), logger); + public TrackerManagementTCP(String hostname, int port, Logger logger) { + super(new HostItem(hostname, port), logger); try { socket = new ServerSocket(tracker.getPort(), 10, tracker.getInetAddress()); } catch (SocketException e) { diff --git a/src/tracker/TrackerManagementUDP.java b/src/tracker/TrackerManagementUDP.java index 6ed2235..6a07bcb 100644 --- a/src/tracker/TrackerManagementUDP.java +++ b/src/tracker/TrackerManagementUDP.java @@ -37,8 +37,8 @@ public class TrackerManagementUDP extends TrackerManagement { * @param port Port used to listen. * @param logger Logger object */ - public TrackerManagementUDP(int port, Logger logger) { - super(new HostItem("localhost", port), logger); + public TrackerManagementUDP(String hostname, int port, Logger logger) { + super(new HostItem(hostname, port), logger); try { socket = new DatagramSocket(tracker.getPort(), tracker.getInetAddress()); } catch (SocketException e) { From 2313b6799d309259d5c34d6b721292b9e3ad45a0 Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 23 Mar 2020 16:42:23 +0100 Subject: [PATCH 04/14] added commentary on default options --- src/tracker/Tracker.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 60ce316..1dc03cd 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -44,7 +44,7 @@ public class Tracker { Scanner scanner = new Scanner(System.in); Tracker t; - System.out.println("Tracker Server, enter hostname to bind : "); + System.out.println("Tracker Server, enter hostname to bind (default = localhost): "); String hostname = scanner.nextLine(); if(hostname.equals("")){ hostname = defaultHostname; @@ -52,7 +52,7 @@ public class Tracker { } else{ System.out.println("using hostname : " + hostname); } - System.out.println("enter port : "); + System.out.println("enter port (default = 6969): "); String port = scanner.nextLine(); if(port.equals("")){ port = defaultPort; From 90872c8f7b697d5c0f520dc6934d1ad1c2f7ff77 Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 23 Mar 2020 16:55:46 +0100 Subject: [PATCH 05/14] changed a variable in display --- src/tracker/Tracker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 1dc03cd..364ab73 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -58,7 +58,7 @@ public class Tracker { port = defaultPort; System.out.println("using default port : " + port); } else { - System.out.println("using port : " + hostname); + System.out.println("using port : " + port); } TestPort testPortTracker = new TestPort(); if(testPortTracker.testPortTracker(Integer.parseInt(port))){ From 226520e209aad5c5347e0fa5def4c06f19a5582a Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 23 Mar 2020 17:15:03 +0100 Subject: [PATCH 06/14] simplified cli usage of server --- src/serverP2P/ServerP2P.java | 78 +++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 7d607b9..82dead1 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -1,10 +1,13 @@ package serverP2P; + +import java.util.Scanner; import serverP2P.ServerManagementUDP; import serverP2P.ServerManagementTCP; import tools.Directories; import tools.Logger; import tools.HostItem; -import java.util.Scanner; +import tools.TestPort; + /** Server only implementation * First argument of main method is port listened by the server, and is mandatory. @@ -37,23 +40,70 @@ public class ServerP2P { /** Main program entry point * first parameter is port number and is mandatory - * to test, run with: java -ea serverP2P.ServerP2P -- + * to test, run with: java -ea serverP2P.ServerP2P * @param args parameters */ public static void main(String [] args) { - if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){ - System.out.println("usage : java serveurP2P.ServeurP2P or java serveurP2P.ServeurP2P -- (default for server localhost:7070 and tracker localhost:6969, range 7000 -> 7999)"); + final String defaultHostname = "localhost"; + final String defaultPortServer = "7070"; + final String defaultPortTracker = "6969"; + Scanner scanner = new Scanner(System.in); + TestPort testPort = new TestPort(); + + System.out.println("Server, enter hostname to bind (default = localhost): "); + String hostnameServer = scanner.nextLine(); + if(hostnameServer.equals("")){ + hostnameServer = defaultHostname; + System.out.println("using default hostname : " + hostnameServer); + } else{ + System.out.println("using hostname : " + hostnameServer); + } + System.out.println("enter port (default = 7070): "); + String portServer = scanner.nextLine(); + if(portServer.equals("")){ + portServer = defaultPortServer; + System.out.println("using default port : " + portServer); } - else{ - ServerP2P s = new ServerP2P(args[1], args[2], args[3]); - ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, "localhost", s.port, s.logger, s.tracker); - ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, "localhost", s.port, s.logger, s.tracker); - Thread tudp = new Thread(smudp); - tudp.setName("server UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(smtcp); - ttcp.setName("server TCP P2P-JAVA-PROJECT"); - ttcp.start(); + else { + if(!testPort.testPortServer(Integer.parseInt(portServer))){ + System.out.println("Wrong port (7000 -> 7999), using default port 7070"); + portServer = defaultPortServer; + } else { + System.out.println("using port : " + portServer); + } } + + System.out.println("enter hostname of tracker (default = localhost): "); + String hostnameTracker = scanner.nextLine(); + if(hostnameTracker.equals("")){ + hostnameTracker = defaultHostname; + System.out.println("tracker default hostname : " + hostnameTracker); + } else{ + System.out.println("tracker hostname : " + hostnameTracker); + } + System.out.println("enter tracker's port (default = 6969): "); + String portTracker = scanner.nextLine(); + if(portTracker.equals("")){ + portTracker = defaultPortTracker; + System.out.println("using default port : " + portTracker); + } + else { + if(!testPort.testPortTracker(Integer.parseInt(portTracker))){ + System.out.println("Wrong port (6000 -> 6999), using default port 6969"); + portTracker = defaultPortTracker; + } else { + System.out.println("using port : " + portTracker); + } + } + + ServerP2P s = new ServerP2P(portServer, hostnameTracker, portTracker); + ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.port, s.logger, s.tracker); + ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.port, s.logger, s.tracker); + Thread tudp = new Thread(smudp); + tudp.setName("server UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(smtcp); + ttcp.setName("server TCP P2P-JAVA-PROJECT"); + ttcp.start(); } } From a0cbd9ef1423b6fd4ed2e109a3e66dc15a088b4a Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 23 Mar 2020 17:18:59 +0100 Subject: [PATCH 07/14] updated usage of programs in readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 866870f..4c5d6d8 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ Les étapes suivantes sont conseillées. # Usage -tracker : java tracker.Tracker or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999) -server : java serveurP2P.ServeurP2P or java serveurP2P.ServeurP2P -- (default for server localhost:7070 and tracker localhost:6969, range 7000 -> 7999) -client/serveur : java clientP2P.ClientP2P or java clientP2P.ClientP2P -- (default for integratedServer localhost:7070 and tracker localhost:6969, range 7000 -> 7999) +tracker : java tracker.Tracker +server : java serveurP2P.ServeurP2P +client/serveur : java clientP2P.ClientP2P ## Étape 1 : Téléchargement à la FTP From 9c5bfc5430ac353fe5d4be620f73ea6de24f7110 Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 23 Mar 2020 18:07:26 +0100 Subject: [PATCH 08/14] added a method to test if hostnames are legitimate --- src/tools/TestLegitIP.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/tools/TestLegitIP.java diff --git a/src/tools/TestLegitIP.java b/src/tools/TestLegitIP.java new file mode 100644 index 0000000..aa271f3 --- /dev/null +++ b/src/tools/TestLegitIP.java @@ -0,0 +1,28 @@ +package tools; + +/** Test if IP are legitimate. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ +public class TestLegitIP { + + public boolean TestIP(String hostname) { + if(hostname.equals("localhost")){ + return true; + } + else{ + String[] ipArray = hostname.split("\\."); + if(ipArray.length != 4){ + return false; + } + for(int i= 0; i < ipArray.length; i++){ + if((Integer.parseInt(ipArray[i]) > 255) || (Integer.parseInt(ipArray[i]) < 0)){ + return false; + } + } + } + return true; + } +} From d0eff567f54d9edf7dcc421c111c131cee8e3340 Mon Sep 17 00:00:00 2001 From: flavien Date: Mon, 23 Mar 2020 18:08:21 +0100 Subject: [PATCH 09/14] added support for tools/testlegitip --- src/tracker/Tracker.java | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 364ab73..7923024 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -7,6 +7,7 @@ import tools.Directories; import tools.Logger; import tools.LogLevel; import tools.TestPort; +import tools.TestLegitIP; /** Tracker implementation * First argument of main method is port listened by the tracker, and is mandatory. @@ -42,16 +43,20 @@ public class Tracker { final String defaultPort = "6969"; final String defaultHostname = "localhost"; Scanner scanner = new Scanner(System.in); + TestLegitIP testLegitIP = new TestLegitIP(); + TestPort testPortTracker = new TestPort(); Tracker t; - System.out.println("Tracker Server, enter hostname to bind (default = localhost): "); - String hostname = scanner.nextLine(); - if(hostname.equals("")){ - hostname = defaultHostname; - System.out.println("using default hostname : " + hostname); - } else{ - System.out.println("using hostname : " + hostname); - } + do{ + System.out.println("Tracker Server, enter hostname to bind (default = localhost): "); + String hostname = scanner.nextLine(); + if(hostname.equals("")){ + hostname = defaultHostname; + System.out.println("using default hostname : " + hostname); + } + } while (!testLegitIP.TestIP(hostname)) + System.out.println("using hostname : " + hostname); + System.out.println("enter port (default = 6969): "); String port = scanner.nextLine(); if(port.equals("")){ @@ -60,7 +65,6 @@ public class Tracker { } else { System.out.println("using port : " + port); } - TestPort testPortTracker = new TestPort(); if(testPortTracker.testPortTracker(Integer.parseInt(port))){ t = new Tracker(port); } From d4bcff6f8c80f03e4a4dc91f67202aa2115245ec Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 24 Mar 2020 11:32:42 +0100 Subject: [PATCH 10/14] retrieve support for agurments --- src/tools/TestLegitIP.java | 28 -------------- src/tracker/Tracker.java | 78 +++++++++++++++++++++----------------- 2 files changed, 44 insertions(+), 62 deletions(-) delete mode 100644 src/tools/TestLegitIP.java diff --git a/src/tools/TestLegitIP.java b/src/tools/TestLegitIP.java deleted file mode 100644 index aa271f3..0000000 --- a/src/tools/TestLegitIP.java +++ /dev/null @@ -1,28 +0,0 @@ -package tools; - -/** Test if IP are legitimate. - * @author Louis Royer - * @author Flavien Haas - * @author JS Auge - * @version 1.0 - */ -public class TestLegitIP { - - public boolean TestIP(String hostname) { - if(hostname.equals("localhost")){ - return true; - } - else{ - String[] ipArray = hostname.split("\\."); - if(ipArray.length != 4){ - return false; - } - for(int i= 0; i < ipArray.length; i++){ - if((Integer.parseInt(ipArray[i]) > 255) || (Integer.parseInt(ipArray[i]) < 0)){ - return false; - } - } - } - return true; - } -} diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 7923024..b81a671 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -7,7 +7,6 @@ import tools.Directories; import tools.Logger; import tools.LogLevel; import tools.TestPort; -import tools.TestLegitIP; /** Tracker implementation * First argument of main method is port listened by the tracker, and is mandatory. @@ -43,44 +42,55 @@ public class Tracker { final String defaultPort = "6969"; final String defaultHostname = "localhost"; Scanner scanner = new Scanner(System.in); - TestLegitIP testLegitIP = new TestLegitIP(); TestPort testPortTracker = new TestPort(); + String hostname; + String port; Tracker t; - do{ - System.out.println("Tracker Server, enter hostname to bind (default = localhost): "); - String hostname = scanner.nextLine(); - if(hostname.equals("")){ - hostname = defaultHostname; - System.out.println("using default hostname : " + hostname); - } - } while (!testLegitIP.TestIP(hostname)) - System.out.println("using hostname : " + hostname); + System.out.println("args.lenght : " + args.length); - System.out.println("enter port (default = 6969): "); - String port = scanner.nextLine(); - if(port.equals("")){ - port = defaultPort; - System.out.println("using default port : " + port); - } else { - System.out.println("using port : " + port); - } - if(testPortTracker.testPortTracker(Integer.parseInt(port))){ - t = new Tracker(port); - } - else { - System.out.println("Wrong port (6000 -> 6999), using default port 6969"); - t = new Tracker(defaultPort); - t.logger.write("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); + if ((args.length != 3) && (args.length != 0)){ + System.out.println("usage : java tracker.Tracker or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999)"); } + else{ + if(args.length == 3){ + hostname = args[1]; + port = args[2]; + } + else{ + System.out.println("Tracker Server, enter hostname to bind (default = localhost): "); + hostname = scanner.nextLine(); + if(hostname.equals("")){ + hostname = defaultHostname; + System.out.println("using default hostname : " + hostname); + } + System.out.println("enter port (default = 6969): "); + port = scanner.nextLine(); + if(port.equals("")){ + port = defaultPort; + System.out.println("using default port : " + port); + } + } - TrackerManagementUDP tmudp = new TrackerManagementUDP(hostname, t.port, t.logger); - TrackerManagementTCP tmtcp = new TrackerManagementTCP(hostname, t.port, t.logger); - Thread tudp = new Thread(tmudp); - tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(tmtcp); - ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); - ttcp.start(); + System.out.println("using hostname : " + hostname); + if(testPortTracker.testPortTracker(Integer.parseInt(port))){ + System.out.println("using port : " + port); + t = new Tracker(port); + } + else { + System.out.println("Wrong port (6000 -> 6999), using default port 6969"); + t = new Tracker(defaultPort); + t.logger.write("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); + } + + TrackerManagementUDP tmudp = new TrackerManagementUDP(hostname, t.port, t.logger); + TrackerManagementTCP tmtcp = new TrackerManagementTCP(hostname, t.port, t.logger); + Thread tudp = new Thread(tmudp); + tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(tmtcp); + ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); + ttcp.start(); + } } } From 702fb399c38b3d9c0f044bca61fb85718461f7bb Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 24 Mar 2020 12:11:06 +0100 Subject: [PATCH 11/14] return testport in method --- src/tools/TestPort.java | 11 +++++++---- src/tracker/Tracker.java | 10 ++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/tools/TestPort.java b/src/tools/TestPort.java index 9f5e351..8a31892 100644 --- a/src/tools/TestPort.java +++ b/src/tools/TestPort.java @@ -1,5 +1,7 @@ package tools; +import javafx.util.Pair; + /** Test ports. * @author Louis Royer * @author Flavien Haas @@ -8,18 +10,19 @@ package tools; */ public class TestPort { - public boolean testPortServer(int port) { + public boolean testPortServer(int port, String message) { if((port > 7000) && (port < 7999)){ return true; } + message = message + "Wrong port (7000 -> 7999), using default port 7070"; return false; } - public boolean testPortTracker(int port) { + public Pair testPortTracker(int port) { if((port > 6000) && (port < 6999)){ - return true; + return new Pair<>(0,""); } - return false; + return new Pair<>(1, "Wrong port (6000 -> 6999), using default port 6969"); } diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index b81a671..53658e1 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -1,6 +1,7 @@ package tracker; import java.util.Scanner; +import javafx.util.Pair; import tracker.TrackerManagementTCP; import tracker.TrackerManagementUDP; import tools.Directories; @@ -42,7 +43,7 @@ public class Tracker { final String defaultPort = "6969"; final String defaultHostname = "localhost"; Scanner scanner = new Scanner(System.in); - TestPort testPortTracker = new TestPort(); + TestPort testPort = new TestPort(); String hostname; String port; Tracker t; @@ -73,14 +74,15 @@ public class Tracker { } System.out.println("using hostname : " + hostname); - if(testPortTracker.testPortTracker(Integer.parseInt(port))){ + Pair PtestPortTracker = testPort.testPortTracker(Integer.parseInt(port)); + if(PtestPortTracker.getKey() == 0){ System.out.println("using port : " + port); t = new Tracker(port); } else { - System.out.println("Wrong port (6000 -> 6999), using default port 6969"); + System.out.println(PtestPortTracker.getValue()); t = new Tracker(defaultPort); - t.logger.write("Wrong port (6000 -> 6999), using default port 6969", LogLevel.Warning); + t.logger.write(PtestPortTracker.getValue(), LogLevel.Warning); } TrackerManagementUDP tmudp = new TrackerManagementUDP(hostname, t.port, t.logger); From 387a487d8370f1b6c54fc0f173d8a177af9b50ee Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 24 Mar 2020 14:00:24 +0100 Subject: [PATCH 12/14] reintroduce usage of arguments in serverP2P --- src/serverP2P/ServerP2P.java | 115 ++++++++++++++++++++--------------- src/tools/TestPort.java | 7 +-- src/tracker/Tracker.java | 4 +- 3 files changed, 70 insertions(+), 56 deletions(-) diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 82dead1..04b3fbc 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -1,6 +1,7 @@ package serverP2P; import java.util.Scanner; +import javafx.util.Pair; import serverP2P.ServerManagementUDP; import serverP2P.ServerManagementTCP; import tools.Directories; @@ -47,63 +48,79 @@ public class ServerP2P { final String defaultHostname = "localhost"; final String defaultPortServer = "7070"; final String defaultPortTracker = "6969"; + String hostnameServer = ""; + String portServer = ""; + String hostnameTracker = ""; + String portTracker = ""; Scanner scanner = new Scanner(System.in); TestPort testPort = new TestPort(); - System.out.println("Server, enter hostname to bind (default = localhost): "); - String hostnameServer = scanner.nextLine(); - if(hostnameServer.equals("")){ - hostnameServer = defaultHostname; - System.out.println("using default hostname : " + hostnameServer); - } else{ - System.out.println("using hostname : " + hostnameServer); - } - System.out.println("enter port (default = 7070): "); - String portServer = scanner.nextLine(); - if(portServer.equals("")){ - portServer = defaultPortServer; - System.out.println("using default port : " + portServer); + if ((args.length != 5) && (args.length != 0)){ + System.out.println("usage : java serveurP2P.ServeurP2P (interactive) or java serveurP2P.ServeurP2P -- "); } - else { - if(!testPort.testPortServer(Integer.parseInt(portServer))){ - System.out.println("Wrong port (7000 -> 7999), using default port 7070"); - portServer = defaultPortServer; - } else { - System.out.println("using port : " + portServer); + else{ + if(args.length == 5){ + hostnameServer = args[1]; + portServer = args[2]; + hostnameTracker = args[3]; + portTracker = args[4]; + } + else{ + System.out.println("Server, enter hostname to bind (default = localhost): "); + hostnameServer = scanner.nextLine(); + if(hostnameServer.equals("")){ + hostnameServer = defaultHostname; + System.out.println("using default hostname : " + hostnameServer); + } + System.out.println("enter port (default = 7070): "); + portServer = scanner.nextLine(); + if(portServer.equals("")){ + portServer = defaultPortServer; + System.out.println("using default port : " + portServer); + } + System.out.println("enter hostname of tracker (default = localhost): "); + hostnameTracker = scanner.nextLine(); + if(hostnameTracker.equals("")){ + hostnameTracker = defaultHostname; + System.out.println("tracker default hostname : " + hostnameTracker); + } + System.out.println("enter tracker's port (default = 6969): "); + portTracker = scanner.nextLine(); + if(portTracker.equals("")){ + portTracker = defaultPortTracker; + System.out.println("using default port : " + portTracker); + } } - } - System.out.println("enter hostname of tracker (default = localhost): "); - String hostnameTracker = scanner.nextLine(); - if(hostnameTracker.equals("")){ - hostnameTracker = defaultHostname; - System.out.println("tracker default hostname : " + hostnameTracker); - } else{ + System.out.println("using hostname : " + hostnameServer); + Pair PtestPortServer = testPort.testPortServer(Integer.parseInt(portServer)); + if(PtestPortServer.getKey() == 0){ + System.out.println("using port : " + portServer); + } + else { + System.out.println(PtestPortServer.getValue()); + portServer = defaultPortServer; + } System.out.println("tracker hostname : " + hostnameTracker); - } - System.out.println("enter tracker's port (default = 6969): "); - String portTracker = scanner.nextLine(); - if(portTracker.equals("")){ - portTracker = defaultPortTracker; - System.out.println("using default port : " + portTracker); - } - else { - if(!testPort.testPortTracker(Integer.parseInt(portTracker))){ - System.out.println("Wrong port (6000 -> 6999), using default port 6969"); - portTracker = defaultPortTracker; - } else { + + Pair PtestPortTracker = testPort.testPortTracker(Integer.parseInt(portTracker)); + if(PtestPortTracker.getKey() == 0){ System.out.println("using port : " + portTracker); } - } + else { + System.out.println(PtestPortTracker.getValue()); + portTracker = defaultPortTracker; + } - ServerP2P s = new ServerP2P(portServer, hostnameTracker, portTracker); - ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.port, s.logger, s.tracker); - ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.port, s.logger, s.tracker); - Thread tudp = new Thread(smudp); - tudp.setName("server UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(smtcp); - ttcp.setName("server TCP P2P-JAVA-PROJECT"); - ttcp.start(); - } + ServerP2P s = new ServerP2P(portServer, hostnameTracker, portTracker); + ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.port, s.logger, s.tracker); + ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.port, s.logger, s.tracker); + Thread tudp = new Thread(smudp); + tudp.setName("server UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(smtcp); + ttcp.setName("server TCP P2P-JAVA-PROJECT"); + ttcp.start(); + } + } } diff --git a/src/tools/TestPort.java b/src/tools/TestPort.java index 8a31892..ae71813 100644 --- a/src/tools/TestPort.java +++ b/src/tools/TestPort.java @@ -10,12 +10,11 @@ import javafx.util.Pair; */ public class TestPort { - public boolean testPortServer(int port, String message) { + public Pair testPortServer(int port) { if((port > 7000) && (port < 7999)){ - return true; + return new Pair<>(0,""); } - message = message + "Wrong port (7000 -> 7999), using default port 7070"; - return false; + return new Pair<>(1, "Wrong port (7000 -> 7999), using default port 7070"); } public Pair testPortTracker(int port) { diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 53658e1..787923c 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -48,10 +48,8 @@ public class Tracker { String port; Tracker t; - System.out.println("args.lenght : " + args.length); - if ((args.length != 3) && (args.length != 0)){ - System.out.println("usage : java tracker.Tracker or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999)"); + System.out.println("usage : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999)"); } else{ if(args.length == 3){ From 64032e84c435d0261e443089bad941e0dfe5c697 Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 24 Mar 2020 16:04:10 +0100 Subject: [PATCH 13/14] finished work --- README.md | 6 +- src/clientP2P/ClientP2P.java | 141 +++++++++++++++++++++++------------ src/serverP2P/ServerP2P.java | 26 +++---- src/tools/TestPort.java | 10 ++- src/tracker/Tracker.java | 7 +- 5 files changed, 121 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 4c5d6d8..00e9a8c 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ Les étapes suivantes sont conseillées. # Usage -tracker : java tracker.Tracker -server : java serveurP2P.ServeurP2P -client/serveur : java clientP2P.ClientP2P +tracker : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- (default port 6969 (range 6000 -> 6999)) +server : java serveurP2P.ServeurP2P (interactive) or java serveurP2P.ServeurP2P -- (default server port: server 7070 (range 7000->7070) and tracker port 6969 (range 7000 -> 7999)) +client/serveur : java clientP2P.ClientP2P or java clientP2P.ClientP2P -- (default tracker port 6969 (range 7000 -> 7999) and server port: server 7070 (range 7000->7070)) ## Étape 1 : Téléchargement à la FTP diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index e0bd9e3..c42aa9a 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -2,14 +2,17 @@ package clientP2P; import java.util.Scanner; import java.util.List; +import javafx.util.Pair; import clientP2P.ClientManagementUDP; import clientP2P.ClientManagementTCP; + import serverP2P.ServerManagementUDP; import serverP2P.ServerManagementTCP; import tools.Logger; import tools.LogLevel; import tools.Directories; import tools.HostItem; +import tools.TestPort; /** Client + Server implementation. * @author Louis Royer @@ -23,20 +26,17 @@ public class ClientP2P { private String partsDir = ".parts/"; private Logger loggerServer; private Logger loggerClient; - private String host; - private int port; + private int portServer; private Directories directories; - private static final int defaultPort = 20000; private HostItem tracker; private Scanner scanner; - /** Initialize loggers if directories and logger are null, * else fail silently. */ public void initDirectoriesAndLoggers() { if (directories == null && loggerServer == null && loggerClient == null) { - directories = new Directories("P2P_JAVA_PROJECT_" + port); + directories = new Directories("P2P_JAVA_PROJECT_" + portServer); directories.createSubdir(logDir); loggerServer = new Logger(directories.getDataHomeDirectory() + logDir + "server.log"); loggerClient = new Logger(directories.getDataHomeDirectory() + logDir + "client.log"); @@ -44,26 +44,16 @@ public class ClientP2P { } } - /** Constructor with portStr as parameter. - * @param portStr String containing port for server listenning. + /** Constructor with portServerstr as parameter. + * @param portServerstr String containing port for server listenning. */ - public ClientP2P(String portStr) { + public ClientP2P(String portServerstr, String hostnameTracker, String portTracker) { scanner = new Scanner(System.in); - tracker = new HostItem("localhost", 30000); // TODO : make it configurable - try{ - port = Integer.valueOf(Integer.parseInt(portStr)); - } catch (NumberFormatException e){ - int oldPort = port; - port = defaultPort; - initDirectoriesAndLoggers(); - System.err.println("Error incorrect port " + oldPort + " using default port " + defaultPort); - loggerServer.write("incorrect port " + oldPort + " using default port " + defaultPort, LogLevel.Info); - } + portServer = Integer.valueOf(Integer.parseInt(portServerstr)); + tracker = new HostItem(hostnameTracker, Integer.valueOf(Integer.parseInt(portTracker))); initDirectoriesAndLoggers(); - host = "localhost"; - System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory()); + System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory()); directories.askOpenDataHomeDirectory(null, scanner); - System.out.println("Please enter list of servers to use; first one will be used to ask list of files"); } /** Main program entry point. @@ -72,25 +62,86 @@ public class ClientP2P { * @param args server listenning port */ public static void main(String [] args) { - if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){ - System.out.println("usage : java clientP2P.ClientP2P or java clientP2P.ClientP2P -- (default for integratedServer localhost:7070 and tracker localhost:6969, range 7000 -> 7999)"); + final String defaultHostname = "localhost"; + final String defaultPortServer = "7070"; + final String defaultPortTracker = "6969"; + String hostnameServer = ""; + String portServer = ""; + String hostnameTracker = ""; + String portTracker = ""; + String protocolClient = ""; + Scanner scanner = new Scanner(System.in); + TestPort testPort = new TestPort(); + + if ((args.length != 6) && (args.length != 0)){ + System.out.println("usage : java clientP2P.ClientP2P or java clientP2P.ClientP2P -- (default tracker port 6969 (range 7000 -> 7999) and server port: server 7070 (range 7000->7070))"); } else{ - ClientP2P c; - try { - c = new ClientP2P(args[1]); - } catch (IndexOutOfBoundsException e){ - c = new ClientP2P("" + defaultPort); + if(args.length == 6){ + protocolClient = args[1]; + hostnameServer = args[2]; + portServer = args[3]; + hostnameTracker = args[4]; + portTracker = args[5]; + } + else{ + System.out.println("Client, wich transport protocol do you want to use (default = TCP): "); + protocolClient = scanner.nextLine(); + System.out.println("server side, enter hostname to bind (default = localhost): "); + hostnameServer = scanner.nextLine(); + if(hostnameServer.equals("")){ + hostnameServer = defaultHostname; + System.out.println("using default hostname : " + hostnameServer); + } + System.out.println("enter port (default = 7070): "); + portServer = scanner.nextLine(); + if(portServer.equals("")){ + portServer = defaultPortServer; + System.out.println("using default port : " + portServer); + } + System.out.println("enter hostname of tracker (default = localhost): "); + hostnameTracker = scanner.nextLine(); + if(hostnameTracker.equals("")){ + hostnameTracker = defaultHostname; + System.out.println("tracker default hostname : " + hostnameTracker); + } + System.out.println("enter tracker's port (default = 6969): "); + portTracker = scanner.nextLine(); + if(portTracker.equals("")){ + portTracker = defaultPortTracker; + System.out.println("using default port : " + portTracker); + } } - // Server threads - ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), "localhost", c.port, c.loggerServer, c.tracker); - ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), "localhost", c.port, c.loggerServer, c.tracker); + System.out.println("using hostname : " + hostnameServer); + Pair PtestPortServer = testPort.testPortServer(Integer.parseInt(portServer)); + if(PtestPortServer.getKey() == 0){ + System.out.println("using port : " + portServer); + } + else { + System.out.println(PtestPortServer.getValue()); + portServer = defaultPortServer; + } + System.out.println("tracker hostname : " + hostnameTracker); + + Pair PtestPortTracker = testPort.testPortTracker(Integer.parseInt(portTracker)); + if(PtestPortTracker.getKey() == 0){ + System.out.println("using port : " + portTracker); + } + else { + System.out.println(PtestPortTracker.getValue()); + portTracker = defaultPortTracker; + } + + ClientP2P c = new ClientP2P(portServer, hostnameTracker, portTracker); + + ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), hostnameServer, c.portServer, c.loggerServer, c.tracker); + ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), hostnameServer, c.portServer, c.loggerServer, c.tracker); Thread tudp = new Thread(smudp); - tudp.setName("server UDP P2P-JAVA-PROJECT (port: " + c.port + ")"); + tudp.setName("server UDP P2P-JAVA-PROJECT"); tudp.start(); Thread ttcp = new Thread(smtcp); - ttcp.setName("server TCP P2P-JAVA-PROJECT (port: " + c.port + ")"); + ttcp.setName("server TCP P2P-JAVA-PROJECT"); ttcp.start(); // Wait a bit before printing client interface @@ -101,19 +152,15 @@ public class ClientP2P { Thread.currentThread().interrupt(); } - // initialize Host lists - System.out.println("Client : Which transport protocol do you want to use? [TCP/udp]"); - c.scanner.hasNextLine(); - String transportchoosen = c.scanner.nextLine(); - Thread t; - switch(transportchoosen){ + Thread tclient; + switch(protocolClient){ case "UDP": case "udp": - case "upd": // alias typo + case "upd": case "2" : System.out.println("Starting with UDP"); ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner); - t = new Thread(cmudp); + tclient = new Thread(cmudp); break; case "TCP": case "tcp": @@ -121,18 +168,16 @@ public class ClientP2P { default: System.out.println("Starting with TCP"); ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner); - t = new Thread(cmtcp); + tclient = new Thread(cmtcp); break; - } - t.setName("client P2P-JAVA-PROJECT"); - t.start(); + } + tclient.setName("client P2P-JAVA-PROJECT"); + tclient.start(); try { - t.join(); + tclient.join(); } catch (InterruptedException e) {} - c.scanner.close(); smudp.setStop(); smtcp.setStop(); - } } } diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 04b3fbc..5228a86 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -18,30 +18,30 @@ import tools.TestPort; * @version 1.0 */ public class ServerP2P { - private int port; + private int portServer; private Directories directories; static private final String subdir = "seeded/"; private Logger logger; private HostItem tracker; - /** Constructor with portStr containing a port number. - * @param portStr String containing port number of listening. + /** Constructor with portServerstr containing a port number. + * @param portServerstr String containing port number of listening. */ - public ServerP2P(String portStr, String trackerHostname, String trackerPortStr) { - port = Integer.valueOf(Integer.parseInt(portStr)); - tracker = new HostItem(trackerHostname, Integer.valueOf(Integer.parseInt(trackerPortStr))); - directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + port); + public ServerP2P(String portServerstr, String hostnameTracker, String portTracker) { + Scanner scanner = new Scanner(System.in); + portServer = Integer.valueOf(Integer.parseInt(portServerstr)); + tracker = new HostItem(hostnameTracker, Integer.valueOf(Integer.parseInt(portTracker))); + directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + portServer); directories.createSubdir(subdir); logger = new Logger(directories.getDataHomeDirectory() + "server.log"); - System.out.println("Server will listen on port " + port + " and serve files from " + directories.getDataHomeDirectory() + subdir); - Scanner scanner = new Scanner(System.in); + System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory() + subdir); directories.askOpenDataHomeDirectory(subdir, scanner); scanner.close(); } /** Main program entry point * first parameter is port number and is mandatory - * to test, run with: java -ea serverP2P.ServerP2P + * to test, run with: java serverP2P.ServerP2P * @param args parameters */ public static void main(String [] args) { @@ -56,7 +56,7 @@ public class ServerP2P { TestPort testPort = new TestPort(); if ((args.length != 5) && (args.length != 0)){ - System.out.println("usage : java serveurP2P.ServeurP2P (interactive) or java serveurP2P.ServeurP2P -- "); + System.out.println("usage : java serveurP2P.ServeurP2P (interactive) or java serveurP2P.ServeurP2P -- (default server port: server 7070 (range 7000->7070) and tracker port 6969 (range 7000 -> 7999))"); } else{ if(args.length == 5){ @@ -113,8 +113,8 @@ public class ServerP2P { } ServerP2P s = new ServerP2P(portServer, hostnameTracker, portTracker); - ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.port, s.logger, s.tracker); - ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.port, s.logger, s.tracker); + ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.portServer, s.logger, s.tracker); + ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.portServer, s.logger, s.tracker); Thread tudp = new Thread(smudp); tudp.setName("server UDP P2P-JAVA-PROJECT"); tudp.start(); diff --git a/src/tools/TestPort.java b/src/tools/TestPort.java index ae71813..d3ca845 100644 --- a/src/tools/TestPort.java +++ b/src/tools/TestPort.java @@ -10,15 +10,21 @@ import javafx.util.Pair; */ public class TestPort { + /** test if port given correspond a range : registered ports, can be used without superuser privileges + * @return Pair + */ public Pair testPortServer(int port) { - if((port > 7000) && (port < 7999)){ + if((port >= 7000) && (port <= 7999)){ return new Pair<>(0,""); } return new Pair<>(1, "Wrong port (7000 -> 7999), using default port 7070"); } + /** test if port given correspond a range : : registered ports, can be used without superuser privileges, 6969 correspond to the unofficial bittorent tracker + * @return Pair + */ public Pair testPortTracker(int port) { - if((port > 6000) && (port < 6999)){ + if((port >= 6000) && (port <= 6999)){ return new Pair<>(0,""); } return new Pair<>(1, "Wrong port (6000 -> 6999), using default port 6969"); diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 787923c..821ab50 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -32,11 +32,13 @@ public class Tracker { Scanner scanner = new Scanner(System.in); directories.askOpenDataHomeDirectory(null, scanner); scanner.close(); + + //t.logger.write(PtestPortTracker.getValue(), LogLevel.Warning); } /** Main program entry point * first parameter is port number and is mandatory - * to test, run with: java -ea serverP2P.ServerP2P + * to test, run with: java serverP2P.ServerP2P * @param args parameters */ public static void main(String [] args) { @@ -49,7 +51,7 @@ public class Tracker { Tracker t; if ((args.length != 3) && (args.length != 0)){ - System.out.println("usage : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- (default localhost:6969, range 6000 -> 6999)"); + System.out.println("usage : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- (default port 6969 (range 6000 -> 6999))"); } else{ if(args.length == 3){ @@ -80,7 +82,6 @@ public class Tracker { else { System.out.println(PtestPortTracker.getValue()); t = new Tracker(defaultPort); - t.logger.write(PtestPortTracker.getValue(), LogLevel.Warning); } TrackerManagementUDP tmudp = new TrackerManagementUDP(hostname, t.port, t.logger); From 7253a34e491562e305f0637bf4aeeb4e0d31f15e Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 24 Mar 2020 17:32:33 +0100 Subject: [PATCH 14/14] Refactoring --- src/clientP2P/ClientP2P.java | 230 +++++++++++++------------ src/serverP2P/ServerManagementTCP.java | 4 +- src/serverP2P/ServerManagementUDP.java | 4 +- src/serverP2P/ServerP2P.java | 138 +++++++-------- src/tools/PortRange.java | 49 ++++++ src/tools/ServerPortRange.java | 10 ++ src/tools/TestPort.java | 35 ---- src/tools/TrackerPortRange.java | 10 ++ src/tracker/Tracker.java | 110 ++++++------ src/tracker/TrackerManagementTCP.java | 6 +- src/tracker/TrackerManagementUDP.java | 8 +- 11 files changed, 327 insertions(+), 277 deletions(-) create mode 100644 src/tools/PortRange.java create mode 100644 src/tools/ServerPortRange.java delete mode 100644 src/tools/TestPort.java create mode 100644 src/tools/TrackerPortRange.java diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index c42aa9a..5985479 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -2,7 +2,6 @@ package clientP2P; import java.util.Scanner; import java.util.List; -import javafx.util.Pair; import clientP2P.ClientManagementUDP; import clientP2P.ClientManagementTCP; @@ -12,7 +11,9 @@ import tools.Logger; import tools.LogLevel; import tools.Directories; import tools.HostItem; -import tools.TestPort; +import tools.ServerPortRange; +import tools.TrackerPortRange; + /** Client + Server implementation. * @author Louis Royer @@ -26,17 +27,17 @@ public class ClientP2P { private String partsDir = ".parts/"; private Logger loggerServer; private Logger loggerClient; - private int portServer; private Directories directories; private HostItem tracker; + private HostItem server; private Scanner scanner; - + /** Initialize loggers if directories and logger are null, * else fail silently. */ public void initDirectoriesAndLoggers() { if (directories == null && loggerServer == null && loggerClient == null) { - directories = new Directories("P2P_JAVA_PROJECT_" + portServer); + directories = new Directories("P2P_JAVA_PROJECT_" + server.getPort()); directories.createSubdir(logDir); loggerServer = new Logger(directories.getDataHomeDirectory() + logDir + "server.log"); loggerClient = new Logger(directories.getDataHomeDirectory() + logDir + "client.log"); @@ -44,18 +45,36 @@ public class ClientP2P { } } - /** Constructor with portServerstr as parameter. - * @param portServerstr String containing port for server listenning. + /** Constructor. + * @param hostnameServer hostname to bind + * @param portServer port to bind + * @param hostnameTracker hostname of tracker + * @param portTracker port of tracker */ - public ClientP2P(String portServerstr, String hostnameTracker, String portTracker) { + public ClientP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker) { scanner = new Scanner(System.in); - portServer = Integer.valueOf(Integer.parseInt(portServerstr)); - tracker = new HostItem(hostnameTracker, Integer.valueOf(Integer.parseInt(portTracker))); + server = new HostItem(hostnameServer, portServer); + tracker = new HostItem(hostnameTracker, portTracker); initDirectoriesAndLoggers(); System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory()); directories.askOpenDataHomeDirectory(null, scanner); } + /** Print cli usage + * @param serverPortRange range of server ports + * @param trackerPortRange range of tracker ports + */ + private static void printUsage(ServerPortRange serverPortRange, TrackerPortRange trackerPortRange) { + System.out.println("usage :"); + System.out.println("\tjava clientP2P.ClientP2P"); + System.out.println("or"); + System.out.println("java clientP2P.ClientP2P -- " + + " " + + " " + + " "); + System.out.println("(" + trackerPortRange + " and " + serverPortRange +")"); + } + /** Main program entry point. * 1rst parameter is optionnal, and is used to * define port used by the server module to listen. If not provided, default to another port. @@ -63,121 +82,120 @@ public class ClientP2P { */ public static void main(String [] args) { final String defaultHostname = "localhost"; - final String defaultPortServer = "7070"; - final String defaultPortTracker = "6969"; String hostnameServer = ""; - String portServer = ""; + int portServer = 0; String hostnameTracker = ""; - String portTracker = ""; + int portTracker = 0; String protocolClient = ""; Scanner scanner = new Scanner(System.in); - TestPort testPort = new TestPort(); + final ServerPortRange serverPortRange = new ServerPortRange(); + final TrackerPortRange trackerPortRange = new TrackerPortRange(); if ((args.length != 6) && (args.length != 0)){ - System.out.println("usage : java clientP2P.ClientP2P or java clientP2P.ClientP2P -- (default tracker port 6969 (range 7000 -> 7999) and server port: server 7070 (range 7000->7070))"); + ClientP2P.printUsage(serverPortRange, trackerPortRange); + System.exit(1); } - else{ - if(args.length == 6){ + else if(args.length == 6){ protocolClient = args[1]; hostnameServer = args[2]; - portServer = args[3]; + portServer = Integer.valueOf(Integer.parseInt(args[3])); hostnameTracker = args[4]; - portTracker = args[5]; + portTracker = Integer.valueOf(Integer.parseInt(args[5])); + } else{ + System.out.println("Client, wich transport protocol do you want to use (default = TCP): "); + protocolClient = scanner.nextLine(); + System.out.println("server side, enter hostname to bind (default = localhost): "); + hostnameServer = scanner.nextLine(); + if(hostnameServer.equals("")){ + hostnameServer = defaultHostname; + System.out.println("using default hostname : " + hostnameServer); } - else{ - System.out.println("Client, wich transport protocol do you want to use (default = TCP): "); - protocolClient = scanner.nextLine(); - System.out.println("server side, enter hostname to bind (default = localhost): "); - hostnameServer = scanner.nextLine(); - if(hostnameServer.equals("")){ - hostnameServer = defaultHostname; - System.out.println("using default hostname : " + hostnameServer); - } - System.out.println("enter port (default = 7070): "); - portServer = scanner.nextLine(); - if(portServer.equals("")){ - portServer = defaultPortServer; - System.out.println("using default port : " + portServer); - } - System.out.println("enter hostname of tracker (default = localhost): "); - hostnameTracker = scanner.nextLine(); - if(hostnameTracker.equals("")){ - hostnameTracker = defaultHostname; - System.out.println("tracker default hostname : " + hostnameTracker); - } - System.out.println("enter tracker's port (default = 6969): "); - portTracker = scanner.nextLine(); - if(portTracker.equals("")){ - portTracker = defaultPortTracker; - System.out.println("using default port : " + portTracker); - } + System.out.println("enter port (default = " + serverPortRange.getDefaultPort() +"): "); + String portServerStr = scanner.nextLine(); + if(portServerStr.equals("")){ + portServer = serverPortRange.getDefaultPort(); + System.out.println("using default port : " + portServer); + } else { + portServer = Integer.valueOf(Integer.parseInt(portServerStr)); } - - System.out.println("using hostname : " + hostnameServer); - Pair PtestPortServer = testPort.testPortServer(Integer.parseInt(portServer)); - if(PtestPortServer.getKey() == 0){ - System.out.println("using port : " + portServer); + System.out.println("enter hostname of tracker (default = localhost): "); + hostnameTracker = scanner.nextLine(); + if(hostnameTracker.equals("")){ + hostnameTracker = defaultHostname; + System.out.println("tracker default hostname : " + hostnameTracker); } - else { - System.out.println(PtestPortServer.getValue()); - portServer = defaultPortServer; + System.out.println("enter tracker's port (default = "+trackerPortRange.getDefaultPort() + "): "); + String portTrackerStr = scanner.nextLine(); + if(portTrackerStr.equals("")){ + portTracker = trackerPortRange.getDefaultPort(); + System.out.println("using default port : " + portTracker); + } else { + portTracker = Integer.valueOf(Integer.parseInt(portTrackerStr)); } - System.out.println("tracker hostname : " + hostnameTracker); + } - Pair PtestPortTracker = testPort.testPortTracker(Integer.parseInt(portTracker)); - if(PtestPortTracker.getKey() == 0){ - System.out.println("using port : " + portTracker); - } - else { - System.out.println(PtestPortTracker.getValue()); - portTracker = defaultPortTracker; - } + System.out.println("using hostname : " + hostnameServer); + if(serverPortRange.isPortInRange(portServer)){ + System.out.println("using port : " + portServer); + } + else { + System.out.println("Port not in range. " + serverPortRange); + portServer = serverPortRange.getDefaultPort(); + } + System.out.println("tracker hostname : " + hostnameTracker); + + if(trackerPortRange.isPortInRange(portTracker)){ + System.out.println("using port : " + portTracker); + } + else { + System.out.println("Port not in range. " + trackerPortRange); + portTracker = trackerPortRange.getDefaultPort(); + } - ClientP2P c = new ClientP2P(portServer, hostnameTracker, portTracker); + ClientP2P c = new ClientP2P(hostnameServer, portServer, hostnameTracker, portTracker); - ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), hostnameServer, c.portServer, c.loggerServer, c.tracker); - ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), hostnameServer, c.portServer, c.loggerServer, c.tracker); - Thread tudp = new Thread(smudp); - tudp.setName("server UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(smtcp); - ttcp.setName("server TCP P2P-JAVA-PROJECT"); - ttcp.start(); + ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); + ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory(), c.server, c.tracker, c.loggerServer); + Thread tudp = new Thread(smudp); + tudp.setName("server UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(smtcp); + ttcp.setName("server TCP P2P-JAVA-PROJECT"); + ttcp.start(); - // Wait a bit before printing client interface - // This is not required, but allow to have a cleaner interface - try { - Thread.sleep(200); - } catch(InterruptedException e) { - Thread.currentThread().interrupt(); - } + // Wait a bit before printing client interface + // This is not required, but allow to have a cleaner interface + try { + Thread.sleep(200); + } catch(InterruptedException e) { + Thread.currentThread().interrupt(); + } - Thread tclient; - switch(protocolClient){ - case "UDP": - case "udp": - case "upd": - case "2" : - System.out.println("Starting with UDP"); - ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner); - tclient = new Thread(cmudp); - break; - case "TCP": - case "tcp": - case "1": - default: - System.out.println("Starting with TCP"); - ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner); - tclient = new Thread(cmtcp); - break; - } - tclient.setName("client P2P-JAVA-PROJECT"); - tclient.start(); - try { - tclient.join(); - } catch (InterruptedException e) {} - smudp.setStop(); - smtcp.setStop(); + Thread tclient; + switch(protocolClient){ + case "UDP": + case "udp": + case "upd": // to avoid users typos + case "2" : + System.out.println("Starting with UDP"); + ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner); + tclient = new Thread(cmudp); + break; + case "TCP": + case "tcp": + case "1": + default: + System.out.println("Starting with TCP"); + ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner); + tclient = new Thread(cmtcp); + break; } + tclient.setName("client P2P-JAVA-PROJECT"); + tclient.start(); + try { + tclient.join(); + } catch (InterruptedException e) {} + smudp.setStop(); + smtcp.setStop(); } } diff --git a/src/serverP2P/ServerManagementTCP.java b/src/serverP2P/ServerManagementTCP.java index d44b79a..fad6ddf 100644 --- a/src/serverP2P/ServerManagementTCP.java +++ b/src/serverP2P/ServerManagementTCP.java @@ -56,8 +56,8 @@ public class ServerManagementTCP extends ServerManagement { * @param logger Logger item * @param tracker Tracker */ - public ServerManagementTCP(String baseDirectory, String hostName, int port, Logger logger, HostItem tracker) { - super(baseDirectory, new HostItem(hostName, port), tracker, logger); + public ServerManagementTCP(String baseDirectory, HostItem server, HostItem tracker, Logger logger) { + super(baseDirectory, server, tracker, logger); assert baseDirectory != null : "baseDirectory is null"; assert server != null : "server is null"; assert tracker != null : "tracker is null"; diff --git a/src/serverP2P/ServerManagementUDP.java b/src/serverP2P/ServerManagementUDP.java index bba1a5b..7ea6b39 100644 --- a/src/serverP2P/ServerManagementUDP.java +++ b/src/serverP2P/ServerManagementUDP.java @@ -54,8 +54,8 @@ public class ServerManagementUDP extends ServerManagement { * @param logger Logger item * @param tracker Tracker */ - public ServerManagementUDP(String baseDirectory, String hostName, int port, Logger logger, HostItem tracker) { - super(baseDirectory, new HostItem(hostName, port), tracker, logger); + public ServerManagementUDP(String baseDirectory, HostItem server, HostItem tracker, Logger logger) { + super(baseDirectory, server, tracker, logger); assert baseDirectory != null : "baseDirectory is null"; assert server != null : "server is null"; assert tracker != null : "tracker is null"; diff --git a/src/serverP2P/ServerP2P.java b/src/serverP2P/ServerP2P.java index 5228a86..7c5a3d3 100644 --- a/src/serverP2P/ServerP2P.java +++ b/src/serverP2P/ServerP2P.java @@ -1,13 +1,13 @@ package serverP2P; import java.util.Scanner; -import javafx.util.Pair; import serverP2P.ServerManagementUDP; import serverP2P.ServerManagementTCP; import tools.Directories; import tools.Logger; import tools.HostItem; -import tools.TestPort; +import tools.ServerPortRange; +import tools.TrackerPortRange; /** Server only implementation @@ -18,19 +18,22 @@ import tools.TestPort; * @version 1.0 */ public class ServerP2P { - private int portServer; private Directories directories; static private final String subdir = "seeded/"; private Logger logger; private HostItem tracker; + private HostItem server; /** Constructor with portServerstr containing a port number. - * @param portServerstr String containing port number of listening. + * @param hostnameServer binded hostname + * @param portServer binded port + * @param hostnameTracker tracker hostname + * @param portTracker tracker port */ - public ServerP2P(String portServerstr, String hostnameTracker, String portTracker) { + public ServerP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker) { Scanner scanner = new Scanner(System.in); - portServer = Integer.valueOf(Integer.parseInt(portServerstr)); - tracker = new HostItem(hostnameTracker, Integer.valueOf(Integer.parseInt(portTracker))); + server = new HostItem(hostnameServer, portServer); + tracker = new HostItem(hostnameTracker, portTracker); directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + portServer); directories.createSubdir(subdir); logger = new Logger(directories.getDataHomeDirectory() + "server.log"); @@ -46,81 +49,80 @@ public class ServerP2P { */ public static void main(String [] args) { final String defaultHostname = "localhost"; - final String defaultPortServer = "7070"; - final String defaultPortTracker = "6969"; + final ServerPortRange serverPortRange = new ServerPortRange(); + final TrackerPortRange trackerPortRange = new TrackerPortRange(); String hostnameServer = ""; - String portServer = ""; + int portServer = 0; String hostnameTracker = ""; - String portTracker = ""; + int portTracker = 0; Scanner scanner = new Scanner(System.in); - TestPort testPort = new TestPort(); if ((args.length != 5) && (args.length != 0)){ - System.out.println("usage : java serveurP2P.ServeurP2P (interactive) or java serveurP2P.ServeurP2P -- (default server port: server 7070 (range 7000->7070) and tracker port 6969 (range 7000 -> 7999))"); + System.out.println("usage : java serveurP2P.ServeurP2P (interactive) or java serveurP2P.ServeurP2P -- (" + serverPortRange + " and " + trackerPortRange +")"); + System.exit(1); } - else{ - if(args.length == 5){ + else if(args.length == 5){ hostnameServer = args[1]; - portServer = args[2]; + portServer = Integer.valueOf(Integer.parseInt(args[2])); hostnameTracker = args[3]; - portTracker = args[4]; + portTracker = Integer.valueOf(Integer.parseInt(args[4])); + } else { + System.out.println("Server, enter hostname to bind (default = localhost): "); + hostnameServer = scanner.nextLine(); + if(hostnameServer.equals("")){ + hostnameServer = defaultHostname; + System.out.println("using default hostname : " + hostnameServer); } - else{ - System.out.println("Server, enter hostname to bind (default = localhost): "); - hostnameServer = scanner.nextLine(); - if(hostnameServer.equals("")){ - hostnameServer = defaultHostname; - System.out.println("using default hostname : " + hostnameServer); - } - System.out.println("enter port (default = 7070): "); - portServer = scanner.nextLine(); - if(portServer.equals("")){ - portServer = defaultPortServer; - System.out.println("using default port : " + portServer); - } - System.out.println("enter hostname of tracker (default = localhost): "); - hostnameTracker = scanner.nextLine(); - if(hostnameTracker.equals("")){ - hostnameTracker = defaultHostname; - System.out.println("tracker default hostname : " + hostnameTracker); - } - System.out.println("enter tracker's port (default = 6969): "); - portTracker = scanner.nextLine(); - if(portTracker.equals("")){ - portTracker = defaultPortTracker; - System.out.println("using default port : " + portTracker); - } + System.out.println("enter port (default = " + serverPortRange.getDefaultPort() + "): "); + String portServerStr = scanner.nextLine(); + if(portServerStr.equals("")){ + portServer = serverPortRange.getDefaultPort(); + System.out.println("using default port : " + portServer); + } else { + portServer = Integer.valueOf(Integer.parseInt(portServerStr)); } - - System.out.println("using hostname : " + hostnameServer); - Pair PtestPortServer = testPort.testPortServer(Integer.parseInt(portServer)); - if(PtestPortServer.getKey() == 0){ - System.out.println("using port : " + portServer); + System.out.println("enter hostname of tracker (default = localhost): "); + hostnameTracker = scanner.nextLine(); + if(hostnameTracker.equals("")){ + hostnameTracker = defaultHostname; + System.out.println("tracker default hostname : " + hostnameTracker); } - else { - System.out.println(PtestPortServer.getValue()); - portServer = defaultPortServer; + System.out.println("enter tracker's port (default = " + trackerPortRange.getDefaultPort() + "): "); + String portTrackerStr = scanner.nextLine(); + if(portTrackerStr.equals("")){ + portTracker = serverPortRange.getDefaultPort(); + System.out.println("using default port : " + portTracker); + } else { + portTracker = Integer.valueOf(Integer.parseInt(portTrackerStr)); } - System.out.println("tracker hostname : " + hostnameTracker); + } - Pair PtestPortTracker = testPort.testPortTracker(Integer.parseInt(portTracker)); - if(PtestPortTracker.getKey() == 0){ - System.out.println("using port : " + portTracker); - } - else { - System.out.println(PtestPortTracker.getValue()); - portTracker = defaultPortTracker; - } + System.out.println("using hostname : " + hostnameServer); + if(serverPortRange.isPortInRange(portServer)) { + System.out.println("using port : " + portServer); + } + else { + System.out.println("Port not in range. " + serverPortRange); + portServer = serverPortRange.getDefaultPort(); + } + System.out.println("tracker hostname : " + hostnameTracker); - ServerP2P s = new ServerP2P(portServer, hostnameTracker, portTracker); - ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.portServer, s.logger, s.tracker); - ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, hostnameServer, s.portServer, s.logger, s.tracker); - Thread tudp = new Thread(smudp); - tudp.setName("server UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(smtcp); - ttcp.setName("server TCP P2P-JAVA-PROJECT"); - ttcp.start(); + if(trackerPortRange.isPortInRange(portTracker)) { + System.out.println("using port : " + portTracker); } + else { + System.out.println("Port not in range. " + trackerPortRange); + portTracker = trackerPortRange.getDefaultPort(); + } + + ServerP2P s = new ServerP2P(hostnameServer, portServer, hostnameTracker, portTracker); + ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); + ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.server, s.tracker, s.logger); + Thread tudp = new Thread(smudp); + tudp.setName("server UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(smtcp); + ttcp.setName("server TCP P2P-JAVA-PROJECT"); + ttcp.start(); } } diff --git a/src/tools/PortRange.java b/src/tools/PortRange.java new file mode 100644 index 0000000..290cc94 --- /dev/null +++ b/src/tools/PortRange.java @@ -0,0 +1,49 @@ +package tools; + +/** Test ports. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ +public class PortRange { + + protected int portMax; + protected int portMin; + protected int defaultPort; + protected String type; + + /** Port range constructor + * @param portMin minimum port + * @param portMax maximum port + * @param defaultPort default port + * @param type type of range + */ + public PortRange(int portMin, int portMax, int defaultPort, String type) { + this.portMax = portMax; + this.portMin = portMin; + this.defaultPort = defaultPort; + this.type = type; + } + + /** test if port given correspond a range : registered ports, can be used without superuser privileges + * @return true if port was valid + */ + public boolean isPortInRange(int port) { + return ((port >= portMin) && (port <= portMax)); + } + + /** To String + * @return String representation + */ + public String toString() { + return "default " + type + "port: " + defaultPort + "(range: " + portMin + " -> " + portMax + ")"; + } + + /** Default port getter + * @return default port + */ + public int getDefaultPort() { + return defaultPort; + } +} diff --git a/src/tools/ServerPortRange.java b/src/tools/ServerPortRange.java new file mode 100644 index 0000000..96722c5 --- /dev/null +++ b/src/tools/ServerPortRange.java @@ -0,0 +1,10 @@ +package tools; +import tools.PortRange; + +public class ServerPortRange extends PortRange { + + /** Constructor */ + public ServerPortRange() { + super(7000, 7999, 7070, "server"); + } +} diff --git a/src/tools/TestPort.java b/src/tools/TestPort.java deleted file mode 100644 index d3ca845..0000000 --- a/src/tools/TestPort.java +++ /dev/null @@ -1,35 +0,0 @@ -package tools; - -import javafx.util.Pair; - -/** Test ports. - * @author Louis Royer - * @author Flavien Haas - * @author JS Auge - * @version 1.0 - */ -public class TestPort { - - /** test if port given correspond a range : registered ports, can be used without superuser privileges - * @return Pair - */ - public Pair testPortServer(int port) { - if((port >= 7000) && (port <= 7999)){ - return new Pair<>(0,""); - } - return new Pair<>(1, "Wrong port (7000 -> 7999), using default port 7070"); - } - - /** test if port given correspond a range : : registered ports, can be used without superuser privileges, 6969 correspond to the unofficial bittorent tracker - * @return Pair - */ - public Pair testPortTracker(int port) { - if((port >= 6000) && (port <= 6999)){ - return new Pair<>(0,""); - } - return new Pair<>(1, "Wrong port (6000 -> 6999), using default port 6969"); - } - - - -} diff --git a/src/tools/TrackerPortRange.java b/src/tools/TrackerPortRange.java new file mode 100644 index 0000000..0ca491b --- /dev/null +++ b/src/tools/TrackerPortRange.java @@ -0,0 +1,10 @@ +package tools; +import tools.PortRange; + +public class TrackerPortRange extends PortRange { + + /** Constructor */ + public TrackerPortRange() { + super(6000, 6999, 6969, "tracker"); + } +} diff --git a/src/tracker/Tracker.java b/src/tracker/Tracker.java index 821ab50..92ff5f5 100644 --- a/src/tracker/Tracker.java +++ b/src/tracker/Tracker.java @@ -1,13 +1,13 @@ package tracker; import java.util.Scanner; -import javafx.util.Pair; import tracker.TrackerManagementTCP; import tracker.TrackerManagementUDP; import tools.Directories; import tools.Logger; import tools.LogLevel; -import tools.TestPort; +import tools.TrackerPortRange; +import tools.HostItem; /** Tracker implementation * First argument of main method is port listened by the tracker, and is mandatory. @@ -17,23 +17,22 @@ import tools.TestPort; * @version 1.0 */ public class Tracker { - private int port; + private HostItem tracker; private Directories directories; private Logger logger; /** Constructor with portStr containing a port number. - * @param portStr String containing port number of listening. + * @param hostname hostname to bind + * @param port port to bind */ - public Tracker(String portStr) { - port = Integer.valueOf(Integer.parseInt(portStr)); + public Tracker(String hostname, int port) { + tracker = new HostItem(hostname, port); directories = new Directories("P2P_JAVA_PROJECT_TRACKER_" + port); logger = new Logger(directories.getDataHomeDirectory() + "tracker.log"); System.out.println("Tracker will listen on port " + port + " and write logs into " + directories.getDataHomeDirectory()); Scanner scanner = new Scanner(System.in); directories.askOpenDataHomeDirectory(null, scanner); scanner.close(); - - //t.logger.write(PtestPortTracker.getValue(), LogLevel.Warning); } /** Main program entry point @@ -41,57 +40,54 @@ public class Tracker { * to test, run with: java serverP2P.ServerP2P * @param args parameters */ - public static void main(String [] args) { - final String defaultPort = "6969"; - final String defaultHostname = "localhost"; - Scanner scanner = new Scanner(System.in); - TestPort testPort = new TestPort(); - String hostname; - String port; - Tracker t; + public static void main(String [] args) { + final TrackerPortRange trackerPortRange = new TrackerPortRange(); + final String defaultHostname = "localhost"; + Scanner scanner = new Scanner(System.in); + String hostname = ""; + int port = 0; + Tracker t; - if ((args.length != 3) && (args.length != 0)){ - System.out.println("usage : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- (default port 6969 (range 6000 -> 6999))"); + if ((args.length != 3) && (args.length != 0)){ + System.out.println("usage : java tracker.Tracker (interactive) or java trackerP2P.trackerP2P -- (" + trackerPortRange +")"); + System.exit(1); + } else if (args.length == 3){ + hostname = args[1]; + port = Integer.valueOf(Integer.parseInt(args[2])); + } else { + System.out.println("Tracker Server, enter hostname to bind (default = localhost): "); + hostname = scanner.nextLine(); + if(hostname.equals("")){ + hostname = defaultHostname; + System.out.println("using default hostname : " + hostname); } - else{ - if(args.length == 3){ - hostname = args[1]; - port = args[2]; - } - else{ - System.out.println("Tracker Server, enter hostname to bind (default = localhost): "); - hostname = scanner.nextLine(); - if(hostname.equals("")){ - hostname = defaultHostname; - System.out.println("using default hostname : " + hostname); - } - System.out.println("enter port (default = 6969): "); - port = scanner.nextLine(); - if(port.equals("")){ - port = defaultPort; - System.out.println("using default port : " + port); - } - } - - System.out.println("using hostname : " + hostname); - Pair PtestPortTracker = testPort.testPortTracker(Integer.parseInt(port)); - if(PtestPortTracker.getKey() == 0){ - System.out.println("using port : " + port); - t = new Tracker(port); - } - else { - System.out.println(PtestPortTracker.getValue()); - t = new Tracker(defaultPort); - } - - TrackerManagementUDP tmudp = new TrackerManagementUDP(hostname, t.port, t.logger); - TrackerManagementTCP tmtcp = new TrackerManagementTCP(hostname, t.port, t.logger); - Thread tudp = new Thread(tmudp); - tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); - tudp.start(); - Thread ttcp = new Thread(tmtcp); - ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); - ttcp.start(); + System.out.println("enter port (default = " + trackerPortRange.getDefaultPort() +"): "); + String portStr = scanner.nextLine(); + if(portStr.equals("")){ + port = trackerPortRange.getDefaultPort(); + System.out.println("using default port : " + port); + } else { + port = Integer.valueOf(Integer.parseInt(portStr)); } } + + System.out.println("using hostname : " + hostname); + if(trackerPortRange.isPortInRange(port)) { + System.out.println("using port : " + port); + t = new Tracker(hostname, port); + } + else { + System.out.println("Port not in range. " + trackerPortRange); + t = new Tracker(hostname, trackerPortRange.getDefaultPort()); + } + + TrackerManagementUDP tmudp = new TrackerManagementUDP(t.tracker, t.logger); + TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.tracker, t.logger); + Thread tudp = new Thread(tmudp); + tudp.setName("Tracker UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(tmtcp); + ttcp.setName("Tracker TCP P2P-JAVA-PROJECT"); + ttcp.start(); + } } diff --git a/src/tracker/TrackerManagementTCP.java b/src/tracker/TrackerManagementTCP.java index afba7bd..603bab5 100644 --- a/src/tracker/TrackerManagementTCP.java +++ b/src/tracker/TrackerManagementTCP.java @@ -38,11 +38,11 @@ public class TrackerManagementTCP extends TrackerManagement { private ServerSocket socket; /** Constructor with port and logger. - * @param port Port used to listen. + * @param tracker hostitem of the tracker. * @param logger Logger object */ - public TrackerManagementTCP(String hostname, int port, Logger logger) { - super(new HostItem(hostname, port), logger); + public TrackerManagementTCP(HostItem tracker, Logger logger) { + super(tracker, logger); try { socket = new ServerSocket(tracker.getPort(), 10, tracker.getInetAddress()); } catch (SocketException e) { diff --git a/src/tracker/TrackerManagementUDP.java b/src/tracker/TrackerManagementUDP.java index 6a07bcb..cdfb890 100644 --- a/src/tracker/TrackerManagementUDP.java +++ b/src/tracker/TrackerManagementUDP.java @@ -34,15 +34,15 @@ import tracker.TrackerManagement; public class TrackerManagementUDP extends TrackerManagement { private DatagramSocket socket; /** Constructor with port and logger. - * @param port Port used to listen. + * @param tracker hostitem of the tracker. * @param logger Logger object */ - public TrackerManagementUDP(String hostname, int port, Logger logger) { - super(new HostItem(hostname, port), logger); + public TrackerManagementUDP(HostItem tracker, Logger logger) { + super(tracker, logger); try { socket = new DatagramSocket(tracker.getPort(), tracker.getInetAddress()); } catch (SocketException e) { - logger.writeUDP("Error: cannot listen on port " + port, LogLevel.Error); + logger.writeUDP("Error: cannot listen on " + tracker, LogLevel.Error); System.exit(-1); } }