From 64032e84c435d0261e443089bad941e0dfe5c697 Mon Sep 17 00:00:00 2001 From: flavien Date: Tue, 24 Mar 2020 16:04:10 +0100 Subject: [PATCH] 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);