From 7253a34e491562e305f0637bf4aeeb4e0d31f15e Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 24 Mar 2020 17:32:33 +0100 Subject: [PATCH] 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); } }